File size: 8,221 Bytes
71797a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# πŸš€ Hugging Face Deployment Checklist

## βœ… Pre-Deployment Checklist

### 1. Files Ready
- [x] `Dockerfile.hf` - HF-compatible Docker configuration
- [x] `app_hf.py` - HF Spaces entry point (port 7860)
- [x] `README_HF.md` - Space description with YAML metadata
- [x] `requirements.txt` - All dependencies included
- [x] `app/` directory - Complete application code
- [x] `.gitignore` - Ignore patterns configured
- [x] `wsgi.py` - WSGI application wrapper

### 2. Configuration Verified
- [x] Port 7860 configured in Dockerfile.hf and app_hf.py
- [x] Database path uses environment variable (DATABASE_PATH=/data/app.db)
- [x] HuggingFace cache configured (/data/.cache/huggingface)
- [x] Session cookies configured for iframe embedding
- [x] Health check endpoint configured
- [x] Models directory configured (models/finetuned/)

### 3. Security
- [ ] **IMPORTANT**: Update FLASK_SECRET_KEY in HF Secrets
  - Use this secure key: `9fd11d101e36efbd3a7893f56d604b860403d247633547586c41453118e69b00`
- [ ] Consider changing ADMIN123 token to something more secure
- [ ] Review .hfignore to exclude sensitive files

---

## 🎯 Deployment Steps

### Option A: Web UI (Recommended - 5 minutes)

#### Step 1: Create Space
1. Go to https://huggingface.co/new-space
2. Login with your HF Pro account
3. Fill in:
   - **Space name**: `participatory-planner`
   - **License**: MIT
   - **SDK**: Docker ⚠️ IMPORTANT
   - **Hardware**: CPU Basic (or CPU Upgrade for Pro)
   - **Visibility**: Public or Private

#### Step 2: Prepare Files for Upload
Run this command to copy HF-specific files:
```bash
cd /home/thadillo/MyProjects/participatory_planner

# Copy HF-specific files to root
cp Dockerfile.hf Dockerfile
cp README_HF.md README.md
```

#### Step 3: Upload Files via Web UI
Upload these files/folders to your Space:
- βœ… `Dockerfile` (the HF version)
- βœ… `README.md` (the HF version with YAML header)
- βœ… `requirements.txt`
- βœ… `app_hf.py`
- βœ… `wsgi.py`
- βœ… `app/` (entire folder with all subfolders)
- βœ… `.gitignore`

**DO NOT upload:**
- ❌ `venv/` (Python virtual environment)
- ❌ `instance/` (local database)
- ❌ `models/finetuned/` (will be created on HF)
- ❌ `.git/` (Git history)
- ❌ `__pycache__/` (Python cache)

#### Step 4: Configure Secrets
1. Go to your Space β†’ Settings β†’ Repository secrets
2. Click "Add a secret"
3. Add:
   - **Name**: `FLASK_SECRET_KEY`
   - **Value**: `9fd11d101e36efbd3a7893f56d604b860403d247633547586c41453118e69b00`
4. (Optional) Add:
   - **Name**: `FLASK_ENV`
   - **Value**: `production`

#### Step 5: Wait for Build
1. Go to "Logs" tab
2. Watch the build process (5-10 minutes first time)
3. Look for: `Running on http://0.0.0.0:7860`
4. Space will show "Building" β†’ "Running"

#### Step 6: Access & Test
1. Visit: `https://huggingface.co/spaces/YOUR_USERNAME/participatory-planner`
2. Login with: `ADMIN123`
3. Test all features:
   - [ ] Registration page loads
   - [ ] Can create tokens
   - [ ] Can submit contributions
   - [ ] AI analysis works
   - [ ] Dashboard displays correctly
   - [ ] Map visualization works
   - [ ] Training panel accessible
   - [ ] Export/Import works

---

### Option B: Git CLI (For Advanced Users)

#### Step 1: Install Git LFS
```bash
git lfs install
```

#### Step 2: Create Space via CLI
```bash
# Install HF CLI
pip install huggingface_hub

# Login to HF
huggingface-cli login

# Create space (replace YOUR_USERNAME)
huggingface-cli repo create participatory-planner --type space --space_sdk docker
```

#### Step 3: Prepare Repository
```bash
cd /home/thadillo/MyProjects/participatory_planner

# Copy HF-specific files
cp Dockerfile.hf Dockerfile
cp README_HF.md README.md

# Add HF remote
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/participatory-planner
```

#### Step 4: Commit and Push
```bash
# Make sure .hfignore is in place
git add .
git commit -m "πŸš€ Initial deployment to Hugging Face Spaces"
git push hf main
```

