Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
@@ -1,371 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import google.generativeai as genai
|
3 |
-
from PIL import Image
|
4 |
-
import os
|
5 |
-
from dotenv import load_dotenv
|
6 |
-
import PyPDF2
|
7 |
-
import io
|
8 |
-
from datetime import datetime
|
9 |
-
|
10 |
-
# Page configuration must be the first Streamlit command
|
11 |
-
st.set_page_config(
|
12 |
-
page_title="OCT Retina Analysis Assistant",
|
13 |
-
page_icon="👁️",
|
14 |
-
layout="wide",
|
15 |
-
initial_sidebar_state="expanded"
|
16 |
-
)
|
17 |
-
|
18 |
-
# Load environment variables
|
19 |
-
load_dotenv()
|
20 |
-
|
21 |
-
# Configure Gemini API
|
22 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
23 |
-
model = genai.GenerativeModel("gemini-2.0-flash-exp")
|
24 |
-
|
25 |
-
# Custom CSS
|
26 |
-
st.markdown("""
|
27 |
-
<style>
|
28 |
-
.main {
|
29 |
-
padding: 2rem;
|
30 |
-
}
|
31 |
-
.stButton>button {
|
32 |
-
width: 100%;
|
33 |
-
background-color: #FF4B4B;
|
34 |
-
color: white;
|
35 |
-
padding: 0.5rem;
|
36 |
-
margin-top: 1rem;
|
37 |
-
}
|
38 |
-
.credit-box {
|
39 |
-
background-color: #f0f2f6;
|
40 |
-
padding: 1rem;
|
41 |
-
border-radius: 0.5rem;
|
42 |
-
margin: 1rem 0;
|
43 |
-
}
|
44 |
-
.header-box {
|
45 |
-
background-color: #FF4B4B;
|
46 |
-
padding: 2rem;
|
47 |
-
border-radius: 0.5rem;
|
48 |
-
color: white;
|
49 |
-
margin-bottom: 2rem;
|
50 |
-
text-align: center;
|
51 |
-
}
|
52 |
-
.image-container {
|
53 |
-
margin: 1rem 0;
|
54 |
-
padding: 1rem;
|
55 |
-
border-radius: 0.5rem;
|
56 |
-
background-color: #f0f2f6;
|
57 |
-
}
|
58 |
-
.analysis-container {
|
59 |
-
margin-top: 1rem;
|
60 |
-
padding: 1rem;
|
61 |
-
border-radius: 0.5rem;
|
62 |
-
background-color: #f0f2f6;
|
63 |
-
}
|
64 |
-
</style>
|
65 |
-
""", unsafe_allow_html=True)
|
66 |
-
|
67 |
-
# System prompts
|
68 |
-
SINGLE_TIMEPOINT_PROMPT = """You are an expert ophthalmologist. Analyze these OCT scans concisely in the following order:
|
69 |
-
|
70 |
-
1. Image Quality Check
|
71 |
-
• Briefly state if images are adequate for interpretation
|
72 |
-
• Note any significant artifacts or limitations
|
73 |
-
|
74 |
-
2. Key Findings (Be Concise)
|
75 |
-
• List only abnormal findings
|
76 |
-
• Focus on clinically significant features
|
77 |
-
• Specify affected layers
|
78 |
-
|
79 |
-
3. Most Likely Diagnosis
|
80 |
-
• State your primary diagnosis
|
81 |
-
• Support with specific findings from images and patient data
|
82 |
-
• Indicate diagnostic confidence level
|
83 |
-
|
84 |
-
4. Differential Diagnoses
|
85 |
-
• List top 2-3 alternative diagnoses
|
86 |
-
• Explain why each is possible but less likely
|
87 |
-
• Link to specific findings that support/refute each
|
88 |
-
|
89 |
-
5. Additional Testing Needed
|
90 |
-
• Specify which additional tests are required and why
|
91 |
-
• List in order of priority
|
92 |
-
• Explain how each test will help confirm diagnosis or guide treatment
|
93 |
-
• Include imaging, lab work, or clinical examinations as needed
|
94 |
-
|
95 |
-
Do not describe normal findings unless clinically relevant."""
|
96 |
-
|
97 |
-
COMPARISON_PROMPT = """You are an expert ophthalmologist. Compare these OCT scans from two timepoints concisely:
|
98 |
-
|
99 |
-
1. Image Quality Check
|
100 |
-
• Confirm both sets are adequate for comparison
|
101 |
-
• Note any limitations affecting comparison
|
102 |
-
|
103 |
-
2. Key Changes (Be Concise)
|
104 |
-
• List significant changes between timepoints
|
105 |
-
• Focus on progression or improvement
|
106 |
-
• Quantify changes where possible
|
107 |
-
|
108 |
-
3. Disease Status
|
109 |
-
• State if condition is stable, improving, or worsening
|
110 |
-
• Support with specific changes in findings
|
111 |
-
• Indicate confidence in assessment
|
112 |
-
|
113 |
-
4. Response to Treatment (if applicable)
|
114 |
-
• Evaluate treatment effectiveness
|
115 |
-
• Identify areas of resistance
|
116 |
-
• Suggest if treatment modification needed
|
117 |
-
|
118 |
-
5. Further Testing Required
|
119 |
-
• Recommend additional tests based on observed changes
|
120 |
-
• Explain urgency level for each test
|
121 |
-
• Specify how results will impact treatment decisions
|
122 |
-
• Include any needed referrals or consultations
|
123 |
-
|
124 |
-
Do not describe unchanged features unless clinically relevant."""
|
125 |
-
|
126 |
-
TREATMENT_GUIDELINES_PROMPT = """Based on the diagnosis and clinical findings, provide evidence-based treatment recommendations:
|
127 |
-
|
128 |
-
1. First-Line Treatment
|
129 |
-
• Specify recommended intervention
|
130 |
-
• Include dosing/frequency if applicable
|
131 |
-
• Note any contraindications
|
132 |
-
|
133 |
-
2. Treatment Rationale
|
134 |
-
• Link to current guidelines
|
135 |
-
• Cite evidence level if available
|
136 |
-
• Explain why this treatment is most appropriate
|
137 |
-
|
138 |
-
3. Monitoring Plan
|
139 |
-
• Follow-up interval
|
140 |
-
• Key features to monitor
|
141 |
-
• Treatment success criteria
|
142 |
-
• Required testing schedule
|
143 |
-
|
144 |
-
4. Alternative Options
|
145 |
-
• List backup treatment options
|
146 |
-
• Specify when to consider switching
|
147 |
-
• Note relative advantages/disadvantages
|
148 |
-
|
149 |
-
5. Concurrent Testing
|
150 |
-
• Specify tests needed during treatment
|
151 |
-
• Include monitoring frequency
|
152 |
-
• List warning signs requiring immediate attention
|
153 |
-
• Detail required baseline and follow-up testing
|
154 |
-
|
155 |
-
Be specific and practical in recommendations."""
|
156 |
-
|
157 |
-
def extract_pdf_text(pdf_file):
|
158 |
-
"""Extract text from uploaded PDF file"""
|
159 |
-
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
160 |
-
text = ""
|
161 |
-
for page in pdf_reader.pages:
|
162 |
-
text += page.extract_text()
|
163 |
-
return text
|
164 |
-
|
165 |
-
def analyze_oct_images(images, timepoint=None, patient_data=None):
|
166 |
-
"""Analyze OCT images with optional timepoint and patient data"""
|
167 |
-
if timepoint:
|
168 |
-
prompt = f"{SINGLE_TIMEPOINT_PROMPT}\n\nTimepoint: {timepoint}\n"
|
169 |
-
else:
|
170 |
-
prompt = f"{SINGLE_TIMEPOINT_PROMPT}\n"
|
171 |
-
|
172 |
-
if patient_data:
|
173 |
-
prompt += f"\nPatient Information:\n{patient_data}\n"
|
174 |
-
|
175 |
-
prompt += "\nPlease analyze these OCT scans:"
|
176 |
-
content = [prompt] + images
|
177 |
-
response = model.generate_content(content)
|
178 |
-
return response.text
|
179 |
-
|
180 |
-
def compare_oct_timepoints(images1, date1, images2, date2, patient_data=None):
|
181 |
-
"""Compare OCT images from two timepoints"""
|
182 |
-
prompt = f"{COMPARISON_PROMPT}\n\nTimepoint 1: {date1}\nTimepoint 2: {date2}\n"
|
183 |
-
|
184 |
-
if patient_data:
|
185 |
-
prompt += f"\nPatient Information:\n{patient_data}\n"
|
186 |
-
|
187 |
-
prompt += "\nPlease compare these OCT scans:"
|
188 |
-
content = [prompt] + images1 + images2
|
189 |
-
response = model.generate_content(content)
|
190 |
-
return response.text
|
191 |
-
|
192 |
-
def get_treatment_recommendations(diagnosis, findings):
|
193 |
-
"""Get treatment recommendations based on guidelines"""
|
194 |
-
prompt = f"{TREATMENT_GUIDELINES_PROMPT}\n\nDiagnosis: {diagnosis}\nFindings: {findings}"
|
195 |
-
response = model.generate_content(prompt)
|
196 |
-
return response.text
|
197 |
-
|
198 |
-
def main():
|
199 |
-
# Header with custom styling
|
200 |
-
st.markdown("""
|
201 |
-
<div class="header-box">
|
202 |
-
<h1>OCT Retina Analysis Assistant</h1>
|
203 |
-
<p>Powered by Google Gemini AI</p>
|
204 |
-
</div>
|
205 |
-
""", unsafe_allow_html=True)
|
206 |
-
|
207 |
-
# Credits
|
208 |
-
st.markdown("""
|
209 |
-
<div class="credit-box">
|
210 |
-
<h3>About</h3>
|
211 |
-
<p>Developed by Dr. Fernando Ly</p>
|
212 |
-
<p>This tool assists in the analysis of OCT retina scans using advanced AI technology.
|
213 |
-
It provides detailed layer analysis and potential diagnoses to support clinical decision-making.</p>
|
214 |
-
<p><strong>Note:</strong> This tool is for assistance only and should not replace professional medical judgment.</p>
|
215 |
-
</div>
|
216 |
-
""", unsafe_allow_html=True)
|
217 |
-
|
218 |
-
# Main content
|
219 |
-
col1, col2 = st.columns([1, 1])
|
220 |
-
|
221 |
-
with col1:
|
222 |
-
# Patient Data Section
|
223 |
-
st.markdown("### Patient Information")
|
224 |
-
patient_pdf = st.file_uploader("Upload Patient Data (PDF)", type=['pdf'])
|
225 |
-
patient_data = None
|
226 |
-
if patient_pdf:
|
227 |
-
patient_data = extract_pdf_text(patient_pdf)
|
228 |
-
with st.expander("View Patient Data"):
|
229 |
-
st.text(patient_data)
|
230 |
-
|
231 |
-
# Scan Upload Section
|
232 |
-
st.markdown("### Upload OCT Scans")
|
233 |
-
timepoint_option = st.radio(
|
234 |
-
"Select scan type:",
|
235 |
-
["Single Timepoint", "Two Timepoints for Comparison"]
|
236 |
-
)
|
237 |
-
|
238 |
-
if timepoint_option == "Single Timepoint":
|
239 |
-
uploaded_files = st.file_uploader(
|
240 |
-
"Choose OCT scans",
|
241 |
-
type=['png', 'jpg', 'jpeg'],
|
242 |
-
accept_multiple_files=True,
|
243 |
-
key="single_timepoint"
|
244 |
-
)
|
245 |
-
|
246 |
-
if uploaded_files:
|
247 |
-
scan_date = st.date_input("Scan Date")
|
248 |
-
st.markdown("### Uploaded Scans")
|
249 |
-
images = []
|
250 |
-
for idx, uploaded_file in enumerate(uploaded_files):
|
251 |
-
with st.expander(f"OCT Scan {idx + 1}", expanded=True):
|
252 |
-
image = Image.open(uploaded_file)
|
253 |
-
images.append(image)
|
254 |
-
st.image(image, use_container_width=True, caption=f"OCT Scan {idx + 1}")
|
255 |
-
|
256 |
-
if st.button("🔍 Analyze Scans"):
|
257 |
-
with st.spinner("Analyzing OCT scans... Please wait."):
|
258 |
-
try:
|
259 |
-
analysis = analyze_oct_images(images, scan_date, patient_data)
|
260 |
-
treatment_recs = get_treatment_recommendations(
|
261 |
-
"Based on the analysis above",
|
262 |
-
"See detailed findings above"
|
263 |
-
)
|
264 |
-
|
265 |
-
with col2:
|
266 |
-
st.markdown("### Analysis Results")
|
267 |
-
st.markdown(f"""
|
268 |
-
<div class="analysis-container">
|
269 |
-
{analysis.replace(chr(10), '<br>')}
|
270 |
-
</div>
|
271 |
-
""", unsafe_allow_html=True)
|
272 |
-
|
273 |
-
st.markdown("### Treatment Recommendations")
|
274 |
-
st.markdown(f"""
|
275 |
-
<div class="analysis-container">
|
276 |
-
{treatment_recs.replace(chr(10), '<br>')}
|
277 |
-
</div>
|
278 |
-
""", unsafe_allow_html=True)
|
279 |
-
|
280 |
-
except Exception as e:
|
281 |
-
st.error(f"An error occurred during analysis: {str(e)}")
|
282 |
-
|
283 |
-
else: # Two Timepoints
|
284 |
-
st.markdown("#### First Timepoint")
|
285 |
-
files1 = st.file_uploader(
|
286 |
-
"Choose first set of OCT scans",
|
287 |
-
type=['png', 'jpg', 'jpeg'],
|
288 |
-
accept_multiple_files=True,
|
289 |
-
key="timepoint1"
|
290 |
-
)
|
291 |
-
date1 = st.date_input("First Scan Date")
|
292 |
-
|
293 |
-
st.markdown("#### Second Timepoint")
|
294 |
-
files2 = st.file_uploader(
|
295 |
-
"Choose second set of OCT scans",
|
296 |
-
type=['png', 'jpg', 'jpeg'],
|
297 |
-
accept_multiple_files=True,
|
298 |
-
key="timepoint2"
|
299 |
-
)
|
300 |
-
date2 = st.date_input("Second Scan Date")
|
301 |
-
|
302 |
-
if files1 and files2:
|
303 |
-
st.markdown("### Uploaded Scans")
|
304 |
-
images1 = []
|
305 |
-
images2 = []
|
306 |
-
|
307 |
-
st.markdown("#### First Timepoint Scans")
|
308 |
-
for idx, uploaded_file in enumerate(files1):
|
309 |
-
with st.expander(f"OCT Scan {idx + 1} - First Timepoint", expanded=False):
|
310 |
-
image = Image.open(uploaded_file)
|
311 |
-
images1.append(image)
|
312 |
-
st.image(image, use_container_width=True)
|
313 |
-
|
314 |
-
st.markdown("#### Second Timepoint Scans")
|
315 |
-
for idx, uploaded_file in enumerate(files2):
|
316 |
-
with st.expander(f"OCT Scan {idx + 1} - Second Timepoint", expanded=False):
|
317 |
-
image = Image.open(uploaded_file)
|
318 |
-
images2.append(image)
|
319 |
-
st.image(image, use_container_width=True)
|
320 |
-
|
321 |
-
if st.button("🔍 Compare Timepoints"):
|
322 |
-
with st.spinner("Analyzing and comparing OCT scans... Please wait."):
|
323 |
-
try:
|
324 |
-
comparison = compare_oct_timepoints(
|
325 |
-
images1, date1,
|
326 |
-
images2, date2,
|
327 |
-
patient_data
|
328 |
-
)
|
329 |
-
treatment_recs = get_treatment_recommendations(
|
330 |
-
"Based on the comparison above",
|
331 |
-
"See detailed findings above"
|
332 |
-
)
|
333 |
-
|
334 |
-
with col2:
|
335 |
-
st.markdown("### Comparison Results")
|
336 |
-
st.markdown(f"""
|
337 |
-
<div class="analysis-container">
|
338 |
-
{comparison.replace(chr(10), '<br>')}
|
339 |
-
</div>
|
340 |
-
""", unsafe_allow_html=True)
|
341 |
-
|
342 |
-
st.markdown("### Treatment Recommendations")
|
343 |
-
st.markdown(f"""
|
344 |
-
<div class="analysis-container">
|
345 |
-
{treatment_recs.replace(chr(10), '<br>')}
|
346 |
-
</div>
|
347 |
-
""", unsafe_allow_html=True)
|
348 |
-
|
349 |
-
except Exception as e:
|
350 |
-
st.error(f"An error occurred during analysis: {str(e)}")
|
351 |
-
|
352 |
-
# Instructions in col2 if no files uploaded
|
353 |
-
if not (patient_pdf or (timepoint_option == "Single Timepoint" and uploaded_files) or
|
354 |
-
(timepoint_option == "Two Timepoints for Comparison" and files1 and files2)):
|
355 |
-
with col2:
|
356 |
-
st.markdown("### Instructions")
|
357 |
-
st.markdown("""
|
358 |
-
1. Upload patient data PDF (optional)
|
359 |
-
2. Choose analysis type (single timepoint or comparison)
|
360 |
-
3. Upload OCT scans for selected timepoint(s)
|
361 |
-
4. Set scan dates
|
362 |
-
5. Click analyze/compare button
|
363 |
-
6. Review analysis and treatment recommendations
|
364 |
-
|
365 |
-
Supported formats:
|
366 |
-
- Patient Data: PDF
|
367 |
-
- OCT Scans: PNG, JPG, JPEG
|
368 |
-
""")
|
369 |
-
|
370 |
-
if __name__ == "__main__":
|
371 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|