Chinez-dev commited on
Commit
6456c87
·
verified ·
1 Parent(s): 66f737f

Update Gradio_UI.py

Browse files
Files changed (1) hide show
  1. Gradio_UI.py +101 -11
Gradio_UI.py CHANGED
@@ -194,11 +194,35 @@ class GradioUI:
194
 
195
  messages.append(gr.ChatMessage(role="user", content=prompt))
196
  yield messages
 
 
 
 
 
 
 
 
 
 
 
197
  for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
 
 
 
198
  messages.append(msg)
199
  yield messages
 
 
 
 
200
  yield messages
201
 
 
 
 
 
 
 
202
  def upload_file(
203
  self,
204
  file,
@@ -261,7 +285,26 @@ class GradioUI:
261
  def launch(self, **kwargs):
262
  import gradio as gr
263
 
264
- with gr.Blocks(fill_height=True) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  stored_messages = gr.State([])
266
  file_uploads_log = gr.State([])
267
  chatbot = gr.Chatbot(
@@ -274,22 +317,69 @@ class GradioUI:
274
  resizeable=True,
275
  scale=1,
276
  )
277
- # If an upload folder is provided, enable the upload feature
278
- if self.file_upload_folder is not None:
279
- upload_file = gr.File(label="Upload a file")
280
- upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
281
- upload_file.change(
282
- self.upload_file,
283
- [upload_file, file_uploads_log],
284
- [upload_status, file_uploads_log],
285
- )
286
- text_input = gr.Textbox(lines=1, label="Chat Message")
 
 
 
 
 
 
 
 
 
 
 
287
  text_input.submit(
288
  self.log_user_message,
289
  [text_input, file_uploads_log],
290
  [stored_messages, text_input],
291
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  demo.launch(debug=True, share=True, **kwargs)
294
 
295
 
 
194
 
195
  messages.append(gr.ChatMessage(role="user", content=prompt))
196
  yield messages
197
+
198
+ # Add loading message
199
+ loading_msg = gr.ChatMessage(
200
+ role="assistant",
201
+ content="Thinking...🤔👩🏽‍⚕️👨🏽‍⚕️",
202
+ metadata={"class": "loading-message"}
203
+ )
204
+ messages.append(loading_msg)
205
+ yield messages
206
+
207
+ # Process agent response
208
  for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
209
+ # Remove loading message before adding real responses
210
+ if len(messages) > 0 and messages[-1].metadata.get("class") == "loading-message":
211
+ messages.pop()
212
  messages.append(msg)
213
  yield messages
214
+
215
+ # Ensure loading message is removed at the end
216
+ if len(messages) > 0 and messages[-1].metadata.get("class") == "loading-message":
217
+ messages.pop()
218
  yield messages
219
 
220
+
221
+ # for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
222
+ # messages.append(msg)
223
+ # yield messages
224
+ # yield messages
225
+
226
  def upload_file(
227
  self,
228
  file,
 
285
  def launch(self, **kwargs):
286
  import gradio as gr
287
 
288
+ with gr.Blocks(
289
+ title="Health Agent",
290
+ fill_height=True,
291
+ css="""
292
+ .loading-message {
293
+ animation: pulse 2s cubic-bezier(.4,0,.6,1) infinite;
294
+ }
295
+ @keyframes pulse {
296
+ 0%, 100% { opacity: 1; }
297
+ 50% { opacity: 0.5; }
298
+ }
299
+ """
300
+ ) as demo:
301
+ gr.Markdown(
302
+ """
303
+ # NELA - AI-Powered Health Advisor
304
+ I am NELA, your AI-powered health advisor. I can help you track your health, provide symptom analysis, suggest possible diagnoses, and offer wellness recommendations.
305
+ Ask me about symptoms, treatments, medications, or how to maintain a healthy lifestyle!
306
+ """
307
+ )
308
  stored_messages = gr.State([])
309
  file_uploads_log = gr.State([])
310
  chatbot = gr.Chatbot(
 
317
  resizeable=True,
318
  scale=1,
319
  )
320
+ text_input = gr.Textbox(
321
+ label="Ask me anything about your health",
322
+ placeholder="Type your question here...",
323
+ )
324
+ submit_button = gr.Button("Send")
325
+ gr.Examples(
326
+ examples=[
327
+ "What could be the possible causes of persistent headaches?",
328
+ "Suggest some home remedies for managing cold and flu symptoms.",
329
+ "What are the early signs of diabetes?",
330
+ "How can I lower my blood pressure naturally?",
331
+ "What should I do if I experience sudden chest pain?",
332
+ "Which foods are best for boosting my immune system?",
333
+ "How can I track my daily health using smart devices?",
334
+ "What are common symptoms of dehydration and how can I prevent it?",
335
+ "Explain the importance of regular health check-ups.",
336
+ "What should I include in a balanced diet for better heart health?"
337
+ ],
338
+ inputs=text_input,
339
+ label="Example Questions"
340
+ )
341
  text_input.submit(
342
  self.log_user_message,
343
  [text_input, file_uploads_log],
344
  [stored_messages, text_input],
345
  ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
346
 
347
+ submit_button.click(
348
+ self.log_user_message,
349
+ [text_input, file_uploads_log],
350
+ [stored_messages, text_input],
351
+ ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
352
+
353
+
354
+ # with gr.Blocks(fill_height=True) as demo:
355
+ # stored_messages = gr.State([])
356
+ # file_uploads_log = gr.State([])
357
+ # chatbot = gr.Chatbot(
358
+ # label="Agent",
359
+ # type="messages",
360
+ # avatar_images=(
361
+ # None,
362
+ # "https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
363
+ # ),
364
+ # resizeable=True,
365
+ # scale=1,
366
+ # )
367
+ # # If an upload folder is provided, enable the upload feature
368
+ # if self.file_upload_folder is not None:
369
+ # upload_file = gr.File(label="Upload a file")
370
+ # upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
371
+ # upload_file.change(
372
+ # self.upload_file,
373
+ # [upload_file, file_uploads_log],
374
+ # [upload_status, file_uploads_log],
375
+ # )
376
+ # text_input = gr.Textbox(lines=1, label="Chat Message")
377
+ # text_input.submit(
378
+ # self.log_user_message,
379
+ # [text_input, file_uploads_log],
380
+ # [stored_messages, text_input],
381
+ # ).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
382
+
383
  demo.launch(debug=True, share=True, **kwargs)
384
 
385