Update app.py
Browse files
app.py
CHANGED
|
@@ -86,14 +86,34 @@ def analyze_ecg_image(image, vision_model="llama-3.2-90b-vision-preview"):
|
|
| 86 |
8. Rhythm classification
|
| 87 |
9. Specific patterns (if any)
|
| 88 |
|
| 89 |
-
Report exact numerical values where visible. Format your response
|
|
|
|
|
|
|
| 90 |
|
| 91 |
If you notice any abnormalities or concerning patterns, highlight them clearly but avoid making definitive diagnoses.
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
Important formatting instructions:
|
| 94 |
-
- Use
|
| 95 |
- Do not use asterisks (**) for emphasis - use proper HTML formatting instead
|
| 96 |
-
-
|
| 97 |
"""
|
| 98 |
|
| 99 |
try:
|
|
@@ -123,7 +143,25 @@ def analyze_ecg_image(image, vision_model="llama-3.2-90b-vision-preview"):
|
|
| 123 |
ecg_analysis = re.sub(r'\*\*([^*]+)\*\*', r'<strong>\1</strong>', ecg_analysis)
|
| 124 |
|
| 125 |
# Make sure all headers are properly formatted (with complete pattern)
|
| 126 |
-
ecg_analysis = re.sub(r'^(#+)\s+(.+)$', r'<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
return ecg_analysis
|
| 129 |
|
|
@@ -153,26 +191,47 @@ PATIENT HISTORY:
|
|
| 153 |
|
| 154 |
TIMESTAMP: {timestamp}
|
| 155 |
|
| 156 |
-
|
| 157 |
-
<strong>Summary of Findings</strong>
|
| 158 |
-
(Your summary here)
|
| 159 |
|
| 160 |
-
<
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
<
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
<
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
<
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
Important formatting instructions:
|
| 173 |
-
- Use
|
| 174 |
- Do not use asterisks (**) for emphasis - use proper HTML formatting instead
|
| 175 |
-
- For any urgent findings, use <
|
| 176 |
"""
|
| 177 |
else:
|
| 178 |
prompt = f"""You are a highly trained cardiologist assistant. Based on the ECG analysis below, provide a comprehensive assessment of the patient's cardiac status. Indicate clearly if there are any concerning findings that require immediate medical attention.
|
|
@@ -182,26 +241,47 @@ ECG ANALYSIS:
|
|
| 182 |
|
| 183 |
TIMESTAMP: {timestamp}
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
-
<
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
-
<
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
-
<
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
-
<
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
Important formatting instructions:
|
| 202 |
-
- Use
|
| 203 |
- Do not use asterisks (**) for emphasis - use proper HTML formatting instead
|
| 204 |
-
- For any urgent findings, use <
|
| 205 |
"""
|
| 206 |
|
| 207 |
try:
|
|
@@ -227,7 +307,53 @@ Important formatting instructions:
|
|
| 227 |
assessment_text = re.sub(r'\*\*([^*]+)\*\*', r'<strong>\1</strong>', assessment_text)
|
| 228 |
|
| 229 |
# Make sure all headers are properly formatted (with complete pattern)
|
| 230 |
-
assessment_text = re.sub(r'^(#+)\s+(.+)$', r'<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
return assessment_text
|
| 233 |
|
|
@@ -256,13 +382,11 @@ MEDICAL ASSESSMENT:
|
|
| 256 |
{assessment}
|
| 257 |
|
| 258 |
TIMESTAMP: {timestamp}
|
| 259 |
-
|
| 260 |
"""
|
| 261 |
|
| 262 |
if patient_history and patient_history.strip():
|
| 263 |
context += f"""PATIENT HISTORY:
|
| 264 |
{patient_history}
|
| 265 |
-
|
| 266 |
"""
|
| 267 |
|
| 268 |
# Construct full chat history for context
|
|
@@ -315,14 +439,14 @@ with gr.Blocks(title="Cardiac ECG Analysis System", theme=gr.themes.Soft()) as a
|
|
| 315 |
with gr.Row():
|
| 316 |
with gr.Column(scale=1):
|
| 317 |
# Input components
|
| 318 |
-
with gr.Group(): #
|
| 319 |
gr.Markdown("### π ECG Image")
|
| 320 |
ecg_image = gr.Image(type="pil", label="Upload ECG Image")
|
| 321 |
# Display fixed model info
|
| 322 |
gr.Markdown("**Vision Model:** llama-3.2-90b-vision-preview")
|
| 323 |
analyze_button = gr.Button("Analyze ECG Image", variant="primary")
|
| 324 |
|
| 325 |
-
with gr.Group(): #
|
| 326 |
gr.Markdown("### π Patient Information")
|
| 327 |
patient_history_text = gr.Textbox(
|
| 328 |
lines=8,
|
|
@@ -335,7 +459,7 @@ with gr.Blocks(title="Cardiac ECG Analysis System", theme=gr.themes.Soft()) as a
|
|
| 335 |
)
|
| 336 |
load_history_button = gr.Button("Load Patient History from File")
|
| 337 |
|
| 338 |
-
with gr.Group(): #
|
| 339 |
gr.Markdown("### π§ Assessment Settings")
|
| 340 |
# Display fixed model info
|
| 341 |
gr.Markdown("**Chat Model:** llama-3.3-70b-versatile")
|
|
@@ -343,18 +467,18 @@ with gr.Blocks(title="Cardiac ECG Analysis System", theme=gr.themes.Soft()) as a
|
|
| 343 |
|
| 344 |
with gr.Column(scale=1):
|
| 345 |
# Output components
|
| 346 |
-
with gr.Group(): #
|
| 347 |
gr.Markdown("### π ECG Analysis Results")
|
| 348 |
ecg_analysis_output = gr.HTML(label="ECG Analysis", elem_id="ecg-analysis")
|
| 349 |
|
| 350 |
-
with gr.Group(): #
|
| 351 |
gr.Markdown("### π Medical Assessment")
|
| 352 |
assessment_output = gr.HTML(label="Assessment", elem_id="assessment-output")
|
| 353 |
|
| 354 |
gr.Markdown("## π¨ββοΈ Doctor's Consultation")
|
| 355 |
gr.Markdown("Ask follow-up questions about the patient's ECG results and medical condition.")
|
| 356 |
|
| 357 |
-
with gr.Group(): #
|
| 358 |
chatbot = gr.Chatbot(label="Consultation", height=400)
|
| 359 |
with gr.Row():
|
| 360 |
message = gr.Textbox(
|
|
|
|
| 86 |
8. Rhythm classification
|
| 87 |
9. Specific patterns (if any)
|
| 88 |
|
| 89 |
+
Report exact numerical values where visible. Format your response using HTML list elements for better readability.
|
| 90 |
+
|
| 91 |
+
If certain measurements aren't visible in the image, indicate that they cannot be determined.
|
| 92 |
|
| 93 |
If you notice any abnormalities or concerning patterns, highlight them clearly but avoid making definitive diagnoses.
|
| 94 |
|
| 95 |
+
Format your response like this:
|
| 96 |
+
<h3>ECG Report</h3>
|
| 97 |
+
<ul>
|
| 98 |
+
<li><strong>Date and Time:</strong> {timestamp}</li>
|
| 99 |
+
<li><strong>Heart Rate:</strong> [value] bpm</li>
|
| 100 |
+
<li><strong>PR Interval:</strong> [value] ms</li>
|
| 101 |
+
<!-- Other measurements -->
|
| 102 |
+
</ul>
|
| 103 |
+
|
| 104 |
+
<h3>Additional Observations</h3>
|
| 105 |
+
<ul>
|
| 106 |
+
<li>Observation 1</li>
|
| 107 |
+
<li>Observation 2</li>
|
| 108 |
+
</ul>
|
| 109 |
+
|
| 110 |
+
<h3>Conclusion</h3>
|
| 111 |
+
<p>[Your conclusion text]</p>
|
| 112 |
+
|
| 113 |
Important formatting instructions:
|
| 114 |
+
- Use HTML elements for structure (<ul>, <li>, <strong>, <h3>, etc.)
|
| 115 |
- Do not use asterisks (**) for emphasis - use proper HTML formatting instead
|
| 116 |
+
- For any urgent findings, use <span style="color:red"> to highlight them
|
| 117 |
"""
|
| 118 |
|
| 119 |
try:
|
|
|
|
| 143 |
ecg_analysis = re.sub(r'\*\*([^*]+)\*\*', r'<strong>\1</strong>', ecg_analysis)
|
| 144 |
|
| 145 |
# Make sure all headers are properly formatted (with complete pattern)
|
| 146 |
+
ecg_analysis = re.sub(r'^(#+)\s+(.+)$', r'<h3>\2</h3>', ecg_analysis, flags=re.MULTILINE)
|
| 147 |
+
|
| 148 |
+
# If there's no HTML formatting at all, wrap in basic structure
|
| 149 |
+
if not re.search(r'<[^>]+>', ecg_analysis):
|
| 150 |
+
lines = ecg_analysis.split('\n')
|
| 151 |
+
formatted_html = "<h3>ECG Report</h3>\n<ul>\n"
|
| 152 |
+
|
| 153 |
+
for line in lines:
|
| 154 |
+
if line.strip():
|
| 155 |
+
# Try to identify key-value pairs
|
| 156 |
+
match = re.match(r'^([^:]+):\s*(.+)$', line)
|
| 157 |
+
if match:
|
| 158 |
+
key, value = match.groups()
|
| 159 |
+
formatted_html += f" <li><strong>{key}:</strong> {value}</li>\n"
|
| 160 |
+
else:
|
| 161 |
+
formatted_html += f" <li>{line}</li>\n"
|
| 162 |
+
|
| 163 |
+
formatted_html += "</ul>"
|
| 164 |
+
ecg_analysis = formatted_html
|
| 165 |
|
| 166 |
return ecg_analysis
|
| 167 |
|
|
|
|
| 191 |
|
| 192 |
TIMESTAMP: {timestamp}
|
| 193 |
|
| 194 |
+
Format your assessment using HTML elements for readability:
|
|
|
|
|
|
|
| 195 |
|
| 196 |
+
<h3>Summary of Findings</h3>
|
| 197 |
+
<ul>
|
| 198 |
+
<li>Finding 1</li>
|
| 199 |
+
<li>Finding 2</li>
|
| 200 |
+
<!-- etc. -->
|
| 201 |
+
</ul>
|
| 202 |
|
| 203 |
+
<h3>Key Abnormalities</h3>
|
| 204 |
+
<ul>
|
| 205 |
+
<li>Abnormality 1</li>
|
| 206 |
+
<li>Abnormality 2 (if any)</li>
|
| 207 |
+
<!-- etc. -->
|
| 208 |
+
</ul>
|
| 209 |
|
| 210 |
+
<h3>Potential Clinical Implications</h3>
|
| 211 |
+
<ul>
|
| 212 |
+
<li>Implication 1</li>
|
| 213 |
+
<li>Implication 2</li>
|
| 214 |
+
<!-- etc. -->
|
| 215 |
+
</ul>
|
| 216 |
|
| 217 |
+
<h3>Recommendation</h3>
|
| 218 |
+
<ul>
|
| 219 |
+
<li>Recommendation 1 (include urgency level)</li>
|
| 220 |
+
<li>Recommendation 2 (if any)</li>
|
| 221 |
+
<!-- etc. -->
|
| 222 |
+
</ul>
|
| 223 |
+
|
| 224 |
+
<h3>Differential Considerations</h3>
|
| 225 |
+
<ul>
|
| 226 |
+
<li>Differential 1</li>
|
| 227 |
+
<li>Differential 2</li>
|
| 228 |
+
<!-- etc. -->
|
| 229 |
+
</ul>
|
| 230 |
|
| 231 |
Important formatting instructions:
|
| 232 |
+
- Use HTML elements for structure (<ul>, <li>, <strong>, <h3>, etc.)
|
| 233 |
- Do not use asterisks (**) for emphasis - use proper HTML formatting instead
|
| 234 |
+
- For any urgent findings, use <span style="color:red"> to highlight them
|
| 235 |
"""
|
| 236 |
else:
|
| 237 |
prompt = f"""You are a highly trained cardiologist assistant. Based on the ECG analysis below, provide a comprehensive assessment of the patient's cardiac status. Indicate clearly if there are any concerning findings that require immediate medical attention.
|
|
|
|
| 241 |
|
| 242 |
TIMESTAMP: {timestamp}
|
| 243 |
|
| 244 |
+
Format your assessment using HTML elements for readability:
|
| 245 |
+
|
| 246 |
+
<h3>Summary of Findings</h3>
|
| 247 |
+
<ul>
|
| 248 |
+
<li>Finding 1</li>
|
| 249 |
+
<li>Finding 2</li>
|
| 250 |
+
<!-- etc. -->
|
| 251 |
+
</ul>
|
| 252 |
|
| 253 |
+
<h3>Key Abnormalities</h3>
|
| 254 |
+
<ul>
|
| 255 |
+
<li>Abnormality 1</li>
|
| 256 |
+
<li>Abnormality 2 (if any)</li>
|
| 257 |
+
<!-- etc. -->
|
| 258 |
+
</ul>
|
| 259 |
|
| 260 |
+
<h3>Potential Clinical Implications</h3>
|
| 261 |
+
<ul>
|
| 262 |
+
<li>Implication 1</li>
|
| 263 |
+
<li>Implication 2</li>
|
| 264 |
+
<!-- etc. -->
|
| 265 |
+
</ul>
|
| 266 |
|
| 267 |
+
<h3>Recommendation</h3>
|
| 268 |
+
<ul>
|
| 269 |
+
<li>Recommendation 1 (include urgency level)</li>
|
| 270 |
+
<li>Recommendation 2 (if any)</li>
|
| 271 |
+
<!-- etc. -->
|
| 272 |
+
</ul>
|
| 273 |
|
| 274 |
+
<h3>Differential Considerations</h3>
|
| 275 |
+
<ul>
|
| 276 |
+
<li>Differential 1</li>
|
| 277 |
+
<li>Differential 2</li>
|
| 278 |
+
<!-- etc. -->
|
| 279 |
+
</ul>
|
| 280 |
|
| 281 |
Important formatting instructions:
|
| 282 |
+
- Use HTML elements for structure (<ul>, <li>, <strong>, <h3>, etc.)
|
| 283 |
- Do not use asterisks (**) for emphasis - use proper HTML formatting instead
|
| 284 |
+
- For any urgent findings, use <span style="color:red"> to highlight them
|
| 285 |
"""
|
| 286 |
|
| 287 |
try:
|
|
|
|
| 307 |
assessment_text = re.sub(r'\*\*([^*]+)\*\*', r'<strong>\1</strong>', assessment_text)
|
| 308 |
|
| 309 |
# Make sure all headers are properly formatted (with complete pattern)
|
| 310 |
+
assessment_text = re.sub(r'^(#+)\s+(.+)$', r'<h3>\2</h3>', assessment_text, flags=re.MULTILINE)
|
| 311 |
+
|
| 312 |
+
# If there's no HTML formatting at all, wrap in basic structure
|
| 313 |
+
if not re.search(r'<[^>]+>', assessment_text):
|
| 314 |
+
sections = [
|
| 315 |
+
"Summary of Findings",
|
| 316 |
+
"Key Abnormalities",
|
| 317 |
+
"Potential Clinical Implications",
|
| 318 |
+
"Recommendation",
|
| 319 |
+
"Differential Considerations"
|
| 320 |
+
]
|
| 321 |
+
|
| 322 |
+
# Split by sections and format
|
| 323 |
+
formatted_html = ""
|
| 324 |
+
current_section = "Summary of Findings" # Default
|
| 325 |
+
content_lines = []
|
| 326 |
+
|
| 327 |
+
lines = assessment_text.split('\n')
|
| 328 |
+
|
| 329 |
+
for line in lines:
|
| 330 |
+
# Check if this line is a section header
|
| 331 |
+
is_section = False
|
| 332 |
+
for section in sections:
|
| 333 |
+
if section.lower() in line.lower():
|
| 334 |
+
# If we have collected content for a previous section, output it
|
| 335 |
+
if content_lines:
|
| 336 |
+
formatted_html += f"<h3>{current_section}</h3>\n<ul>\n"
|
| 337 |
+
for content_line in content_lines:
|
| 338 |
+
formatted_html += f" <li>{content_line}</li>\n"
|
| 339 |
+
formatted_html += "</ul>\n"
|
| 340 |
+
content_lines = []
|
| 341 |
+
|
| 342 |
+
current_section = section
|
| 343 |
+
is_section = True
|
| 344 |
+
break
|
| 345 |
+
|
| 346 |
+
if not is_section and line.strip():
|
| 347 |
+
content_lines.append(line.strip())
|
| 348 |
+
|
| 349 |
+
# Don't forget the last section
|
| 350 |
+
if content_lines:
|
| 351 |
+
formatted_html += f"<h3>{current_section}</h3>\n<ul>\n"
|
| 352 |
+
for content_line in content_lines:
|
| 353 |
+
formatted_html += f" <li>{content_line}</li>\n"
|
| 354 |
+
formatted_html += "</ul>\n"
|
| 355 |
+
|
| 356 |
+
assessment_text = formatted_html if formatted_html else assessment_text
|
| 357 |
|
| 358 |
return assessment_text
|
| 359 |
|
|
|
|
| 382 |
{assessment}
|
| 383 |
|
| 384 |
TIMESTAMP: {timestamp}
|
|
|
|
| 385 |
"""
|
| 386 |
|
| 387 |
if patient_history and patient_history.strip():
|
| 388 |
context += f"""PATIENT HISTORY:
|
| 389 |
{patient_history}
|
|
|
|
| 390 |
"""
|
| 391 |
|
| 392 |
# Construct full chat history for context
|
|
|
|
| 439 |
with gr.Row():
|
| 440 |
with gr.Column(scale=1):
|
| 441 |
# Input components
|
| 442 |
+
with gr.Group(): # Using Group instead of Box
|
| 443 |
gr.Markdown("### π ECG Image")
|
| 444 |
ecg_image = gr.Image(type="pil", label="Upload ECG Image")
|
| 445 |
# Display fixed model info
|
| 446 |
gr.Markdown("**Vision Model:** llama-3.2-90b-vision-preview")
|
| 447 |
analyze_button = gr.Button("Analyze ECG Image", variant="primary")
|
| 448 |
|
| 449 |
+
with gr.Group(): # Using Group instead of Box
|
| 450 |
gr.Markdown("### π Patient Information")
|
| 451 |
patient_history_text = gr.Textbox(
|
| 452 |
lines=8,
|
|
|
|
| 459 |
)
|
| 460 |
load_history_button = gr.Button("Load Patient History from File")
|
| 461 |
|
| 462 |
+
with gr.Group(): # Using Group instead of Box
|
| 463 |
gr.Markdown("### π§ Assessment Settings")
|
| 464 |
# Display fixed model info
|
| 465 |
gr.Markdown("**Chat Model:** llama-3.3-70b-versatile")
|
|
|
|
| 467 |
|
| 468 |
with gr.Column(scale=1):
|
| 469 |
# Output components
|
| 470 |
+
with gr.Group(): # Using Group instead of Box
|
| 471 |
gr.Markdown("### π ECG Analysis Results")
|
| 472 |
ecg_analysis_output = gr.HTML(label="ECG Analysis", elem_id="ecg-analysis")
|
| 473 |
|
| 474 |
+
with gr.Group(): # Using Group instead of Box
|
| 475 |
gr.Markdown("### π Medical Assessment")
|
| 476 |
assessment_output = gr.HTML(label="Assessment", elem_id="assessment-output")
|
| 477 |
|
| 478 |
gr.Markdown("## π¨ββοΈ Doctor's Consultation")
|
| 479 |
gr.Markdown("Ask follow-up questions about the patient's ECG results and medical condition.")
|
| 480 |
|
| 481 |
+
with gr.Group(): # Using Group instead of Box
|
| 482 |
chatbot = gr.Chatbot(label="Consultation", height=400)
|
| 483 |
with gr.Row():
|
| 484 |
message = gr.Textbox(
|