Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ class MediClearBackend:
|
|
| 21 |
|
| 22 |
@staticmethod
|
| 23 |
def analyze_medical_image(image_path):
|
| 24 |
-
"""Step 1: Extract technical info"""
|
| 25 |
try:
|
| 26 |
base64_image = MediClearBackend.encode_image(image_path)
|
| 27 |
|
|
@@ -55,8 +55,29 @@ class MediClearBackend:
|
|
| 55 |
raise Exception(f"Image Analysis Error: {str(e)}")
|
| 56 |
|
| 57 |
@staticmethod
|
| 58 |
-
def
|
| 59 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
if not HYPERBOLIC_KEY:
|
| 62 |
raise Exception("Hyperbolic API key not configured")
|
|
@@ -68,19 +89,24 @@ class MediClearBackend:
|
|
| 68 |
}
|
| 69 |
|
| 70 |
system_prompt = """You are a senior medical doctor with 50 years of experience.
|
| 71 |
-
Your role is to
|
| 72 |
Never mention the doctors name whatsoever and never ask any counter questions.
|
|
|
|
|
|
|
|
|
|
| 73 |
Structure:
|
| 74 |
-
1. Warm greeting.
|
| 75 |
-
2. Overall assessment.
|
| 76 |
-
3. Key findings
|
| 77 |
-
4.
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
data = {
|
| 81 |
"messages": [
|
| 82 |
{"role": "system", "content": system_prompt},
|
| 83 |
-
{"role": "user", "content":
|
| 84 |
],
|
| 85 |
"model": "moonshotai/Kimi-K2-Instruct", # This is the Kimi K2 model
|
| 86 |
"max_tokens": 4096,
|
|
@@ -106,23 +132,18 @@ def process_single_image(image_file):
|
|
| 106 |
technical_data = backend.analyze_medical_image(image_file.name)
|
| 107 |
|
| 108 |
# Step 2: Patient-friendly summary with Kimi K2
|
| 109 |
-
final_report = backend.
|
| 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
|
|
@@ -131,46 +152,49 @@ def process_single_image(image_file):
|
|
| 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"""
|
| 135 |
backend = MediClearBackend()
|
| 136 |
|
| 137 |
if not image_files:
|
| 138 |
return "β Please upload at least one medical image."
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
|
| 144 |
---
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 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 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
def create_gradio_interface():
|
| 176 |
"""Create the Gradio interface"""
|
|
@@ -209,7 +233,7 @@ def create_gradio_interface():
|
|
| 209 |
border: 1px solid #4CAF50 !important;
|
| 210 |
border-radius: 8px !important;
|
| 211 |
padding: 15px !important;
|
| 212 |
-
margin:
|
| 213 |
}
|
| 214 |
.processing {
|
| 215 |
background: #FFD60A20 !important;
|
|
@@ -298,7 +322,7 @@ def create_gradio_interface():
|
|
| 298 |
|
| 299 |
# Multiple image upload
|
| 300 |
multiple_images = gr.File(
|
| 301 |
-
label="Batch Image Analysis (Multiple)",
|
| 302 |
file_types=[".jpg", ".jpeg", ".png", ".bmp"],
|
| 303 |
file_count="multiple",
|
| 304 |
type="filepath"
|
|
@@ -320,9 +344,15 @@ def create_gradio_interface():
|
|
| 320 |
# Clear button
|
| 321 |
clear_btn = gr.Button("ποΈ Clear All", variant="stop")
|
| 322 |
|
| 323 |
-
#
|
| 324 |
with gr.Group(elem_classes="processing"):
|
| 325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
# Disclaimer
|
| 328 |
with gr.Group(elem_classes="warning"):
|
|
@@ -405,7 +435,7 @@ def create_gradio_interface():
|
|
| 405 |
gr.Markdown("""
|
| 406 |
1. **Upload** a medical report image
|
| 407 |
2. **Click Analyze** to get AI-powered medical insights
|
| 408 |
-
3. **
|
| 409 |
4. **Always consult** with healthcare professionals for medical decisions
|
| 410 |
""")
|
| 411 |
|
|
|
|
| 21 |
|
| 22 |
@staticmethod
|
| 23 |
def analyze_medical_image(image_path):
|
| 24 |
+
"""Step 1: Extract technical info from a single image"""
|
| 25 |
try:
|
| 26 |
base64_image = MediClearBackend.encode_image(image_path)
|
| 27 |
|
|
|
|
| 55 |
raise Exception(f"Image Analysis Error: {str(e)}")
|
| 56 |
|
| 57 |
@staticmethod
|
| 58 |
+
def extract_text_from_multiple_images(image_files):
|
| 59 |
+
"""Extract and combine text from multiple medical images"""
|
| 60 |
+
combined_text = "COMBINED MEDICAL REPORTS ANALYSIS:\n\n"
|
| 61 |
+
|
| 62 |
+
for index, image_file in enumerate(image_files, 1):
|
| 63 |
+
try:
|
| 64 |
+
filename = Path(image_file.name).name
|
| 65 |
+
combined_text += f"=== REPORT {index}: {filename} ===\n"
|
| 66 |
+
|
| 67 |
+
# Extract text from this image
|
| 68 |
+
image_text = MediClearBackend.analyze_medical_image(image_file.name)
|
| 69 |
+
combined_text += f"{image_text}\n\n"
|
| 70 |
+
combined_text += "=" * 50 + "\n\n"
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
combined_text += f"β Failed to process {filename}: {str(e)}\n\n"
|
| 74 |
+
combined_text += "=" * 50 + "\n\n"
|
| 75 |
+
|
| 76 |
+
return combined_text
|
| 77 |
+
|
| 78 |
+
@staticmethod
|
| 79 |
+
def summarize_combined_reports(combined_text):
|
| 80 |
+
"""Step 2: Convert combined technical info to patient-friendly summary"""
|
| 81 |
try:
|
| 82 |
if not HYPERBOLIC_KEY:
|
| 83 |
raise Exception("Hyperbolic API key not configured")
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
system_prompt = """You are a senior medical doctor with 50 years of experience.
|
| 92 |
+
Your role is to analyze multiple medical reports and provide a comprehensive, unified analysis to patients in simple, reassuring, everyday language.
|
| 93 |
Never mention the doctors name whatsoever and never ask any counter questions.
|
| 94 |
+
|
| 95 |
+
You will receive multiple medical reports combined together. Analyze them as a complete patient case and provide:
|
| 96 |
+
|
| 97 |
Structure:
|
| 98 |
+
1. Warm greeting acknowledging this is a comprehensive analysis of multiple reports.
|
| 99 |
+
2. Overall unified assessment of the patient's condition across all reports.
|
| 100 |
+
3. Key findings from all reports, highlighting patterns, consistencies, or important observations.
|
| 101 |
+
4. Comprehensive recommendations based on the complete picture.
|
| 102 |
+
5. Reassuring closing.
|
| 103 |
+
|
| 104 |
+
Speak directly to the patient and provide a holistic view of their medical situation."""
|
| 105 |
|
| 106 |
data = {
|
| 107 |
"messages": [
|
| 108 |
{"role": "system", "content": system_prompt},
|
| 109 |
+
{"role": "user", "content": combined_text}
|
| 110 |
],
|
| 111 |
"model": "moonshotai/Kimi-K2-Instruct", # This is the Kimi K2 model
|
| 112 |
"max_tokens": 4096,
|
|
|
|
| 132 |
technical_data = backend.analyze_medical_image(image_file.name)
|
| 133 |
|
| 134 |
# Step 2: Patient-friendly summary with Kimi K2
|
| 135 |
+
final_report = backend.summarize_combined_reports(technical_data)
|
| 136 |
|
| 137 |
# Format the final report as Markdown
|
| 138 |
filename = Path(image_file.name).name
|
| 139 |
formatted_report = f"""
|
| 140 |
# π©Ί MEDICAL ANALYSIS REPORT: {filename}
|
|
|
|
| 141 |
---
|
|
|
|
| 142 |
## π¬ Technical Analysis
|
| 143 |
*Medical image processing complete*
|
|
|
|
| 144 |
## π Patient Report Summary
|
| 145 |
{final_report}
|
|
|
|
| 146 |
---
|
|
|
|
| 147 |
> β
**Analysis Complete** - This tool provides AI-powered insights and is not a substitute for professional medical diagnosis.
|
| 148 |
"""
|
| 149 |
return formatted_report
|
|
|
|
| 152 |
return f"## β Error\n**Error processing image:** {str(e)}"
|
| 153 |
|
| 154 |
def process_multiple_images(image_files):
|
| 155 |
+
"""Process multiple medical images and return a SINGLE combined analysis"""
|
| 156 |
backend = MediClearBackend()
|
| 157 |
|
| 158 |
if not image_files:
|
| 159 |
return "β Please upload at least one medical image."
|
| 160 |
|
| 161 |
+
try:
|
| 162 |
+
# Step 1: Extract and combine text from all images
|
| 163 |
+
combined_text = backend.extract_text_from_multiple_images(image_files)
|
| 164 |
+
|
| 165 |
+
# Step 2: Generate a single comprehensive summary from combined text
|
| 166 |
+
comprehensive_summary = backend.summarize_combined_reports(combined_text)
|
| 167 |
+
|
| 168 |
+
# Format as a single comprehensive report
|
| 169 |
+
full_report = f"""# π©Ί COMPREHENSIVE MEDICAL ANALYSIS
|
| 170 |
|
| 171 |
+
## π Analysis of {len(image_files)} Medical Reports
|
| 172 |
+
|
| 173 |
+
**Combined insights from all uploaded medical images:**
|
| 174 |
|
| 175 |
---
|
| 176 |
|
| 177 |
+
## π Comprehensive Patient Summary
|
| 178 |
+
|
| 179 |
+
{comprehensive_summary}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
---
|
| 182 |
+
|
| 183 |
+
## π Reports Analyzed:
|
| 184 |
"""
|
| 185 |
+
# List all analyzed files
|
| 186 |
+
for index, image_file in enumerate(image_files, 1):
|
| 187 |
+
filename = Path(image_file.name).name
|
| 188 |
+
full_report += f"- **Report {index}:** {filename}\n"
|
| 189 |
+
|
| 190 |
+
full_report += """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
---
|
| 192 |
+
> π **Comprehensive Analysis Complete** - This unified analysis provides insights across all your medical reports. Remember: This tool provides AI-powered insights and is not a substitute for professional medical diagnosis.
|
|
|
|
| 193 |
"""
|
| 194 |
+
return full_report
|
| 195 |
+
|
| 196 |
+
except Exception as e:
|
| 197 |
+
return f"## β Processing Error\n**Failed to process multiple images:** {str(e)}"
|
| 198 |
|
| 199 |
def create_gradio_interface():
|
| 200 |
"""Create the Gradio interface"""
|
|
|
|
| 233 |
border: 1px solid #4CAF50 !important;
|
| 234 |
border-radius: 8px !important;
|
| 235 |
padding: 15px !important;
|
| 236 |
+
margin: 10x 0 !important;
|
| 237 |
}
|
| 238 |
.processing {
|
| 239 |
background: #FFD60A20 !important;
|
|
|
|
| 322 |
|
| 323 |
# Multiple image upload
|
| 324 |
multiple_images = gr.File(
|
| 325 |
+
label="Batch Image Analysis (Multiple) - Combined Report",
|
| 326 |
file_types=[".jpg", ".jpeg", ".png", ".bmp"],
|
| 327 |
file_count="multiple",
|
| 328 |
type="filepath"
|
|
|
|
| 344 |
# Clear button
|
| 345 |
clear_btn = gr.Button("ποΈ Clear All", variant="stop")
|
| 346 |
|
| 347 |
+
# Processing Info
|
| 348 |
with gr.Group(elem_classes="processing"):
|
| 349 |
+
gr.Markdown(
|
| 350 |
+
"""
|
| 351 |
+
### π Processing Information
|
| 352 |
+
**Single Image:** Individual analysis
|
| 353 |
+
**Multiple Images:** Combined comprehensive analysis from all reports
|
| 354 |
+
"""
|
| 355 |
+
)
|
| 356 |
|
| 357 |
# Disclaimer
|
| 358 |
with gr.Group(elem_classes="warning"):
|
|
|
|
| 435 |
gr.Markdown("""
|
| 436 |
1. **Upload** a medical report image
|
| 437 |
2. **Click Analyze** to get AI-powered medical insights
|
| 438 |
+
3. **For multiple images:** Get a single combined analysis of all reports
|
| 439 |
4. **Always consult** with healthcare professionals for medical decisions
|
| 440 |
""")
|
| 441 |
|