File size: 8,868 Bytes
0b86477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# πŸš€ Deploying MedSAM to HuggingFace Space

## Step-by-Step Guide

### Step 1: Create New Space

1. Go to: https://huggingface.co/new-space
2. Fill in details:
   - **Owner:** Your username
   - **Space name:** `medsam-inference`
   - **License:** Apache 2.0
   - **Select SDK:** Gradio
   - **Space hardware:** 
     - Start with **CPU basic** (free)
     - Upgrade to **T4 small** ($0.60/hour) for better performance
   - **Visibility:** Public or Private

3. Click **Create Space**

### Step 2: Upload Files

You have two options:

#### Option A: Using Git (Recommended)

```bash
# Clone your new Space
git clone https://huggingface.co/spaces/YOUR_USERNAME/medsam-inference
cd medsam-inference

# Copy files from this directory
cp /path/to/huggingface_space/* .

# Download your model from HuggingFace
# Option 1: Download via Python
python3 << EOF
from huggingface_hub import hf_hub_download
hf_hub_download(
    repo_id="Aniketg6/Fine-Tuned-MedSAM",
    filename="medsam_vit_b.pth",
    local_dir=".",
    local_dir_use_symlinks=False
)
EOF

# Option 2: Download manually
# Go to https://huggingface.co/Aniketg6/Fine-Tuned-MedSAM
# Download medsam_vit_b.pth (375 MB)
# Place it in this directory

# Initialize Git LFS (for large files)
git lfs install
git lfs track "*.pth"

# Add and commit
git add .
git commit -m "Initial commit: MedSAM inference API"
git push
```

#### Option B: Using Web Interface

1. In your Space, click **Files** tab
2. Click **Add file** β†’ **Upload files**
3. Upload:
   - `app.py`
   - `requirements.txt`
   - `README.md`
   - `.gitattributes`
4. For the model file (`medsam_vit_b.pth`):
   - Download from: https://huggingface.co/Aniketg6/Fine-Tuned-MedSAM
   - Upload to your Space (375 MB)

### Step 3: Wait for Build

- HuggingFace will automatically build your Space
- Check the **Logs** tab for build progress
- Should take 3-5 minutes
- Once done, your Space will be live!

### Step 4: Test Your Space

Visit: `https://huggingface.co/spaces/YOUR_USERNAME/medsam-inference`

You should see:
- βœ… Interactive UI with two tabs
- βœ… API Interface for programmatic access
- βœ… Simple Interface for manual testing

### Step 5: Get Your API Endpoint

Your API endpoint is:
```
https://YOUR_USERNAME-medsam-inference.hf.space/api/predict
```

Or use Gradio's direct endpoint:
```
https://YOUR_USERNAME-medsam-inference.hf.space/run/predict
```

---

## Testing Your Space

### Test via Web UI

1. Go to your Space URL
2. Click **Simple Interface** tab
3. Upload an image
4. Enter X, Y coordinates
5. Click **Segment**
6. See the mask output!

### Test via Python

```python
import requests
import json
import base64
from PIL import Image
import numpy as np

# Your Space URL
SPACE_URL = "https://YOUR_USERNAME-medsam-inference.hf.space"

def call_medsam_space(image_path, point_coords, point_labels, multimask=True):
    """
    Call your MedSAM Space
    
    Args:
        image_path: Path to image file
        point_coords: List of [x, y] coordinates, e.g., [[100, 150]]
        point_labels: List of labels (1=foreground, 0=background), e.g., [1]
        multimask: Whether to output multiple masks
    
    Returns:
        Dictionary with masks and scores
    """
    # Read and encode image
    with open(image_path, "rb") as f:
        img_base64 = base64.b64encode(f.read()).decode()
    
    # Prepare points JSON
    points_json = json.dumps({
        "coords": point_coords,
        "labels": point_labels,
        "multimask_output": multimask
    })
    
    # Call API
    response = requests.post(
        f"{SPACE_URL}/api/predict",
        json={
            "data": [
                f"data:image/jpeg;base64,{img_base64}",
                points_json
            ]
        }
    )
    
    # Parse result
    result = response.json()
    output_json = result["data"][0]  # Gradio wraps output in data array
    
    return json.loads(output_json)

# Example usage
if __name__ == "__main__":
    result = call_medsam_space(
        image_path="test_image.jpg",
        point_coords=[[200, 150]],
        point_labels=[1],
        multimask=True
    )
    
    if result['success']:
        print(f"βœ… Segmentation successful!")
        print(f"   Number of masks: {result['num_masks']}")
        print(f"   Scores: {result['scores']}")
        
        # Get best mask
        best_idx = np.argmax(result['scores'])
        best_mask_data = result['masks'][best_idx]['mask_data']
        best_mask = np.array(best_mask_data, dtype=bool)
        print(f"   Best mask shape: {best_mask.shape}")
    else:
        print(f"❌ Error: {result['error']}")
```

---

## Integration with Your Backend

Now update your `app.py` to use this Space:

```python
# In backend/app.py or backend/hf_inference.py

import requests
import json
import base64
from io import BytesIO
from PIL import Image
import numpy as np

# Your Space URL
MEDSAM_SPACE_URL = "https://YOUR_USERNAME-medsam-inference.hf.space/api/predict"

def call_medsam_space(image_array, point_coords, point_labels, multimask_output=True):
    """
    Call MedSAM Space API
    
    Args:
        image_array: numpy array of image
        point_coords: numpy array [[x, y]]
        point_labels: numpy array [1] or [0]
        multimask_output: bool
    
    Returns:
        masks, scores (matching original SAM interface)
    """
    try:
        # Convert numpy array to base64
        image = Image.fromarray(image_array)
        buffered = BytesIO()
        image.save(buffered, format="PNG")
        img_base64 = base64.b64encode(buffered.getvalue()).decode()
        
        # Prepare points JSON
        points_json = json.dumps({
            "coords": point_coords.tolist(),
            "labels": point_labels.tolist(),
            "multimask_output": multimask_output
        })
        
        # Call Space API
        response = requests.post(
            MEDSAM_SPACE_URL,
            json={
                "data": [
                    f"data:image/png;base64,{img_base64}",
                    points_json
                ]
            },
            timeout=60
        )
        
        # Parse result
        result = response.json()
        output_json = result["data"][0]
        output = json.loads(output_json)
        
        if not output['success']:
            raise Exception(output['error'])
        
        # Convert back to numpy arrays (matching SAM interface)
        masks = []
        for mask_data in output['masks']:
            mask = np.array(mask_data['mask_data'], dtype=bool)
            masks.append(mask)
        
        masks = np.array(masks)
        scores = np.array(output['scores'])
        
        return masks, scores, None  # Return None for logits (not needed)
        
    except Exception as e:
        print(f"Error calling MedSAM Space: {e}")
        raise

# Replace your SAM predictor calls with this:
# OLD:
# sam_predictor.set_image(image_array)
# masks, scores, _ = sam_predictor.predict(
#     point_coords=np.array([[x, y]]),
#     point_labels=np.array([1]),
#     multimask_output=True
# )

# NEW:
# masks, scores, _ = call_medsam_space(
#     image_array,
#     point_coords=np.array([[x, y]]),
#     point_labels=np.array([1]),
#     multimask_output=True
# )
```

---

## Cost & Performance

### Free Tier (CPU Basic):
- βœ… **Free!**
- ⚠️ Slower inference (~5-10 seconds per image)
- ⚠️ May sleep after inactivity
- βœ… Good for testing and low usage

### Paid Tier (T4 Small GPU):
- πŸ’° **$0.60/hour** (~$432/month if always on)
- βœ… Fast inference (~1-2 seconds per image)
- βœ… No sleep mode
- βœ… Better for production

### Upgrade to GPU:

1. Go to your Space settings
2. Click **Settings** tab
3. Under **Space hardware**, select **T4 small**
4. Click **Update**

---

## Troubleshooting

### "Application startup failed"
- Check logs for errors
- Make sure `medsam_vit_b.pth` is uploaded
- Verify `requirements.txt` is correct

### "Out of memory"
- Upgrade to GPU hardware
- Reduce image size before sending

### "Space is sleeping"
- Free tier spaces sleep after 48h inactivity
- First request will wake it up (takes 10-20s)
- Upgrade to paid tier for always-on

### API returns error
- Check input format matches examples
- Verify coordinates are within image bounds
- Check Space logs for detailed errors

---

## Next Steps

1. βœ… Deploy Space
2. βœ… Test via web UI
3. βœ… Test via Python script
4. βœ… Integrate with your backend
5. βœ… Deploy your backend to Vercel/Railway
6. βœ… Deploy frontend to Vercel
7. πŸŽ‰ Done!

---

## Alternative: Use Inference Endpoints

For production, consider **HuggingFace Inference Endpoints**:
- Dedicated infrastructure
- Auto-scaling
- Better performance
- $0.60/hour minimum

See: https://huggingface.co/inference-endpoints

---

**Questions? Check HuggingFace Spaces docs:**
https://huggingface.co/docs/hub/spaces