Amarthya7 commited on
Commit
2e3cc98
·
verified ·
1 Parent(s): ff12c25

Upload 6 files

Browse files
Files changed (6) hide show
  1. DOCUMENTATION.md +335 -0
  2. README.md +63 -374
  3. app.py +57 -0
  4. mediSync.log +80 -0
  5. requirements.txt +11 -0
  6. space.yml +9 -0
DOCUMENTATION.md ADDED
@@ -0,0 +1,335 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # MediSync: Multi-Modal Medical Analysis System
2
+
3
+ ## Comprehensive Technical Documentation
4
+
5
+ ### Table of Contents
6
+ 1. [Introduction](#introduction)
7
+ 2. [System Architecture](#system-architecture)
8
+ 3. [Installation](#installation)
9
+ 4. [Usage](#usage)
10
+ 5. [Core Components](#core-components)
11
+ 6. [Model Details](#model-details)
12
+ 7. [API Reference](#api-reference)
13
+ 8. [Extending the System](#extending-the-system)
14
+ 9. [Troubleshooting](#troubleshooting)
15
+ 10. [References](#references)
16
+
17
+ ---
18
+
19
+ ## Introduction
20
+
21
+ MediSync is a multi-modal AI system that combines X-ray image analysis with medical report text processing to provide comprehensive medical insights. By leveraging state-of-the-art deep learning models for both vision and language understanding, MediSync can:
22
+
23
+ - Analyze chest X-ray images to detect abnormalities
24
+ - Extract key clinical information from medical reports
25
+ - Fuse insights from both modalities for enhanced diagnosis support
26
+ - Provide comprehensive visualization of analysis results
27
+
28
+ This AI system demonstrates the power of multi-modal fusion in the healthcare domain, where integrating information from multiple sources can lead to more robust and accurate analyses.
29
+
30
+ ## System Architecture
31
+
32
+ MediSync follows a modular architecture with three main components:
33
+
34
+ 1. **Image Analysis Module**: Processes X-ray images using pre-trained vision models
35
+ 2. **Text Analysis Module**: Analyzes medical reports using NLP models
36
+ 3. **Multimodal Fusion Module**: Combines insights from both modalities
37
+
38
+ The system uses the following high-level workflow:
39
+
40
+ ```
41
+ ┌─────────────────┐
42
+ │ X-ray Image │
43
+ └────────┬────────┘
44
+
45
+
46
+ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
47
+ │ Preprocessing │───▶│ Image Analysis │───▶│ │
48
+ └─────────────────┘ └─────────────────┘ │ │
49
+ │ Multimodal │
50
+ ┌─────────────────┐ ┌─────────────────┐ │ Fusion │───▶ Results
51
+ │ Medical Report │───▶│ Text Analysis │───▶│ │
52
+ └─────────────────┘ └─────────────────┘ │ │
53
+ └─────────────────┘
54
+ ```
55
+
56
+ ## Installation
57
+
58
+ ### Prerequisites
59
+ - Python 3.8 or higher
60
+ - Pip package manager
61
+
62
+ ### Setup Instructions
63
+
64
+ 1. Clone the repository:
65
+ ```bash
66
+ git clone [repository-url]
67
+ cd mediSync
68
+ ```
69
+
70
+ 2. Install dependencies:
71
+ ```bash
72
+ pip install -r requirements.txt
73
+ ```
74
+
75
+ 3. Download sample data:
76
+ ```bash
77
+ python -m mediSync.utils.download_samples
78
+ ```
79
+
80
+ ## Usage
81
+
82
+ ### Running the Application
83
+
84
+ To launch the MediSync application with the Gradio interface:
85
+
86
+ ```bash
87
+ python run.py
88
+ ```
89
+
90
+ This will:
91
+ 1. Download sample data if not already present
92
+ 2. Initialize the application
93
+ 3. Launch the Gradio web interface
94
+
95
+ ### Web Interface
96
+
97
+ MediSync provides a user-friendly web interface with three main tabs:
98
+
99
+ 1. **Multimodal Analysis**: Upload an X-ray image and enter a medical report for combined analysis
100
+ 2. **Image Analysis**: Upload an X-ray image for image-only analysis
101
+ 3. **Text Analysis**: Enter a medical report for text-only analysis
102
+
103
+ ### Command Line Usage
104
+
105
+ You can also use the core components directly from Python:
106
+
107
+ ```python
108
+ from mediSync.models import XRayImageAnalyzer, MedicalReportAnalyzer, MultimodalFusion
109
+
110
+ # Initialize models
111
+ fusion_model = MultimodalFusion()
112
+
113
+ # Analyze image and text
114
+ results = fusion_model.analyze("path/to/image.jpg", "Medical report text...")
115
+
116
+ # Get explanation
117
+ explanation = fusion_model.get_explanation(results)
118
+ print(explanation)
119
+ ```
120
+
121
+ ## Core Components
122
+
123
+ ### Image Analysis Module
124
+
125
+ The `XRayImageAnalyzer` class is responsible for analyzing X-ray images:
126
+
127
+ - Uses the DeiT (Data-efficient image Transformers) model fine-tuned on chest X-rays
128
+ - Detects abnormalities and classifies findings
129
+ - Provides confidence scores and primary findings
130
+
131
+ Key methods:
132
+ - `analyze(image_path)`: Analyzes an X-ray image
133
+ - `get_explanation(results)`: Generates a human-readable explanation
134
+
135
+ ### Text Analysis Module
136
+
137
+ The `MedicalReportAnalyzer` class processes medical report text:
138
+
139
+ - Extracts medical entities (conditions, treatments, tests)
140
+ - Assesses severity level
141
+ - Extracts key findings
142
+ - Suggests follow-up actions
143
+
144
+ Key methods:
145
+ - `extract_entities(text)`: Extracts medical entities
146
+ - `assess_severity(text)`: Determines severity level
147
+ - `extract_findings(text)`: Extracts key clinical findings
148
+ - `suggest_followup(text, entities, severity)`: Suggests follow-up actions
149
+ - `analyze(text)`: Performs comprehensive analysis
150
+
151
+ ### Multimodal Fusion Module
152
+
153
+ The `MultimodalFusion` class combines insights from both modalities:
154
+
155
+ - Calculates agreement between image and text analyses
156
+ - Determines confidence-weighted findings
157
+ - Provides comprehensive severity assessment
158
+ - Merges follow-up recommendations
159
+
160
+ Key methods:
161
+ - `analyze_image(image_path)`: Analyzes image only
162
+ - `analyze_text(text)`: Analyzes text only
163
+ - `analyze(image_path, report_text)`: Performs multimodal analysis
164
+ - `get_explanation(fused_results)`: Generates comprehensive explanation
165
+
166
+ ## Model Details
167
+
168
+ ### X-ray Analysis Model
169
+
170
+ - **Model**: facebook/deit-base-patch16-224-medical-cxr
171
+ - **Architecture**: Data-efficient image Transformer (DeiT)
172
+ - **Training Data**: Chest X-ray datasets
173
+ - **Input Size**: 224x224 pixels
174
+ - **Output**: Classification probabilities for various conditions
175
+
176
+ ### Medical Text Analysis Models
177
+
178
+ - **Entity Recognition Model**: samrawal/bert-base-uncased_medical-ner
179
+ - **Classification Model**: medicalai/ClinicalBERT
180
+ - **Architecture**: BERT-based transformer models
181
+ - **Training Data**: Medical text and reports
182
+
183
+ ## API Reference
184
+
185
+ ### XRayImageAnalyzer
186
+
187
+ ```python
188
+ from mediSync.models import XRayImageAnalyzer
189
+
190
+ # Initialize
191
+ analyzer = XRayImageAnalyzer(model_name="facebook/deit-base-patch16-224-medical-cxr")
192
+
193
+ # Analyze image
194
+ results = analyzer.analyze("path/to/image.jpg")
195
+
196
+ # Get explanation
197
+ explanation = analyzer.get_explanation(results)
198
+ ```
199
+
200
+ ### MedicalReportAnalyzer
201
+
202
+ ```python
203
+ from mediSync.models import MedicalReportAnalyzer
204
+
205
+ # Initialize
206
+ analyzer = MedicalReportAnalyzer()
207
+
208
+ # Analyze report
209
+ results = analyzer.analyze("Medical report text...")
210
+
211
+ # Access specific components
212
+ entities = results["entities"]
213
+ severity = results["severity"]
214
+ findings = results["findings"]
215
+ recommendations = results["followup_recommendations"]
216
+ ```
217
+
218
+ ### MultimodalFusion
219
+
220
+ ```python
221
+ from mediSync.models import MultimodalFusion
222
+
223
+ # Initialize
224
+ fusion = MultimodalFusion()
225
+
226
+ # Multimodal analysis
227
+ results = fusion.analyze("path/to/image.jpg", "Medical report text...")
228
+
229
+ # Get explanation
230
+ explanation = fusion.get_explanation(results)
231
+ ```
232
+
233
+ ## Extending the System
234
+
235
+ ### Adding New Models
236
+
237
+ To add a new image analysis model:
238
+
239
+ 1. Create a new class that follows the same interface as `XRayImageAnalyzer`
240
+ 2. Update the `MultimodalFusion` class to use your new model
241
+
242
+ ```python
243
+ class NewXRayModel:
244
+ def __init__(self, model_name, device=None):
245
+ # Initialize your model
246
+ pass
247
+
248
+ def analyze(self, image_path):
249
+ # Implement analysis logic
250
+ return results
251
+
252
+ def get_explanation(self, results):
253
+ # Generate explanation
254
+ return explanation
255
+ ```
256
+
257
+ ### Custom Preprocessing
258
+
259
+ You can extend the preprocessing utilities in `utils/preprocessing.py` for custom data preparation:
260
+
261
+ ```python
262
+ def my_custom_preprocessor(image_path, **kwargs):
263
+ # Implement custom preprocessing
264
+ return processed_image
265
+ ```
266
+
267
+ ### Visualization Extensions
268
+
269
+ To add new visualization options, extend the utilities in `utils/visualization.py`:
270
+
271
+ ```python
272
+ def my_custom_visualization(results, **kwargs):
273
+ # Create custom visualization
274
+ return figure
275
+ ```
276
+
277
+ ## Troubleshooting
278
+
279
+ ### Common Issues
280
+
281
+ 1. **Model Loading Errors**
282
+ - Ensure you have a stable internet connection for downloading models
283
+ - Check that you have sufficient disk space
284
+ - Try specifying a different model checkpoint
285
+
286
+ 2. **Image Processing Errors**
287
+ - Ensure images are in a supported format (JPEG, PNG)
288
+ - Check that the image is a valid X-ray image
289
+ - Try preprocessing the image manually using the utility functions
290
+
291
+ 3. **Performance Issues**
292
+ - For faster inference, use a GPU if available
293
+ - Reduce image resolution if processing is too slow
294
+ - Use the text-only analysis for quicker results
295
+
296
+ ### Logging
297
+
298
+ MediSync uses Python's logging module for debug information:
299
+
300
+ ```python
301
+ import logging
302
+ logging.basicConfig(level=logging.DEBUG)
303
+ ```
304
+
305
+ Log files are saved to `mediSync.log` in the application directory.
306
+
307
+ ## References
308
+
309
+ ### Datasets
310
+
311
+ - [MIMIC-CXR](https://physionet.org/content/mimic-cxr/2.0.0/): Large dataset of chest radiographs with reports
312
+ - [ChestX-ray14](https://www.nih.gov/news-events/news-releases/nih-clinical-center-provides-one-largest-publicly-available-chest-x-ray-datasets-scientific-community): NIH dataset of chest X-rays
313
+
314
+ ### Papers
315
+
316
+ - He, K., et al. (2020). "Vision Transformers for Medical Image Analysis"
317
+ - Irvin, J., et al. (2019). "CheXpert: A Large Chest Radiograph Dataset with Uncertainty Labels and Expert Comparison"
318
+ - Johnson, A.E.W., et al. (2019). "MIMIC-CXR-JPG, a large publicly available database of labeled chest radiographs"
319
+
320
+ ### Tools and Libraries
321
+
322
+ - [Hugging Face Transformers](https://huggingface.co/docs/transformers/index)
323
+ - [PyTorch](https://pytorch.org/)
324
+ - [Gradio](https://gradio.app/)
325
+
326
+ ---
327
+
328
+ ## License
329
+
330
+ This project is licensed under the MIT License - see the LICENSE file for details.
331
+
332
+ ## Acknowledgments
333
+
334
+ - The development of MediSync was inspired by recent advances in multi-modal learning in healthcare.
335
+ - Special thanks to the open-source community for providing pre-trained models and tools.
README.md CHANGED
@@ -1,374 +1,63 @@
1
- ---
2
- title: MediSync
3
- emoji: 🩺
4
- colorFrom: blue
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 5.20.1
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
-
13
- # MediSync: Multi-Modal Medical Analysis System
14
-
15
- MediSync is an AI-powered healthcare solution that combines X-ray image analysis with patient report text processing to provide comprehensive medical insights.
16
-
17
- ## Features
18
-
19
- - **X-ray Image Analysis**: Detects abnormalities in chest X-rays using pre-trained vision models from Hugging Face.
20
- - **Medical Report Processing**: Extracts key information from patient reports using NLP models.
21
- - **Multi-modal Integration**: Combines insights from both image and text data for more accurate diagnosis suggestions.
22
- - **User-friendly Interface**: Simple web interface for uploading images and reports.
23
-
24
- ## Project Structure
25
-
26
- ```
27
- mediSync/
28
- ├── app.py # Main application with Gradio interface
29
- ├── models/
30
- │ ├── image_analyzer.py # X-ray image analysis module
31
- │ ├── text_analyzer.py # Medical report text analysis module
32
- │ └── multimodal_fusion.py # Fusion of image and text insights
33
- ├── utils/
34
- │ ├── preprocessing.py # Data preprocessing utilities
35
- │ └── visualization.py # Result visualization utilities
36
- ├── data/
37
- │ └── sample/ # Sample data for testing
38
- └── tests/ # Unit tests
39
- ```
40
- # MediSync: Multi-Modal Medical Analysis System
41
-
42
- ## Comprehensive Technical Documentation
43
-
44
- ### Table of Contents
45
- 1. [Introduction](#introduction)
46
- 2. [System Architecture](#system-architecture)
47
- 3. [Installation](#installation)
48
- 4. [Usage](#usage)
49
- 5. [Core Components](#core-components)
50
- 6. [Model Details](#model-details)
51
- 7. [API Reference](#api-reference)
52
- 8. [Extending the System](#extending-the-system)
53
- 9. [Troubleshooting](#troubleshooting)
54
- 10. [References](#references)
55
-
56
- ---
57
-
58
- ## Introduction
59
-
60
- MediSync is a multi-modal AI system that combines X-ray image analysis with medical report text processing to provide comprehensive medical insights. By leveraging state-of-the-art deep learning models for both vision and language understanding, MediSync can:
61
-
62
- - Analyze chest X-ray images to detect abnormalities
63
- - Extract key clinical information from medical reports
64
- - Fuse insights from both modalities for enhanced diagnosis support
65
- - Provide comprehensive visualization of analysis results
66
-
67
- This AI system demonstrates the power of multi-modal fusion in the healthcare domain, where integrating information from multiple sources can lead to more robust and accurate analyses.
68
-
69
- ## System Architecture
70
-
71
- MediSync follows a modular architecture with three main components:
72
-
73
- 1. **Image Analysis Module**: Processes X-ray images using pre-trained vision models
74
- 2. **Text Analysis Module**: Analyzes medical reports using NLP models
75
- 3. **Multimodal Fusion Module**: Combines insights from both modalities
76
-
77
- The system uses the following high-level workflow:
78
-
79
- ```
80
- ┌─────────────────┐
81
- │ X-ray Image │
82
- └────────┬────────┘
83
-
84
-
85
- ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
86
- │ Preprocessing │───▶│ Image Analysis │───▶│ │
87
- └─────────────────┘ └─────────────────┘ │ │
88
- │ Multimodal │
89
- ┌─────────────────┐ ┌─────────────────┐ │ Fusion │───▶ Results
90
- │ Medical Report │───▶│ Text Analysis │───▶│ │
91
- └─────────────────┘ └─────────────────┘ │ │
92
- └─────────────────┘
93
- ```
94
-
95
- ## Installation
96
-
97
- ### Prerequisites
98
- - Python 3.8 or higher
99
- - Pip package manager
100
-
101
- ### Setup Instructions
102
-
103
- 1. Clone the repository:
104
- ```bash
105
- git clone [repository-url]
106
- cd mediSync
107
- ```
108
-
109
- 2. Install dependencies:
110
- ```bash
111
- pip install -r requirements.txt
112
- ```
113
-
114
- 3. Download sample data:
115
- ```bash
116
- python -m mediSync.utils.download_samples
117
- ```
118
-
119
- ## Usage
120
-
121
- ### Running the Application
122
-
123
- To launch the MediSync application with the Gradio interface:
124
-
125
- ```bash
126
- python run.py
127
- ```
128
-
129
- This will:
130
- 1. Download sample data if not already present
131
- 2. Initialize the application
132
- 3. Launch the Gradio web interface
133
-
134
- ### Web Interface
135
-
136
- MediSync provides a user-friendly web interface with three main tabs:
137
-
138
- 1. **Multimodal Analysis**: Upload an X-ray image and enter a medical report for combined analysis
139
- 2. **Image Analysis**: Upload an X-ray image for image-only analysis
140
- 3. **Text Analysis**: Enter a medical report for text-only analysis
141
-
142
- ### Command Line Usage
143
-
144
- You can also use the core components directly from Python:
145
-
146
- ```python
147
- from mediSync.models import XRayImageAnalyzer, MedicalReportAnalyzer, MultimodalFusion
148
-
149
- # Initialize models
150
- fusion_model = MultimodalFusion()
151
-
152
- # Analyze image and text
153
- results = fusion_model.analyze("path/to/image.jpg", "Medical report text...")
154
-
155
- # Get explanation
156
- explanation = fusion_model.get_explanation(results)
157
- print(explanation)
158
- ```
159
-
160
- ## Core Components
161
-
162
- ### Image Analysis Module
163
-
164
- The `XRayImageAnalyzer` class is responsible for analyzing X-ray images:
165
-
166
- - Uses the DeiT (Data-efficient image Transformers) model fine-tuned on chest X-rays
167
- - Detects abnormalities and classifies findings
168
- - Provides confidence scores and primary findings
169
-
170
- Key methods:
171
- - `analyze(image_path)`: Analyzes an X-ray image
172
- - `get_explanation(results)`: Generates a human-readable explanation
173
-
174
- ### Text Analysis Module
175
-
176
- The `MedicalReportAnalyzer` class processes medical report text:
177
-
178
- - Extracts medical entities (conditions, treatments, tests)
179
- - Assesses severity level
180
- - Extracts key findings
181
- - Suggests follow-up actions
182
-
183
- Key methods:
184
- - `extract_entities(text)`: Extracts medical entities
185
- - `assess_severity(text)`: Determines severity level
186
- - `extract_findings(text)`: Extracts key clinical findings
187
- - `suggest_followup(text, entities, severity)`: Suggests follow-up actions
188
- - `analyze(text)`: Performs comprehensive analysis
189
-
190
- ### Multimodal Fusion Module
191
-
192
- The `MultimodalFusion` class combines insights from both modalities:
193
-
194
- - Calculates agreement between image and text analyses
195
- - Determines confidence-weighted findings
196
- - Provides comprehensive severity assessment
197
- - Merges follow-up recommendations
198
-
199
- Key methods:
200
- - `analyze_image(image_path)`: Analyzes image only
201
- - `analyze_text(text)`: Analyzes text only
202
- - `analyze(image_path, report_text)`: Performs multimodal analysis
203
- - `get_explanation(fused_results)`: Generates comprehensive explanation
204
-
205
- ## Model Details
206
-
207
- ### X-ray Analysis Model
208
-
209
- - **Model**: facebook/deit-base-patch16-224-medical-cxr
210
- - **Architecture**: Data-efficient image Transformer (DeiT)
211
- - **Training Data**: Chest X-ray datasets
212
- - **Input Size**: 224x224 pixels
213
- - **Output**: Classification probabilities for various conditions
214
-
215
- ### Medical Text Analysis Models
216
-
217
- - **Entity Recognition Model**: samrawal/bert-base-uncased_medical-ner
218
- - **Classification Model**: medicalai/ClinicalBERT
219
- - **Architecture**: BERT-based transformer models
220
- - **Training Data**: Medical text and reports
221
-
222
- ## API Reference
223
-
224
- ### XRayImageAnalyzer
225
-
226
- ```python
227
- from mediSync.models import XRayImageAnalyzer
228
-
229
- # Initialize
230
- analyzer = XRayImageAnalyzer(model_name="facebook/deit-base-patch16-224-medical-cxr")
231
-
232
- # Analyze image
233
- results = analyzer.analyze("path/to/image.jpg")
234
-
235
- # Get explanation
236
- explanation = analyzer.get_explanation(results)
237
- ```
238
-
239
- ### MedicalReportAnalyzer
240
-
241
- ```python
242
- from mediSync.models import MedicalReportAnalyzer
243
-
244
- # Initialize
245
- analyzer = MedicalReportAnalyzer()
246
-
247
- # Analyze report
248
- results = analyzer.analyze("Medical report text...")
249
-
250
- # Access specific components
251
- entities = results["entities"]
252
- severity = results["severity"]
253
- findings = results["findings"]
254
- recommendations = results["followup_recommendations"]
255
- ```
256
-
257
- ### MultimodalFusion
258
-
259
- ```python
260
- from mediSync.models import MultimodalFusion
261
-
262
- # Initialize
263
- fusion = MultimodalFusion()
264
-
265
- # Multimodal analysis
266
- results = fusion.analyze("path/to/image.jpg", "Medical report text...")
267
-
268
- # Get explanation
269
- explanation = fusion.get_explanation(results)
270
- ```
271
-
272
- ## Extending the System
273
-
274
- ### Adding New Models
275
-
276
- To add a new image analysis model:
277
-
278
- 1. Create a new class that follows the same interface as `XRayImageAnalyzer`
279
- 2. Update the `MultimodalFusion` class to use your new model
280
-
281
- ```python
282
- class NewXRayModel:
283
- def __init__(self, model_name, device=None):
284
- # Initialize your model
285
- pass
286
-
287
- def analyze(self, image_path):
288
- # Implement analysis logic
289
- return results
290
-
291
- def get_explanation(self, results):
292
- # Generate explanation
293
- return explanation
294
- ```
295
-
296
- ### Custom Preprocessing
297
-
298
- You can extend the preprocessing utilities in `utils/preprocessing.py` for custom data preparation:
299
-
300
- ```python
301
- def my_custom_preprocessor(image_path, **kwargs):
302
- # Implement custom preprocessing
303
- return processed_image
304
- ```
305
-
306
- ### Visualization Extensions
307
-
308
- To add new visualization options, extend the utilities in `utils/visualization.py`:
309
-
310
- ```python
311
- def my_custom_visualization(results, **kwargs):
312
- # Create custom visualization
313
- return figure
314
- ```
315
-
316
- ## Troubleshooting
317
-
318
- ### Common Issues
319
-
320
- 1. **Model Loading Errors**
321
- - Ensure you have a stable internet connection for downloading models
322
- - Check that you have sufficient disk space
323
- - Try specifying a different model checkpoint
324
-
325
- 2. **Image Processing Errors**
326
- - Ensure images are in a supported format (JPEG, PNG)
327
- - Check that the image is a valid X-ray image
328
- - Try preprocessing the image manually using the utility functions
329
-
330
- 3. **Performance Issues**
331
- - For faster inference, use a GPU if available
332
- - Reduce image resolution if processing is too slow
333
- - Use the text-only analysis for quicker results
334
-
335
- ### Logging
336
-
337
- MediSync uses Python's logging module for debug information:
338
-
339
- ```python
340
- import logging
341
- logging.basicConfig(level=logging.DEBUG)
342
- ```
343
-
344
- Log files are saved to `mediSync.log` in the application directory.
345
-
346
- ## References
347
-
348
- ### Datasets
349
-
350
- - [MIMIC-CXR](https://physionet.org/content/mimic-cxr/2.0.0/): Large dataset of chest radiographs with reports
351
- - [ChestX-ray14](https://www.nih.gov/news-events/news-releases/nih-clinical-center-provides-one-largest-publicly-available-chest-x-ray-datasets-scientific-community): NIH dataset of chest X-rays
352
-
353
- ### Papers
354
-
355
- - He, K., et al. (2020). "Vision Transformers for Medical Image Analysis"
356
- - Irvin, J., et al. (2019). "CheXpert: A Large Chest Radiograph Dataset with Uncertainty Labels and Expert Comparison"
357
- - Johnson, A.E.W., et al. (2019). "MIMIC-CXR-JPG, a large publicly available database of labeled chest radiographs"
358
-
359
- ### Tools and Libraries
360
-
361
- - [Hugging Face Transformers](https://huggingface.co/docs/transformers/index)
362
- - [PyTorch](https://pytorch.org/)
363
- - [Gradio](https://gradio.app/)
364
-
365
- ---
366
-
367
- ## License
368
-
369
- This project is licensed under the MIT License - see the LICENSE file for details.
370
-
371
- ## Acknowledgments
372
-
373
- - The development of MediSync was inspired by recent advances in multi-modal learning in healthcare.
374
- - Special thanks to the open-source community for providing pre-trained models and tools.
 
1
+ # MediSync: Multi-Modal Medical Analysis System
2
+
3
+ [![Open In HF Spaces](https://huggingface.co/datasets/huggingface/badges/raw/main/open-in-hf-spaces-sm.svg)](https://huggingface.co/spaces/MediSync)
4
+
5
+ ## Overview
6
+
7
+ MediSync is an AI-powered healthcare solution that combines X-ray image analysis with patient report text processing to provide comprehensive medical insights. This demo showcases how multiple AI models can work together to provide a more complete analysis than either could alone.
8
+
9
+ ## Features
10
+
11
+ - **X-ray Image Analysis**: Detects abnormalities in chest X-rays using pre-trained vision models
12
+ - **Medical Report Processing**: Extracts key information from patient reports using NLP models
13
+ - **Multi-modal Integration**: Combines insights from both image and text data for more accurate diagnosis suggestions
14
+ - **Image Enhancement**: Provides image enhancement options for better visualization
15
+
16
+ ## How to Use This Space
17
+
18
+ 1. **Upload an X-ray Image**: Click on the upload field to add your chest X-ray image
19
+ 2. **Enter Medical Report Text**: Type or paste the corresponding radiologist's report
20
+ 3. **Choose Analysis Type**:
21
+ - **Multimodal Analysis**: Combines both image and text data
22
+ - **Image Analysis**: Focuses only on the X-ray image
23
+ - **Text Analysis**: Extracts key information from the medical report
24
+
25
+ ## Models Used
26
+
27
+ - **X-ray Analysis**: facebook/deit-base-patch16-224-medical-cxr
28
+ - **Medical Text Analysis**: medicalai/ClinicalBERT
29
+ - **Additional Support Models**: Medical entity recognition models
30
+
31
+ ## Example Inputs
32
+
33
+ The app comes with example inputs you can use to test its functionality:
34
+ - Sample chest X-rays with varying conditions
35
+ - Example medical reports with different findings
36
+
37
+ ## Important Disclaimer
38
+
39
+ This system is designed as a support tool and should NOT replace professional medical diagnosis. It is intended for educational and research purposes only. Always consult with healthcare professionals for medical decisions.
40
+
41
+ ## Technical Details
42
+
43
+ MediSync is built with:
44
+ - Gradio for the user interface
45
+ - PyTorch and Transformers for the model backends
46
+ - Advanced visualization utilities for result interpretation
47
+
48
+ ## Citation & Credits
49
+
50
+ If you use MediSync in your research, please cite:
51
+
52
+ ```
53
+ @software{MediSync2023,
54
+ author = {MediSync Team},
55
+ title = {MediSync: Multi-Modal Medical Analysis System},
56
+ year = {2023},
57
+ url = {https://huggingface.co/spaces/MediSync}
58
+ }
59
+ ```
60
+
61
+ ## License
62
+
63
+ This project is released under the MIT License.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ MediSync: Multi-Modal Medical Analysis System - Hugging Face Spaces Entry Point
3
+ ==============================================================================
4
+ This is the main entry point for the Hugging Face Space deployment.
5
+ """
6
+
7
+ import logging
8
+ import os
9
+ import sys
10
+ from pathlib import Path
11
+
12
+ # Configure logging
13
+ logging.basicConfig(
14
+ level=logging.INFO,
15
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
16
+ handlers=[logging.StreamHandler()],
17
+ )
18
+ logger = logging.getLogger(__name__)
19
+
20
+ # Add the current directory to Python path
21
+ current_dir = Path(__file__).resolve().parent
22
+ sys.path.append(str(current_dir))
23
+
24
+ # Pre-download models (important for Hugging Face Spaces)
25
+ try:
26
+ logger.info("Pre-downloading models for caching...")
27
+ from transformers import AutoModel, AutoModelForImageClassification, AutoTokenizer
28
+
29
+ # Set cache directory (HF Spaces specific)
30
+ os.environ["TRANSFORMERS_CACHE"] = os.path.join(os.getcwd(), "model_cache")
31
+ os.makedirs(os.environ["TRANSFORMERS_CACHE"], exist_ok=True)
32
+
33
+ # Medical vision model
34
+ vision_model_name = "facebook/deit-base-patch16-224-medical-cxr"
35
+ logger.info(f"Downloading vision model: {vision_model_name}")
36
+ AutoModelForImageClassification.from_pretrained(vision_model_name)
37
+
38
+ # Clinical BERT model
39
+ text_model_name = "medicalai/ClinicalBERT"
40
+ logger.info(f"Downloading text model: {text_model_name}")
41
+ AutoModel.from_pretrained(text_model_name)
42
+ AutoTokenizer.from_pretrained(text_model_name)
43
+
44
+ logger.info("All models downloaded and cached successfully!")
45
+ except Exception as e:
46
+ logger.warning(f"Warning: Could not pre-download models: {e}")
47
+
48
+ # Import and run the MediSync application
49
+ from mediSync.app import create_interface
50
+
51
+ # Create and launch the interface
52
+ logger.info("Creating and launching the interface...")
53
+ interface = create_interface()
54
+
55
+ # For Hugging Face Spaces
56
+ if __name__ == "__main__":
57
+ interface.launch()
mediSync.log ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 2025-03-10 22:44:41,854 - __main__ - INFO - Starting MediSync setup
2
+ 2025-03-10 22:44:41,854 - __main__ - INFO - Checking for sample data
3
+ 2025-03-10 22:44:42,626 - __main__ - INFO - No sample images found. Downloading...
4
+ 2025-03-10 22:44:42,626 - mediSync.utils.download_samples - INFO - Downloading https://prod-images-static.radiopaedia.org/images/53448173/322830a37f0fa0852773ca2db3e8d8_big_gallery.jpeg to D:\Multi Model AI Problem Solving\mediSync\data\sample\normal_chest_xray.jpg
5
+ 2025-03-10 22:44:43,532 - mediSync.utils.download_samples - ERROR - Error downloading https://prod-images-static.radiopaedia.org/images/53448173/322830a37f0fa0852773ca2db3e8d8_big_gallery.jpeg: HTTP Error 404: Not Found
6
+ 2025-03-10 22:44:43,532 - mediSync.utils.download_samples - INFO - Downloading https://prod-images-static.radiopaedia.org/images/52465460/e4d8791bd7502ab72af8d9e5c322db_big_gallery.jpg to D:\Multi Model AI Problem Solving\mediSync\data\sample\pneumonia_xray.jpg
7
+ 2025-03-10 22:44:43,988 - mediSync.utils.download_samples - ERROR - Error downloading https://prod-images-static.radiopaedia.org/images/52465460/e4d8791bd7502ab72af8d9e5c322db_big_gallery.jpg: HTTP Error 404: Not Found
8
+ 2025-03-10 22:44:43,988 - mediSync.utils.download_samples - INFO - Downloading https://prod-images-static.radiopaedia.org/images/556520/cf17c05750adb04b2a6e23afb47c7d_big_gallery.jpg to D:\Multi Model AI Problem Solving\mediSync\data\sample\cardiomegaly_xray.jpg
9
+ 2025-03-10 22:44:44,605 - mediSync.utils.download_samples - ERROR - Error downloading https://prod-images-static.radiopaedia.org/images/556520/cf17c05750adb04b2a6e23afb47c7d_big_gallery.jpg: HTTP Error 404: Not Found
10
+ 2025-03-10 22:44:44,605 - mediSync.utils.download_samples - INFO - Downloading https://prod-images-static.radiopaedia.org/images/19972291/41eed1a2cdad06d26c3f415a6ed65a_big_gallery.jpeg to D:\Multi Model AI Problem Solving\mediSync\data\sample\nodule_xray.jpg
11
+ 2025-03-10 22:44:45,107 - mediSync.utils.download_samples - ERROR - Error downloading https://prod-images-static.radiopaedia.org/images/19972291/41eed1a2cdad06d26c3f415a6ed65a_big_gallery.jpeg: HTTP Error 404: Not Found
12
+ 2025-03-10 22:44:45,107 - mediSync.utils.download_samples - INFO - Downloaded 0 out of 4 images
13
+ 2025-03-10 22:44:45,107 - mediSync.utils.download_samples - INFO - Created sample info file: D:\Multi Model AI Problem Solving\mediSync\data\sample\sample_info.txt
14
+ 2025-03-10 22:44:45,107 - __main__ - INFO - Launching MediSync application
15
+ 2025-03-10 22:44:48,918 - httpx - INFO - HTTP Request: GET https://api.gradio.app/gradio-messaging/en "HTTP/1.1 200 OK"
16
+ 2025-03-10 22:45:02,402 - mediSync.app - INFO - Initializing MediSync application
17
+ 2025-03-10 22:45:03,005 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
18
+ 2025-03-10 22:45:03,294 - httpx - INFO - HTTP Request: GET http://127.0.0.1:7860/gradio_api/startup-events "HTTP/1.1 200 OK"
19
+ 2025-03-10 22:45:03,722 - httpx - INFO - HTTP Request: HEAD http://127.0.0.1:7860/ "HTTP/1.1 200 OK"
20
+ 2025-03-10 22:46:47,162 - mediSync.app - INFO - Loading models...
21
+ 2025-03-10 22:46:47,162 - models.multimodal_fusion - INFO - Using device: cpu
22
+ 2025-03-10 22:46:47,178 - models.image_analyzer - INFO - Using device: cpu
23
+ 2025-03-10 22:46:47,391 - models.image_analyzer - ERROR - Failed to load model: facebook/deit-base-patch16-224-medical-cxr is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'
24
+ If this is a private repository, make sure to pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`
25
+ 2025-03-10 22:46:47,391 - models.multimodal_fusion - ERROR - Failed to initialize image analyzer: facebook/deit-base-patch16-224-medical-cxr is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'
26
+ If this is a private repository, make sure to pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`
27
+ 2025-03-10 22:46:47,391 - models.text_analyzer - INFO - Using device: cpu
28
+ 2025-03-10 22:46:47,490 - models.text_analyzer - ERROR - Failed to load NER model: samrawal/bert-base-uncased_medical-ner is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'
29
+ If this is a private repository, make sure to pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`
30
+ 2025-03-10 22:46:58,283 - models.text_analyzer - INFO - Successfully loaded classifier model: medicalai/ClinicalBERT
31
+ 2025-03-10 22:46:58,283 - models.multimodal_fusion - INFO - Successfully initialized text analyzer
32
+ 2025-03-10 22:46:58,283 - mediSync.app - INFO - Models loaded successfully
33
+ 2025-03-10 22:46:58,379 - mediSync.app - INFO - Performing multimodal analysis
34
+ 2025-03-10 22:46:58,379 - models.multimodal_fusion - WARNING - Image analyzer not available
35
+ 2025-03-10 22:46:58,379 - models.text_analyzer - WARNING - NER model not available
36
+ 2025-03-10 22:46:58,379 - models.multimodal_fusion - INFO - Agreement score between modalities: 0.25
37
+ 2025-03-10 22:47:44,547 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmp19zqepvi\upload.png
38
+ 2025-03-10 22:47:55,128 - mediSync.app - INFO - Analyzing medical report text
39
+ 2025-03-10 22:47:55,128 - models.text_analyzer - WARNING - NER model not available
40
+ 2025-03-10 22:48:20,220 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmpowd6qwty\upload.png
41
+ 2025-03-10 22:55:22,019 - mediSync.app - INFO - Performing multimodal analysis
42
+ 2025-03-10 22:55:22,019 - models.multimodal_fusion - WARNING - Image analyzer not available
43
+ 2025-03-10 22:55:22,025 - models.text_analyzer - WARNING - NER model not available
44
+ 2025-03-10 22:55:22,025 - models.multimodal_fusion - INFO - Agreement score between modalities: 0.25
45
+ 2025-03-10 22:55:35,606 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmpipzmsxch\upload.png
46
+ 2025-03-10 22:55:45,116 - mediSync.app - INFO - Analyzing medical report text
47
+ 2025-03-10 22:55:45,116 - models.text_analyzer - WARNING - NER model not available
48
+ 2025-03-10 23:01:32,009 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmpo88f1uaw\upload.png
49
+ 2025-03-10 23:05:24,892 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmpnckk4w27\upload.png
50
+ 2025-03-10 23:05:45,332 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmpxab_m30m\upload.png
51
+ 2025-03-10 23:06:01,387 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmpcit78i2e\upload.png
52
+ 2025-03-10 23:06:04,157 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmpivli_abx\upload.png
53
+ 2025-03-10 23:11:47,429 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmp4cpwvo5h\upload.png
54
+ 2025-03-10 23:22:27,118 - mediSync.app - INFO - Performing multimodal analysis
55
+ 2025-03-10 23:22:27,118 - models.multimodal_fusion - WARNING - Image analyzer not available
56
+ 2025-03-10 23:22:27,118 - models.text_analyzer - WARNING - NER model not available
57
+ 2025-03-10 23:22:27,118 - models.multimodal_fusion - INFO - Agreement score between modalities: 0.25
58
+ 2025-03-10 23:24:22,378 - mediSync.app - INFO - Performing multimodal analysis
59
+ 2025-03-10 23:24:22,378 - models.multimodal_fusion - WARNING - Image analyzer not available
60
+ 2025-03-10 23:24:22,378 - models.text_analyzer - WARNING - NER model not available
61
+ 2025-03-10 23:24:22,378 - models.multimodal_fusion - INFO - Agreement score between modalities: 0.25
62
+ 2025-03-10 23:24:35,252 - mediSync.app - INFO - Enhancing image: C:\Users\amart\AppData\Local\Temp\tmp947ie5e1\upload.png
63
+ 2025-03-10 23:24:37,554 - mediSync.app - INFO - Performing multimodal analysis
64
+ 2025-03-10 23:24:37,554 - models.multimodal_fusion - WARNING - Image analyzer not available
65
+ 2025-03-10 23:24:37,554 - models.text_analyzer - WARNING - NER model not available
66
+ 2025-03-10 23:24:37,554 - models.multimodal_fusion - INFO - Agreement score between modalities: 0.25
67
+ 2025-03-10 23:24:58,770 - mediSync.app - INFO - Analyzing medical report text
68
+ 2025-03-10 23:24:58,770 - models.text_analyzer - WARNING - NER model not available
69
+ 2025-03-10 23:25:25,652 - mediSync.app - INFO - Analyzing medical report text
70
+ 2025-03-10 23:25:25,652 - models.text_analyzer - WARNING - NER model not available
71
+ 2025-03-10 23:27:06,100 - mediSync.app - INFO - Performing multimodal analysis
72
+ 2025-03-10 23:27:06,100 - models.multimodal_fusion - WARNING - Image analyzer not available
73
+ 2025-03-10 23:27:06,100 - models.text_analyzer - WARNING - NER model not available
74
+ 2025-03-10 23:27:06,100 - models.multimodal_fusion - INFO - Agreement score between modalities: 0.25
75
+ 2025-03-10 23:27:46,183 - mediSync.app - INFO - Performing multimodal analysis
76
+ 2025-03-10 23:27:46,183 - models.multimodal_fusion - WARNING - Image analyzer not available
77
+ 2025-03-10 23:27:46,183 - models.text_analyzer - WARNING - NER model not available
78
+ 2025-03-10 23:27:46,183 - models.multimodal_fusion - INFO - Agreement score between modalities: 0.25
79
+ 2025-03-10 23:28:15,394 - mediSync.app - INFO - Analyzing medical report text
80
+ 2025-03-10 23:28:15,394 - models.text_analyzer - WARNING - NER model not available
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ torch>=2.0.0
2
+ transformers>=4.30.0
3
+ datasets>=2.12.0
4
+ pillow>=9.0.0
5
+ matplotlib>=3.7.0
6
+ pandas>=2.0.0
7
+ numpy>=1.24.0
8
+ scikit-learn>=1.2.0
9
+ gradio>=4.0.0
10
+ huggingface_hub>=0.16.0
11
+ opencv-python>=4.7.0
space.yml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ title: MediSync - Multi-Modal Medical Analysis System
2
+ emoji: 🩺
3
+ colorFrom: blue
4
+ colorTo: indigo
5
+ sdk: gradio
6
+ sdk_version: 4.0.0
7
+ app_file: app.py
8
+ pinned: false
9
+ license: mit