Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
# Food database
|
| 4 |
FOOD_DATABASE = {
|
| 5 |
"veg": {
|
| 6 |
"paneer": {
|
|
@@ -62,12 +62,14 @@ class ChatState:
|
|
| 62 |
chat_state = ChatState()
|
| 63 |
|
| 64 |
def process_message(message, history):
|
|
|
|
| 65 |
if chat_state.step == "initial":
|
| 66 |
if "yes" in message.lower() or "yeah" in message.lower():
|
| 67 |
chat_state.step = "category"
|
| 68 |
return "Great! Please select your preferred category: Vegetarian, Non-Vegetarian."
|
| 69 |
return "I'm here to help you customize your food. Would you like to proceed? (Yes/No)"
|
| 70 |
|
|
|
|
| 71 |
elif chat_state.step == "category":
|
| 72 |
chat_state.category = message.lower()
|
| 73 |
chat_state.step = "ingredient"
|
|
@@ -78,21 +80,36 @@ def process_message(message, history):
|
|
| 78 |
return f"Great! What type of meat would you prefer? Available options: {', '.join(NONVEG_TYPES)}"
|
| 79 |
return "Please select either Vegetarian or Non-Vegetarian."
|
| 80 |
|
|
|
|
| 81 |
elif chat_state.step == "ingredient":
|
| 82 |
-
chat_state.ingredient = message.lower()
|
| 83 |
chat_state.step = "nutrition"
|
| 84 |
return f"Perfect! Now, select your nutrition preference: {', '.join(NUTRITION_OPTIONS)}"
|
| 85 |
|
|
|
|
| 86 |
elif chat_state.step == "nutrition":
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
category = "nonveg" if "non" in chat_state.category else "veg"
|
| 89 |
ingredient = chat_state.ingredient
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
try:
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
for item in food_items:
|
| 95 |
response += f"\n• {item['name']} ({item['calories']} cal, {item['protein']}g protein)\n {item['description']}"
|
|
|
|
| 96 |
chat_state.step = "initial" # Reset for new conversation
|
| 97 |
return response
|
| 98 |
except KeyError:
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Food database
|
| 4 |
FOOD_DATABASE = {
|
| 5 |
"veg": {
|
| 6 |
"paneer": {
|
|
|
|
| 62 |
chat_state = ChatState()
|
| 63 |
|
| 64 |
def process_message(message, history):
|
| 65 |
+
# Step 1: If initial step (asks if user wants to proceed)
|
| 66 |
if chat_state.step == "initial":
|
| 67 |
if "yes" in message.lower() or "yeah" in message.lower():
|
| 68 |
chat_state.step = "category"
|
| 69 |
return "Great! Please select your preferred category: Vegetarian, Non-Vegetarian."
|
| 70 |
return "I'm here to help you customize your food. Would you like to proceed? (Yes/No)"
|
| 71 |
|
| 72 |
+
# Step 2: Category selection
|
| 73 |
elif chat_state.step == "category":
|
| 74 |
chat_state.category = message.lower()
|
| 75 |
chat_state.step = "ingredient"
|
|
|
|
| 80 |
return f"Great! What type of meat would you prefer? Available options: {', '.join(NONVEG_TYPES)}"
|
| 81 |
return "Please select either Vegetarian or Non-Vegetarian."
|
| 82 |
|
| 83 |
+
# Step 3: Ingredient selection
|
| 84 |
elif chat_state.step == "ingredient":
|
| 85 |
+
chat_state.ingredient = message.lower().strip() # Ensure no extra spaces or case issues
|
| 86 |
chat_state.step = "nutrition"
|
| 87 |
return f"Perfect! Now, select your nutrition preference: {', '.join(NUTRITION_OPTIONS)}"
|
| 88 |
|
| 89 |
+
# Step 4: Nutrition preference
|
| 90 |
elif chat_state.step == "nutrition":
|
| 91 |
+
# Sanitize and normalize input (remove spaces, handle case)
|
| 92 |
+
nutrition = message.lower().replace(" ", "").strip() # Remove spaces and make lowercase
|
| 93 |
+
chat_state.nutrition = nutrition
|
| 94 |
category = "nonveg" if "non" in chat_state.category else "veg"
|
| 95 |
ingredient = chat_state.ingredient
|
| 96 |
+
|
| 97 |
+
# Check if the ingredient exists in the database for the selected category
|
| 98 |
+
if ingredient not in FOOD_DATABASE[category]:
|
| 99 |
+
chat_state.step = "initial" # Reset for new conversation
|
| 100 |
+
return f"Sorry, I don't have any data for {ingredient}. Please select another ingredient."
|
| 101 |
+
|
| 102 |
+
# Try to fetch the food items from the correct category, ingredient, and nutrition preference
|
| 103 |
try:
|
| 104 |
+
# Access food items based on category, ingredient, and nutrition preference
|
| 105 |
+
food_items = FOOD_DATABASE[category][ingredient][nutrition]
|
| 106 |
+
if not food_items:
|
| 107 |
+
return f"Sorry, no {nutrition.replace('_', ' ').title()} dishes are available for {ingredient}."
|
| 108 |
+
|
| 109 |
+
response = f"Here are some {nutrition.replace('_', ' ').title()} {ingredient} dishes for you:\n"
|
| 110 |
for item in food_items:
|
| 111 |
response += f"\n• {item['name']} ({item['calories']} cal, {item['protein']}g protein)\n {item['description']}"
|
| 112 |
+
|
| 113 |
chat_state.step = "initial" # Reset for new conversation
|
| 114 |
return response
|
| 115 |
except KeyError:
|