Spaces:
Sleeping
Sleeping
Update
Browse files- app.py +48 -10
- requirements.txt +2 -2
app.py
CHANGED
@@ -34,16 +34,43 @@ def handle_user_turn(user_input, chat_history, user_role, sid):
|
|
34 |
if not user_input.strip():
|
35 |
return chat_history, ""
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
if not MODAL_BACKEND_URL:
|
40 |
-
|
|
|
41 |
return chat_history, ""
|
42 |
|
43 |
thinking_steps = ["Analyzing...", "Searching sources...", "Synthesizing..."]
|
|
|
|
|
|
|
|
|
|
|
44 |
for step in thinking_steps:
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
time.sleep(0.4)
|
49 |
|
@@ -57,8 +84,14 @@ def handle_user_turn(user_input, chat_history, user_role, sid):
|
|
57 |
reply = response.json().get("final_markdown", "No response received.")
|
58 |
except Exception as e:
|
59 |
reply = f"❗ Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
chat_history[-1] = (user_input, reply)
|
62 |
yield chat_history, ""
|
63 |
|
64 |
with gr.Blocks(css=CUSTOM_CSS, title="MediAgent AI") as demo:
|
@@ -74,10 +107,12 @@ with gr.Blocks(css=CUSTOM_CSS, title="MediAgent AI") as demo:
|
|
74 |
<li>🗣 Ask in English, Spanish, Hindi, or French</li>
|
75 |
<li>🎯 <b>Patient</b> = simplified advice | <b>Doctor</b> = in-depth, technical insights</li>
|
76 |
<li>🔗 Answers are backed by PubMed, FDA, etc.</li>
|
|
|
|
|
77 |
<li>🛡️ Detects emergencies, adds safety notices</li>
|
78 |
</ul>
|
79 |
</div>
|
80 |
-
""")
|
81 |
|
82 |
with gr.Row():
|
83 |
with gr.Column(scale=1):
|
@@ -88,7 +123,7 @@ with gr.Blocks(css=CUSTOM_CSS, title="MediAgent AI") as demo:
|
|
88 |
info="Choose your user type for tailored responses"
|
89 |
)
|
90 |
with gr.Column(scale=3):
|
91 |
-
chatbot = gr.Chatbot(label="Conversation", height=480)
|
92 |
|
93 |
with gr.Row():
|
94 |
user_textbox = gr.Textbox(
|
@@ -102,10 +137,13 @@ with gr.Blocks(css=CUSTOM_CSS, title="MediAgent AI") as demo:
|
|
102 |
examples=[
|
103 |
"What are the side effects of metformin?",
|
104 |
"Explain Crohn’s disease vs Ulcerative Colitis for a doctor",
|
105 |
-
"¿Cuáles son los síntomas de la hipertensión?"
|
|
|
|
|
|
|
106 |
],
|
107 |
inputs=[user_textbox]
|
108 |
-
)
|
109 |
|
110 |
gr.ClearButton([chatbot, user_textbox], value="🔄 Clear")
|
111 |
|
@@ -123,4 +161,4 @@ with gr.Blocks(css=CUSTOM_CSS, title="MediAgent AI") as demo:
|
|
123 |
|
124 |
if __name__ == "__main__":
|
125 |
demo.queue()
|
126 |
-
demo.launch()
|
|
|
34 |
if not user_input.strip():
|
35 |
return chat_history, ""
|
36 |
|
37 |
+
# Gradio's 'messages' type for chatbot expects dictionaries
|
38 |
+
# Ensure chat_history is initialized with dictionaries or converted
|
39 |
+
if not chat_history or not isinstance(chat_history[0], dict):
|
40 |
+
# Convert existing tuples to dictionaries if needed, or start fresh
|
41 |
+
new_chat_history = []
|
42 |
+
for user_msg, bot_msg in chat_history:
|
43 |
+
if user_msg is not None:
|
44 |
+
new_chat_history.append({"role": "user", "content": user_msg})
|
45 |
+
if bot_msg is not None:
|
46 |
+
new_chat_history.append({"role": "assistant", "content": bot_msg})
|
47 |
+
chat_history = new_chat_history
|
48 |
+
|
49 |
+
chat_history.append({"role": "user", "content": user_input}) # Changed to dictionary format
|
50 |
|
51 |
if not MODAL_BACKEND_URL:
|
52 |
+
# For an error, append as assistant message
|
53 |
+
chat_history.append({"role": "assistant", "content": "❗ Configuration error: `MODAL_BACKEND_URL` not set."})
|
54 |
return chat_history, ""
|
55 |
|
56 |
thinking_steps = ["Analyzing...", "Searching sources...", "Synthesizing..."]
|
57 |
+
|
58 |
+
# Store the last user message to update it later
|
59 |
+
# This assumes the last message is always the user's latest input
|
60 |
+
last_user_message_index = len(chat_history) - 1
|
61 |
+
|
62 |
for step in thinking_steps:
|
63 |
+
# Create a temporary 'thinking' message from the assistant
|
64 |
+
temp_message = {"role": "assistant", "content": f"<div class='chatbot-message bot'><em>{step}</em></div>"}
|
65 |
+
|
66 |
+
# If there's an existing assistant thinking message, update it in place
|
67 |
+
# Otherwise, append a new one
|
68 |
+
if len(chat_history) > last_user_message_index + 1 and chat_history[last_user_message_index + 1]["role"] == "assistant":
|
69 |
+
chat_history[last_user_message_index + 1] = temp_message
|
70 |
+
else:
|
71 |
+
chat_history.append(temp_message)
|
72 |
+
|
73 |
+
yield chat_history, "" # Yield with the temporary thinking message
|
74 |
|
75 |
time.sleep(0.4)
|
76 |
|
|
|
84 |
reply = response.json().get("final_markdown", "No response received.")
|
85 |
except Exception as e:
|
86 |
reply = f"❗ Error: {str(e)}"
|
87 |
+
|
88 |
+
# Update the last assistant message with the final reply
|
89 |
+
# This should replace the thinking message or be appended if no thinking message was added
|
90 |
+
if len(chat_history) > last_user_message_index + 1 and chat_history[last_user_message_index + 1]["role"] == "assistant":
|
91 |
+
chat_history[last_user_message_index + 1] = {"role": "assistant", "content": reply}
|
92 |
+
else:
|
93 |
+
chat_history.append({"role": "assistant", "content": reply}) # Should not typically happen if thinking steps run
|
94 |
|
|
|
95 |
yield chat_history, ""
|
96 |
|
97 |
with gr.Blocks(css=CUSTOM_CSS, title="MediAgent AI") as demo:
|
|
|
107 |
<li>🗣 Ask in English, Spanish, Hindi, or French</li>
|
108 |
<li>🎯 <b>Patient</b> = simplified advice | <b>Doctor</b> = in-depth, technical insights</li>
|
109 |
<li>🔗 Answers are backed by PubMed, FDA, etc.</li>
|
110 |
+
<li>💊 Can provide information on **drug side effects, interactions, and official label details.**</li>
|
111 |
+
<li>🔬 Can search for **clinical trials and medical research.**</li>
|
112 |
<li>🛡️ Detects emergencies, adds safety notices</li>
|
113 |
</ul>
|
114 |
</div>
|
115 |
+
""") # Changed Markdown content
|
116 |
|
117 |
with gr.Row():
|
118 |
with gr.Column(scale=1):
|
|
|
123 |
info="Choose your user type for tailored responses"
|
124 |
)
|
125 |
with gr.Column(scale=3):
|
126 |
+
chatbot = gr.Chatbot(label="Conversation", height=480, type='messages') # Added type='messages'
|
127 |
|
128 |
with gr.Row():
|
129 |
user_textbox = gr.Textbox(
|
|
|
137 |
examples=[
|
138 |
"What are the side effects of metformin?",
|
139 |
"Explain Crohn’s disease vs Ulcerative Colitis for a doctor",
|
140 |
+
"¿Cuáles son los síntomas de la hipertensión?",
|
141 |
+
"Can I take ibuprofen with lisinopril? (for interactions)", # New example
|
142 |
+
"What is the FDA label information for acetaminophen?", # New example
|
143 |
+
"Are there any ongoing clinical trials for breast cancer?", # New example
|
144 |
],
|
145 |
inputs=[user_textbox]
|
146 |
+
) # Changed Examples content
|
147 |
|
148 |
gr.ClearButton([chatbot, user_textbox], value="🔄 Clear")
|
149 |
|
|
|
161 |
|
162 |
if __name__ == "__main__":
|
163 |
demo.queue()
|
164 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
# File: requirements.txt
|
2 |
|
3 |
-
gradio==
|
4 |
-
requests
|
|
|
1 |
# File: requirements.txt
|
2 |
|
3 |
+
gradio==5.33.0
|
4 |
+
requests
|