-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
238 lines (202 loc) · 6.59 KB
/
Copy pathapp.py
File metadata and controls
238 lines (202 loc) · 6.59 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
import streamlit as st
from backend import app, extract_pdf_text
import re
import pandas as pd
# ==================================================
# PAGE CONFIG
# ==================================================
st.set_page_config(page_title="Doc Assis", layout="wide")
# ==================================================
# RESET FUNCTION
# ==================================================
def reset_app():
st.session_state.chat = []
st.session_state.history_text = ""
st.session_state.lab_text = ""
st.session_state.final_diagnosis = ""
st.rerun()
# ==================================================
# SESSION STATE
# ==================================================
for key, default in {
"chat": [],
"history_text": "",
"lab_text": "",
"final_diagnosis": ""
}.items():
if key not in st.session_state:
st.session_state[key] = default
# ==================================================
# CSS (BUTTON + SIDEBAR FIX)
# ==================================================
st.markdown("""
<style>
.stApp { background: #f8fafc; }
.block-container { padding: 2.5rem; }
body, p, span, div { color: #020617 !important; }
h1, h2, h3 { font-weight: 700; color: #020617 !important; }
/* Header */
.header-card {
background: linear-gradient(135deg, #1f77ff, #2563eb);
padding: 2rem;
border-radius: 22px;
color: white;
text-align: center;
margin-bottom: 2rem;
}
/* Cards */
.card {
background: white;
border-radius: 18px;
padding: 1.5rem;
box-shadow: 0 6px 18px rgba(0,0,0,0.08);
margin-bottom: 1.5rem;
}
/* Chat */
div[data-testid="stChatMessage"][data-role="user"] {
background: #dbeafe;
border-radius: 14px;
padding: 12px;
}
div[data-testid="stChatMessage"][data-role="assistant"] {
background: white;
border-left: 5px solid #1f77ff;
border-radius: 14px;
padding: 12px;
}
/* Diagnosis */
.diagnosis-card {
background: #ecfeff;
border-left: 6px solid #0ea5e9;
border-radius: 18px;
padding: 1.5rem;
}
/* Sidebar */
section[data-testid="stSidebar"] {
background: #020617;
}
section[data-testid="stSidebar"] * {
color: white !important;
}
/* Input */
textarea {
min-height: 120px !important;
border-radius: 16px !important;
border: 2px solid #1f77ff !important;
}
/* ✅ FORCE BUTTON COLOR (FIXES DARK BUTTON) */
div.stButton > button {
background: linear-gradient(135deg, #1f77ff, #2563eb) !important;
color: white !important;
border-radius: 18px !important;
font-weight: 700 !important;
font-size: 1rem !important;
border: none !important;
}
div.stButton > button:hover {
background: linear-gradient(135deg, #2563eb, #1f77ff) !important;
}
</style>
""", unsafe_allow_html=True)
# ==================================================
# HEADER + RESET BUTTON
# ==================================================
if st.button("🩺 Doc Assis – New Patient", use_container_width=True):
reset_app()
st.markdown("""
<div class="header-card">
<h1>Doc Assis</h1>
<p>Clinical Decision Support AI</p>
</div>
""", unsafe_allow_html=True)
# ==================================================
# UPLOAD SECTION
# ==================================================
st.markdown("<div class='card'><h3>📄 Upload Patient Records</h3></div>", unsafe_allow_html=True)
c1, c2 = st.columns(2)
with c1:
history_pdf = st.file_uploader("Patient History PDF", type="pdf")
if history_pdf:
st.session_state.history_text = extract_pdf_text(history_pdf)
st.success(f"Uploaded: {history_pdf.name}")
with c2:
lab_pdf = st.file_uploader("Lab Results PDF", type="pdf")
if lab_pdf:
st.session_state.lab_text = extract_pdf_text(lab_pdf)
st.success(f"Uploaded: {lab_pdf.name}")
# ==================================================
# SIDEBAR – SNAPSHOT + CHART (FIXED)
# ==================================================
if st.session_state.history_text or st.session_state.lab_text:
st.sidebar.title("🧾 Patient Snapshot")
combined = st.session_state.history_text + "\n" + st.session_state.lab_text
def find(pattern):
m = re.search(pattern, combined, re.IGNORECASE)
return m.group(1) if m else None
name = find(r"name[:\- ]+([A-Za-z ]+)")
age = find(r"age[:\- ]+(\d+)")
gender = find(r"gender[:\- ]+(male|female|other)")
st.sidebar.markdown(
f"**Name:** {name or 'Not available'}\n\n"
f"**Age:** {age or 'Not available'}\n\n"
f"**Gender:** {gender or 'Not available'}"
)
# ✅ LAB CHART (THIS WAS MISSING)
labs = {}
hba1c = find(r"hba1c[:\- ]+([\d.]+)")
chol = find(r"cholesterol[:\- ]+([\d.]+)")
bp = find(r"(\d{2,3})/\d{2,3}")
if hba1c:
labs["HbA1c"] = float(hba1c)
if chol:
labs["Cholesterol"] = float(chol)
if bp:
labs["Systolic BP"] = float(bp)
if labs:
st.sidebar.markdown("---")
st.sidebar.subheader("📊 Lab Chart")
df = pd.DataFrame(labs.values(), index=labs.keys(), columns=["Value"])
st.sidebar.bar_chart(df)
# ==================================================
# CHAT SECTION
# ==================================================
st.markdown("<div class='card'><h3>💬 Clinical Conversation</h3></div>", unsafe_allow_html=True)
for msg in st.session_state.chat:
with st.chat_message(msg["role"]):
st.write(msg["content"])
# ==================================================
# CHAT INPUT
# ==================================================
user_input = st.chat_input("Describe symptoms or answer follow-up questions")
if user_input:
st.session_state.chat.append({"role": "user", "content": user_input})
conversation = [
f"{m['role'].upper()}: {m['content']}"
for m in st.session_state.chat
]
result = app.invoke({
"history_text": st.session_state.history_text,
"lab_text": st.session_state.lab_text,
"conversation": conversation
})
if result.get("diagnosis"):
st.session_state.final_diagnosis = result["diagnosis"]
st.session_state.chat.append({
"role": "assistant",
"content": result["followup_question"]
})
st.rerun()
# ==================================================
# FINAL DIAGNOSIS
# ==================================================
if st.session_state.final_diagnosis:
st.markdown(
f"""
<div class="diagnosis-card">
<h3>🩺 Final Clinical Impression</h3>
<p>{st.session_state.final_diagnosis}</p>
<p><i>⚠ For clinical decision support only. Doctor confirmation required.</i></p>
</div>
""",
unsafe_allow_html=True
)