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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import json
|
10 |
+
from enum import Enum
|
11 |
+
|
12 |
+
class PromptFormat(Enum):
|
13 |
+
XML = "xml"
|
14 |
+
JSON = "json"
|
15 |
+
MARKDOWN = "markdown"
|
16 |
+
|
17 |
+
@dataclass
|
18 |
+
class PatientMetadata:
|
19 |
+
age: int
|
20 |
+
smoking_status: str
|
21 |
+
family_history: bool
|
22 |
+
menopause_status: str
|
23 |
+
previous_mammogram: bool
|
24 |
+
breast_density: str
|
25 |
+
hormone_therapy: bool
|
26 |
+
|
27 |
+
class MicrowaveBreastAnalyzer:
|
28 |
+
def __init__(self, hf_token: str, prompt_format: PromptFormat = PromptFormat.XML):
|
29 |
+
"""Initialize the analyzer with models and specified prompt format."""
|
30 |
+
print(f"Initializing system with {prompt_format.value} prompt format...")
|
31 |
+
|
32 |
+
# Set prompt format
|
33 |
+
self.prompt_format = prompt_format
|
34 |
+
|
35 |
+
# Login to Hugging Face
|
36 |
+
login(token=hf_token)
|
37 |
+
|
38 |
+
# Initialize vision pipelines for tumor detection and size classification
|
39 |
+
self.tumor_classifier = pipeline(
|
40 |
+
"image-classification",
|
41 |
+
model="SIATCN/vit_tumor_classifier",
|
42 |
+
device="cpu"
|
43 |
+
)
|
44 |
+
|
45 |
+
self.size_classifier = pipeline(
|
46 |
+
"image-classification",
|
47 |
+
model="SIATCN/vit_tumor_radius_detection_finetuned",
|
48 |
+
device="cpu"
|
49 |
+
)
|
50 |
+
|
51 |
+
# Initialize Mistral client for report generation
|
52 |
+
self.report_generator = InferenceClient(
|
53 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
54 |
+
token=hf_token
|
55 |
+
)
|
56 |
+
|
57 |
+
print("Initialization complete!")
|
58 |
+
|
59 |
+
def _generate_synthetic_metadata(self) -> PatientMetadata:
|
60 |
+
"""Generate realistic patient metadata for screening."""
|
61 |
+
age = random.randint(40, 75)
|
62 |
+
smoking_status = random.choice(["Never Smoker", "Former Smoker", "Current Smoker"])
|
63 |
+
family_history = random.choice([True, False])
|
64 |
+
menopause_status = "Post-menopausal" if age > 50 else "Pre-menopausal"
|
65 |
+
previous_mammogram = random.choice([True, False])
|
66 |
+
breast_density = random.choice([
|
67 |
+
"A: Almost entirely fatty",
|
68 |
+
"B: Scattered fibroglandular",
|
69 |
+
"C: Heterogeneously dense",
|
70 |
+
"D: Extremely dense"
|
71 |
+
])
|
72 |
+
hormone_therapy = random.choice([True, False])
|
73 |
+
|
74 |
+
return PatientMetadata(
|
75 |
+
age=age,
|
76 |
+
smoking_status=smoking_status,
|
77 |
+
family_history=family_history,
|
78 |
+
menopause_status=menopause_status,
|
79 |
+
previous_mammogram=previous_mammogram,
|
80 |
+
breast_density=breast_density,
|
81 |
+
hormone_therapy=hormone_therapy
|
82 |
+
)
|
83 |
+
|
84 |
+
def _process_image(self, image: Image.Image) -> Image.Image:
|
85 |
+
"""Process input image for model consumption."""
|
86 |
+
if image.mode != 'RGB':
|
87 |
+
image = image.convert('RGB')
|
88 |
+
return image.resize((224, 224))
|
89 |
+
|
90 |
+
def _generate_xml_prompt(self, has_tumor: bool, tumor_size: str, metadata: PatientMetadata) -> str:
|
91 |
+
"""Generate XML-style prompt."""
|
92 |
+
return f"""<s>[INST] Generate a structured medical report for a microwave breast imaging scan using the following format exactly.
|
93 |
+
Keep sections consistent and use proper medical terminology. Be concise yet thorough.
|
94 |
+
|
95 |
+
EXAMINATION PERFORMED:
|
96 |
+
- Microwave Breast Imaging Scan
|
97 |
+
- Date: {datetime.now().strftime('%B %d, %Y')}
|
98 |
+
|
99 |
+
IMAGING FINDINGS:
|
100 |
+
Primary Finding: {'Abnormal area detected' if has_tumor else 'No abnormalities detected'}
|
101 |
+
{f'Detected Mass Size: {tumor_size} cm' if has_tumor else ''}
|
102 |
+
|
103 |
+
PATIENT HISTORY:
|
104 |
+
- Age: {metadata.age} years
|
105 |
+
- Menopausal Status: {metadata.menopause_status}
|
106 |
+
- Previous Screening: {'Yes' if metadata.previous_mammogram else 'No'}
|
107 |
+
- Tissue Characteristics: {metadata.breast_density}
|
108 |
+
|
109 |
+
RISK FACTORS:
|
110 |
+
{f'• Family History: {"Present" if metadata.family_history else "None"}'}
|
111 |
+
• Smoking Status: {metadata.smoking_status}
|
112 |
+
• Hormone Therapy: {'Yes' if metadata.hormone_therapy else 'No'}
|
113 |
+
|
114 |
+
Please generate a report with these exact sections:
|
115 |
+
|
116 |
+
1. DETAILED FINDINGS
|
117 |
+
[Describe the microwave imaging findings in detail, including location and characteristics of any detected abnormalities]
|
118 |
+
|
119 |
+
2. INTERPRETATION
|
120 |
+
[Provide a clear assessment of the microwave imaging results and their clinical significance]
|
121 |
+
|
122 |
+
3. RECOMMENDATIONS
|
123 |
+
[List specific follow-up actions and timeline]
|
124 |
+
|
125 |
+
4. TECHNICAL NOTES
|
126 |
+
[Include any relevant information about the scan quality and any technical considerations]
|
127 |
+
|
128 |
+
Format each section consistently and maintain professional medical terminology throughout. Note that this uses microwave imaging technology, not mammography. [/INST]</s>"""
|
129 |
+
|
130 |
+
def _generate_json_prompt(self, has_tumor: bool, tumor_size: str, metadata: PatientMetadata) -> str:
|
131 |
+
"""Generate JSON-style prompt."""
|
132 |
+
prompt_data = {
|
133 |
+
"instruction": "Generate a structured medical report for a microwave breast imaging scan",
|
134 |
+
"format_requirements": "Keep sections consistent and use proper medical terminology. Be concise yet thorough.",
|
135 |
+
"input_data": {
|
136 |
+
"examination": {
|
137 |
+
"type": "Microwave Breast Imaging Scan",
|
138 |
+
"date": datetime.now().strftime('%B %d, %Y')
|
139 |
+
},
|
140 |
+
"imaging_findings": {
|
141 |
+
"primary_finding": "Abnormal area detected" if has_tumor else "No abnormalities detected",
|
142 |
+
"mass_size": f"{tumor_size} cm" if has_tumor else None
|
143 |
+
},
|
144 |
+
"patient_history": {
|
145 |
+
"age": metadata.age,
|
146 |
+
"menopausal_status": metadata.menopause_status,
|
147 |
+
"previous_screening": metadata.previous_mammogram,
|
148 |
+
"tissue_characteristics": metadata.breast_density
|
149 |
+
},
|
150 |
+
"risk_factors": {
|
151 |
+
"family_history": "Present" if metadata.family_history else "None",
|
152 |
+
"smoking_status": metadata.smoking_status,
|
153 |
+
"hormone_therapy": "Yes" if metadata.hormone_therapy else "No"
|
154 |
+
}
|
155 |
+
},
|
156 |
+
"required_sections": [
|
157 |
+
"DETAILED FINDINGS",
|
158 |
+
"INTERPRETATION",
|
159 |
+
"RECOMMENDATIONS",
|
160 |
+
"TECHNICAL NOTES"
|
161 |
+
],
|
162 |
+
"section_guidelines": {
|
163 |
+
"DETAILED_FINDINGS": "Describe the microwave imaging findings in detail, including location and characteristics of any detected abnormalities",
|
164 |
+
"INTERPRETATION": "Provide a clear assessment of the microwave imaging results and their clinical significance",
|
165 |
+
"RECOMMENDATIONS": "List specific follow-up actions and timeline",
|
166 |
+
"TECHNICAL_NOTES": "Include any relevant information about the scan quality and any technical considerations"
|
167 |
+
}
|
168 |
+
}
|
169 |
+
|
170 |
+
return f"<s>[INST] {json.dumps(prompt_data, indent=2)} [/INST]</s>"
|
171 |
+
|
172 |
+
def _generate_markdown_prompt(self, has_tumor: bool, tumor_size: str, metadata: PatientMetadata) -> str:
|
173 |
+
"""Generate Markdown-style prompt."""
|
174 |
+
return f"""<s>[INST]
|
175 |
+
# Medical Report Generation Request
|
176 |
+
|
177 |
+
## Context
|
178 |
+
Generate a structured medical report for a microwave breast imaging scan.
|
179 |
+
|
180 |
+
## Current Examination Data
|
181 |
+
* **Type:** Microwave Breast Imaging Scan
|
182 |
+
* **Date:** {datetime.now().strftime('%B %d, %Y')}
|
183 |
+
|
184 |
+
## Current Findings
|
185 |
+
* **Primary Finding:** {"Abnormal area detected" if has_tumor else "No abnormalities detected"}
|
186 |
+
* **Mass Size:** {f"{tumor_size} cm" if has_tumor else "N/A"}
|
187 |
+
|
188 |
+
## Patient Information
|
189 |
+
* **Age:** {metadata.age} years
|
190 |
+
* **Menopausal Status:** {metadata.menopause_status}
|
191 |
+
* **Previous Screening:** {"Yes" if metadata.previous_mammogram else "No"}
|
192 |
+
* **Tissue Characteristics:** {metadata.breast_density}
|
193 |
+
|
194 |
+
## Risk Assessment
|
195 |
+
* **Family History:** {"Present" if metadata.family_history else "None"}
|
196 |
+
* **Smoking Status:** {metadata.smoking_status}
|
197 |
+
* **Hormone Therapy:** {"Yes" if metadata.hormone_therapy else "No"}
|
198 |
+
|
199 |
+
## Required Report Sections
|
200 |
+
1. **Detailed Findings**
|
201 |
+
- Include location and characteristics of any detected abnormalities
|
202 |
+
2. **Interpretation**
|
203 |
+
- Assess microwave imaging results and clinical significance
|
204 |
+
3. **Recommendations**
|
205 |
+
- Specify follow-up actions and timeline
|
206 |
+
4. **Technical Notes**
|
207 |
+
- Document scan quality and technical considerations
|
208 |
+
|
209 |
+
Please maintain professional medical terminology throughout the report.
|
210 |
+
[/INST]</s>"""
|
211 |
+
|
212 |
+
def _generate_medical_report(self, has_tumor: bool, tumor_size: str, metadata: PatientMetadata) -> str:
|
213 |
+
"""Generate a standardized report for microwave breast imaging."""
|
214 |
+
# Select prompt format based on configuration
|
215 |
+
if self.prompt_format == PromptFormat.XML:
|
216 |
+
prompt = self._generate_xml_prompt(has_tumor, tumor_size, metadata)
|
217 |
+
elif self.prompt_format == PromptFormat.JSON:
|
218 |
+
prompt = self._generate_json_prompt(has_tumor, tumor_size, metadata)
|
219 |
+
else: # PromptFormat.MARKDOWN
|
220 |
+
prompt = self._generate_markdown_prompt(has_tumor, tumor_size, metadata)
|
221 |
+
|
222 |
+
# Generate response using Mistral
|
223 |
+
response = self.report_generator.text_generation(
|
224 |
+
prompt,
|
225 |
+
max_new_tokens=800,
|
226 |
+
temperature=0.3,
|
227 |
+
top_p=0.9,
|
228 |
+
repetition_penalty=1.1,
|
229 |
+
do_sample=True,
|
230 |
+
seed=42
|
231 |
+
)
|
232 |
+
|
233 |
+
# Post-process the response to ensure consistent formatting
|
234 |
+
formatted_response = f"""MICROWAVE BREAST IMAGING REPORT
|
235 |
+
Date: {datetime.now().strftime('%B %d, %Y')}
|
236 |
+
----------------------------------------
|
237 |
+
|
238 |
+
{response.strip()}
|
239 |
+
|
240 |
+
----------------------------------------
|
241 |
+
NOTE: This report was generated using AI assistance and should be reviewed by a qualified healthcare professional.
|
242 |
+
This screening was performed using microwave imaging technology."""
|
243 |
+
|
244 |
+
return formatted_response
|
245 |
+
|
246 |
+
def analyze(self, image: Image.Image) -> str:
|
247 |
+
"""Main analysis pipeline with standardized output."""
|
248 |
+
try:
|
249 |
+
processed_image = self._process_image(image)
|
250 |
+
metadata = self._generate_synthetic_metadata()
|
251 |
+
|
252 |
+
# Detect tumor
|
253 |
+
tumor_result = self.tumor_classifier(processed_image)
|
254 |
+
has_tumor = tumor_result[0]['label'] == 'tumor'
|
255 |
+
tumor_confidence = tumor_result[0]['score']
|
256 |
+
|
257 |
+
# Measure size if tumor detected
|
258 |
+
size_result = self.size_classifier(processed_image)
|
259 |
+
tumor_size = size_result[0]['label'].replace('tumor-', '')
|
260 |
+
|
261 |
+
# Generate report
|
262 |
+
report = self._generate_medical_report(has_tumor, tumor_size, metadata)
|
263 |
+
|
264 |
+
return f"""MICROWAVE BREAST IMAGING ANALYSIS
|
265 |
+
========================================
|
266 |
+
|
267 |
+
INITIAL SCAN ASSESSMENT:
|
268 |
+
{'⚠️ ABNORMAL FINDING DETECTED' if has_tumor else '✓ NO ABNORMALITIES DETECTED'}
|
269 |
+
Detection Confidence: {tumor_confidence:.2%}
|
270 |
+
{f'Estimated Mass Size: {tumor_size} cm' if has_tumor else ''}
|
271 |
+
|
272 |
+
----------------------------------------
|
273 |
+
{report}"""
|
274 |
+
|
275 |
+
except Exception as e:
|
276 |
+
import traceback
|
277 |
+
return f"Error during analysis: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
278 |
+
|
279 |
+
def create_interface(hf_token: str, prompt_format: PromptFormat = PromptFormat.XML) -> gr.Interface:
|
280 |
+
"""Create the Gradio interface."""
|
281 |
+
analyzer = MicrowaveBreastAnalyzer(hf_token, prompt_format)
|
282 |
+
|
283 |
+
interface = gr.Interface(
|
284 |
+
fn=analyzer.analyze,
|
285 |
+
inputs=[
|
286 |
+
gr.Image(type="pil", label="Upload Microwave Breast Image for Analysis")
|
287 |
+
],
|
288 |
+
outputs=[
|
289 |
+
gr.Textbox(label="Analysis Results", lines=20)
|
290 |
+
],
|
291 |
+
title=f"Microwave Breast Imaging Analysis System ({prompt_format.value.upper()} Format)",
|
292 |
+
description="""Upload a microwave breast image for comprehensive analysis. The system will:
|
293 |
+
1. Detect the presence of tumors using microwave imaging technology
|
294 |
+
2. Classify tumor size if present
|
295 |
+
3. Generate a detailed medical report with recommendations
|
296 |
+
|
297 |
+
Note: This system uses microwave imaging technology for breast screening, which offers a safe,
|
298 |
+
radiation-free alternative to traditional mammography.""",
|
299 |
+
)
|
300 |
+
|
301 |
+
return interface
|
302 |
+
|
303 |
+
if __name__ == "__main__":
|
304 |
+
print("Starting microwave breast imaging analysis system...")
|
305 |
+
# Load HuggingFace token from secrets
|
306 |
+
HF_TOKEN = os.environ.get("HUGGINGFACE_TOKEN")
|
307 |
+
if not HF_TOKEN:
|
308 |
+
raise ValueError("Please set HUGGINGFACE_TOKEN environment variable")
|
309 |
+
|
310 |
+
# Create interfaces for different formats
|
311 |
+
interface_xml = create_interface(HF_TOKEN, PromptFormat.XML)
|
312 |
+
interface_json = create_interface(HF_TOKEN, PromptFormat.JSON)
|
313 |
+
interface_markdown = create_interface(HF_TOKEN, PromptFormat.MARKDOWN)
|
314 |
+
|
315 |
+
# Launch the XML version by default
|
316 |
+
interface_xml.launch(
|
317 |
+
debug=True,
|
318 |
+
server_name="0.0.0.0",
|
319 |
+
server_port=7860,
|
320 |
+
share=False
|
321 |
+
)
|