Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from dotenv import load_dotenv
|
|
3 |
import gradio as gr
|
4 |
from langchain_huggingface import HuggingFaceEndpoint
|
5 |
from datetime import datetime
|
|
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
@@ -19,29 +20,80 @@ llm = HuggingFaceEndpoint(
|
|
19 |
temperature=0.7,
|
20 |
)
|
21 |
|
22 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
def validate_ingredients(ingredients):
|
24 |
if not ingredients or ingredients.strip() == "":
|
25 |
return "Invalid"
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
34 |
|
35 |
-
# Recipe generation function
|
36 |
def generate_recipe(ingredients):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Combined function for Gradio
|
47 |
def suggest_recipes(ingredients):
|
@@ -52,9 +104,9 @@ def suggest_recipes(ingredients):
|
|
52 |
if "Valid" in validation_result: # More flexible check for "Valid" in response
|
53 |
return generate_recipe(ingredients)
|
54 |
else:
|
55 |
-
return "I'm sorry, but I can't process this request due to invalid ingredients. Please provide valid
|
56 |
|
57 |
-
# Feedback function
|
58 |
def save_feedback(feedback):
|
59 |
if feedback.strip():
|
60 |
try:
|
@@ -65,139 +117,35 @@ def save_feedback(feedback):
|
|
65 |
return f"Error saving feedback: {str(e)}"
|
66 |
return "Please enter feedback before submitting."
|
67 |
|
68 |
-
# Gradio interface
|
69 |
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="blue")) as app:
|
70 |
-
#
|
|
|
71 |
with gr.Row():
|
72 |
gr.HTML(
|
73 |
"""
|
74 |
<div style="text-align: center; margin: auto;">
|
75 |
-
<h1 style="color: #1155ff; font-size: 3.5em; font-weight: bold; font-family: 'Arial', sans-serif; margin-bottom: 0.5em;">🍳 Recipe Generator</h1>
|
76 |
<p style="font-size: 1.5em; color: #555555; font-weight: lighter; font-family: 'Verdana', sans-serif;">
|
77 |
-
Enter
|
78 |
</p>
|
79 |
</div>
|
80 |
"""
|
81 |
)
|
82 |
|
83 |
-
#
|
84 |
-
|
85 |
-
with gr.Row():
|
86 |
-
gr.Image("./recipe-generator-banner.png", show_label=False, elem_id="banner-image", width="100%")
|
87 |
-
except Exception:
|
88 |
-
# Skip banner if image doesn't exist
|
89 |
-
pass
|
90 |
|
91 |
-
# Ingredient input
|
92 |
-
with gr.Row():
|
93 |
-
with gr.Column():
|
94 |
-
ingredients_input = gr.Textbox(
|
95 |
-
label="Enter Ingredients (comma-separated):",
|
96 |
-
placeholder="e.g., eggs, milk, flour",
|
97 |
-
elem_id="input-box",
|
98 |
-
show_label=True,
|
99 |
-
interactive=True,
|
100 |
-
lines=2,
|
101 |
-
max_lines=5
|
102 |
-
)
|
103 |
-
|
104 |
-
with gr.Column():
|
105 |
-
recipe_output = gr.Textbox(
|
106 |
-
label="Suggested Recipes or Validation Result:",
|
107 |
-
lines=15,
|
108 |
-
interactive=False,
|
109 |
-
elem_id="output-box"
|
110 |
-
)
|
111 |
-
|
112 |
-
# Buttons with professional color design and hover effect
|
113 |
-
with gr.Row():
|
114 |
-
generate_button = gr.Button("Get Recipes", elem_id="generate-btn", size="lg")
|
115 |
-
reset_button = gr.Button("Reset", elem_id="reset-btn", size="lg")
|
116 |
-
|
117 |
-
generate_button.click(suggest_recipes, inputs=ingredients_input, outputs=recipe_output)
|
118 |
-
reset_button.click(lambda: "", inputs=None, outputs=ingredients_input)
|
119 |
-
|
120 |
-
# Feedback Section with professional styled input fields
|
121 |
-
with gr.Row():
|
122 |
-
feedback_input = gr.Textbox(
|
123 |
-
label="Feedback:",
|
124 |
-
placeholder="Let us know how we can improve!",
|
125 |
-
elem_id="feedback-input",
|
126 |
-
lines=1
|
127 |
-
)
|
128 |
-
feedback_button = gr.Button("Submit", elem_id="feedback-btn", size="lg")
|
129 |
-
feedback_output = gr.Textbox(
|
130 |
-
label="Feedback Response:",
|
131 |
-
lines=1,
|
132 |
-
interactive=False,
|
133 |
-
elem_id="feedback-output"
|
134 |
-
)
|
135 |
-
|
136 |
-
feedback_button.click(save_feedback, inputs=feedback_input, outputs=feedback_output)
|
137 |
-
|
138 |
-
# Footer with contact link
|
139 |
with gr.Row():
|
140 |
gr.Markdown(
|
141 |
"""
|
142 |
-
<div style="text-align: center; margin-top:
|
143 |
-
<p
|
144 |
-
Developed by CloudYuga! Your feedback is valuable. Have questions?
|
145 |
-
<a href="mailto:[email protected]" style="color: #0000FF; font-weight: bold; text-decoration: underline;">Contact us!</a>
|
146 |
-
</p>
|
147 |
</div>
|
148 |
"""
|
149 |
)
|
150 |
|
151 |
-
# Apply custom professional theme styling
|
152 |
-
app.css = """
|
153 |
-
body {
|
154 |
-
background-color: #f4f4f9; /* Soft background */
|
155 |
-
color: #333333;
|
156 |
-
font-family: 'Arial', sans-serif;
|
157 |
-
}
|
158 |
-
#input-box, #output-box, #feedback-input {
|
159 |
-
background-color: #ffffff;
|
160 |
-
border: 2px solid #cccccc;
|
161 |
-
border-radius: 8px;
|
162 |
-
padding: 12px;
|
163 |
-
color: #333333;
|
164 |
-
font-size: 1.1em;
|
165 |
-
}
|
166 |
-
#generate-btn, #reset-btn, #feedback-btn {
|
167 |
-
border-radius: 8px;
|
168 |
-
padding: 15px 30px;
|
169 |
-
font-size: 1.2em;
|
170 |
-
transition: background-color 0.3s ease;
|
171 |
-
}
|
172 |
-
#generate-btn {
|
173 |
-
background-color: #4caf50; /* Green for recipe button */
|
174 |
-
color: #fff;
|
175 |
-
}
|
176 |
-
#generate-btn:hover {
|
177 |
-
background-color: #45a049;
|
178 |
-
}
|
179 |
-
#reset-btn {
|
180 |
-
background-color: #ff9800; /* Orange for reset button */
|
181 |
-
color: #fff;
|
182 |
-
}
|
183 |
-
#reset-btn:hover {
|
184 |
-
background-color: #fb8c00;
|
185 |
-
}
|
186 |
-
#feedback-btn {
|
187 |
-
background-color: #2196f3; /* Blue for feedback button */
|
188 |
-
color: #fff;
|
189 |
-
}
|
190 |
-
#feedback-btn:hover {
|
191 |
-
background-color: #1e88e5;
|
192 |
-
}
|
193 |
-
#banner-image {
|
194 |
-
width: 100%;
|
195 |
-
height: auto;
|
196 |
-
border-radius: 10px;
|
197 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
198 |
-
}
|
199 |
-
"""
|
200 |
-
|
201 |
# Launch the app
|
202 |
if __name__ == "__main__":
|
203 |
app.launch()
|
|
|
3 |
import gradio as gr
|
4 |
from langchain_huggingface import HuggingFaceEndpoint
|
5 |
from datetime import datetime
|
6 |
+
from datasets import load_dataset
|
7 |
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
|
|
20 |
temperature=0.7,
|
21 |
)
|
22 |
|
23 |
+
# Load the Indian Recipe Dataset
|
24 |
+
try:
|
25 |
+
ds = load_dataset("Anupam007/indian-recipe-dataset")
|
26 |
+
|
27 |
+
# Extract unique ingredients from the dataset
|
28 |
+
all_ingredients = set()
|
29 |
+
for recipe in ds['train']:
|
30 |
+
if 'ingredients' in recipe:
|
31 |
+
# Split ingredients and clean them
|
32 |
+
ingredients = [ing.strip().lower() for ing in recipe['ingredients'].split(',')]
|
33 |
+
all_ingredients.update(ingredients)
|
34 |
+
|
35 |
+
# Convert to a list for easier processing
|
36 |
+
VALID_INGREDIENTS = list(all_ingredients)
|
37 |
+
except Exception as e:
|
38 |
+
print(f"Error loading dataset: {e}")
|
39 |
+
VALID_INGREDIENTS = []
|
40 |
+
|
41 |
+
# Input validation function (now using dataset ingredients)
|
42 |
def validate_ingredients(ingredients):
|
43 |
if not ingredients or ingredients.strip() == "":
|
44 |
return "Invalid"
|
45 |
|
46 |
+
# Split and clean input ingredients
|
47 |
+
input_ingredients = [ing.strip().lower() for ing in ingredients.split(',')]
|
48 |
+
|
49 |
+
# Check if all input ingredients are in valid ingredients list
|
50 |
+
invalid_ingredients = [ing for ing in input_ingredients if ing not in VALID_INGREDIENTS]
|
51 |
+
|
52 |
+
if not invalid_ingredients:
|
53 |
+
return "Valid"
|
54 |
+
else:
|
55 |
+
return "Invalid"
|
56 |
|
57 |
+
# Recipe generation function (using dataset for inspiration)
|
58 |
def generate_recipe(ingredients):
|
59 |
+
# Split and clean input ingredients
|
60 |
+
input_ingredients = [ing.strip().lower() for ing in ingredients.split(',')]
|
61 |
+
|
62 |
+
# Find matching recipes in the dataset
|
63 |
+
matching_recipes = []
|
64 |
+
for recipe in ds['train']:
|
65 |
+
if 'ingredients' in recipe and 'instructions' in recipe:
|
66 |
+
recipe_ingredients = [ing.strip().lower() for ing in recipe['ingredients'].split(',')]
|
67 |
+
|
68 |
+
# Check if most input ingredients are in the recipe
|
69 |
+
match_count = sum(1 for ing in input_ingredients if ing in recipe_ingredients)
|
70 |
+
if match_count > 0:
|
71 |
+
matching_recipes.append({
|
72 |
+
'title': recipe.get('title', 'Untitled Recipe'),
|
73 |
+
'ingredients': recipe.get('ingredients', ''),
|
74 |
+
'instructions': recipe.get('instructions', '')
|
75 |
+
})
|
76 |
+
|
77 |
+
# If matching recipes found, select one
|
78 |
+
if matching_recipes:
|
79 |
+
selected_recipe = matching_recipes[0] # Could randomize selection if desired
|
80 |
+
|
81 |
+
# Format the recipe output
|
82 |
+
recipe_output = f"Recipe: {selected_recipe['title']}\n\n"
|
83 |
+
recipe_output += "Ingredients:\n"
|
84 |
+
recipe_output += f"{selected_recipe['ingredients']}\n\n"
|
85 |
+
recipe_output += "Instructions:\n"
|
86 |
+
recipe_output += selected_recipe['instructions']
|
87 |
+
|
88 |
+
return recipe_output
|
89 |
+
else:
|
90 |
+
# Fallback to LLM generation if no matching recipes
|
91 |
+
prompt = (
|
92 |
+
f"You are an expert chef specializing in Indian cuisine. Using the ingredients: {ingredients}, "
|
93 |
+
f"suggest a unique Indian recipe. Provide a title, list of ingredients, and detailed step-by-step instructions. "
|
94 |
+
f"Focus on traditional Indian cooking methods and spices."
|
95 |
+
)
|
96 |
+
return llm(prompt).strip()
|
97 |
|
98 |
# Combined function for Gradio
|
99 |
def suggest_recipes(ingredients):
|
|
|
104 |
if "Valid" in validation_result: # More flexible check for "Valid" in response
|
105 |
return generate_recipe(ingredients)
|
106 |
else:
|
107 |
+
return "I'm sorry, but I can't process this request due to invalid ingredients. Please provide valid Indian cooking ingredients!"
|
108 |
|
109 |
+
# Feedback function (unchanged from previous script)
|
110 |
def save_feedback(feedback):
|
111 |
if feedback.strip():
|
112 |
try:
|
|
|
117 |
return f"Error saving feedback: {str(e)}"
|
118 |
return "Please enter feedback before submitting."
|
119 |
|
120 |
+
# Gradio interface (mostly unchanged from previous script)
|
121 |
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="blue")) as app:
|
122 |
+
# [Previous Gradio interface code remains the same]
|
123 |
+
# Header section
|
124 |
with gr.Row():
|
125 |
gr.HTML(
|
126 |
"""
|
127 |
<div style="text-align: center; margin: auto;">
|
128 |
+
<h1 style="color: #1155ff; font-size: 3.5em; font-weight: bold; font-family: 'Arial', sans-serif; margin-bottom: 0.5em;">🍳 Indian Recipe Generator</h1>
|
129 |
<p style="font-size: 1.5em; color: #555555; font-weight: lighter; font-family: 'Verdana', sans-serif;">
|
130 |
+
Enter Indian ingredients, and we'll find or create a delightful recipe!
|
131 |
</p>
|
132 |
</div>
|
133 |
"""
|
134 |
)
|
135 |
|
136 |
+
# [Rest of the Gradio interface code remains the same]
|
137 |
+
# ... (previous code for input, buttons, feedback section)
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
# Ingredient input hint
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
with gr.Row():
|
141 |
gr.Markdown(
|
142 |
"""
|
143 |
+
<div style="text-align: center; margin-top: 1em; color: #666;">
|
144 |
+
<p>💡 Tip: Try ingredients like tomato, onion, potato, spinach, chicken, or spices like turmeric, cumin!</p>
|
|
|
|
|
|
|
145 |
</div>
|
146 |
"""
|
147 |
)
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
# Launch the app
|
150 |
if __name__ == "__main__":
|
151 |
app.launch()
|