edeler commited on
Commit
2244e1b
·
2 Parent(s): c9eeb0c bc96d42

Merge branch 'pr/1' into main - resolve conflicts keeping updated versions

Browse files
Files changed (4) hide show
  1. README.md +112 -0
  2. app.py +1 -18
  3. packages.txt +1 -1
  4. requirements.txt +11 -11
README.md CHANGED
@@ -122,3 +122,115 @@ This project is for research and educational purposes. Medical applications shou
122
  ## Support
123
 
124
  For issues or questions, please refer to the Hugging Face Space documentation or create an issue in the project repository.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  ## Support
123
 
124
  For issues or questions, please refer to the Hugging Face Space documentation or create an issue in the project repository.
125
+ =======
126
+ ---
127
+ title: Medical Image Analysis Tool
128
+ emoji: 🏥
129
+ colorFrom: blue
130
+ colorTo: green
131
+ sdk: gradio
132
+ sdk_version: 5.49.1
133
+ app_file: app.py
134
+ pinned: false
135
+ license: mit
136
+ ---
137
+
138
+ # 🏥 Medical Image Analysis Tool
139
+
140
+ An AI-powered medical image analysis application using advanced detection models and large language models for medical image interpretation.
141
+
142
+ ## Features
143
+
144
+ - **Advanced Object Detection**: Uses RF-DETR (Real-time Fine-grained Detection Transformer) for precise object detection
145
+ - **Medical AI Analysis**: Integrates MedGemma, a specialized medical vision-language model
146
+ - **Interactive Interface**: Built with Gradio for easy web-based interaction
147
+ - **Configurable Thresholds**: Adjustable confidence thresholds for detection sensitivity
148
+ - **Model Size Selection**: Choose between MedGemma 4B (faster) or 27B (more accurate) models
149
+ - **GPU Acceleration**: Optimized for GPU usage when available with 4-bit quantization
150
+ - **Automatic Model Downloads**: Models download automatically from Hugging Face Hub
151
+
152
+ ## Models Used
153
+
154
+ - **RF-DETR Medium**: State-of-the-art object detection model
155
+ - **MedGemma 4B/27B**: Medical-specialized vision-language models for analysis and descriptions
156
+ - 4B model: Faster inference, lower memory usage
157
+ - 27B model: Higher accuracy, requires more resources
158
+
159
+ ## Usage
160
+
161
+ 1. **Upload Image**: Click on the image upload area or drag and drop a medical image
162
+ 2. **Adjust Settings**:
163
+ - Use the confidence threshold slider to control detection sensitivity
164
+ - Select model size (4B for speed, 27B for accuracy)
165
+ 3. **Analyze**: Click "Analyze Image" to run the AI analysis
166
+ 4. **View Results**: See the annotated image with detected objects and AI-generated descriptions
167
+
168
+ ## Installation & Setup
169
+
170
+ This application is designed to run on Hugging Face Spaces. The following files are required:
171
+
172
+ - `app.py` - Main application file (optimized for Spaces)
173
+ - `requirements.txt` - Python dependencies
174
+ - `packages.txt` - System packages
175
+ - `README.md` - This documentation
176
+
177
+ ## Model Loading
178
+
179
+ **RF-DETR Model:**
180
+ - Upload your trained `rf-detr-medium.pth` file to the Space
181
+ - The application will automatically find and load it
182
+
183
+ **MedGemma Models:**
184
+ - Models download automatically from Hugging Face Hub on first use
185
+ - No manual installation required
186
+ - Choose between 4B (faster) or 27B (more accurate) models
187
+
188
+ ## Space Configuration
189
+
190
+ For optimal performance, configure your Space settings:
191
+ - **Hardware**: GPU (T4 minimum, A100 recommended for 27B models)
192
+ - **Storage**: Enable persistent storage for model caching
193
+ - **Timeout**: 30+ minutes for large model downloads
194
+
195
+ ## Technical Details
196
+
197
+ - **Framework**: PyTorch + Transformers
198
+ - **Interface**: Gradio
199
+ - **Computer Vision**: OpenCV, PIL, Supervision
200
+ - **Hardware**: Optimized for both CPU and GPU inference
201
+
202
+ ## Performance Tips
203
+
204
+ - **Model Selection**: Use MedGemma 4B for faster processing or 27B for higher accuracy
205
+ - **Confidence Thresholds**: Higher values reduce false positives but may miss subtle findings
206
+ - **GPU Acceleration**: The application automatically uses GPU acceleration when available
207
+ - **Memory Optimization**: Uses 4-bit quantization to reduce memory usage
208
+ - **Model Caching**: Models are cached after first load for faster subsequent analyses
209
+
210
+ ## Limitations
211
+
212
+ - Requires significant computational resources for optimal performance
213
+ - Best suited for medical imaging applications
214
+ - Results should be verified by qualified medical professionals
215
+
216
+ ## Development
217
+
218
+ To run locally:
219
+
220
+ ```bash
221
+ pip install -r requirements.txt
222
+ python app.py
223
+ ```
224
+
225
+ **Note**: For local development, you'll need to:
226
+ 1. Install the RF-DETR package or ensure it's available
227
+ 2. Place your `rf-detr-medium.pth` file in the project directory
228
+ 3. Models will download automatically on first run
229
+
230
+ ## License
231
+
232
+ This project is for research and educational purposes. Medical applications should be developed and validated according to appropriate regulatory standards.
233
+
234
+ ## Support
235
+
236
+ For issues or questions, please refer to the Hugging Face Space documentation or create an issue in the project repository.
app.py CHANGED
@@ -90,25 +90,8 @@ memory_manager = MemoryManager()
90
 
91
  def find_checkpoint() -> Optional[str]:
92
  """Find RF-DETR checkpoint in various locations."""
93
- # Check for HuggingFace model repository first (recommended)
94
- import os
95
- hf_model_id = os.environ.get("RFDETR_HF_REPO")
96
- if hf_model_id:
97
- try:
98
- from huggingface_hub import hf_hub_download
99
- print(f"Downloading RF-DETR from HuggingFace Hub: {hf_model_id}")
100
- checkpoint_path = hf_hub_download(
101
- repo_id=hf_model_id,
102
- filename="rf-detr-medium.pth",
103
- cache_dir="/tmp/hf_cache"
104
- )
105
- return checkpoint_path
106
- except Exception as e:
107
- print(f"Failed to download from HF Hub: {e}")
108
-
109
- # Fallback to local files
110
  candidates = [
111
- "rf-detr-medium.pth", # Current directory (direct upload)
112
  "/tmp/results/checkpoint_best_total.pth",
113
  "/tmp/results/checkpoint_best_ema.pth",
114
  "/tmp/results/checkpoint_best_regular.pth",
 
90
 
91
  def find_checkpoint() -> Optional[str]:
92
  """Find RF-DETR checkpoint in various locations."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  candidates = [
94
+ "rf-detr-medium.pth", # Current directory
95
  "/tmp/results/checkpoint_best_total.pth",
96
  "/tmp/results/checkpoint_best_ema.pth",
97
  "/tmp/results/checkpoint_best_regular.pth",
packages.txt CHANGED
@@ -2,7 +2,7 @@ libgl1-mesa-glx
2
  libglib2.0-0
3
  libsm6
4
  libxext6
5
- libxrender1
6
  libgomp1
7
  ffmpeg
8
  build-essential
 
2
  libglib2.0-0
3
  libsm6
4
  libxext6
5
+ libxrender
6
  libgomp1
7
  ffmpeg
8
  build-essential
requirements.txt CHANGED
@@ -1,11 +1,11 @@
1
- torch>=2.0.0
2
- transformers>=4.30.0
3
- gradio>=4.0.0
4
- pillow>=10.0.0
5
- opencv-python>=4.8.0
6
- supervision>=0.18.0
7
- psutil>=5.9.0
8
- numpy>=1.24.0
9
- imageio>=2.31.0
10
- imageio-ffmpeg>=0.4.8
11
- requests>=2.31.0
 
1
+ torch
2
+ transformers>
3
+ gradio
4
+ pillow
5
+ opencv-python
6
+ supervision>
7
+ psutil
8
+ numpy
9
+ imageio
10
+ imageio-ffmpeg
11
+ requests