Update app.py
Browse files
app.py
CHANGED
|
@@ -69,7 +69,7 @@ class MediClearBackend:
|
|
| 69 |
|
| 70 |
system_prompt = """You are a senior medical doctor with 50 years of experience.
|
| 71 |
Your role is to explain medical reports to patients in simple, reassuring, everyday language.
|
| 72 |
-
Never mention the doctors name whatsoever.
|
| 73 |
Structure:
|
| 74 |
1. Warm greeting.
|
| 75 |
2. Overall assessment.
|
|
@@ -108,23 +108,27 @@ def process_single_image(image_file):
|
|
| 108 |
# Step 2: Patient-friendly summary with Kimi K2
|
| 109 |
final_report = backend.summarize_for_patient(technical_data)
|
| 110 |
|
| 111 |
-
# Format the final report
|
| 112 |
filename = Path(image_file.name).name
|
| 113 |
formatted_report = f"""
|
| 114 |
-
{
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
{final_report}
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
"""
|
| 124 |
return formatted_report
|
| 125 |
|
| 126 |
except Exception as e:
|
| 127 |
-
return f"β Error processing image: {str(e)}"
|
| 128 |
|
| 129 |
def process_multiple_images(image_files):
|
| 130 |
"""Process multiple medical images and return combined analysis"""
|
|
@@ -133,31 +137,39 @@ def process_multiple_images(image_files):
|
|
| 133 |
if not image_files:
|
| 134 |
return "β Please upload at least one medical image."
|
| 135 |
|
| 136 |
-
full_report = f"π©Ί SEHATSCAN - COMPREHENSIVE MEDICAL ANALYSIS
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
| 140 |
total = len(image_files)
|
| 141 |
|
| 142 |
for index, image_file in enumerate(image_files, 1):
|
| 143 |
try:
|
| 144 |
filename = Path(image_file.name).name
|
| 145 |
-
full_report += f"
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
| 147 |
# Process the image
|
| 148 |
technical_data = backend.analyze_medical_image(image_file.name)
|
| 149 |
patient_summary = backend.summarize_for_patient(technical_data)
|
| 150 |
|
| 151 |
full_report += f"{patient_summary}\n\n"
|
| 152 |
-
full_report += "
|
|
|
|
| 153 |
|
| 154 |
except Exception as e:
|
| 155 |
-
full_report += f"β Failed to process {filename}: {str(e)}\n\n"
|
| 156 |
-
|
| 157 |
-
full_report += f"{'='*70}\n"
|
| 158 |
-
full_report += "π All analyses complete. Remember: This tool provides AI-powered insights and is not a substitute for professional medical diagnosis.\n"
|
| 159 |
-
full_report += f"{'='*70}"
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
return full_report
|
| 162 |
|
| 163 |
def create_gradio_interface():
|
|
@@ -167,6 +179,7 @@ def create_gradio_interface():
|
|
| 167 |
custom_css = """
|
| 168 |
.gradio-container {
|
| 169 |
background: linear-gradient(135deg, #1A1A1A 0%, #2D2D2D 100%);
|
|
|
|
| 170 |
}
|
| 171 |
.dark {
|
| 172 |
background: transparent !important;
|
|
@@ -205,6 +218,48 @@ def create_gradio_interface():
|
|
| 205 |
padding: 15px !important;
|
| 206 |
margin: 10px 0 !important;
|
| 207 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
"""
|
| 209 |
|
| 210 |
with gr.Blocks(
|
|
@@ -269,7 +324,7 @@ def create_gradio_interface():
|
|
| 269 |
with gr.Group(elem_classes="processing"):
|
| 270 |
gr.Markdown(
|
| 271 |
"""
|
| 272 |
-
π€
|
| 273 |
- **Step 1**: Groq + Llama 4 Scout (Medical Image Analysis)
|
| 274 |
- **Step 2**: Hyperbolic + Kimi K2 (Patient Communication)
|
| 275 |
"""
|
|
@@ -279,7 +334,7 @@ def create_gradio_interface():
|
|
| 279 |
with gr.Group(elem_classes="warning"):
|
| 280 |
gr.Markdown(
|
| 281 |
"""
|
| 282 |
-
β οΈ
|
| 283 |
This tool provides AI-powered insights and is not a substitute for professional medical diagnosis,
|
| 284 |
treatment, or medical advice. Always consult with qualified healthcare providers for medical decisions.
|
| 285 |
"""
|
|
@@ -290,12 +345,11 @@ def create_gradio_interface():
|
|
| 290 |
with gr.Group(elem_classes="panel"):
|
| 291 |
gr.Markdown("### π Doctor's Analysis Report")
|
| 292 |
|
| 293 |
-
|
|
|
|
| 294 |
label="Analysis Results",
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
show_copy_button=True,
|
| 298 |
-
container=False
|
| 299 |
)
|
| 300 |
|
| 301 |
# Output actions
|
|
@@ -313,59 +367,50 @@ def create_gradio_interface():
|
|
| 313 |
"""
|
| 314 |
)
|
| 315 |
|
| 316 |
-
# Event handlers
|
| 317 |
analyze_single_btn.click(
|
| 318 |
fn=process_single_image,
|
| 319 |
inputs=[single_image],
|
| 320 |
-
outputs=[
|
| 321 |
api_name="analyze_single"
|
| 322 |
)
|
| 323 |
|
| 324 |
analyze_batch_btn.click(
|
| 325 |
fn=process_multiple_images,
|
| 326 |
inputs=[multiple_images],
|
| 327 |
-
outputs=[
|
| 328 |
api_name="analyze_batch"
|
| 329 |
)
|
| 330 |
|
| 331 |
# Clear functionality
|
| 332 |
def clear_all():
|
| 333 |
-
return None, None, ""
|
| 334 |
|
| 335 |
clear_btn.click(
|
| 336 |
fn=clear_all,
|
| 337 |
-
outputs=[single_image, multiple_images,
|
| 338 |
)
|
| 339 |
|
| 340 |
-
# Copy functionality
|
| 341 |
-
def copy_to_clipboard(text):
|
| 342 |
-
return gr.update(value=text)
|
| 343 |
-
|
| 344 |
copy_btn.click(
|
| 345 |
-
fn=
|
| 346 |
-
|
| 347 |
-
outputs=[output_text]
|
| 348 |
)
|
| 349 |
|
| 350 |
# Export functionality
|
| 351 |
-
def export_report(
|
| 352 |
-
|
| 353 |
-
timestamp = time.strftime("%Y%m%d-%H%M%S")
|
| 354 |
-
filename = f"sehatscan_report_{timestamp}.txt"
|
| 355 |
-
return f"β
Report ready! You can copy the text above and save it as '{filename}'"
|
| 356 |
-
return "β No analysis to export."
|
| 357 |
|
| 358 |
export_btn.click(
|
| 359 |
fn=export_report,
|
| 360 |
-
|
| 361 |
-
outputs=[output_text]
|
| 362 |
)
|
| 363 |
|
| 364 |
# Instructions
|
| 365 |
gr.Markdown("### π― How to use:")
|
| 366 |
gr.Markdown("""
|
| 367 |
1. **Upload** a medical image (X-ray, MRI, CT scan, ultrasound, etc.)
|
| 368 |
-
2. **Click Analyze** to get AI-powered medical insights
|
| 369 |
3. **Review** the patient-friendly explanation (powered by Kimi K2)
|
| 370 |
4. **Always consult** with healthcare professionals for medical decisions
|
| 371 |
""")
|
|
|
|
| 69 |
|
| 70 |
system_prompt = """You are a senior medical doctor with 50 years of experience.
|
| 71 |
Your role is to explain medical reports to patients in simple, reassuring, everyday language.
|
| 72 |
+
Never mention the doctors name whatsoever and never ask any counter questions.
|
| 73 |
Structure:
|
| 74 |
1. Warm greeting.
|
| 75 |
2. Overall assessment.
|
|
|
|
| 108 |
# Step 2: Patient-friendly summary with Kimi K2
|
| 109 |
final_report = backend.summarize_for_patient(technical_data)
|
| 110 |
|
| 111 |
+
# Format the final report as Markdown
|
| 112 |
filename = Path(image_file.name).name
|
| 113 |
formatted_report = f"""
|
| 114 |
+
# π©Ί MEDICAL ANALYSIS REPORT: {filename}
|
| 115 |
+
|
| 116 |
+
---
|
| 117 |
+
|
| 118 |
+
## π¬ Technical Analysis
|
| 119 |
+
*Medical image processing complete*
|
| 120 |
+
|
| 121 |
+
## π Patient Report Summary
|
| 122 |
{final_report}
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
> β
**Analysis Complete** - This tool provides AI-powered insights and is not a substitute for professional medical diagnosis.
|
| 127 |
"""
|
| 128 |
return formatted_report
|
| 129 |
|
| 130 |
except Exception as e:
|
| 131 |
+
return f"## β Error\n**Error processing image:** {str(e)}"
|
| 132 |
|
| 133 |
def process_multiple_images(image_files):
|
| 134 |
"""Process multiple medical images and return combined analysis"""
|
|
|
|
| 137 |
if not image_files:
|
| 138 |
return "β Please upload at least one medical image."
|
| 139 |
|
| 140 |
+
full_report = f"""# π©Ί SEHATSCAN - COMPREHENSIVE MEDICAL ANALYSIS
|
| 141 |
+
|
| 142 |
+
**π¬ Step 1: Groq (Llama 4 Scout) | π Step 2: Hyperbolic (Kimi K2)**
|
| 143 |
+
|
| 144 |
+
---
|
| 145 |
+
|
| 146 |
+
"""
|
| 147 |
total = len(image_files)
|
| 148 |
|
| 149 |
for index, image_file in enumerate(image_files, 1):
|
| 150 |
try:
|
| 151 |
filename = Path(image_file.name).name
|
| 152 |
+
full_report += f"""
|
| 153 |
+
## π IMAGE {index}/{total}: {filename}
|
| 154 |
+
|
| 155 |
+
---
|
| 156 |
+
"""
|
| 157 |
# Process the image
|
| 158 |
technical_data = backend.analyze_medical_image(image_file.name)
|
| 159 |
patient_summary = backend.summarize_for_patient(technical_data)
|
| 160 |
|
| 161 |
full_report += f"{patient_summary}\n\n"
|
| 162 |
+
full_report += "---\n"
|
| 163 |
+
full_report += f"**β
Analysis complete for {filename}**\n\n"
|
| 164 |
|
| 165 |
except Exception as e:
|
| 166 |
+
full_report += f"## β Processing Failed\n**Failed to process {filename}:** {str(e)}\n\n---\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
full_report += """
|
| 169 |
+
---
|
| 170 |
+
|
| 171 |
+
> π **All analyses complete.** Remember: This tool provides AI-powered insights and is not a substitute for professional medical diagnosis.
|
| 172 |
+
"""
|
| 173 |
return full_report
|
| 174 |
|
| 175 |
def create_gradio_interface():
|
|
|
|
| 179 |
custom_css = """
|
| 180 |
.gradio-container {
|
| 181 |
background: linear-gradient(135deg, #1A1A1A 0%, #2D2D2D 100%);
|
| 182 |
+
font-family: 'Segoe UI', system-ui, sans-serif;
|
| 183 |
}
|
| 184 |
.dark {
|
| 185 |
background: transparent !important;
|
|
|
|
| 218 |
padding: 15px !important;
|
| 219 |
margin: 10px 0 !important;
|
| 220 |
}
|
| 221 |
+
.markdown-output {
|
| 222 |
+
background: #1a1a1a !important;
|
| 223 |
+
border-radius: 8px !important;
|
| 224 |
+
padding: 20px !important;
|
| 225 |
+
}
|
| 226 |
+
.markdown-output h1, .markdown-output h2, .markdown-output h3 {
|
| 227 |
+
color: #FFD60A !important;
|
| 228 |
+
margin-top: 1em !important;
|
| 229 |
+
margin-bottom: 0.5em !important;
|
| 230 |
+
}
|
| 231 |
+
.markdown-output p {
|
| 232 |
+
color: #E8E8E8 !important;
|
| 233 |
+
line-height: 1.6 !important;
|
| 234 |
+
}
|
| 235 |
+
.markdown-output ul, .markdown-output ol {
|
| 236 |
+
color: #E8E8E8 !important;
|
| 237 |
+
margin-left: 20px !important;
|
| 238 |
+
}
|
| 239 |
+
.markdown-output blockquote {
|
| 240 |
+
border-left: 4px solid #FFD60A !important;
|
| 241 |
+
margin: 1em 0 !important;
|
| 242 |
+
padding-left: 1em !important;
|
| 243 |
+
color: #B0B0B0 !important;
|
| 244 |
+
font-style: italic !important;
|
| 245 |
+
}
|
| 246 |
+
.markdown-output code {
|
| 247 |
+
background: #404040 !important;
|
| 248 |
+
padding: 2px 6px !important;
|
| 249 |
+
border-radius: 4px !important;
|
| 250 |
+
color: #E8E8E8 !important;
|
| 251 |
+
}
|
| 252 |
+
.markdown-output pre {
|
| 253 |
+
background: #404040 !important;
|
| 254 |
+
padding: 15px !important;
|
| 255 |
+
border-radius: 8px !important;
|
| 256 |
+
overflow-x: auto !important;
|
| 257 |
+
}
|
| 258 |
+
.markdown-output hr {
|
| 259 |
+
border: none !important;
|
| 260 |
+
border-top: 2px solid #404040 !important;
|
| 261 |
+
margin: 2em 0 !important;
|
| 262 |
+
}
|
| 263 |
"""
|
| 264 |
|
| 265 |
with gr.Blocks(
|
|
|
|
| 324 |
with gr.Group(elem_classes="processing"):
|
| 325 |
gr.Markdown(
|
| 326 |
"""
|
| 327 |
+
### π€ AI Models Used:
|
| 328 |
- **Step 1**: Groq + Llama 4 Scout (Medical Image Analysis)
|
| 329 |
- **Step 2**: Hyperbolic + Kimi K2 (Patient Communication)
|
| 330 |
"""
|
|
|
|
| 334 |
with gr.Group(elem_classes="warning"):
|
| 335 |
gr.Markdown(
|
| 336 |
"""
|
| 337 |
+
### β οΈ Medical Disclaimer
|
| 338 |
This tool provides AI-powered insights and is not a substitute for professional medical diagnosis,
|
| 339 |
treatment, or medical advice. Always consult with qualified healthcare providers for medical decisions.
|
| 340 |
"""
|
|
|
|
| 345 |
with gr.Group(elem_classes="panel"):
|
| 346 |
gr.Markdown("### π Doctor's Analysis Report")
|
| 347 |
|
| 348 |
+
# Changed from Textbox to Markdown for pure markdown rendering
|
| 349 |
+
output_markdown = gr.Markdown(
|
| 350 |
label="Analysis Results",
|
| 351 |
+
value="*Upload a medical image and click analyze to see the results here...*",
|
| 352 |
+
elem_classes="markdown-output"
|
|
|
|
|
|
|
| 353 |
)
|
| 354 |
|
| 355 |
# Output actions
|
|
|
|
| 367 |
"""
|
| 368 |
)
|
| 369 |
|
| 370 |
+
# Event handlers - updated to use Markdown output
|
| 371 |
analyze_single_btn.click(
|
| 372 |
fn=process_single_image,
|
| 373 |
inputs=[single_image],
|
| 374 |
+
outputs=[output_markdown],
|
| 375 |
api_name="analyze_single"
|
| 376 |
)
|
| 377 |
|
| 378 |
analyze_batch_btn.click(
|
| 379 |
fn=process_multiple_images,
|
| 380 |
inputs=[multiple_images],
|
| 381 |
+
outputs=[output_markdown],
|
| 382 |
api_name="analyze_batch"
|
| 383 |
)
|
| 384 |
|
| 385 |
# Clear functionality
|
| 386 |
def clear_all():
|
| 387 |
+
return None, None, "*Upload a medical image and click analyze to see the results here...*"
|
| 388 |
|
| 389 |
clear_btn.click(
|
| 390 |
fn=clear_all,
|
| 391 |
+
outputs=[single_image, multiple_images, output_markdown]
|
| 392 |
)
|
| 393 |
|
| 394 |
+
# Copy functionality - for Markdown we'll use a different approach
|
|
|
|
|
|
|
|
|
|
| 395 |
copy_btn.click(
|
| 396 |
+
fn=lambda: gr.Info("Use the copy button in the Markdown output or select and copy the text directly"),
|
| 397 |
+
outputs=None
|
|
|
|
| 398 |
)
|
| 399 |
|
| 400 |
# Export functionality
|
| 401 |
+
def export_report():
|
| 402 |
+
return gr.Info("To export, please select and copy the Markdown content above, then paste it into a text file.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
|
| 404 |
export_btn.click(
|
| 405 |
fn=export_report,
|
| 406 |
+
outputs=None
|
|
|
|
| 407 |
)
|
| 408 |
|
| 409 |
# Instructions
|
| 410 |
gr.Markdown("### π― How to use:")
|
| 411 |
gr.Markdown("""
|
| 412 |
1. **Upload** a medical image (X-ray, MRI, CT scan, ultrasound, etc.)
|
| 413 |
+
2. **Click Analyze** to get AI-powered medical insights
|
| 414 |
3. **Review** the patient-friendly explanation (powered by Kimi K2)
|
| 415 |
4. **Always consult** with healthcare professionals for medical decisions
|
| 416 |
""")
|