Spaces:
Runtime error

medi-bot / app.py
devserg's picture
Fix launch() for HuggingFace Spaces (server_name=0.0.0.0)
08b116a
Raw History Blame Contribute Delete
3.49 kB
"""MediBot — AI Medical Symptom Checker. Entry point for Gradio and HuggingFace Spaces."""
import os
import sys
import uuid
sys.path.insert(0, os.path.dirname(__file__))
import config # loads .env and sets KMP_DUPLICATE_LIB_OK
import gradio as gr
from langchain_core.messages import HumanMessage
from agents.react_agent import build_agent
print("Initializing MediBot...")
_graph, _ = build_agent()
print("MediBot ready.")
DISCLAIMER = (
"**⚠️ Medical Disclaimer:** MediBot is an AI assistant for informational purposes only. "
"It does NOT replace professional medical advice, diagnosis, or treatment. "
"Always consult a licensed healthcare provider for medical concerns."
)
EXAMPLES = [
"I have fever, headache and chills",
"I feel itchy with skin rash",
"Fatigue, weight loss and excessive thirst",
"Chest pain and shortness of breath",
"What are the precautions for malaria?",
"How severe is vomiting as a symptom?",
]
def respond(message: str, history: list, thread_id: str):
if not message.strip():
return history, "", thread_id
cfg = {"configurable": {"thread_id": thread_id}}
try:
result = _graph.invoke({"messages": [HumanMessage(content=message)]}, cfg)
reply = result["messages"][-1].content
except Exception as e:
reply = f"Error: {str(e)[:200]}"
history = history + [
{"role": "user", "content": message},
{"role": "assistant", "content": reply},
]
return history, "", thread_id
def new_conversation():
return [], "", str(uuid.uuid4())
with gr.Blocks(title="MediBot") as demo:
thread_id_state = gr.State(str(uuid.uuid4()))
gr.Markdown("# 🏥 MediBot — AI Medical Symptom Checker")
gr.Markdown(DISCLAIMER)
chatbot = gr.Chatbot(label="MediBot Chat", height=450)
msg_box = gr.Textbox(
label="Your symptoms",
placeholder="Describe your symptoms here...",
lines=2,
)
with gr.Row():
send_btn = gr.Button("Send", variant="primary")
clear_btn = gr.Button("🆕 New Conversation", variant="secondary")
gr.Markdown("**Quick examples:**")
with gr.Row():
ex_btns_row1 = [gr.Button(EXAMPLES[i], size="sm") for i in range(3)]
with gr.Row():
ex_btns_row2 = [gr.Button(EXAMPLES[i], size="sm") for i in range(3, 6)]
gr.Markdown("---")
gr.Markdown(
"### How to Use\n"
"1. Type your symptoms in the box above and click **Send**\n"
"2. MediBot identifies possible diseases and gives precautions\n"
"3. Ask follow-up questions — MediBot remembers the conversation\n"
"4. Click **🆕 New Conversation** to clear history and start fresh\n\n"
"*Built with LangGraph ReAct Agents · FAISS Vector Search · OpenAI GPT-4o-mini*"
)
# Event wiring
send_btn.click(
respond,
inputs=[msg_box, chatbot, thread_id_state],
outputs=[chatbot, msg_box, thread_id_state],
)
msg_box.submit(
respond,
inputs=[msg_box, chatbot, thread_id_state],
outputs=[chatbot, msg_box, thread_id_state],
)
clear_btn.click(
new_conversation,
inputs=[],
outputs=[chatbot, msg_box, thread_id_state],
)
all_ex_btns = ex_btns_row1 + ex_btns_row2
for btn, ex in zip(all_ex_btns, EXAMPLES):
btn.click(fn=lambda e=ex: e, inputs=[], outputs=msg_box)
demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)