File size: 3,801 Bytes
f74cf62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# VREyeSAM - Model Security & Protection Guide

## πŸ”’ Overview

VREyeSAM is protected with multiple security layers to prevent model weight extraction and ensure safe deployment.

## Security Measures Implemented

### 1. **Model Weight Protection**
- βœ… Model weights are loaded at startup and never exposed to the client
- βœ… Weights are managed in `model_server.py` using a singleton pattern
- βœ… Checkpoint paths are resolved internally and never sent to the frontend

### 2. **File System Isolation**
- βœ… Checkpoint files have restricted permissions (600)
- βœ… Only the inference API is exposed to users
- βœ… Raw file access is blocked

### 3. **API-Only Architecture**
- βœ… No direct model file downloads
- βœ… Only prediction results are returned to users
- βœ… Model internals stay hidden

## Deployment to Hugging Face Spaces

### Prerequisites
1. HuggingFace account with Spaces access
2. Model weights in private HuggingFace repository
3. Docker setup for containerized deployment

### Step 1: Create Private Model Repository

```bash
# Clone your model repo (if not already done)
# Ensure checkpoints are NOT committed to git
# Add to .gitignore if needed
```

### Step 2: Deploy to HF Spaces

1. Go to [Hugging Face Spaces](https://huggingface.co/spaces)
2. Click "Create new Space"
3. Fill in details:
   - **Space name**: vreyesam
   - **License**: MIT
   - **SDK**: Docker
   - **Visibility**: Public (only code, not weights)
4. After creation, upload your `Dockerfile` and code files

### Step 3: Authentication for Model Downloads

For accessing private model weights during Docker build:

1. Create HuggingFace token: https://huggingface.co/settings/tokens
2. Set in Spaces environment (Settings β†’ Secrets with HF_TOKEN)
3. OR use direct URL with token (not recommended, keep private)

### Step 4: Verify Security

Before deployment:

```bash
# Check what files will be uploaded
git status
git ls-files | grep -E '\.(pt|pth|torch|bin)$'

# Should output: (nothing - no weights!)
```

## Security Checklist

- [ ] Model weights are in `.gitignore`
- [ ] Checkpoint paths are not hardcoded in code
- [ ] Only `model_server.py` handles weight loading
- [ ] Docker build uses secure downloads
- [ ] `.env` files are in `.gitignore`
- [ ] Frontend cannot access file paths
- [ ] API only exposes prediction results

## Best Practices

### βœ… DO:
- Keep model weights private and download during deployment
- Use environment variables for configuration
- Only expose prediction API endpoints
- Log errors without exposing paths
- Use Hugging Face tokens securely in Spaces secrets

### ❌ DON'T:
- Commit model weights to git
- Hardcode checkpoint paths in code
- Expose debug routes that show model structure
- Log full file paths to users
- Include weights in Docker layers visible to users

## Troubleshooting

### Issue: "Model weights not found"
1. Verify `.gitignore` contains checkpoint paths
2. Check Dockerfile correctly downloads from HuggingFace
3. Ensure HF_TOKEN is set in Spaces secrets

### Issue: "File path exposed in error"
1. Update `model_server.py` to not show paths
2. Generic error messages only: "Model initialization failed"
3. Check logs don't contain sensitive details

## Advanced Security

### Optional: Encrypt Weights
```python
# In model_server.py
from cryptography.fernet import Fernet
encrypted_weights = Fernet(key).encrypt(state_dict)
```

### Optional: Disable Direct File Access
```python
# Set file permissions
chmod 600 segment-anything-2/checkpoints/*
# Only the app process can read them
```

## Support

For security questions or issues:
- Check the [GitHub Issues](https://github.com/GeetanjaliGTZ/VREyeSAM/issues)
- Contact: geetanjalisharma546@gmail.com

---

**Last Updated**: March 2025
**Security Level**: High Protection βœ