Subbu1304 commited on
Commit
910a44f
·
verified ·
1 Parent(s): e885b2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -119
app.py CHANGED
@@ -1,130 +1,83 @@
1
  import gradio as gr
2
 
3
- # Food database (same as your provided code)
4
- FOOD_DATABASE = {
5
- "veg": {
6
- "paneer": {
7
- "lowCalorie": [
8
- {"name": "Grilled Paneer Tikka", "description": "Marinated and grilled paneer cubes with spices", "calories": 180, "protein": 14},
9
- {"name": "Paneer Bhurji", "description": "Scrambled paneer with vegetables", "calories": 200, "protein": 16}
10
- ],
11
- "highProtein": [
12
- {"name": "Protein Paneer Bowl", "description": "High protein paneer with quinoa", "calories": 320, "protein": 24},
13
- {"name": "Paneer Steak", "description": "Thick cut paneer steak with herbs", "calories": 280, "protein": 22}
14
- ]
15
- },
16
- "mushroom": {
17
- "lowCalorie": [
18
- {"name": "Grilled Mushroom Caps", "description": "Herb-stuffed mushroom caps", "calories": 120, "protein": 8},
19
- {"name": "Mushroom Soup", "description": "Creamy mushroom soup", "calories": 150, "protein": 6}
20
- ]
21
- },
22
- "aloo": {
23
- "lowCalorie": [
24
- {"name": "Baked Potato Wedges", "description": "Spiced and baked potato wedges", "calories": 160, "protein": 4},
25
- {"name": "Aloo Tikki", "description": "Spiced potato patties", "calories": 180, "protein": 3}
26
- ]
27
- }
28
- },
29
- "nonveg": {
30
- "chicken": {
31
- "highProtein": [
32
- {"name": "Grilled Chicken Breast", "description": "Herb-marinated grilled chicken", "calories": 250, "protein": 30},
33
- {"name": "Chicken Tikka", "description": "Tandoori spiced chicken pieces", "calories": 280, "protein": 32}
34
- ]
35
- },
36
- "fish": {
37
- "lowCalorie": [
38
- {"name": "Grilled Salmon", "description": "Lemon herb grilled salmon", "calories": 220, "protein": 25},
39
- {"name": "Steamed Fish", "description": "Ginger-garlic steamed fish", "calories": 180, "protein": 22}
40
- ]
41
- },
42
- "mutton": {
43
- "balanced": [
44
- {"name": "Mutton Curry", "description": "Traditional spiced mutton curry", "calories": 350, "protein": 28},
45
- {"name": "Grilled Lamb Chops", "description": "Herb-crusted lamb chops", "calories": 380, "protein": 32}
46
- ]
47
- }
48
- }
49
- }
50
-
51
- VEG_INGREDIENTS = ["Paneer", "Mushroom", "Aloo", "Brinjal", "Palak"]
52
- NONVEG_TYPES = ["Chicken", "Fish", "Mutton", "Pork"]
53
- NUTRITION_OPTIONS = ["Low Calorie", "High Protein", "Low Carb", "Balanced", "Gluten Free"]
54
 
55
- class ChatState:
56
- def __init__(self):
57
- self.step = "initial"
58
- self.category = ""
59
- self.ingredient = ""
60
 
61
- chat_state = ChatState()
 
 
 
62
 
