-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
644 lines (418 loc) · 12.7 KB
/
Copy pathapp.py
File metadata and controls
644 lines (418 loc) · 12.7 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import streamlit as st
from utils.pdf_reader import extract_text
from utils.gemini import explain_report
from utils.gemini_vision import explain_image
from utils.chat import chat_with_report
from utils.medicine import explain_medicine
# ---------------- PAGE CONFIG ---------------- #
st.set_page_config(
page_title="GeneGuide AI",
page_icon="🧬",
layout="wide"
)
# ---------------- SESSION STATE ---------------- #
if "explanation" not in st.session_state:
st.session_state.explanation = None
if "sections" not in st.session_state:
st.session_state.sections = {}
if "snapshot" not in st.session_state:
st.session_state.snapshot = {}
# ---------------- SIDEBAR ---------------- #
with st.sidebar:
st.title("🧬 GeneGuide AI")
st.caption("Your AI Health Assistant")
st.divider()
st.subheader("🚀 Features")
st.markdown("""
🏠 **Home**
📄 **Medical Report Explainer**
🩺 **AI Health Snapshot**
💬 **AI Chat**
💊 **Medicine Explainer**
🌍 **English & Urdu**
🔒 **Secure & Private**
""")
st.divider()
st.success("CTRL-V Hackathon 2026")
# ---------------- CUSTOM CSS ---------------- #
st.markdown("""
<style>
.stApp{
background:#F8FAFC;
}
/* Headings */
h1{
color:#5B21B6;
font-weight:800;
}
h2,h3{
color:#1E293B;
}
/* Upload Box */
div[data-testid="stFileUploader"]{
border:3px dashed #C084FC;
border-radius:20px;
padding:20px;
background:white;
}
/* Alerts */
div[data-testid="stAlert"]{
border-radius:15px;
}
/* Tabs */
.stTabs [data-baseweb="tab-list"]{
gap:12px;
}
.stTabs [data-baseweb="tab"]{
background:#F3E8FF;
color:#6D28D9;
border-radius:15px;
padding:12px 22px;
font-weight:700;
}
.stTabs [aria-selected="true"]{
background:#A855F7;
color:white;
}
</style>
""", unsafe_allow_html=True)
# ---------------- HERO ---------------- #
left, right = st.columns([1.3,1])
with left:
st.title("🧬 GeneGuide AI")
st.markdown("## Because Every Patient Deserves to Understand Their Health")
st.write("""
A medical report should provide clarity—not confusion.
GeneGuide AI transforms complex medical language into simple,
easy-to-understand explanations so patients and families can better
understand their reports without replacing professional medical advice.
🤖 AI-powered explanations
📄 Report analysis
💬 Interactive AI assistant
💊 Medicine explanations
🌍 Multilingual support
""")
with right:
st.image(
"assets/robot.jpg",
width="stretch"
)
st.divider()
st.info("""
### 🎯 Our Mission
Healthcare information should be understandable by everyone.
GeneGuide AI helps patients better understand their reports,
reducing confusion while encouraging consultation with healthcare professionals.
""")
# ---------------- FEATURE CARDS ---------------- #
c1,c2,c3,c4=st.columns(4)
with c1:
st.info("📄\n\n**Easy to Understand**")
with c2:
st.info("🤖\n\n**AI Powered**")
with c3:
st.info("🔒\n\n**Secure**")
with c4:
st.info("❤️\n\n**Patient Friendly**")
st.divider()
# ---------------- LANGUAGE ---------------- #
language=st.selectbox(
"🌍 Choose Explanation Language",
["English","Urdu"]
)
st.markdown("## ♿ Accessibility Features")
st.markdown("""
✅ Simple language
✅ English & Urdu
✅ AI explanations
✅ Patient-friendly summaries
✅ Medicine explanations
🔊 Voice Assistant (Coming Soon)
""")
st.divider()
st.markdown("## ⚙️ How GeneGuide AI Works")
step1, step2, step3 = st.columns(3)
with step1:
st.info("""
### 📤 Step 1
Upload your medical report
(PDF or Image)
""")
with step2:
st.info("""
### 🤖 Step 2
AI analyzes the report
and explains medical terms.
""")
with step3:
st.info("""
### ❤️ Step 3
Receive an easy-to-understand
summary with recommendations.
""")
st.success("""
🔒 **Privacy First**
Your uploaded medical reports are analyzed only for generating explanations.
GeneGuide AI does not store your personal medical data.
""")
# ---------------- FILE UPLOADER ---------------- #
st.subheader("📤 Upload Your Medical Report")
st.caption("Supported formats: PDF • PNG • JPG • JPEG • JFIF")
uploaded_file=st.file_uploader(
"Choose your report",
type=["pdf","png","jpg","jpeg","jfif"]
)
# ---------------- HELPER FUNCTIONS ---------------- #
def split_sections(text):
sections={
"HEALTH SNAPSHOT":"",
"SUMMARY":"",
"MEDICAL TERMS":"",
"QUESTIONS FOR THE DOCTOR":"",
"NEXT STEPS":"",
"DISCLAIMER":""
}
current=None
for line in text.splitlines():
line=line.strip()
if line.startswith("## HEALTH SNAPSHOT"):
current="HEALTH SNAPSHOT"
continue
elif line.startswith("## SUMMARY"):
current="SUMMARY"
continue
elif line.startswith("## MEDICAL TERMS"):
current="MEDICAL TERMS"
continue
elif line.startswith("## QUESTIONS FOR THE DOCTOR"):
current="QUESTIONS FOR THE DOCTOR"
continue
elif line.startswith("## NEXT STEPS"):
current="NEXT STEPS"
continue
elif line.startswith("## DISCLAIMER"):
current="DISCLAIMER"
continue
if current:
sections[current]+=line+"\n"
return sections
def parse_snapshot(snapshot_text):
data={}
for line in snapshot_text.splitlines():
if ":" in line:
key,value=line.split(":",1)
data[key.strip()]=value.strip()
return data
# ---------------- PROCESS REPORT ---------------- #
if uploaded_file:
st.success("✅ Report uploaded successfully!")
# ---------- IMAGE PREVIEW ---------- #
if uploaded_file.type.startswith("image"):
st.image(
uploaded_file,
caption="Uploaded Medical Report",
width="stretch"
)
# ---------- PDF PREVIEW ---------- #
elif uploaded_file.type == "application/pdf":
text = extract_text(uploaded_file)
st.subheader("📄 Extracted Report")
st.text_area(
"Report Text",
text,
height=250
)
if "⚠️" in text:
st.warning(
"Scanned PDF detected.\n\n"
"Please upload the report as an image for better AI analysis."
)
st.divider()
# ---------- ANALYZE BUTTON ---------- #
if st.button("🧬 Analyze Report with AI", width="stretch"):
try:
with st.spinner(
"🧠 Reading report... 🔬 Understanding medical terminology... 🤖 Generating patient-friendly explanation..."
):
if uploaded_file.type == "application/pdf":
explanation = explain_report(
text,
language
)
else:
explanation = explain_image(
uploaded_file,
language
)
st.session_state.explanation = explanation
sections = split_sections(explanation)
st.session_state.sections = sections
st.session_state.snapshot = parse_snapshot(
sections["HEALTH SNAPSHOT"]
)
except Exception as e:
st.exception(e)
st.stop()
if st.session_state.explanation:
explanation = st.session_state.explanation
sections = st.session_state.sections
snapshot = st.session_state.snapshot
# Continue with your Report Overview,
# AI Health Snapshot,
# Tabs,
# Download button,
# AI Chat,
# Medicine Explainer, etc.
# ---------- SHOW RESULTS ---------- #
if st.session_state.explanation:
explanation = st.session_state.explanation
sections = st.session_state.sections
snapshot = st.session_state.snapshot
st.success("✅ Report analyzed successfully!")
st.markdown("## 🩺 AI Health Snapshot")
left, right = st.columns([1, 2])
with left:
st.markdown("**👤 Patient**")
st.markdown("**🫁 Body Part**")
st.markdown("**🚨 Risk Level**")
st.markdown("**📄 Report Type**")
st.markdown("**🩺 Diagnosis**")
st.markdown("**👨⚕️ Recommended Action**")
with right:
st.write(snapshot.get("Patient Name", "N/A"))
st.write(snapshot.get("Body Part", "N/A"))
st.write(snapshot.get("Severity", "N/A"))
st.write(snapshot.get("Report Type", "N/A"))
st.write(snapshot.get("Diagnosis", "N/A"))
st.write(snapshot.get("Urgency", "Consult Doctor"))
st.divider()
st.subheader("📊 Report Overview")
o1, o2, o3, o4 = st.columns(4)
with o1:
st.metric(
"📄 File",
uploaded_file.name
)
with o2:
st.metric(
"🌍 Language",
language
)
with o3:
st.metric(
"🤖 AI Status",
"Completed"
)
with o4:
st.metric(
"📑 Format",
uploaded_file.type.split("/")[-1].upper()
)
st.divider()
# ---------- REPORT TABS ---------- #
tab1, tab2, tab3, tab4, tab5 = st.tabs([
"📌 Summary",
"🩺 Medical Terms",
"👨⚕️ Ask Doctor",
"🚀 Next Steps",
"⚠️ Disclaimer"
])
with tab1:
st.markdown("### 📌 Easy Summary")
st.info(
sections["SUMMARY"]
)
with tab2:
st.markdown("### 🩺 Medical Terms")
st.success(
sections["MEDICAL TERMS"]
)
with tab3:
st.markdown("### 👨⚕️ Questions For Your Doctor")
st.warning(
sections["QUESTIONS FOR THE DOCTOR"]
)
with tab4:
st.markdown("### 🚀 Recommended Next Steps")
st.success(
sections["NEXT STEPS"]
)
with tab5:
st.markdown("### ⚠️ Important Disclaimer")
st.error(
sections["DISCLAIMER"]
)
st.download_button(
"📥 Download AI Report",
explanation,
file_name="GeneGuide_AI_Report.txt",
mime="text/plain",
width="stretch"
)
# ---------------- AI CHAT ---------------- #
st.divider()
st.subheader("🩺 Personal Health Assistant")
question = st.text_input(
"Ask anything about your report...",
key="chat_question"
)
if st.session_state.get("explanation"):
if st.button("💬 Ask GeneGuide AI", width="stretch"):
if question.strip():
with st.spinner("🤖 Thinking..."):
answer = chat_with_report(
st.session_state.explanation,
question
)
st.success(answer)
else:
st.info("📄 Upload and analyze a medical report first to chat with GeneGuide AI.")
st.button(
"💬 Ask AI",
disabled=True,
width="stretch"
)
# ---------------- MEDICINE EXPLAINER ---------------- #
st.divider()
st.subheader("💊 Medicine Explainer")
medicine = st.text_input(
"Enter medicine name...",
key="medicine"
)
if st.button("💊 Explain Medicine", width="stretch"):
if medicine.strip():
from utils.medicine import explain_medicine
with st.spinner("💊 Reading medicine information..."):
result = explain_medicine(medicine)
st.success(result)
# ---------------- VOICE ASSISTANT ---------------- #
if st.session_state.explanation:
st.divider()
st.subheader("🔊 Voice Assistant")
st.info(
"""
🎤 **Coming Soon!**
GeneGuide AI will soon be able to read your medical report aloud.
This feature is being developed to help:
• 👵 Elderly patients
• 👁️ Visually impaired users
• 📖 Users who prefer listening instead of reading
"""
)
# ---------------- FOOTER ---------------- #
st.divider()
st.warning(
"⚠️ GeneGuide AI provides educational information only. "
"Always consult a qualified healthcare professional before making medical decisions."
)
st.markdown(
"""
<center>
<h3>🧬 GeneGuide AI</h3>
<p>Made with ❤️ to make healthcare information accessible for everyone..</p>
<p>Empowering patients through accessible AI-powered healthcare explanations. 🧬</p>
<p><b>CTRL-V Hackathon 2026</b></p>
</center>
""",
unsafe_allow_html=True
)