Spaces:
Sleeping
Sleeping
File size: 9,026 Bytes
68091f2 949d5bc 68091f2 949d5bc 68091f2 949d5bc cc6796c 68091f2 cc6796c 68091f2 949d5bc 68091f2 65217f3 68091f2 57b2d01 68091f2 57b2d01 65217f3 68091f2 57b2d01 65217f3 57b2d01 68091f2 65217f3 68091f2 57b2d01 65217f3 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 57b2d01 68091f2 65217f3 68091f2 949d5bc 65217f3 98607a5 65217f3 98607a5 65217f3 68091f2 cc6796c 65217f3 68091f2 bfbdf32 |
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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
import gradio as gr
from rag_dspy import MedicalRAG
rag_chain = MedicalRAG()
sample_questions = [
"What are the most common symptoms of lupus?",
"How is type 2 diabetes usually managed in adults?",
"What are the first-line medications for treating hypertension?",
]
def qa_bot(user_question, history, min_year, max_year, specialty):
history = history or []
if not user_question.strip():
return history, history
history.append({"role": "user", "content": user_question})
result = rag_chain.forward(user_question, min_year, max_year, specialty)
answer = result.final_answer
history.append({"role": "assistant", "content": answer})
return history, history
css = """
html, body, #root, .gradio-container {
margin: 0 !important;
padding: 0 !important;
}
body {
background: #f5f7fa !important;
font-family: 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif !important;
color: #212529 !important;
margin: 0;
padding: 0;
}
#main-row {
display: flex;
flex-direction: row;
width: 100vw;
min-height: 100vh;
box-sizing: border-box;
gap: 0;
}
#readme-col {
flex: 1 1 0;
background: #f5f7fa;
padding: 0 0 0 0;
border-right: 1px solid #e3e7ef;
height: 108vh;
overflow-y: auto;
box-sizing: border-box;
}
#readme-markdown {
border-radius: 5px;
box-shadow: 0 4px 24px rgba(60, 80, 120, 0.07), 0 1.5px 4px rgba(60, 80, 120, 0.05);
padding: 32px 32px 24px 32px;
margin: 0;
border: none;
overflow-y: auto;
box-sizing: border-box;
flex: 1;
}
#main-card {
background: #fff !important;
border-radius: 5px;
box-shadow: 0 4px 24px rgba(60, 80, 120, 0.07), 0 1.5px 4px rgba(60, 80, 120, 0.05);
padding: 32px 32px 24px 32px;
margin: 0;
border: none;
overflow-y: auto;
box-sizing: border-box;
flex: 1;
}
#chatbot {
background: #f9fbfd !important;
border-radius: 5px !important;
box-shadow: 0 2px 8px rgba(60, 80, 120, 0.06);
margin-bottom: 18px !important;
border: 1px solid #e3e7ef !important;
padding: 16px !important;
color: #212529 !important;
}
.suggestion-btn {
margin: 0 12px 18px 0 !important;
background: #eaf4ff !important;
color: #2563eb !important;
border-radius: 10px !important;
font-weight: 500 !important;
font-size: 1rem !important;
border: 1.5px solid #bcdfff !important;
transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.suggestion-btn:hover {
background: #d2eaff !important;
color: #174ea6 !important;
border-color: #2563eb !important;
}
#medical-title {
text-align: center;
color: #2563eb;
font-size: 2.3rem;
font-weight: 700;
margin-bottom: 18px;
letter-spacing: 1px;
}
#user-input {
border-radius: 10px !important;
border: 1.5px solid #bcdfff !important;
padding: 10px 16px !important;
font-size: 1.1rem !important;
background: #fff !important;
color: #212529 !important;
}
#user-input::placeholder {
color: #8ca0b3 !important;
opacity: 1 !important;
}
#submit-btn {
background: linear-gradient(90deg, #2563eb 0%, #38bdf8 100%) !important;
color: #fff !important;
border-radius: 10px !important;
font-size: 1.3rem !important;
min-width: 56px;
min-height: 44px;
border: none !important;
box-shadow: 0 2px 8px rgba(60, 80, 120, 0.07);
transition: background 0.2s;
}
#submit-btn:hover {
background: linear-gradient(90deg, #174ea6 0%, #0ea5e9 100%) !important;
color: #fff !important;
}
@media (max-width: 900px) {
#main-row {
flex-direction: column;
width: 100vw;
}
#readme-col, #main-card {
min-width: 0 !important;
max-width: 100vw !important;
border-radius: 0 !important;
height: auto !important;
}
#readme-markdown {
padding: 24px 12px 16px 12px;
}
}
@media (max-width: 600px) {
#main-row {
flex-direction: column;
width: 100vw;
}
#readme-col, #main-card {
min-width: 0 !important;
max-width: 100vw !important;
border-radius: 0 !important;
height: auto !important;
}
#readme-markdown {
padding: 14px 6px 10px 6px;
font-size: 0.98rem;
}
#main-card {
padding: 16px 6px 10px 6px !important;
}
}
"""
readme_content = """
# Medical QA Chatbot
This is a Chain-of-Thought powered medical chatbot that:
- Retrieves answers from a Qdrant Cloud vector DB
- Uses Stanford DSPy to reason step-by-step
- Supports filtering by year and specialty
- Uses dense + ColBERT multivectors for retrieval
---
## How to Use
- Add your `OPENAI_API_KEY` to `.env`
- Add your `QDRANT_API_KEY` to `.env`
- Add your `QDRANT_CLOUD_URL` to `.env`
- Make sure `qdrant-client` points to your
Qdrant Cloud instance in `rag_dspy.py`
- Run `python app.py`
## Sample Questions
### General Medical Knowledge
- What are the most common symptoms of lupus?
- How is type 2 diabetes usually managed in adults?
- What is the difference between viral and bacterial pneumonia?
### Treatment & Medication
- What are the first-line medications for treating hypertension?
- How does metformin work to lower blood sugar?
"""
def suggestion_click(q, history, min_year, max_year, specialty):
return qa_bot(q, history, min_year, max_year, specialty)
with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as demo:
with gr.Row(elem_id="main-row"):
with gr.Column(elem_id="main-card"):
chatbot = gr.Chatbot(label="", elem_id="chatbot", type="messages", height=300)
state = gr.State([])
specialty_options = [
"Rheumatology", "Psychiatry", "Pulmonology & Respiratory Medicine", "Nephrology", "Public Health & Epidemiology",
"Medical Research & Methodology", "Pharmacy & Pharmacology", "Hematology", "Oncology", "Medical Ethics & Law",
"Medical Technology & Informatics", "Infectious Disease", "Basic Medical Sciences", "Allergology", "Geriatrics",
"Cardiology", "Gastroenterology & Hepatology", "General Surgery", "General Pediatrics", "Endocrinology & Metabolism",
"Vascular Surgery", "Radiology & Imaging", "Obstetrics & Gynecology", "Orthopedic Surgery", "Neurology",
"Family Medicine & Primary Care", "Psychology & Behavioral Health", "Otorhinolaryngology (ENT)", "General Internal Medicine",
"Anesthesiology", "Physical & Rehabilitation Medicine", "Medical Education", "Healthcare Administration & Management",
"Non-Medical Sciences & Disciplines", "Dermatology", "Critical Care & Intensive Care", "Urology", "Complementary & Alternative Medicine",
"Cardiothoracic Surgery", "Neurosurgery", "Pediatric Subspecialties", "Occupational & Environmental Health", "Ophthalmology",
"Emergency Medicine", "Dental & Oral Medicine", "Biomedical Engineering", "Pathology & Laboratory Medicine", "Transplant Surgery",
"Preventive Medicine", "Genetics", "Nursing", "Allied Health Professions", "Plastic & Reconstructive Surgery", "Others",
"Toxicology", "General Medicine"
]
specialty_dropdown = gr.Dropdown(choices=specialty_options, value="General Medicine", label="Specialty", scale=2, elem_id="specialty-dropdown")
with gr.Row():
min_year_slider = gr.Slider(minimum=1793, maximum=2021, value=1990, step=1, label="Min Year", scale=2, elem_id="min-year-slider")
max_year_slider = gr.Slider(minimum=1793, maximum=2021, value=2021, step=1, label="Max Year", scale=2, elem_id="max-year-slider")
with gr.Row():
user_input = gr.Textbox(placeholder="Type a medical question...", show_label=False, lines=1, scale=8, elem_id="user-input")
submit_btn = gr.Button(value="➤", scale=1, elem_id="submit-btn")
with gr.Row():
suggestion_buttons = []
for i, q in enumerate(sample_questions):
btn = gr.Button(q, elem_id=f"suggestion-{i}", elem_classes=["suggestion-btn"])
suggestion_buttons.append(btn)
submit_btn.click(qa_bot, inputs=[user_input, state, min_year_slider, max_year_slider, specialty_dropdown], outputs=[chatbot, state])
user_input.submit(qa_bot, inputs=[user_input, state, min_year_slider, max_year_slider, specialty_dropdown], outputs=[chatbot, state])
for btn, q in zip(suggestion_buttons, sample_questions):
btn.click(suggestion_click, inputs=[gr.State(q), state, min_year_slider, max_year_slider, specialty_dropdown], outputs=[chatbot, state])
with gr.Column(elem_id="readme-col"):
gr.Markdown(readme_content, elem_id="readme-markdown")
if __name__ == "__main__":
demo.launch()
|