63
- def process_message(message, history):
64
- if chat_state.step == "initial":
65
- if "yes" in message.lower() or "yeah" in message.lower():
66
- chat_state.step = "category"
67
- return [{"role": "bot", "content": "Great! Please select your preferred category: Vegetarian or Non-Vegetarian."}], []
68
-
69
- return [{"role": "bot", "content": "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"
74
-
75
- if "vegetarian" in message.lower():
76
- return [{"role": "bot", "content": f"Great! What main ingredient would you like? Available options: {', '.join(VEG_INGREDIENTS)}"}], []
77
- elif "non-vegetarian" in message.lower():
78
- return [{"role": "bot", "content": f"Great! What type of meat would you prefer? Available options: {', '.join(NONVEG_TYPES)}"}], []
79
-
80
- return [{"role": "bot", "content": "Please select either Vegetarian or Non-Vegetarian."}], []
81
-
82
- elif chat_state.step == "ingredient":
83
- chat_state.ingredient = message.lower()
84
- chat_state.step = "nutrition"
85
-
86
- return [{"role": "bot", "content": f"Perfect! Now, select your nutrition preference: {', '.join(NUTRITION_OPTIONS)}"}], []
87
-
88
- elif chat_state.step == "nutrition":
89
- nutrition = message.lower().replace(" ", "")
90
- category = "nonveg" if "non" in chat_state.category else "veg"
91
- ingredient = chat_state.ingredient
92
-
93
- try:
94
- food_items = FOOD_DATABASE[category][ingredient][nutrition]
95
- response = f"Here are some {nutrition} {ingredient} dishes for you:\n"
96
- for item in food_items:
97
- response += f"\n• {item['name']} ({item['calories']} cal, {item['protein']}g protein)\n {item['description']}"
98
- chat_state.step = "initial" # Reset for new conversation
99
- return [{"role": "bot", "content": response}], []
100
- except KeyError:
101
- chat_state.step = "initial" # Reset for new conversation
102
- return [{"role": "bot", "content": f"Sorry, I don't have any dishes for this combination."}], []
103
 
104
- return [{"role": "bot", "content": "I'm not sure how to help with that. Would you like to start over?"}], []
 
 
 
 
 
 
 
 
105
 
106
- def create_gradio_interface():
107
- with gr.Blocks() as demo:
108
- chatbot = gr.Chatbot([], type="messages") # Set the type to 'messages' to avoid warning
109
- msg = gr.Textbox(show_label=False, placeholder="Enter text and press enter")
110
- clear = gr.Button("Clear")
111
-
112
- def user(user_message, history):
113
- return "", history + [{"role": "user", "content": user_message}]
 
 
 
 
 
 
114
 
115
- def bot(history):
116
- user_message = history[-1]["content"]
117
- bot_message, buttons = process_message(user_message, history)
118
- history[-1]["content"] = bot_message[0]["content"] # Update the message content
119
-
120
- return history
121
 
122
- msg.submit(user, [msg, chatbot], [msg, chatbot]).then(bot, chatbot, chatbot)
123
- clear.click(lambda: None, None, chatbot)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
 
125
- return demo
 
 
 
 
 
126
 
127
- # Launch the Gradio app
128
- if __name__ == "__main__":
129
- demo = create_gradio_interface()
130
- demo.launch(share=True)
 
1
  import gradio as gr
2
 
3
+ # Function that handles the user input and provides responses
4
+ def chatbot(input_text, state):
5
+ # Initializing state if None
6
+ if state is None:
7
+ state = {"step": 1}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ # Step 1: Ask what the user wants to customize
10
+ if state["step"] == 1:
11
+ state["step"] = 2
12
+ return "Hey, what would you like to customize?", state
 
13
 
14
+ # Step 2: Ask if the user wants veg or nonveg
15
+ if state["step"] == 2:
16
+ state["step"] = 3
17
+ return "Would you like to customize a veg or nonveg dish?", state
18
 
19
+ # Step 3: Ask for nutritional selection (based on veg or nonveg)
20
+ if state["step"] == 3:
21
+ if input_text.lower() in ["veg", "nonveg"]:
22
+ state["step"] = 4
23
+ return f"Great! You've selected {input_text}. Now, what are your nutritional preferences?", state
24
+ return "Please select either 'veg' or 'nonveg'.", state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # Step 4: Show ingredients based on veg or nonveg
27
+ if state["step"] == 4:
28
+ if input_text.lower() == "veg":
29
+ state["step"] = 5
30
+ return "Please choose an ingredient: Paneer, Gobi, Babycorn, Spinach, Mushroom, Tofu, Corn, Peas, Broccoli, Cauliflower", state
31
+ elif input_text.lower() == "nonveg":
32
+ state["step"] = 6
33
+ return "Please choose an ingredient: Fish, Chicken, Mutton, Prawns, Duck, Turkey, Crab, Lobster, Shrimp, Squid", state
34
+ return "Please select either 'veg' or 'nonveg'.", state
35
 
36
+ # Step 5: Show dish items based on ingredient selection for veg
37
+ if state["step"] == 5:
38
+ veg_items = {
39
+ "paneer": ["Paneer Tikka", "Paneer Butter Masala", "Paneer Lababdar", "Paneer Makhani", "Palak Paneer", "Paneer Pakora", "Paneer Bhurji", "Shahi Paneer", "Kadai Paneer", "Paneer Kofta"],
40
+ "gobi": ["Gobi Manchurian", "Aloo Gobi", "Gobi Paratha", "Gobi Fry", "Gobi Masala", "Gobi Do Pyaza", "Gobi Curry", "Gobi Pulao", "Gobi Pakora", "Gobi Stir Fry"],
41
+ "babycorn": ["Babycorn Fry", "Babycorn Manchurian", "Babycorn Pakora", "Babycorn Masala", "Babycorn Pulao", "Babycorn Stir Fry", "Babycorn Biryani", "Babycorn Curry", "Babycorn Tikka", "Babycorn Chutney"],
42
+ "spinach": ["Palak Paneer", "Spinach Soup", "Spinach Paratha", "Palak Chaat", "Spinach Curry", "Spinach Pulao", "Spinach Biryani", "Spinach and Cheese Toast", "Spinach Frittata", "Spinach Pakora"],
43
+ "mushroom": ["Mushroom Masala", "Mushroom Soup", "Mushroom Biryani", "Mushroom Curry", "Mushroom Stir Fry", "Mushroom Tikka", "Mushroom Paratha", "Mushroom Pulao", "Mushroom Manchurian", "Mushroom Pakora"],
44
+ "tofu": ["Tofu Stir Fry", "Tofu Curry", "Tofu Tikka", "Tofu Soup", "Tofu Salad", "Tofu Stir Fried Rice", "Tofu Pulao", "Tofu Pakora", "Tofu Biryani", "Tofu Wrap"],
45
+ "corn": ["Sweet Corn Soup", "Corn Pakora", "Corn Stir Fry", "Corn Tikki", "Corn Pulao", "Corn Biryani", "Corn Masala", "Corn Fritters", "Corn Salad", "Corn and Peas Curry"],
46
+ "peas": ["Matar Paneer", "Matar Masala", "Matar Pulao", "Matar Tikki", "Matar Paratha", "Matar Pakora", "Matar Chaat", "Matar Biryani", "Matar Curry", "Matar Stir Fry"],
47
+ "broccoli": ["Broccoli Stir Fry", "Broccoli Soup", "Broccoli Salad", "Broccoli Pakora", "Broccoli Tikki", "Broccoli Curry", "Broccoli Pulao", "Broccoli Frittata", "Broccoli Manchurian", "Broccoli Cheese Bake"],
48
+ "cauliflower": ["Gobi Manchurian", "Aloo Gobi", "Gobi Pakora", "Gobi Tikka", "Gobi Masala", "Gobi Pulao", "Aloo Gobi Paratha", "Gobi Curry", "Gobi Stir Fry", "Gobi Cheese Bake"]
49
+ }
50
 
51
+ ingredient = input_text.lower()
52
+ if ingredient in veg_items:
53
+ return f"Here are some {ingredient} dishes: {', '.join(veg_items[ingredient])}", state
54
+ return "Please select a valid ingredient from the list.", state
 
 
55
 
56
+ # Step 6: Show dish items based on ingredient selection for nonveg
57
+ if state["step"] == 6:
58
+ nonveg_items = {
59
+ "fish": ["Fish Curry", "Grilled Fish", "Fish Fry", "Fish Tikka", "Fish Biryani", "Fish Korma", "Fish Masala", "Fish Soup", "Fish and Chips", "Fish Amritsari"],
60
+ "chicken": ["Chicken Curry", "Chicken Biryani", "Grilled Chicken", "Chicken Tikka", "Chicken Pakora", "Chicken Fry", "Chicken Korma", "Chicken Masala", "Chicken Shawarma", "Chicken Pulao"],
61
+ "mutton": ["Mutton Rogan Josh", "Mutton Biryani", "Mutton Curry", "Mutton Korma", "Mutton Fry", "Mutton Seekh Kebab", "Mutton Nihari", "Mutton Do Pyaza", "Mutton Masala", "Mutton Kofta"],
62
+ "prawns": ["Prawn Curry", "Prawn Masala", "Prawn Biryani", "Prawn Fry", "Prawn Tikka", "Prawn Soup", "Prawn Pulao", "Prawn Korma", "Prawn Grill", "Prawn Prawn Pakora"],
63
+ "duck": ["Duck Curry", "Duck Roast", "Duck Biryani", "Duck Korma", "Duck Fry", "Duck Soup", "Duck Stir Fry", "Duck Tikka", "Duck Pulao", "Duck Shawarma"],
64
+ "turkey": ["Turkey Roast", "Turkey Biryani", "Turkey Soup", "Turkey Masala", "Turkey Korma", "Turkey Stir Fry", "Turkey Tikka", "Turkey Grill", "Turkey Pulao", "Turkey Tacos"],
65
+ "crab": ["Crab Curry", "Crab Fry", "Crab Soup", "Crab Masala", "Crab Biryani", "Crab Pulao", "Crab Cakes", "Crab Tikka", "Crab Stir Fry", "Crab Roast"],
66
+ "lobster": ["Lobster Curry", "Lobster Grilled", "Lobster Soup", "Lobster Biryani", "Lobster Masala", "Lobster Tikka", "Lobster Stir Fry", "Lobster Pulao", "Lobster Shawarma", "Lobster Pakora"],
67
+ "shrimp": ["Shrimp Biryani", "Shrimp Curry", "Shrimp Tikka", "Shrimp Masala", "Shrimp Fry", "Shrimp Pulao", "Shrimp Soup", "Shrimp Grill", "Shrimp Korma", "Shrimp Pakora"],
68
+ "squid": ["Squid Curry", "Squid Masala", "Grilled Squid", "Squid Soup", "Squid Stir Fry", "Squid Biryani", "Squid Tikka", "Squid Pakora", "Squid Pulao", "Squid Korma"]
69
+ }
70
+
71
+ ingredient = input_text.lower()
72
+ if ingredient in nonveg_items:
73
+ return f"Here are some {ingredient} dishes: {', '.join(nonveg_items[ingredient])}", state
74
+ return "Please select a valid ingredient from the list.", state
75
 
76
+ # Set up the Gradio interface
77
+ iface = gr.Interface(fn=chatbot,
78
+ inputs=["text", "state"],
79
+ outputs=["text", "state"],
80
+ live=True,
81
+ allow_flagging="never")
82
 
83
+ iface.launch()