-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
367 lines (320 loc) · 13 KB
/
streamlit_app.py
File metadata and controls
367 lines (320 loc) · 13 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
Evidence-Bound Drug RAG - Streamlit UI
Production-ready with dark mode and improved UX
"""
import streamlit as st
import requests
# ============================================================================
# PAGE CONFIG (Must be first)
# ============================================================================
st.set_page_config(
page_title="Evidence-Bound Drug RAG",
page_icon="💊",
layout="wide",
initial_sidebar_state="expanded"
)
# ============================================================================
# DARK MODE STYLING (IMPROVED)
# ============================================================================
st.markdown("""
<style>
/* Dark theme */
.stApp {
background-color: #0e1117;
color: #fafafa;
}
section[data-testid="stSidebar"] {
background-color: #262730;
}
/* Headers */
.main-header {
font-size: 2.5rem;
font-weight: bold;
color: #1E88E5;
text-align: center;
margin-bottom: 1rem;
}
.sub-header {
font-size: 1.2rem;
color: #b0b0b0;
text-align: center;
margin-bottom: 2rem;
}
/* ✅ ISSUE 3 FIX: Answer card that pops */
.answer-card {
background-color: #1a1e23;
padding: 1.5rem;
border-radius: 0.75rem;
margin: 1rem 0;
border-left: 4px solid #1E88E5;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}
/* Cards */
.metric-card {
background-color: #1e1e1e;
padding: 1rem;
border-radius: 0.5rem;
margin: 0.5rem 0;
}
.citation {
background-color: #1a2332;
padding: 0.5rem;
border-left: 3px solid #1E88E5;
margin: 0.5rem 0;
color: #fafafa;
}
.refusal-box {
background-color: #2d2416;
padding: 1rem;
border-left: 3px solid #ff9800;
margin: 1rem 0;
color: #fafafa;
}
/* Text colors */
h1, h2, h3, h4, h5, h6 {
color: #fafafa !important;
}
p, div, span, label {
color: #fafafa !important;
}
/* Buttons */
.stButton > button {
background-color: #1E88E5;
color: white;
}
.stButton > button:hover {
background-color: #1565C0;
color: white;
}
</style>
""", unsafe_allow_html=True)
# ============================================================================
# API CONFIGURATION
# ============================================================================
try:
API_URL = st.secrets["API_URL"]
except:
# Use Docker service name for container-to-container communication
import os
API_URL = os.getenv("API_URL", "http://localhost:7860")
# ============================================================================
# SIDEBAR
# ============================================================================
with st.sidebar:
st.header("⚙️ Settings")
# Retriever settings
st.subheader("🎛️ Retrieval Settings")
retriever_type = st.selectbox(
"Method",
["vector", "hybrid", "bm25"],
index=0,
help="Hybrid = BM25 + Vector (Recommended)"
)
top_k = st.slider(
"Top-K chunks",
min_value=3,
max_value=15,
value=8,
help="More chunks = more context but slower"
)
st.divider()
# System stats
st.subheader("📊 System Stats")
try:
stats_response = requests.get(f"{API_URL}/stats", timeout=5)
if stats_response.status_code == 200:
stats = stats_response.json()
col1, col2 = st.columns(2)
with col1:
st.metric("Chunks", stats.get("total_chunks", "N/A"))
with col2:
st.metric("Drugs", len(stats.get("drugs_covered", [])))
with st.expander("📋 View all drugs"):
drugs = stats.get("drugs_covered", [])
if drugs:
st.write(", ".join(sorted(drugs)))
else:
st.warning("⚠️ API not responding")
except:
st.error("❌ Cannot connect to API")
st.divider()
# About section
st.subheader("ℹ️ About")
st.markdown("""
**Version:** 1.0.0
**RAGAS Score:** 0.71/1.0 ✅
- Faithfulness: 0.80
- Relevancy: 0.70
- Context Recall: 0.62
**Features:**
- 🔍 Hybrid retrieval
- 📚 853 evidence chunks
- 🏛️ FDA + NICE + WHO
- 🚫 Refusal policy
- 💰 Free (Groq LLM)
""")
# ============================================================================
# MAIN CONTENT
# ============================================================================
st.markdown('<div class="main-header">💊 Evidence-Bound Drug RAG</div>', unsafe_allow_html=True)
st.markdown('<div class="sub-header">Ask questions about pharmaceutical drugs backed by FDA, NICE & WHO documentation</div>', unsafe_allow_html=True)
st.divider()
# Example queries
with st.expander("💡 Example Queries (Click to view)", expanded=False):
col1, col2 = st.columns(2)
with col1:
st.markdown("**✅ Allowed Questions:**")
st.code("What are the side effects of warfarin?", language=None)
st.code("What is the recommended dosage of metformin?", language=None)
st.code("What are contraindications for amoxicillin?", language=None)
st.code("What interactions does atorvastatin have?", language=None)
with col2:
st.markdown("**❌ Refused Questions:**")
st.code("Should I stop taking my medication?", language=None)
st.code("Can I take ibuprofen with alcohol?", language=None)
st.code("What's the best drug for my condition?", language=None)
st.code("How much should I take?", language=None)
st.divider()
# ✅ ISSUE 2 FIX: Better placeholder text
# Query form (prevents blank screen issues)
with st.form("query_form"):
query = st.text_area(
"🔍 Ask your question:",
height=120,
placeholder="Try: What are the side effects of warfarin?", # ✅ More helpful!
help="Ask about side effects, dosages, contraindications, or interactions"
)
col1, col2 = st.columns([1, 4])
with col1:
submit_button = st.form_submit_button("🚀 Ask", type="primary", use_container_width=True)
with col2:
st.caption("💡 Tip: Be specific for better results")
# ============================================================================
# QUERY PROCESSING
# ============================================================================
if submit_button and query.strip():
with st.spinner("🔎 Retrieving evidence and generating answer..."):
try:
response = requests.post(
f"{API_URL}/ask",
json={
"query": query,
"top_k": top_k,
"retriever_type": retriever_type
},
timeout=60
)
if response.status_code == 200:
result = response.json()
st.divider()
# Handle refusal
if result.get("is_refusal", False):
st.markdown(
f'<div class="refusal-box">'
f'<strong>⚠️ Query Refused</strong><br><br>'
f'{result["answer"]}'
f'</div>',
unsafe_allow_html=True
)
st.info("""
**Why was this refused?**
This system follows strict medical safety policies:
- ❌ No personal medical advice
- ❌ No diagnostic recommendations
- ❌ No treatment decisions
- ❌ No dosing for individuals
✅ **Always consult a healthcare professional for medical decisions.**
""")
else:
# Success - show answer
st.success("✅ Answer generated from evidence-based sources")
# ✅ ISSUE 1 & 3 FIX: Answer in lighter card
st.markdown(
f'<div class="answer-card">'
f'<h3 style="margin-top: 0; color: #1E88E5;">📝 Answer</h3>'
f'{result["answer"]}'
f'</div>',
unsafe_allow_html=True
)
# ✅ ISSUE 4 FIX: Better metric labels
st.divider()
col1, col2, col3 = st.columns(3)
with col1:
st.metric("⏱️ Response Time", f"{result.get('total_latency_ms', 0):.0f}ms")
with col2:
st.metric("📚 Sources Found", result.get("chunks_retrieved", 0)) # ✅ Clearer!
with col3:
st.metric("📎 Evidence Used", result.get("chunks_cited", 0)) # ✅ Clearer!
# Authorities used
if result.get("authorities_used"):
authorities = ", ".join(result["authorities_used"])
st.info(f"🏛️ **Authoritative Sources:** {authorities}")
# Citations
cited_chunks = result.get("cited_chunks", [])
if cited_chunks:
with st.expander(f"📄 View {len(cited_chunks)} Citations", expanded=False):
for i, chunk_id in enumerate(cited_chunks, 1):
st.markdown(
f'<div class="citation"><strong>[{i}]</strong> {chunk_id}</div>',
unsafe_allow_html=True
)
# ✅ ISSUE 5 FIX: Cost moved to technical details only
# Technical details
with st.expander("🔧 Technical Details", expanded=False):
st.json({
"query": result.get("query"),
"retriever_type": retriever_type,
"top_k": top_k,
"retrieval_time_ms": result.get("retrieval_time_ms", 0),
"generation_time_ms": result.get("generation_time_ms", 0),
"total_latency_ms": result.get("total_latency_ms", 0),
"total_tokens": result.get("total_tokens", 0),
"cost_usd": result.get("cost_usd", 0.0), # ✅ Hidden from main view
"is_refusal": result.get("is_refusal", False),
"chunks_retrieved": result.get("chunks_retrieved", 0),
"chunks_cited": result.get("chunks_cited", 0)
})
elif response.status_code == 422:
st.error("❌ Invalid request format")
st.code(response.text)
else:
st.error(f"❌ API Error: HTTP {response.status_code}")
st.code(response.text)
except requests.Timeout:
st.error("⏱️ Request timed out (60s). API might be overloaded.")
except requests.ConnectionError:
st.error("❌ Cannot connect to API. Make sure it's running at:")
st.code(API_URL)
except Exception as e:
st.error(f"❌ Unexpected error: {str(e)}")
elif submit_button:
st.warning("⚠️ Please enter a question before submitting")
# ============================================================================
# FOOTER
# ============================================================================
st.divider()
st.markdown("""
<div style="text-align: center; color: #888; font-size: 0.9rem; padding: 1rem;">
<strong>Evidence-Bound Drug RAG</strong> |
Built with FastAPI + Groq + ChromaDB + Streamlit |
RAGAS Score: 0.71/1.0 ✅ |
Zero Hallucinations Policy |
<a href="https://github.com/yourusername/evidence-bound-drug-rag" target="_blank" style="color: #1E88E5;">GitHub</a>
</div>
""", unsafe_allow_html=True)
# Disclaimer
with st.expander("⚠️ Important Medical Disclaimer"):
st.warning("""
**MEDICAL DISCLAIMER**
This system is for **informational and educational purposes only**. It is **NOT**:
- A substitute for professional medical advice
- A diagnostic tool
- A treatment recommendation system
- A prescription or dosing guide
**Always:**
- Consult qualified healthcare professionals for medical decisions
- Follow your doctor's prescribed treatment plan
- Report any adverse effects to your healthcare provider
- Verify all information with official sources
**Emergency:** Call your local emergency number for medical emergencies.
""")