Subbu1304 commited on
Commit
aa772c5
·
verified ·
1 Parent(s): cc0f4d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -2
app.py CHANGED
@@ -121,6 +121,54 @@ def process_message(message, history):
121
 
122
  try:
123
  food_items = FOOD_DATABASE[category][ingredient][nutrition]
124
- response = f"Here are some {message} {ingredient} dishes for you:\n"
125
  for item in food_items:
126
- response += f"\n• {item['name']} ({item
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  try:
123
  food_items = FOOD_DATABASE[category][ingredient][nutrition]
124
+ response = f"Here are some {nutrition} {ingredient} dishes for you:\n"
125
  for item in food_items:
126
+ response += f"\n• {item['name']} ({item['calories']} cal, {item['protein']}g protein)\n {item['description']}"
127
+ chat_state.step = "initial" # Reset for new conversation
128
+ return {
129
+ 'role': 'bot',
130
+ 'content': response
131
+ }, []
132
+ except KeyError:
133
+ chat_state.step = "initial" # Reset for new conversation
134
+ return {
135
+ 'role': 'bot',
136
+ 'content': f"Sorry, I don't have any dishes for this combination."
137
+ }, []
138
+
139
+ return {
140
+ 'role': 'bot',
141
+ 'content': "I'm not sure how to help with that. Would you like to start over?"
142
+ }, []
143
+
144
+ def create_gradio_interface():
145
+ with gr.Blocks() as demo:
146
+ chatbot = gr.Chatbot([], type='messages') # Set the type to 'messages' to avoid warning
147
+ msg = gr.Textbox(show_label=False, placeholder="Enter text and press enter")
148
+ clear = gr.Button("Clear")
149
+
150
+ # Buttons for category selection will appear in the chat stream
151
+ def user(user_message, history):
152
+ return "", history + [[user_message, None]]
153
+
154
+ def bot(history):
155
+ user_message = history[-1][0]
156
+ bot_message, buttons = process_message(user_message, history)
157
+ history[-1][1] = bot_message
158
+
159
+ # Display buttons for category selection after the initial message
160
+ if buttons:
161
+ button_elements = [gr.Button(button) for button in buttons]
162
+ return history + [[bot_message, button_elements]]
163
+
164
+ return history
165
+
166
+ msg.submit(user, [msg, chatbot], [msg, chatbot]).then(bot, chatbot, chatbot)
167
+ clear.click(lambda: None, None, chatbot)
168
+
169
+ return demo
170
+
171
+ # Launch the Gradio app
172
+ if __name__ == "__main__":
173
+ demo = create_gradio_interface()
174
+ demo.launch(share=True)