#### Step 5: Configure secrets via Web UI
(Same as Option A, Step 4)

---

## πŸ“‹ Post-Deployment Verification

### Essential Tests
- [ ] Space builds successfully (check Logs)
- [ ] App is accessible at Space URL
- [ ] Admin login works (ADMIN123)
- [ ] Database persists between restarts
- [ ] AI model loads successfully
- [ ] File uploads work
- [ ] Map loads correctly

### Performance Checks
- [ ] First load time < 3 seconds (after warm-up)
- [ ] AI analysis completes in < 5 seconds
- [ ] No memory errors in logs
- [ ] Model caching works (subsequent loads faster)

### Security Checks
- [ ] FLASK_SECRET_KEY is set in Secrets (not in code)
- [ ] No sensitive data in logs
- [ ] HTTPS works correctly
- [ ] Session cookies work in iframe

---

## πŸ”§ Troubleshooting

### Build Fails
**Error**: "Out of memory during build"
- **Solution**: Upgrade to CPU Upgrade hardware in Settings

**Error**: "Port 7860 not responding"
- **Solution**: Verify Dockerfile exposes 7860 and app_hf.py uses it

### Runtime Issues
**Error**: "Database locked" or "Database resets"
- **Solution**: Verify DATABASE_PATH=/data/app.db in Dockerfile

**Error**: "Model download timeout"
- **Solution**: First download takes 10+ minutes. Be patient. Check Logs.

**Error**: "Can't access Space"
- **Solution**: Check Space visibility (Settings). Set to Public.

### AI Model Issues
**Error**: "Transformers error on first run"
- **Solution**: Models download on first use. Check HF_HOME=/data/.cache

**Error**: "CUDA/GPU errors"
- **Solution**: App uses CPU by default. Don't select GPU hardware unless needed.

---

## πŸ“Š Monitoring

### Daily Checks
- View Logs tab for errors
- Check Space status badge (green = good)
- Verify database size (Settings β†’ Storage)

### Weekly Maintenance
- Export data backup via admin panel
- Review error logs
- Check model storage size
- Update dependencies if needed

---

## πŸ”„ Updates & Rollbacks

### To Update Your Space
Via Git:
```bash
git add .
git commit -m "Update: description of changes"
git push hf main
```

Via Web UI:
1. Go to Files tab
2. Edit files directly
3. Commit changes

### To Rollback
1. Go to Files β†’ Commits
2. Find last working commit
3. Click "Revert to this commit"

---

## πŸ’‘ Optimization Tips

### For Better Performance
- Enable CPU Upgrade (4 vCPU, 32GB RAM) - Free with Pro!
- Use model presets (DeBERTa-v3-small recommended)
- Set persistent storage for model cache

### For Production Use
1. Change admin token from ADMIN123
2. Enable Space authentication (Settings)
3. Set up custom domain (Pro feature)
4. Enable always-on (Pro feature)
5. Set up monitoring alerts

---

## πŸŽ‰ Success Criteria

Your deployment is successful when:
- βœ… Space status shows "Running" (green badge)
- βœ… No errors in Logs for 5 minutes
- βœ… Admin login works
- βœ… AI analysis completes successfully
- βœ… Data persists after refresh
- βœ… All features work as in local development

---

## πŸ“ž Support Resources

- **HF Spaces Docs**: https://huggingface.co/docs/hub/spaces
- **HF Discord**: https://hf.co/join/discord
- **App Logs**: Your Space β†’ Logs tab
- **HF Status**: https://status.huggingface.co

---

## πŸ” Important Security Notes

**CRITICAL - Before going public:**

1. **Change Admin Token** in `app/models/models.py`:
   ```python
   if not Token.query.filter_by(token='YOUR_SECURE_TOKEN').first():
       admin_token = Token(token='YOUR_SECURE_TOKEN', type='admin', ...)
   ```

2. **Use HF Secrets** (never commit secrets):
   - FLASK_SECRET_KEY (already set)
   - Any API keys
   - Database credentials (if using external DB)

3. **Consider Space Authentication**:
   - Settings β†’ Enable authentication
   - Require HF login to access

4. **For Confidential Sessions**:
   - Set Space to Private
   - Use password protection
   - Regular data backups

---

## πŸ“ Final Notes

**Estimated Deployment Time**: 10-15 minutes (first time)

**Resources Used** (with HF Pro):
- Storage: ~2GB (model cache + database)
- RAM: ~1-2GB during inference
- CPU: 2-4 cores recommended

**Cost**: $0 (included in HF Pro subscription) πŸŽ‰

**Next Step**: Click "Create Space" on huggingface.co/new-space and follow the checklist above!

---

**Good luck with your deployment! πŸš€**