-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathishaan_python.py
More file actions
376 lines (313 loc) · 16.2 KB
/
ishaan_python.py
File metadata and controls
376 lines (313 loc) · 16.2 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
import streamlit as st
import pandas as pd
import fitz
import re
from langchain_community.document_loaders import PyMuPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain_community.vectorstores import FAISS
st.set_page_config(page_title="AI Clinical Trial Management System", layout="wide")
st.title("AI Clinical Trial Management System")
# -------------------------- SESSION STATE INIT --------------------------
if 'volunteers_df' not in st.session_state:
st.session_state.volunteers_df = pd.DataFrame()
st.session_state.history_admin = []
st.session_state.history_medical = []
st.session_state.criteria = None
st.session_state.vectorstore = None
# -------------------------- UPDATED PDF PARSER FUNCTION --------------------------
def parse_pdf(uploaded_file):
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
text = ""
for page in doc:
text += page.get_text()
# Extract criteria using regex patterns
criteria = {
"min_age": None,
"max_age": None,
"condition": None,
"biomarker": None,
"stages": [],
"gender": "Any",
"exclude_diabetes": "No",
"exclude_pregnant": "Yes" # Default to safe option
}
# Age extraction
age_match = re.search(r'Age between (\d+) and (\d+) years', text)
if age_match:
criteria["min_age"] = int(age_match.group(1))
criteria["max_age"] = int(age_match.group(2))
# Condition extraction
if 'Non-Small Cell Lung Cancer (NSCLC)' in text:
criteria["condition"] = "NSCLC"
# Biomarker extraction
biomarker_match = re.search(r'EGFR-positive \(EGFR\+\)', text)
if biomarker_match:
criteria["biomarker"] = "EGFR+"
# Stage extraction
stage_match = re.search(r'Stage (III) or (IV)', text)
if stage_match:
criteria["stages"] = [stage_match.group(1), stage_match.group(2)]
# Exclusion criteria
if 'Pregnant volunteers excluded' in text:
criteria["exclude_pregnant"] = "Yes"
if 'Volunteers with known diabetes not excluded' in text:
criteria["exclude_diabetes"] = "No"
# Validate all required fields
required_fields = ['min_age', 'max_age', 'condition', 'biomarker', 'stages']
missing_fields = [field for field in required_fields if not criteria[field]]
if missing_fields:
raise ValueError(f"Invalid PDF format. Missing: {', '.join(missing_fields)}")
# Create vectorstore
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
chunks = splitter.create_documents([text])
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(chunks, embeddings)
return criteria, vectorstore
# -------------------------- FILTER FUNCTION --------------------------
def filter_volunteers(df, criteria):
result = df.copy()
result = result[(result['Age'] >= criteria["min_age"]) & (result['Age'] <= criteria["max_age"])]
if criteria["condition"]:
result = result[result["Condition"].str.contains("NSCLC|Non-Small Cell", case=False, na=False)]
if criteria["biomarker"]:
result = result[result["BiomarkerStatus"].str.upper() == criteria["biomarker"].upper()]
if criteria["stages"]:
result = result[result["DiseaseStage"].isin(criteria["stages"])]
if criteria["gender"] != "Any":
result = result[result["Gender"].str.lower() == criteria["gender"].lower()]
if criteria["exclude_diabetes"] == "Yes":
result = result[result["Diabetes"].str.lower() != "yes"]
if criteria["exclude_pregnant"] == "Yes":
result = result[result["Pregnant"].str.lower() != "yes"]
return result
# -------------------------- RULE-BASED QA FUNCTION --------------------------
def answer_question(q, df, criteria=None):
q_lower = q.lower()
# ========== MEDICAL PANEL IMPROVEMENTS START ==========
if criteria: # Medical company logic
# Handle greetings
if any(word in q_lower for word in ["hello", "hi", "hey"]):
return "Hello! I'm your medical trial assistant. Ask me about eligibility criteria or volunteer demographics."
# Prevent raw ID listings
if "list" in q_lower or "show" in q_lower:
clean_df = df.drop_duplicates("VolunteerID")
if len(clean_df) > 15:
return f"Found {len(clean_df)} eligible volunteers. Try adding filters like:\n- 'Female volunteers under 50'\n- 'EGFR+ patients from Mumbai'"
return ", ".join(clean_df["VolunteerID"].tolist())
# GPT-powered answers for medical panel
llm = ChatOpenAI(temperature=0)
prompt = f"""
MEDICAL CRITERIA:
{criteria}
VOLUNTEER DATA:
{df[["VolunteerID", "Age", "Gender", "Condition", "BiomarkerStatus"]].to_string()}
QUESTION: {q}
ANSWER RULES:
1. Reference criteria when discussing eligibility
2. Keep responses under 3 sentences
3. Use exact numbers from data
4. Never list more than 5 IDs
"""
return llm.predict(prompt)
# ========== MEDICAL PANEL IMPROVEMENTS END ==========
# Original admin panel logic below
if "how many" in q_lower:
if "female" in q_lower:
return f"There are {len(df[df['Gender'].str.lower() == 'female'])} eligible female volunteers."
if "male" in q_lower:
return f"There are {len(df[df['Gender'].str.lower() == 'male'])} eligible male volunteers."
if "diabetes" in q_lower:
return f"There are {len(df[df['Diabetes'].str.lower() == 'yes'])} volunteers with diabetes."
if "pregnant" in q_lower:
return f"There are {len(df[df['Pregnant'].str.lower() == 'yes'])} pregnant volunteers."
return f"There are {len(df)} eligible volunteers."
elif "list" in q_lower or "show" in q_lower:
return ", ".join(df["VolunteerID"]) if not df.empty else "No eligible volunteers."
elif "why is volunteer" in q_lower and "not eligible" in q_lower:
vid = re.findall(r'volunteer\s*([vV]\d+)', q_lower)
if vid:
vid = vid[0].upper()
full_df = st.session_state.volunteers_df
row = full_df[full_df["VolunteerID"].str.upper() == vid]
if row.empty:
return f"Volunteer ID {vid} not found in the dataset."
if vid in df["VolunteerID"].str.upper().values:
return f"Sir/Madam, the volunteer {vid} is fully eligible for the clinical trial."
else:
reasons = []
r = row.iloc[0]
if r["Age"] < criteria["min_age"] or r["Age"] > criteria["max_age"]:
reasons.append("Age not in eligible range.")
if criteria["condition"] and criteria["condition"].lower() not in str(r["Condition"]).lower():
reasons.append("Condition does not match.")
if criteria["biomarker"] and str(r["BiomarkerStatus"]).upper() != criteria["biomarker"].upper():
reasons.append("Biomarker does not match.")
if criteria["stages"] and r["DiseaseStage"] not in criteria["stages"]:
reasons.append("Disease stage does not match.")
if criteria["gender"] != "Any" and r["Gender"].lower() != criteria["gender"].lower():
reasons.append("Gender does not match.")
if criteria["exclude_diabetes"] == "Yes" and str(r["Diabetes"]).lower() == "yes":
reasons.append("Has diabetes.")
if criteria["exclude_pregnant"] == "Yes" and str(r["Pregnant"]).lower() == "yes":
reasons.append("Is pregnant.")
if reasons:
return f"Volunteer {vid} is not eligible because: " + ", ".join(reasons)
else:
return f"Volunteer {vid} is not eligible but specific reasons could not be determined."
else:
return "Volunteer ID not recognized."
return None
# -------------------------- GPT QA FUNCTION --------------------------
def gpt_answer(q, df):
llm = ChatOpenAI(temperature=0)
volunteer_data = df[["VolunteerID", "Age", "Gender", "DiseaseStage", "BiomarkerStatus", "Region"]].to_string(index=False)
prompt = PromptTemplate(
template="""
You are an AI assistant helping to answer queries about clinical trial volunteers.
Data:
{data}
Question:
{question}
Answer:""",
input_variables=["data", "question"]
)
final_prompt = prompt.format(data=volunteer_data, question=q)
return llm.predict(final_prompt)
# -------------------------- ENTITY EXTRACTION --------------------------
def extract_entities(q):
genders = []
stages = []
regions = []
biomarkers = []
ages = []
q_lower = q.lower()
if "male" in q_lower:
genders.append("Male")
if "female" in q_lower:
genders.append("Female")
for s in ["I", "II", "III", "IV"]:
if f"stage {s}".lower() in q_lower:
stages.append(s)
for b in ["EGFR+", "ALK+", "KRAS+", "ROS1+"]:
if b.lower() in q_lower:
biomarkers.append(b)
region_keywords = ["Delhi", "Mumbai", "Kolkata", "Chennai", "Hyderabad"]
for r in region_keywords:
if r.lower() in q_lower:
regions.append(r)
age_numbers = re.findall(r'(?:above|over|greater than|older than|age)\s*(\d+)', q_lower)
if age_numbers:
ages.append(int(age_numbers[0]))
return {
"gender": genders,
"stage": stages,
"region": regions,
"biomarker": biomarkers,
"age": ages
}
# -------------------------- UI --------------------------
tab1, tab2 = st.tabs(["TechVitals Admin", "Medical Company"])
# -------------------------- ADMIN PANEL --------------------------
with tab1:
st.header("TechVitals Admin Portal")
uploaded_csv = st.file_uploader("Upload Volunteers CSV", type="csv")
if uploaded_csv:
st.session_state.volunteers_df = pd.read_csv(uploaded_csv)
df = st.session_state.volunteers_df
if df.empty:
st.warning("Please upload a Volunteers CSV to continue.")
else:
st.subheader("Filter Volunteers")
min_age, max_age = st.slider("Age Range", 0, 100, (30, 75), key="admin_age")
gender = st.selectbox("Gender", ["All"] + sorted(df["Gender"].dropna().unique()), key="admin_gender")
region = st.selectbox("Region", ["All"] + sorted(df["Region"].dropna().unique()), key="admin_region")
filtered = df[(df["Age"] >= min_age) & (df["Age"] <= max_age)]
if gender != "All":
filtered = filtered[filtered["Gender"] == gender]
if region != "All":
filtered = filtered[filtered["Region"] == region]
st.subheader("Volunteer List")
st.dataframe(filtered)
st.subheader("Ask a Question (AI Powered)")
q = st.text_input("Ask about volunteers:", key="admin_q")
if st.button("Ask", key="admin_ask"):
ans = answer_question(q, filtered)
if ans is None:
ans = gpt_answer(q, filtered)
st.session_state.history_admin.append((q, ans))
st.success(ans)
if st.session_state.history_admin:
with st.expander("Conversation History"):
for ques, ans in st.session_state.history_admin:
st.markdown(f"**Q:** {ques}\n\n**A:** {ans}")
# -------------------------- UPDATED MEDICAL COMPANY PANEL --------------------------
with tab2:
st.header("Medical Company Portal")
if st.session_state.volunteers_df.empty:
st.warning("Waiting for TechVitals to add the dataset...")
else:
st.write(f"TechVitals has provided a dataset with columns like {list(st.session_state.volunteers_df.columns)} and with {len(st.session_state.volunteers_df)} volunteers.")
pdf_file = st.file_uploader("Upload Trial Criteria PDF", type=["pdf"])
if pdf_file:
try:
with st.spinner("Processing PDF..."):
criteria, vectorstore = parse_pdf(pdf_file)
st.session_state.criteria = criteria
st.session_state.vectorstore = vectorstore
# Display extracted criteria
with st.expander("Extracted Medicine Criteria", expanded=True):
st.markdown(f"""
**Inclusion Criteria:**
- Age: {criteria['min_age']}-{criteria['max_age']} years
- Condition: {criteria['condition']}
- Biomarker: {criteria['biomarker']}
- Stages: {', '.join(criteria['stages'])}
**Exclusion Criteria:**
- Exclude Pregnant: {criteria['exclude_pregnant']}
- Exclude Diabetes: {criteria['exclude_diabetes']}
""")
st.success("Medical criteria processed successfully!")
except Exception as e:
st.error(f"Invalid PDF: {str(e)}. Please upload a valid clinical trial criteria PDF.")
st.session_state.criteria = None
st.session_state.vectorstore = None
if st.session_state.criteria:
eligible = filter_volunteers(st.session_state.volunteers_df, st.session_state.criteria)
st.success(f"Total eligible volunteers: {len(eligible.drop_duplicates('VolunteerID'))}")
# Filters
min_age_m, max_age_m = st.slider("Age Range", 0, 100,
(st.session_state.criteria["min_age"], st.session_state.criteria["max_age"]),
key="medical_age")
gender_m = st.selectbox("Gender", ["All"] + sorted(eligible["Gender"].dropna().unique()), key="medical_gender")
stage_m = st.selectbox("Disease Stage", ["All"] + sorted(eligible["DiseaseStage"].dropna().unique()),
key="medical_stage")
biomarker_m = st.selectbox("Biomarker", ["All"] + sorted(eligible["BiomarkerStatus"].dropna().unique()),
key="medical_biomarker")
region_m = st.selectbox("Region", ["All"] + sorted(eligible["Region"].dropna().unique()),
key="medical_region")
filtered_med = eligible[(eligible["Age"] >= min_age_m) & (eligible["Age"] <= max_age_m)]
if gender_m != "All":
filtered_med = filtered_med[filtered_med["Gender"] == gender_m]
if stage_m != "All":
filtered_med = filtered_med[filtered_med["DiseaseStage"] == stage_m]
if biomarker_m != "All":
filtered_med = filtered_med[filtered_med["BiomarkerStatus"] == biomarker_m]
if region_m != "All":
filtered_med = filtered_med[filtered_med["Region"] == region_m]
st.subheader("Eligible Volunteers")
st.dataframe(
filtered_med[["VolunteerID", "Email", "DiseaseStage", "BiomarkerStatus", "Gender", "Region", "Age"]]
.drop_duplicates("VolunteerID")
)
st.subheader("Ask Trial Questions")
q2 = st.text_input("Ask about eligible volunteers:", key="medical_q")
if st.button("Ask", key="medical_ask"):
ans2 = answer_question(q2, filtered_med, st.session_state.criteria)
st.session_state.history_medical.append((q2, ans2))
st.success(ans2)
if st.session_state.history_medical:
with st.expander("Conversation History"):
for ques2, ans2 in st.session_state.history_medical:
st.markdown(f"**Q:** {ques2}\n\n**A:** {ans2}")