Subbu1304 commited on
Commit
acdab73
·
verified ·
1 Parent(s): a5bdb90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py CHANGED
@@ -99,3 +99,71 @@ def process_message(message, history):
99
  food_items = FOOD_DATABASE[category][chat_state.ingredient]
100
  food_item_names = [item['name'] for nutrition in food_items for item in food_items[nutrition]]
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  food_items = FOOD_DATABASE[category][chat_state.ingredient]
100
  food_item_names = [item['name'] for nutrition in food_items for item in food_items[nutrition]]
101
 
102
+ # Show available food items and move to nutrition selection
103
+ chat_state.step = "nutrition"
104
+ return f"Great! Here are the available food items for {message}:\n{', '.join(food_item_names)}\nNow, select your nutrition preference: {', '.join(NUTRITION_OPTIONS)}"
105
+
106
+ # Step 4: Nutrition preference
107
+ elif chat_state.step == "nutrition":
108
+ # Sanitize and normalize input (remove spaces, handle case)
109
+ nutrition = message.lower().replace(" ", "").strip() # Remove spaces and make lowercase
110
+ chat_state.nutrition = nutrition
111
+ category = "nonveg" if "non" in chat_state.category else "veg"
112
+ ingredient = chat_state.ingredient
113
+
114
+ # Try to fetch the food items from the correct category, ingredient, and nutrition preference
115
+ try:
116
+ # Access food items based on category, ingredient, and nutrition preference
117
+ food_items = FOOD_DATABASE[category][ingredient][nutrition]
118
+ if not food_items:
119
+ return f"Sorry, no {nutrition.replace('_', ' ').title()} dishes are available for {ingredient}."
120
+
121
+ response = f"Here are some {nutrition.replace('_', ' ').title()} {ingredient} dishes for you:\n"
122
+ for item in food_items:
123
+ response += f"\n• {item['name']} ({item['calories']} cal, {item['protein']}g protein)\n {item['description']}"
124
+
125
+ chat_state.step = "initial" # Reset for new conversation
126
+ return response
127
+ except KeyError:
128
+ chat_state.step = "initial" # Reset for new conversation
129
+ return f"Sorry, I don't have any dishes for this combination."
130
+
131
+ return "I'm not sure how to help with that. Would you like to start over?"
132
+
133
+ # Gradio interface for the chatbot
134
+ def create_gradio_interface():
135
+ with gr.Blocks() as demo:
136
+ category_btns = gr.Radio(choices=["Vegetarian", "Non-Vegetarian"], label="Select Category")
137
+ ingredient_btns = gr.Radio(choices=VEG_INGREDIENTS + NONVEG_TYPES, label="Select Ingredient")
138
+ nutrition_btns = gr.Radio(choices=NUTRITION_OPTIONS, label="Select Nutrition")
139
+ chatbot = gr.Chatbot([])
140
+ msg = gr.Textbox(show_label=False, placeholder="Enter text and press enter")
141
+ clear = gr.Button("Clear")
142
+
143
+ # Function for user message input and history handling
144
+ def user(user_message, history):
145
+ return "", history + [[user_message, None]]
146
+
147
+ # Function to handle bot's response
148
+ def bot(history):
149
+ user_message = history[-1][0]
150
+ bot_message = process_message(user_message, history)
151
+ history[-1][1] = bot_message
152
+ return history
153
+
154
+ # Handle category selection and pass to the next steps
155
+ category_btns.change(lambda category: process_message(category, []), category_btns, chatbot)
156
+ ingredient_btns.change(lambda ingredient: process_message(ingredient, []), ingredient_btns, chatbot)
157
+ nutrition_btns.change(lambda nutrition: process_message(nutrition, []), nutrition_btns, chatbot)
158
+
159
+ msg.submit(user, [msg, chatbot], [msg, chatbot]).then(bot, chatbot, chatbot)
160
+ clear.click(lambda: None, None, chatbot)
161
+
162
+ return demo
163
+
164
+ # Start the Gradio interface and Flask app together
165
+ if __name__ == "__main__":
166
+ demo = create_gradio_interface()
167
+ demo.launch(share=True, inline=True)
168
+ # Run Flask app
169
+ app.run(debug=True, use_reloader=False)