SIATCN commited on
Commit
e43847e
·
verified ·
1 Parent(s): 08b56e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -51
app.py CHANGED
@@ -1,21 +1,100 @@
1
- def _generate_medical_report(self, has_tumor: bool, tumor_size: str, metadata: PatientMetadata) -> str:
2
- """Generate a standardized medical report using Mistral."""
3
- prompt = f"""<s>[INST] Generate a structured medical report for a breast imaging scan using the following format exactly.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  Keep sections consistent and use proper medical terminology. Be concise yet thorough.
5
 
6
  EXAMINATION PERFORMED:
7
- - Diagnostic Digital Mammogram
8
  - Date: {datetime.now().strftime('%B %d, %Y')}
9
 
10
- CLINICAL FINDINGS:
11
  Primary Finding: {'Abnormal area detected' if has_tumor else 'No abnormalities detected'}
12
- {f'Lesion Size: {tumor_size} cm' if has_tumor else ''}
13
 
14
  PATIENT HISTORY:
15
  - Age: {metadata.age} years
16
  - Menopausal Status: {metadata.menopause_status}
17
- - Previous Mammogram: {'Yes' if metadata.previous_mammogram else 'No'}
18
- - Breast Tissue Density: {metadata.breast_density}
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 mammographic findings in detail, including location, characteristics, and comparison with any previous studies if available]
29
 
30
- 2. IMPRESSION
31
- [Provide a clear assessment using BI-RADS classification if applicable]
32
 
33
  3. RECOMMENDATIONS
34
  [List specific follow-up actions and timeline]
35
 
36
- 4. ADDITIONAL NOTES
37
- [Include any relevant screening considerations or risk-management suggestions]
38
-
39
- Format each section consistently and maintain professional medical terminology throughout. [/INST]</s>"""
40
-
41
- # Generate response using Mistral
42
- response = self.report_generator.text_generation(
43
- prompt,
44
- max_new_tokens=800, # Increased for comprehensive report
45
- temperature=0.3, # Low temperature for consistency
46
- top_p=0.9,
47
- repetition_penalty=1.1,
48
- do_sample=True,
49
- seed=42
50
- )
51
-
52
- # Post-process the response to ensure consistent formatting
53
- formatted_response = f"""MAMMOGRAM REPORT
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
- All findings and recommendations should be clinically verified."""
62
 
63
- return formatted_response
64
 
65
- # Update the analyze method to ensure consistent output formatting
66
- def analyze(self, image: Image.Image) -> str:
67
- """Main analysis pipeline with standardized output."""
68
- try:
69
- processed_image = self._process_image(image)
70
- metadata = self._generate_synthetic_metadata()
71
 
72
- # Detect tumor
73
- tumor_result = self.tumor_classifier(processed_image)
74
- has_tumor = tumor_result[0]['label'] == 'tumor'
75
- tumor_confidence = tumor_result[0]['score']
76
 
77
- # Measure size if tumor detected
78
- size_result = self.size_classifier(processed_image)
79
- tumor_size = size_result[0]['label'].replace('tumor-', '')
80
 
81
- # Generate report
82
- report = self._generate_medical_report(has_tumor, tumor_size, metadata)
83
 
84
- return f"""BREAST IMAGING ANALYSIS REPORT
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
- except Exception as e:
96
- import traceback
97
- return f"ERROR IN ANALYSIS:\n{str(e)}\n\nDebug Information:\n{traceback.format_exc()}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )