File size: 9,068 Bytes
12c0045
 
 
 
 
 
3ceeab6
12c0045
 
 
 
 
d611a6e
 
 
 
 
 
 
 
 
 
46d6674
 
 
d611a6e
 
 
 
46d6674
 
 
d611a6e
 
 
 
46d6674
 
 
d611a6e
 
 
 
 
 
 
46d6674
d611a6e
 
46d6674
d611a6e
46d6674
d611a6e
9d1ea3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cbc475
9d1ea3d
 
 
 
 
 
 
 
 
5cbc475
46d6674
 
 
 
 
 
d611a6e
 
 
 
 
 
 
 
 
 
46d6674
 
 
2244e1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46d6674
 
d611a6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46d6674
 
 
 
 
d611a6e
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
---

title: Medical Image Analysis Tool
emoji: πŸ₯
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: "5.49.1"
app_file: app.py
pinned: false
license: mit
---


# πŸ₯ Medical Image Analysis Tool

An AI-powered medical image analysis application using advanced detection models and large language models for medical image interpretation.

## Features

- **Advanced Object Detection**: Uses RF-DETR (Real-time Fine-grained Detection Transformer) for precise object detection
- **Medical AI Analysis**: Integrates MedGemma, a specialized medical vision-language model
- **Interactive Interface**: Built with Gradio for easy web-based interaction
- **Configurable Thresholds**: Adjustable confidence thresholds for detection sensitivity
- **Model Size Selection**: Choose between MedGemma 4B (faster) or 27B (more accurate) models
- **GPU Acceleration**: Optimized for GPU usage when available with 4-bit quantization
- **Automatic Model Downloads**: Models download automatically from Hugging Face Hub

## Models Used

- **RF-DETR Medium**: State-of-the-art object detection model
- **MedGemma 4B/27B**: Medical-specialized vision-language models for analysis and descriptions
  - 4B model: Faster inference, lower memory usage
  - 27B model: Higher accuracy, requires more resources

## Usage

1. **Upload Image**: Click on the image upload area or drag and drop a medical image
2. **Adjust Settings**:
   - Use the confidence threshold slider to control detection sensitivity
   - Select model size (4B for speed, 27B for accuracy)
3. **Analyze**: Click "Analyze Image" to run the AI analysis
4. **View Results**: See the annotated image with detected objects and AI-generated descriptions

## Installation & Setup

This application is designed to run on Hugging Face Spaces. The following files are required:

- `app.py` - Main application file (optimized for Spaces)
- `requirements.txt` - Python dependencies
- `packages.txt` - System packages
- `README.md` - This documentation

## Model Loading

### πŸ”‘ Required: Hugging Face Token (for MedGemma)

**MedGemma is a gated model**. To use AI-powered text analysis, you must:

