Spaces:
Runtime error

File size: 3,491 Bytes
4adf7aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08b116a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""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)