Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# Configure logging
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
# Load environment variables
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
# Configure OpenAI
|
| 15 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 16 |
+
if not openai.api_key:
|
| 17 |
+
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
| 18 |
+
|
| 19 |
+
def generate_recipe(query, diet_preference=None, cuisine_type=None):
|
| 20 |
+
"""Generate a recipe with optional diet and cuisine preferences"""
|
| 21 |
+
logger.info(f"Generating recipe for query: {query}, diet: {diet_preference}, cuisine: {cuisine_type}")
|
| 22 |
+
|
| 23 |
+
if not query:
|
| 24 |
+
raise ValueError("Recipe query is required")
|
| 25 |
+
|
| 26 |
+
# Create a detailed prompt for the recipe
|
| 27 |
+
prompt = f"""Create a detailed recipe for {query}"""
|
| 28 |
+
if diet_preference:
|
| 29 |
+
prompt += f" that is {diet_preference}"
|
| 30 |
+
if cuisine_type:
|
| 31 |
+
prompt += f" in {cuisine_type} style"
|
| 32 |
+
|
| 33 |
+
prompt += """\n\nFormat the recipe in markdown with the following sections:
|
| 34 |
+
1. Brief Description
|
| 35 |
+
2. Ingredients (as a bulleted list)
|
| 36 |
+
3. Instructions (as numbered steps)
|
| 37 |
+
4. Tips (as a bulleted list)
|
| 38 |
+
5. Nutritional Information (as a bulleted list)
|
| 39 |
+
|
| 40 |
+
Use markdown formatting like:
|
| 41 |
+
- Headers (###)
|
| 42 |
+
- Bold text (**)
|
| 43 |
+
- Lists (- and 1.)
|
| 44 |
+
- Sections (>)
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
# Generate recipe text
|
| 49 |
+
completion = openai.chat.completions.create(
|
| 50 |
+
model="gpt-3.5-turbo",
|
| 51 |
+
messages=[
|
| 52 |
+
{"role": "system", "content": "You are a professional chef who provides detailed recipes with ingredients, instructions, nutritional information, and cooking tips. Format your responses in markdown."},
|
| 53 |
+
{"role": "user", "content": prompt}
|
| 54 |
+
],
|
| 55 |
+
temperature=0.7
|
| 56 |
+
)
|
| 57 |
+
recipe_text = completion.choices[0].message.content
|
| 58 |
+
|
| 59 |
+
# Generate recipe image
|
| 60 |
+
image_response = openai.images.generate(
|
| 61 |
+
model="dall-e-3",
|
| 62 |
+
prompt=f"Professional food photography of {query}, appetizing, high-quality, restaurant style",
|
| 63 |
+
n=1,
|
| 64 |
+
size="1024x1024"
|
| 65 |
+
)
|
| 66 |
+
image_url = image_response.data[0].url
|
| 67 |
+
|
| 68 |
+
# Get learning resources (simplified version)
|
| 69 |
+
learning_resources = [
|
| 70 |
+
{
|
| 71 |
+
"title": f"Master the Art of {query}",
|
| 72 |
+
"url": f"https://cooking-school.example.com/learn/{query.lower().replace(' ', '-')}",
|
| 73 |
+
"type": "video"
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"title": f"Tips and Tricks for Perfect {query}",
|
| 77 |
+
"url": f"https://recipes.example.com/tips/{query.lower().replace(' ', '-')}",
|
| 78 |
+
"type": "article"
|
| 79 |
+
}
|
| 80 |
+
]
|
| 81 |
+
|
| 82 |
+
return recipe_text, image_url, learning_resources
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
logger.error(f"Error generating recipe: {str(e)}")
|
| 86 |
+
raise
|
| 87 |
+
|
| 88 |
+
def format_learning_resources(resources):
|
| 89 |
+
"""Format learning resources as a markdown list"""
|
| 90 |
+
if not resources:
|
| 91 |
+
return "No learning resources available."
|
| 92 |
+
|
| 93 |
+
return "\n".join([f"- **{r['title']}** ({r['type']}): {r['url']}" for r in resources])
|
| 94 |
+
|
| 95 |
+
def recipe_generation_app():
|
| 96 |
+
"""Create Gradio interface for recipe generation"""
|
| 97 |
+
# Inputs
|
| 98 |
+
recipe_input = gr.Textbox(label="Recipe Query", placeholder="Enter a recipe name (e.g., chocolate chip cookies)")
|
| 99 |
+
diet_input = gr.Dropdown(
|
| 100 |
+
label="Diet Preference",
|
| 101 |
+
choices=["None", "Vegetarian", "Vegan", "Gluten-Free", "Keto"],
|
| 102 |
+
value="None"
|
| 103 |
+
)
|
| 104 |
+
cuisine_input = gr.Dropdown(
|
| 105 |
+
label="Cuisine Type",
|
| 106 |
+
choices=["None", "Italian", "Mexican", "Chinese", "Indian", "French"],
|
| 107 |
+
value="None"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Outputs
|
| 111 |
+
recipe_output = gr.Markdown(label="Generated Recipe")
|
| 112 |
+
image_output = gr.Image(label="Recipe Image")
|
| 113 |
+
resources_output = gr.Markdown(label="Learning Resources")
|
| 114 |
+
|
| 115 |
+
# Define the app interface
|
| 116 |
+
demo = gr.Interface(
|
| 117 |
+
fn=lambda query, diet, cuisine: (
|
| 118 |
+
*generate_recipe(
|
| 119 |
+
query,
|
| 120 |
+
diet if diet != "None" else None,
|
| 121 |
+
cuisine if cuisine != "None" else None
|
| 122 |
+
)[:2],
|
| 123 |
+
format_learning_resources(generate_recipe(
|
| 124 |
+
query,
|
| 125 |
+
diet if diet != "None" else None,
|
| 126 |
+
cuisine if cuisine != "None" else None
|
| 127 |
+
)[2])
|
| 128 |
+
),
|
| 129 |
+
inputs=[recipe_input, diet_input, cuisine_input],
|
| 130 |
+
outputs=[recipe_output, image_output, resources_output],
|
| 131 |
+
title="🍳 AI Recipe Assistant",
|
| 132 |
+
description="Generate delicious recipes with AI-powered suggestions!"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
return demo
|
| 136 |
+
|
| 137 |
+
# Launch the Gradio app
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
app = recipe_generation_app()
|
| 140 |
+
app.launch(share=True)
|