1. Go to your **Space Settings** β†’ **Repository secrets**
2. Add a new secret:
   - **Name**: `HF_TOKEN`
   - **Value**: Your Hugging Face token (get it from https://huggingface.co/settings/tokens)
3. **Important**: Accept the model license at https://huggingface.co/google/medgemma-4b-it
4. Save and restart your Space

**Without the token:** Object detection will still work, but AI text analysis will be disabled.

---

**MedGemma Models (Automatic):**
- Models download automatically from Hugging Face Hub on first use (with valid token)
- Uses MedGemma 4B for efficient AI-powered analysis
- 4-bit quantization for reduced memory usage

**RF-DETR Model (Automatic from HF Model Repo):**
- Model automatically downloads from `edeler/lorai` on Hugging Face
- No manual upload needed - configured in the app
- Cached locally after first download for faster subsequent runs
- Model file: `lorai.pth` (135MB)

## Space Configuration

For optimal performance, configure your Space settings:
- **Hardware**: GPU (T4 minimum, A100 recommended for 27B models)
- **Storage**: Enable persistent storage for model caching
- **Timeout**: 30+ minutes for large model downloads

## Technical Details

- **Framework**: PyTorch + Transformers
- **Interface**: Gradio
- **Computer Vision**: OpenCV, PIL, Supervision
- **Hardware**: Optimized for both CPU and GPU inference

## Performance Tips

- **Model Selection**: Use MedGemma 4B for faster processing or 27B for higher accuracy
- **Confidence Thresholds**: Higher values reduce false positives but may miss subtle findings
- **GPU Acceleration**: The application automatically uses GPU acceleration when available
- **Memory Optimization**: Uses 4-bit quantization to reduce memory usage
- **Model Caching**: Models are cached after first load for faster subsequent analyses

## Limitations

- Requires significant computational resources for optimal performance
- Best suited for medical imaging applications
- Results should be verified by qualified medical professionals

## Development

To run locally:

```bash

pip install -r requirements.txt

python app.py

```

**Note**: For local development, you'll need to:
1. Install the RF-DETR package or ensure it's available
2. Place your `rf-detr-medium.pth` file in the project directory
3. Models will download automatically on first run

## License

This project is for research and educational purposes. Medical applications should be developed and validated according to appropriate regulatory standards.

## Support

For issues or questions, please refer to the Hugging Face Space documentation or create an issue in the project repository.
=======
---
title: Medical Image Analysis Tool
emoji: πŸ₯
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: 5.49.1

app_file: app.py
pinned: false
license: mit
---

# πŸ₯ Medical Image Analysis Tool

An AI-powered medical image analysis application using advanced detection models and large language models for medical image interpretation.

## Features

- **Advanced Object Detection**: Uses RF-DETR (Real-time Fine-grained Detection Transformer) for precise object detection
- **Medical AI Analysis**: Integrates MedGemma, a specialized medical vision-language model
- **Interactive Interface**: Built with Gradio for easy web-based interaction
- **Configurable Thresholds**: Adjustable confidence thresholds for detection sensitivity
- **Model Size Selection**: Choose between MedGemma 4B (faster) or 27B (more accurate) models
- **GPU Acceleration**: Optimized for GPU usage when available with 4-bit quantization
- **Automatic Model Downloads**: Models download automatically from Hugging Face Hub

## Models Used

- **RF-DETR Medium**: State-of-the-art object detection model
- **MedGemma 4B/27B**: Medical-specialized vision-language models for analysis and descriptions
  - 4B model: Faster inference, lower memory usage
  - 27B model: Higher accuracy, requires more resources

## Usage

1. **Upload Image**: Click on the image upload area or drag and drop a medical image
2. **Adjust Settings**:
   - Use the confidence threshold slider to control detection sensitivity
   - Select model size (4B for speed, 27B for accuracy)
3. **Analyze**: Click "Analyze Image" to run the AI analysis
4. **View Results**: See the annotated image with detected objects and AI-generated descriptions

## Installation & Setup

This application is designed to run on Hugging Face Spaces. The following files are required:

- `app.py` - Main application file (optimized for Spaces)
- `requirements.txt` - Python dependencies
- `packages.txt` - System packages
- `README.md` - This documentation

## Model Loading

**RF-DETR Model:**
- Upload your trained `rf-detr-medium.pth` file to the Space
- The application will automatically find and load it

**MedGemma Models:**
- Models download automatically from Hugging Face Hub on first use
- No manual installation required
- Choose between 4B (faster) or 27B (more accurate) models

## Space Configuration

For optimal performance, configure your Space settings:
- **Hardware**: GPU (T4 minimum, A100 recommended for 27B models)
- **Storage**: Enable persistent storage for model caching
- **Timeout**: 30+ minutes for large model downloads

## Technical Details

- **Framework**: PyTorch + Transformers
- **Interface**: Gradio
- **Computer Vision**: OpenCV, PIL, Supervision
- **Hardware**: Optimized for both CPU and GPU inference

## Performance Tips

- **Model Selection**: Use MedGemma 4B for faster processing or 27B for higher accuracy
- **Confidence Thresholds**: Higher values reduce false positives but may miss subtle findings
- **GPU Acceleration**: The application automatically uses GPU acceleration when available
- **Memory Optimization**: Uses 4-bit quantization to reduce memory usage
- **Model Caching**: Models are cached after first load for faster subsequent analyses

## Limitations

- Requires significant computational resources for optimal performance
- Best suited for medical imaging applications
- Results should be verified by qualified medical professionals

## Development

To run locally:

```bash

pip install -r requirements.txt

python app.py

```

**Note**: For local development, you'll need to:
1. Install the RF-DETR package or ensure it's available
2. Place your `rf-detr-medium.pth` file in the project directory
3. Models will download automatically on first run

## License

This project is for research and educational purposes. Medical applications should be developed and validated according to appropriate regulatory standards.

## Support

For issues or questions, please refer to the Hugging Face Space documentation or create an issue in the project repository.