Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,100 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
Keep sections consistent and use proper medical terminology. Be concise yet thorough.
|
5 |
|
6 |
EXAMINATION PERFORMED:
|
7 |
-
-
|
8 |
- Date: {datetime.now().strftime('%B %d, %Y')}
|
9 |
|
10 |
-
|
11 |
Primary Finding: {'Abnormal area detected' if has_tumor else 'No abnormalities detected'}
|
12 |
-
{f'
|
13 |
|
14 |
PATIENT HISTORY:
|
15 |
- Age: {metadata.age} years
|
16 |
- Menopausal Status: {metadata.menopause_status}
|
17 |
-
- Previous
|
18 |
-
-
|
19 |
|
20 |
RISK FACTORS:
|
21 |
{f'• Family History: {"Present" if metadata.family_history else "None"}'}
|
@@ -25,32 +104,32 @@ RISK FACTORS:
|
|
25 |
Please generate a report with these exact sections:
|
26 |
|
27 |
1. DETAILED FINDINGS
|
28 |
-
[Describe the
|
29 |
|
30 |
-
2.
|
31 |
-
[Provide a clear assessment
|
32 |
|
33 |
3. RECOMMENDATIONS
|
34 |
[List specific follow-up actions and timeline]
|
35 |
|
36 |
-
4.
|
37 |
-
[Include any relevant
|
38 |
-
|
39 |
-
Format each section consistently and maintain professional medical terminology throughout. [/INST]</s>"""
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
Date: {datetime.now().strftime('%B %d, %Y')}
|
55 |
----------------------------------------
|
56 |
|
@@ -58,40 +137,79 @@ Date: {datetime.now().strftime('%B %d, %Y')}
|
|
58 |
|
59 |
----------------------------------------
|
60 |
NOTE: This report was generated using AI assistance and should be reviewed by a qualified healthcare professional.
|
61 |
-
|
62 |
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
metadata = self._generate_synthetic_metadata()
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
|
81 |
-
|
82 |
-
|
83 |
|
84 |
-
|
85 |
========================================
|
86 |
|
87 |
INITIAL SCAN ASSESSMENT:
|
88 |
{'⚠️ ABNORMAL FINDING DETECTED' if has_tumor else '✓ NO ABNORMALITIES DETECTED'}
|
89 |
Detection Confidence: {tumor_confidence:.2%}
|
90 |
-
{f'Estimated Size: {tumor_size} cm' if has_tumor else ''}
|
91 |
|
92 |
----------------------------------------
|
93 |
{report}"""
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from dataclasses import dataclass
|
4 |
+
import random
|
5 |
+
from transformers import pipeline
|
6 |
+
from huggingface_hub import InferenceClient, login
|
7 |
+
import os
|
8 |
+
from datetime import datetime
|
9 |
+
|
10 |
+
@dataclass
|
11 |
+
class PatientMetadata:
|
12 |
+
age: int
|
13 |
+
smoking_status: str
|
14 |
+
family_history: bool
|
15 |
+
menopause_status: str
|
16 |
+
previous_mammogram: bool
|
17 |
+
breast_density: str
|
18 |
+
hormone_therapy: bool
|
19 |
+
|
20 |
+
class MicrowaveBreastAnalyzer:
|
21 |
+
def __init__(self, hf_token: str):
|
22 |
+
"""Initialize the analyzer with models."""
|
23 |
+
print("Initializing system...")
|
24 |
+
|
25 |
+
# Login to Hugging Face
|
26 |
+
login(token=hf_token)
|
27 |
+
|
28 |
+
# Initialize vision pipelines for tumor detection and size classification
|
29 |
+
self.tumor_classifier = pipeline(
|
30 |
+
"image-classification",
|
31 |
+
model="SIATCN/vit_tumor_classifier",
|
32 |
+
device="cpu"
|
33 |
+
)
|
34 |
+
|
35 |
+
self.size_classifier = pipeline(
|
36 |
+
"image-classification",
|
37 |
+
model="SIATCN/vit_tumor_radius_detection_finetuned",
|
38 |
+
device="cpu"
|
39 |
+
)
|
40 |
+
|
41 |
+
# Initialize Mistral client for report generation
|
42 |
+
self.report_generator = InferenceClient(
|
43 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
44 |
+
token=hf_token
|
45 |
+
)
|
46 |
+
|
47 |
+
print("Initialization complete!")
|
48 |
+
|
49 |
+
def _generate_synthetic_metadata(self) -> PatientMetadata:
|
50 |
+
"""Generate realistic patient metadata for screening."""
|
51 |
+
age = random.randint(40, 75)
|
52 |
+
smoking_status = random.choice(["Never Smoker", "Former Smoker", "Current Smoker"])
|
53 |
+
family_history = random.choice([True, False])
|
54 |
+
menopause_status = "Post-menopausal" if age > 50 else "Pre-menopausal"
|
55 |
+
previous_mammogram = random.choice([True, False])
|
56 |
+
breast_density = random.choice([
|
57 |
+
"A: Almost entirely fatty",
|
58 |
+
"B: Scattered fibroglandular",
|
59 |
+
"C: Heterogeneously dense",
|
60 |
+
"D: Extremely dense"
|
61 |
+
])
|
62 |
+
hormone_therapy = random.choice([True, False])
|
63 |
+
|
64 |
+
return PatientMetadata(
|
65 |
+
age=age,
|
66 |
+
smoking_status=smoking_status,
|
67 |
+
family_history=family_history,
|
68 |
+
menopause_status=menopause_status,
|
69 |
+
previous_mammogram=previous_mammogram,
|
70 |
+
breast_density=breast_density,
|
71 |
+
hormone_therapy=hormone_therapy
|
72 |
+
)
|
73 |
+
|
74 |
+
def _process_image(self, image: Image.Image) -> Image.Image:
|
75 |
+
"""Process input image for model consumption."""
|
76 |
+
if image.mode != 'RGB':
|
77 |
+
image = image.convert('RGB')
|
78 |
+
return image.resize((224, 224))
|
79 |
+
|
80 |
+
def _generate_medical_report(self, has_tumor: bool, tumor_size: str, metadata: PatientMetadata) -> str:
|
81 |
+
"""Generate a standardized report for microwave breast imaging."""
|
82 |
+
prompt = f"""<s>[INST] Generate a structured medical report for a microwave breast imaging scan using the following format exactly.
|
83 |
Keep sections consistent and use proper medical terminology. Be concise yet thorough.
|
84 |
|
85 |
EXAMINATION PERFORMED:
|
86 |
+
- Microwave Breast Imaging Scan
|
87 |
- Date: {datetime.now().strftime('%B %d, %Y')}
|
88 |
|
89 |
+
IMAGING FINDINGS:
|
90 |
Primary Finding: {'Abnormal area detected' if has_tumor else 'No abnormalities detected'}
|
91 |
+
{f'Detected Mass Size: {tumor_size} cm' if has_tumor else ''}
|
92 |
|
93 |
PATIENT HISTORY:
|
94 |
- Age: {metadata.age} years
|
95 |
- Menopausal Status: {metadata.menopause_status}
|
96 |
+
- Previous Screening: {'Yes' if metadata.previous_mammogram else 'No'}
|
97 |
+
- Tissue Characteristics: {metadata.breast_density}
|
98 |
|
99 |
RISK FACTORS:
|
100 |
{f'• Family History: {"Present" if metadata.family_history else "None"}'}
|
|
|
104 |
Please generate a report with these exact sections:
|
105 |
|
106 |
1. DETAILED FINDINGS
|
107 |
+
[Describe the microwave imaging findings in detail, including location and characteristics of any detected abnormalities]
|
108 |
|
109 |
+
2. INTERPRETATION
|
110 |
+
[Provide a clear assessment of the microwave imaging results and their clinical significance]
|
111 |
|
112 |
3. RECOMMENDATIONS
|
113 |
[List specific follow-up actions and timeline]
|
114 |
|
115 |
+
4. TECHNICAL NOTES
|
116 |
+
[Include any relevant information about the scan quality and any technical considerations]
|
117 |
+
|
118 |
+
Format each section consistently and maintain professional medical terminology throughout. Note that this uses microwave imaging technology, not mammography. [/INST]</s>"""
|
119 |
+
|
120 |
+
# Generate response using Mistral
|
121 |
+
response = self.report_generator.text_generation(
|
122 |
+
prompt,
|
123 |
+
max_new_tokens=800,
|
124 |
+
temperature=0.3,
|
125 |
+
top_p=0.9,
|
126 |
+
repetition_penalty=1.1,
|
127 |
+
do_sample=True,
|
128 |
+
seed=42
|
129 |
+
)
|
130 |
+
|
131 |
+
# Post-process the response to ensure consistent formatting
|
132 |
+
formatted_response = f"""MICROWAVE BREAST IMAGING REPORT
|
133 |
Date: {datetime.now().strftime('%B %d, %Y')}
|
134 |
----------------------------------------
|
135 |
|
|
|
137 |
|
138 |
----------------------------------------
|
139 |
NOTE: This report was generated using AI assistance and should be reviewed by a qualified healthcare professional.
|
140 |
+
This screening was performed using microwave imaging technology."""
|
141 |
|
142 |
+
return formatted_response
|
143 |
|
144 |
+
def analyze(self, image: Image.Image) -> str:
|
145 |
+
"""Main analysis pipeline with standardized output."""
|
146 |
+
try:
|
147 |
+
processed_image = self._process_image(image)
|
148 |
+
metadata = self._generate_synthetic_metadata()
|
|
|
149 |
|
150 |
+
# Detect tumor
|
151 |
+
tumor_result = self.tumor_classifier(processed_image)
|
152 |
+
has_tumor = tumor_result[0]['label'] == 'tumor'
|
153 |
+
tumor_confidence = tumor_result[0]['score']
|
154 |
|
155 |
+
# Measure size if tumor detected
|
156 |
+
size_result = self.size_classifier(processed_image)
|
157 |
+
tumor_size = size_result[0]['label'].replace('tumor-', '')
|
158 |
|
159 |
+
# Generate report
|
160 |
+
report = self._generate_medical_report(has_tumor, tumor_size, metadata)
|
161 |
|
162 |
+
return f"""MICROWAVE BREAST IMAGING ANALYSIS
|
163 |
========================================
|
164 |
|
165 |
INITIAL SCAN ASSESSMENT:
|
166 |
{'⚠️ ABNORMAL FINDING DETECTED' if has_tumor else '✓ NO ABNORMALITIES DETECTED'}
|
167 |
Detection Confidence: {tumor_confidence:.2%}
|
168 |
+
{f'Estimated Mass Size: {tumor_size} cm' if has_tumor else ''}
|
169 |
|
170 |
----------------------------------------
|
171 |
{report}"""
|
172 |
|
173 |
+
except Exception as e:
|
174 |
+
import traceback
|
175 |
+
return f"Error during analysis: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
176 |
+
|
177 |
+
def create_interface(hf_token: str) -> gr.Interface:
|
178 |
+
"""Create the Gradio interface."""
|
179 |
+
analyzer = MicrowaveBreastAnalyzer(hf_token)
|
180 |
+
|
181 |
+
interface = gr.Interface(
|
182 |
+
fn=analyzer.analyze,
|
183 |
+
inputs=[
|
184 |
+
gr.Image(type="pil", label="Upload Microwave Breast Image for Analysis")
|
185 |
+
],
|
186 |
+
outputs=[
|
187 |
+
gr.Textbox(label="Analysis Results", lines=20)
|
188 |
+
],
|
189 |
+
title="Microwave Breast Imaging Analysis System",
|
190 |
+
description="""Upload a microwave breast image for comprehensive analysis. The system will:
|
191 |
+
1. Detect the presence of tumors using microwave imaging technology
|
192 |
+
2. Classify tumor size if present
|
193 |
+
3. Generate a detailed medical report with recommendations
|
194 |
+
|
195 |
+
Note: This system uses microwave imaging technology for breast screening, which offers a safe,
|
196 |
+
radiation-free alternative to traditional mammography.""",
|
197 |
+
)
|
198 |
+
|
199 |
+
return interface
|
200 |
+
|
201 |
+
if __name__ == "__main__":
|
202 |
+
print("Starting microwave breast imaging analysis system...")
|
203 |
+
# Load HuggingFace token from secrets
|
204 |
+
HF_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
|
205 |
+
if not HF_TOKEN:
|
206 |
+
raise ValueError("Please set HUGGINGFACE_TOKEN environment variable")
|
207 |
+
|
208 |
+
interface = create_interface(HF_TOKEN)
|
209 |
+
# Modified launch parameters for Spaces
|
210 |
+
interface.launch(
|
211 |
+
debug=True,
|
212 |
+
server_name="0.0.0.0",
|
213 |
+
server_port=7860,
|
214 |
+
share=False
|
215 |
+
)
|