Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -32,13 +32,30 @@ try:
|
|
32 |
ingredients = [ing.strip().lower() for ing in recipe['ingredients'].split(',')]
|
33 |
all_ingredients.update(ingredients)
|
34 |
|
35 |
-
#
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
except Exception as e:
|
38 |
print(f"Error loading dataset: {e}")
|
39 |
-
|
40 |
|
41 |
-
# Input validation function
|
42 |
def validate_ingredients(ingredients):
|
43 |
if not ingredients or ingredients.strip() == "":
|
44 |
return "Invalid"
|
@@ -46,15 +63,25 @@ def validate_ingredients(ingredients):
|
|
46 |
# Split and clean input ingredients
|
47 |
input_ingredients = [ing.strip().lower() for ing in ingredients.split(',')]
|
48 |
|
49 |
-
# Check if all input ingredients
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
|
|
53 |
return "Valid"
|
54 |
else:
|
55 |
return "Invalid"
|
56 |
|
57 |
-
# Recipe generation function
|
58 |
def generate_recipe(ingredients):
|
59 |
# Split and clean input ingredients
|
60 |
input_ingredients = [ing.strip().lower() for ing in ingredients.split(',')]
|
@@ -65,8 +92,11 @@ def generate_recipe(ingredients):
|
|
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
|
69 |
-
match_count = sum(1 for ing in input_ingredients
|
|
|
|
|
|
|
70 |
if match_count > 0:
|
71 |
matching_recipes.append({
|
72 |
'title': recipe.get('title', 'Untitled Recipe'),
|
@@ -76,7 +106,8 @@ def generate_recipe(ingredients):
|
|
76 |
|
77 |
# If matching recipes found, select one
|
78 |
if matching_recipes:
|
79 |
-
|
|
|
80 |
|
81 |
# Format the recipe output
|
82 |
recipe_output = f"Recipe: {selected_recipe['title']}\n\n"
|
@@ -101,7 +132,7 @@ def suggest_recipes(ingredients):
|
|
101 |
return "Please enter ingredients before submitting."
|
102 |
|
103 |
validation_result = validate_ingredients(ingredients)
|
104 |
-
if "Valid" in validation_result:
|
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!"
|
|
|
32 |
ingredients = [ing.strip().lower() for ing in recipe['ingredients'].split(',')]
|
33 |
all_ingredients.update(ingredients)
|
34 |
|
35 |
+
# Expand ingredient variants
|
36 |
+
INGREDIENT_VARIANTS = {}
|
37 |
+
for ing in all_ingredients:
|
38 |
+
# Add singular and plural variants
|
39 |
+
singular = ing.rstrip('s')
|
40 |
+
plural = ing + 's'
|
41 |
+
INGREDIENT_VARIANTS[singular] = [singular, plural]
|
42 |
+
INGREDIENT_VARIANTS[plural] = [singular, plural]
|
43 |
+
|
44 |
+
# Combine with direct mappings for common ingredients
|
45 |
+
INGREDIENT_VARIANTS.update({
|
46 |
+
'potato': ['potato', 'potatoes'],
|
47 |
+
'tomato': ['tomato', 'tomatoes'],
|
48 |
+
'onion': ['onion', 'onions'],
|
49 |
+
'spinach': ['spinach', 'spinaches'],
|
50 |
+
'chicken': ['chicken'],
|
51 |
+
'turmeric': ['turmeric'],
|
52 |
+
'cumin': ['cumin']
|
53 |
+
})
|
54 |
except Exception as e:
|
55 |
print(f"Error loading dataset: {e}")
|
56 |
+
INGREDIENT_VARIANTS = {}
|
57 |
|
58 |
+
# Input validation function
|
59 |
def validate_ingredients(ingredients):
|
60 |
if not ingredients or ingredients.strip() == "":
|
61 |
return "Invalid"
|
|
|
63 |
# Split and clean input ingredients
|
64 |
input_ingredients = [ing.strip().lower() for ing in ingredients.split(',')]
|
65 |
|
66 |
+
# Check if all input ingredients have a valid variant
|
67 |
+
valid_matches = []
|
68 |
+
for ing in input_ingredients:
|
69 |
+
matched = False
|
70 |
+
for key, variants in INGREDIENT_VARIANTS.items():
|
71 |
+
if ing in variants:
|
72 |
+
valid_matches.append(ing)
|
73 |
+
matched = True
|
74 |
+
break
|
75 |
+
if not matched:
|
76 |
+
print(f"No match found for ingredient: {ing}")
|
77 |
|
78 |
+
# If all ingredients have a match, return Valid
|
79 |
+
if len(valid_matches) == len(input_ingredients):
|
80 |
return "Valid"
|
81 |
else:
|
82 |
return "Invalid"
|
83 |
|
84 |
+
# Recipe generation function
|
85 |
def generate_recipe(ingredients):
|
86 |
# Split and clean input ingredients
|
87 |
input_ingredients = [ing.strip().lower() for ing in ingredients.split(',')]
|
|
|
92 |
if 'ingredients' in recipe and 'instructions' in recipe:
|
93 |
recipe_ingredients = [ing.strip().lower() for ing in recipe['ingredients'].split(',')]
|
94 |
|
95 |
+
# Check if input ingredients are in the recipe
|
96 |
+
match_count = sum(1 for ing in input_ingredients
|
97 |
+
for recipe_ing in recipe_ingredients
|
98 |
+
if any(variant in recipe_ing for variant in INGREDIENT_VARIANTS.get(ing, [ing])))
|
99 |
+
|
100 |
if match_count > 0:
|
101 |
matching_recipes.append({
|
102 |
'title': recipe.get('title', 'Untitled Recipe'),
|
|
|
106 |
|
107 |
# If matching recipes found, select one
|
108 |
if matching_recipes:
|
109 |
+
# Choose first matching recipe (could randomize)
|
110 |
+
selected_recipe = matching_recipes[0]
|
111 |
|
112 |
# Format the recipe output
|
113 |
recipe_output = f"Recipe: {selected_recipe['title']}\n\n"
|
|
|
132 |
return "Please enter ingredients before submitting."
|
133 |
|
134 |
validation_result = validate_ingredients(ingredients)
|
135 |
+
if "Valid" in validation_result:
|
136 |
return generate_recipe(ingredients)
|
137 |
else:
|
138 |
return "I'm sorry, but I can't process this request due to invalid ingredients. Please provide valid Indian cooking ingredients!"
|