Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,143 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
user_input = gr.Textbox(placeholder="Type your message here...", lines=1)
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import json
|
3 |
+
from flask import Flask, render_template
|
4 |
+
|
5 |
+
# Initialize Flask app
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
# Food database (same as you provided)
|
9 |
+
FOOD_DATABASE = {
|
10 |
+
"veg": {
|
11 |
+
"paneer": {
|
12 |
+
"lowCalorie": [
|
13 |
+
{"name": "Grilled Paneer Tikka", "description": "Marinated and grilled paneer cubes with spices", "calories": 180, "protein": 14},
|
14 |
+
{"name": "Paneer Bhurji", "description": "Scrambled paneer with vegetables", "calories": 200, "protein": 16}
|
15 |
+
],
|
16 |
+
"highProtein": [
|
17 |
+
{"name": "Protein Paneer Bowl", "description": "High protein paneer with quinoa", "calories": 320, "protein": 24},
|
18 |
+
{"name": "Paneer Steak", "description": "Thick cut paneer steak with herbs", "calories": 280, "protein": 22}
|
19 |
+
]
|
20 |
+
},
|
21 |
+
"mushroom": {
|
22 |
+
"lowCalorie": [
|
23 |
+
{"name": "Grilled Mushroom Caps", "description": "Herb-stuffed mushroom caps", "calories": 120, "protein": 8},
|
24 |
+
{"name": "Mushroom Soup", "description": "Creamy mushroom soup", "calories": 150, "protein": 6}
|
25 |
+
]
|
26 |
+
},
|
27 |
+
"aloo": {
|
28 |
+
"lowCalorie": [
|
29 |
+
{"name": "Baked Potato Wedges", "description": "Spiced and baked potato wedges", "calories": 160, "protein": 4},
|
30 |
+
{"name": "Aloo Tikki", "description": "Spiced potato patties", "calories": 180, "protein": 3}
|
31 |
+
]
|
32 |
+
}
|
33 |
+
},
|
34 |
+
"nonveg": {
|
35 |
+
"chicken": {
|
36 |
+
"highProtein": [
|
37 |
+
{"name": "Grilled Chicken Breast", "description": "Herb-marinated grilled chicken", "calories": 250, "protein": 30},
|
38 |
+
{"name": "Chicken Tikka", "description": "Tandoori spiced chicken pieces", "calories": 280, "protein": 32}
|
39 |
+
]
|
40 |
+
},
|
41 |
+
"fish": {
|
42 |
+
"lowCalorie": [
|
43 |
+
{"name": "Grilled Salmon", "description": "Lemon herb grilled salmon", "calories": 220, "protein": 25},
|
44 |
+
{"name": "Steamed Fish", "description": "Ginger-garlic steamed fish", "calories": 180, "protein": 22}
|
45 |
+
]
|
46 |
+
},
|
47 |
+
"mutton": {
|
48 |
+
"balanced": [
|
49 |
+
{"name": "Mutton Curry", "description": "Traditional spiced mutton curry", "calories": 350, "protein": 28},
|
50 |
+
{"name": "Grilled Lamb Chops", "description": "Herb-crusted lamb chops", "calories": 380, "protein": 32}
|
51 |
+
]
|
52 |
+
}
|
53 |
+
}
|
54 |
+
}
|
55 |
+
|
56 |
+
VEG_INGREDIENTS = ["Paneer", "Mushroom", "Aloo", "Brinjal", "Palak"]
|
57 |
+
NONVEG_TYPES = ["Chicken", "Fish", "Mutton", "Pork"]
|
58 |
+
NUTRITION_OPTIONS = ["Low Calorie", "High Protein", "Low Carb", "Balanced", "Gluten Free"]
|
59 |
|
60 |
+
class ChatState:
|
61 |
+
def __init__(self):
|
62 |
+
self.step = "initial"
|
63 |
+
self.category = ""
|
64 |
+
self.ingredient = ""
|
65 |
|
66 |
+
chat_state = ChatState()
|
|
|
67 |
|
68 |
+
def process_message(message, history):
|
69 |
+
if chat_state.step == "initial":
|
70 |
+
if "yes" in message.lower() or "yeah" in message.lower():
|
71 |
+
chat_state.step = "category"
|
72 |
+
return "Great! Please select your preferred category: Vegetarian, Non-Vegetarian, or Vegan"
|
73 |
+
return "I'm here to help you customize your food. Would you like to proceed? (Yes/No)"
|
74 |
+
|
75 |
+
elif chat_state.step == "category":
|
76 |
+
chat_state.category = message.lower()
|
77 |
+
chat_state.step = "ingredient"
|
78 |
+
|
79 |
+
if "vegetarian" in message.lower():
|
80 |
+
return f"Excellent! What main ingredient would you like? Available options: {', '.join(VEG_INGREDIENTS)}"
|
81 |
+
elif "non-vegetarian" in message.lower():
|
82 |
+
return f"Great choice! What type of meat would you prefer? Available options: {', '.join(NONVEG_TYPES)}"
|
83 |
+
return "Please select either Vegetarian, Non-Vegetarian, or Vegan"
|
84 |
+
|
85 |
+
elif chat_state.step == "ingredient":
|
86 |
+
chat_state.ingredient = message.lower()
|
87 |
+
chat_state.step = "nutrition"
|
88 |
+
return f"Perfect! Now, select your nutrition preference: {', '.join(NUTRITION_OPTIONS)}"
|
89 |
+
|
90 |
+
elif chat_state.step == "nutrition":
|
91 |
+
nutrition = message.lower().replace(" ", "")
|
92 |
+
category = "nonveg" if "non" in chat_state.category else "veg"
|
93 |
+
ingredient = chat_state.ingredient
|
94 |
+
|
95 |
+
try:
|
96 |
+
food_items = FOOD_DATABASE[category][ingredient][nutrition]
|
97 |
+
response = f"Here are some {message} {ingredient} dishes for you:\n"
|
98 |
+
for item in food_items:
|
99 |
+
response += f"\n• {item['name']} ({item['calories']} cal, {item['protein']}g protein)\n {item['description']}"
|
100 |
+
chat_state.step = "initial" # Reset for new conversation
|
101 |
+
return response
|
102 |
+
except:
|
103 |
+
chat_state.step = "initial" # Reset for new conversation
|
104 |
+
return f"I'll help our chef prepare a special {message} {ingredient} dish for you!"
|
105 |
+
|
106 |
+
return "I'm not sure how to help with that. Would you like to start over? (Yes/No)"
|
107 |
|
108 |
+
# Gradio interface for the chatbot
|
109 |
+
def gradio_interface():
|
110 |
+
with gr.Blocks() as demo:
|
111 |
+
chatbot = gr.Chatbot([])
|
112 |
+
msg = gr.Textbox(show_label=False, placeholder="Enter text and press enter")
|
113 |
+
clear = gr.Button("Clear")
|
114 |
+
|
115 |
+
def user(user_message, history):
|
116 |
+
return "", history + [[user_message, None]]
|
117 |
+
|
118 |
+
def bot(history):
|
119 |
+
user_message = history[-1][0]
|
120 |
+
bot_message = process_message(user_message, history)
|
121 |
+
history[-1][1] = bot_message
|
122 |
+
return history
|
123 |
+
|
124 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot]).then(
|
125 |
+
bot, chatbot, chatbot
|
126 |
)
|
127 |
+
|
128 |
+
clear.click(lambda: None, None, chatbot)
|
129 |
+
|
130 |
+
return demo
|
131 |
+
|
132 |
+
@app.route("/")
|
133 |
+
def index():
|
134 |
+
return render_template("index.html")
|
135 |
|
136 |
+
@app.route("/chat")
|
137 |
+
def chat():
|
138 |
+
demo = gradio_interface()
|
139 |
+
demo.launch(inline=True)
|
140 |
+
return "Chatbot launched!"
|
141 |
|
142 |
if __name__ == "__main__":
|
143 |
+
app.run(debug=True)
|