thadillo Claude commited on
Commit
18bfe26
Β·
1 Parent(s): d038974

Fix HuggingFace model cache permission error

Browse files

Issue: Models pre-downloaded during Docker build were not readable
by the non-root user that HuggingFace Spaces uses at runtime.

Error: [Errno 13] Permission denied: '/app/.cache/huggingface/models--facebook--bart-large-mnli'

Solution: Add chmod -R 777 /app/.cache after downloading models
to ensure the cache directory is readable by any user.

This fixes the repeated "Error analyzing submission" and "Error loading model"
errors that were occurring when analyzing submissions on HuggingFace Spaces.

Also added comprehensive deployment and setup documentation.

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (3) hide show
  1. Dockerfile +2 -1
  2. PUSH_TO_HUGGINGFACE.md +137 -0
  3. SETUP_INSTRUCTIONS.md +270 -0
Dockerfile CHANGED
@@ -56,7 +56,8 @@ RUN python -c "from transformers import pipeline; \
56
  pipeline('zero-shot-classification', model='MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli', device=-1); \
57
  print('Downloading DistilBART-MNLI...'); \
58
  pipeline('zero-shot-classification', model='valhalla/distilbart-mnli-12-3', device=-1); \
59
- print('All models downloaded successfully')"
 
60
 
61
  # Hugging Face Spaces uses port 7860
62
  EXPOSE 7860
 
56
  pipeline('zero-shot-classification', model='MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli', device=-1); \
57
  print('Downloading DistilBART-MNLI...'); \
58
  pipeline('zero-shot-classification', model='valhalla/distilbart-mnli-12-3', device=-1); \
59
+ print('All models downloaded successfully')" \
60
+ && chmod -R 777 /app/.cache
61
 
62
  # Hugging Face Spaces uses port 7860
63
  EXPOSE 7860
PUSH_TO_HUGGINGFACE.md ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # How to Push to HuggingFace
2
+
3
+ ## The Issue
4
+
5
+ Git push to HuggingFace isn't prompting for authentication because we're in a non-interactive environment. Here's how to authenticate properly:
6
+
7
+ ---
8
+
9
+ ## Solution: Use HuggingFace CLI to Login
10
+
11
+ ### Step 1: Login with HuggingFace CLI
12
+
13
+ Run this command in your terminal:
14
+
15
+ ```bash
16
+ huggingface-cli login
17
+ ```
18
+
19
+ When prompted:
20
+ - Enter your **new HuggingFace write token** (the one you created after revoking the old one)
21
+ - The token should start with `hf_...`
22
+ - Choose **Yes** when asked to add token as git credential
23
+
24
+ This will:
25
+ 1. Store your HuggingFace token securely
26
+ 2. Configure git to use it for HuggingFace operations
27
+ 3. Enable pushing to your HuggingFace Space
28
+
29
+ ### Step 2: Push to HuggingFace
30
+
31
+ After logging in, push your changes:
32
+
33
+ ```bash
34
+ cd /home/thadillo/MyProjects/participatory_planner
35
+ git push hf feature/sentence-level-categorization:main
36
+ ```
37
+
38
+ This should now work without prompting!
39
+
40
+ ---
41
+
42
+ ## Alternative: Manually Add HuggingFace Credential
43
+
44
+ If the CLI login doesn't work, you can manually add the credential:
45
+
46
+ ```bash
47
+ # Add HuggingFace credential to git credentials file
48
+ echo "https://thadillo:YOUR_NEW_HF_TOKEN@huggingface.co" >> ~/.git-credentials
49
+ ```
50
+
51
+ Replace `YOUR_NEW_HF_TOKEN` with your actual HuggingFace token.
52
+
53
+ Then push:
54
+ ```bash
55
+ git push hf feature/sentence-level-categorization:main
56
+ ```
57
+
58
+ ---
59
+
60
+ ## Alternative: Use Git with Token in URL (One-time)
61
+
62
+ If you prefer a one-time push without storing credentials:
63
+
64
+ ```bash
65
+ cd /home/thadillo/MyProjects/participatory_planner
66
+
67
+ # Push with token embedded (replace YOUR_TOKEN)
68
+ git push https://thadillo:YOUR_NEW_HF_TOKEN@huggingface.co/spaces/thadillo/participatory-planner feature/sentence-level-categorization:main
69
+ ```
70
+
71
+ **Warning**: This doesn't store the credential, so you'll need to do this every time.
72
+
73
+ ---
74
+
75
+ ## Verify the Push
76
+
77
+ After pushing successfully, verify:
78
+
79
+ 1. **Check HuggingFace Space**: https://huggingface.co/spaces/thadillo/participatory-planner
80
+ 2. **Check Build Logs**: https://huggingface.co/spaces/thadillo/participatory-planner/logs
81
+ 3. **Verify Files Updated**: The commit should show in the Space's Files tab
82
+
83
+ ---
84
+
85
+ ## What Happens Next
86
+
87
+ Once pushed, HuggingFace will:
88
+ 1. Automatically rebuild your Space
89
+ 2. Apply all the security fixes
90
+ 3. Fix the deployment errors (TRANSFORMERS_CACHE, matplotlib, SQLite)
91
+
92
+ **Don't forget**: You still need to set the HuggingFace Spaces secrets (FLASK_SECRET_KEY and ADMIN_TOKEN) as described in SETUP_INSTRUCTIONS.md!
93
+
94
+ ---
95
+
96
+ ## Quick Commands Summary
97
+
98
+ ```bash
99
+ # Option 1: Login with HuggingFace CLI (recommended)
100
+ huggingface-cli login
101
+ git push hf feature/sentence-level-categorization:main
102
+
103
+ # Option 2: Manual credential file edit
104
+ echo "https://thadillo:YOUR_HF_TOKEN@huggingface.co" >> ~/.git-credentials
105
+ git push hf feature/sentence-level-categorization:main
106
+
107
+ # Option 3: One-time push with token
108
+ git push https://thadillo:YOUR_HF_TOKEN@huggingface.co/spaces/thadillo/participatory-planner feature/sentence-level-categorization:main
109
+ ```
110
+
111
+ ---
112
+
113
+ ## Troubleshooting
114
+
115
+ ### Error: "Authentication failed"
116
+ - Double-check your HuggingFace token is correct
117
+ - Ensure it's a **write** token (not read-only)
118
+ - Verify the token hasn't expired
119
+
120
+ ### Error: "could not read Username"
121
+ - This means git isn't getting credentials properly
122
+ - Use `huggingface-cli login` to fix this
123
+
124
+ ### Error: "remote rejected"
125
+ - Check you have write access to the Space
126
+ - Verify the Space name is correct: `thadillo/participatory-planner`
127
+
128
+ ---
129
+
130
+ ## Current Status
131
+
132
+ - βœ… All changes committed to git
133
+ - βœ… Pushed to GitHub successfully
134
+ - ⏳ **Waiting for you to push to HuggingFace**
135
+ - ⏳ Waiting for HuggingFace Spaces secrets to be set
136
+
137
+ Follow the steps above to complete the deployment!
SETUP_INSTRUCTIONS.md ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Security Setup Instructions
2
+
3
+ ## Summary
4
+
5
+ All security improvements have been successfully committed and pushed to GitHub. Follow the instructions below to complete the deployment to HuggingFace and set up your environment variables.
6
+
7
+ ## What Was Fixed
8
+
9
+ ### Security Improvements βœ…
10
+ - Removed hardcoded ADMIN123 token (now environment-based with auto-generation)
11
+ - Rotated Flask secret key with fail-fast validation
12
+ - Added rate limiting to authentication endpoints
13
+ - Removed exposed HuggingFace token from git config
14
+ - Created comprehensive SECURITY.md documentation
15
+
16
+ ### HuggingFace Deployment Fixes βœ…
17
+ - Fixed TRANSFORMERS_CACHE deprecation warning
18
+ - Resolved matplotlib permission errors
19
+ - Improved SQLite database locking issues
20
+ - Fixed sentence text display in PDF exports
21
+
22
+ ---
23
+
24
+ ## Step 1: Push to HuggingFace
25
+
26
+ Since you revoked the old HuggingFace token, you need to authenticate with your new token:
27
+
28
+ ```bash
29
+ cd /home/thadillo/MyProjects/participatory_planner
30
+
31
+ # Push to HuggingFace (you'll be prompted for credentials)
32
+ git push hf feature/sentence-level-categorization:main
33
+ ```
34
+
35
+ When prompted:
36
+ - **Username**: `thadillo`
37
+ - **Password**: Enter your **NEW HuggingFace write token** (starts with `hf_...`)
38
+
39
+ The credentials will be stored in git credential helper for future pushes.
40
+
41
+ ---
42
+
43
+ ## Step 2: Configure HuggingFace Spaces Secrets
44
+
45
+ Go to your HuggingFace Space settings and add these **Repository Secrets**:
46
+
47
+ https://huggingface.co/spaces/thadillo/participatory-planner/settings
48
+
49
+ ### Required Secrets:
50
+
51
+ 1. **FLASK_SECRET_KEY** (Critical - for session security)
52
+ ```
53
+ Generate a new one with:
54
+ python -c "import secrets; print(secrets.token_hex(32))"
55
+
56
+ Example output (DO NOT USE THIS - generate your own):
57
+ c5eafdcbd348d3176bf1fcf52d90357575f7bc1986b7baeb67bb9d140f401881
58
+ ```
59
+
60
+ 2. **ADMIN_TOKEN** (Critical - for admin access)
61
+ ```
62
+ Generate a new one with:
63
+ python -c "import secrets; print(secrets.token_urlsafe(16))"
64
+
65
+ Example output (DO NOT USE THIS - generate your own):
66
+ xulmKoajFS07akF9Eos9Tg
67
+ ```
68
+
69
+ **SAVE THIS TOKEN SECURELY** - You'll need it to log in to the admin panel!
70
+
71
+ ### How to Add Secrets in HuggingFace:
72
+
73
+ 1. Go to your Space: https://huggingface.co/spaces/thadillo/participatory-planner
74
+ 2. Click **Settings** tab
75
+ 3. Scroll to **Repository Secrets** section
76
+ 4. Click **New Secret**
77
+ 5. Add each secret:
78
+ - Name: `FLASK_SECRET_KEY`
79
+ - Value: (paste the generated secret key)
80
+ - Click **Add**
81
+ 6. Repeat for `ADMIN_TOKEN`
82
+
83
+ ---
84
+
85
+ ## Step 3: Configure Local Development Environment
86
+
87
+ Update your local `.env` file with the **SAME** values you set in HuggingFace:
88
+
89
+ ```bash
90
+ cd /home/thadillo/MyProjects/participatory_planner
91
+
92
+ # Edit your .env file
93
+ nano .env
94
+ ```
95
+
96
+ Your `.env` file should contain:
97
+
98
+ ```env
99
+ # Flask Configuration
100
+ FLASK_SECRET_KEY=<PASTE THE SAME VALUE YOU USED IN HUGGINGFACE>
101
+ FLASK_ENV=development
102
+
103
+ # Model Configuration
104
+ MODELS_DIR=models/finetuned
105
+ CUDA_VISIBLE_DEVICES=-1
106
+
107
+ # Admin Token (must match HuggingFace secret)
108
+ ADMIN_TOKEN=<PASTE THE SAME VALUE YOU USED IN HUGGINGFACE>
109
+ ```
110
+
111
+ **Important**: Use the **EXACT SAME** values for `FLASK_SECRET_KEY` and `ADMIN_TOKEN` that you set in HuggingFace. This ensures your local environment matches production.
112
+
113
+ ---
114
+
115
+ ## Step 4: Verify Local Setup
116
+
117
+ Test that your local environment works correctly:
118
+
119
+ ```bash
120
+ cd /home/thadillo/MyProjects/participatory_planner
121
+
122
+ # Start the application
123
+ ./start.sh
124
+ ```
125
+
126
+ You should see output like:
127
+ ```
128
+ πŸ” Admin token: Check startup logs or set ADMIN_TOKEN in .env
129
+ πŸš€ Starting application...
130
+ ```
131
+
132
+ If you didn't set `ADMIN_TOKEN` in `.env`, a random token will be generated and displayed **ONCE** in the logs. Save it immediately!
133
+
134
+ ---
135
+
136
+ ## Step 5: Test Admin Access
137
+
138
+ ### Local Testing:
139
+ 1. Start the application: `./start.sh`
140
+ 2. Go to: http://localhost:5000/generate
141
+ 3. Generate a new token using your **ADMIN_TOKEN**
142
+ 4. Use the generated token to test the application
143
+
144
+ ### HuggingFace Testing:
145
+ 1. Wait for HuggingFace to rebuild (after pushing changes)
146
+ 2. Go to: https://thadillo-participatory-planner.hf.space/generate
147
+ 3. Generate a new token using your **ADMIN_TOKEN**
148
+ 4. Verify the application works without the previous errors
149
+
150
+ ---
151
+
152
+ ## Step 6: Verify HuggingFace Deployment
153
+
154
+ After pushing to HuggingFace, monitor the build logs:
155
+
156
+ https://huggingface.co/spaces/thadillo/participatory-planner/logs
157
+
158
+ **Check for these confirmations:**
159
+ 1. βœ… No TRANSFORMERS_CACHE deprecation warning
160
+ 2. βœ… No matplotlib permission errors
161
+ 3. βœ… No SQLite database locking errors
162
+ 4. βœ… Application starts successfully
163
+
164
+ ---
165
+
166
+ ## Security Best Practices Going Forward
167
+
168
+ ### DO:
169
+ βœ… Keep `.env` file local and never commit it to git
170
+ βœ… Use different secrets for development and production
171
+ βœ… Rotate secrets periodically (every 3-6 months)
172
+ βœ… Store production secrets only in HuggingFace Spaces secrets
173
+ βœ… Use strong, randomly generated tokens (via `secrets` module)
174
+
175
+ ### DON'T:
176
+ ❌ Never commit `.env` to git
177
+ ❌ Never hardcode secrets in code
178
+ ❌ Never share your ADMIN_TOKEN publicly
179
+ ❌ Never use simple passwords like "ADMIN123"
180
+ ❌ Never include tokens in git remote URLs
181
+
182
+ ---
183
+
184
+ ## Quick Reference Commands
185
+
186
+ ### Generate Flask Secret Key:
187
+ ```bash
188
+ python -c "import secrets; print(secrets.token_hex(32))"
189
+ ```
190
+
191
+ ### Generate Admin Token:
192
+ ```bash
193
+ python -c "import secrets; print(secrets.token_urlsafe(16))"
194
+ ```
195
+
196
+ ### Push to HuggingFace:
197
+ ```bash
198
+ git push hf feature/sentence-level-categorization:main
199
+ ```
200
+
201
+ ### Start Local Server:
202
+ ```bash
203
+ ./start.sh
204
+ ```
205
+
206
+ ### View HuggingFace Logs:
207
+ https://huggingface.co/spaces/thadillo/participatory-planner/logs
208
+
209
+ ### View HuggingFace Settings:
210
+ https://huggingface.co/spaces/thadillo/participatory-planner/settings
211
+
212
+ ---
213
+
214
+ ## Troubleshooting
215
+
216
+ ### If HuggingFace deployment fails:
217
+ 1. Check the build logs for error messages
218
+ 2. Verify all secrets are set correctly in HuggingFace settings
219
+ 3. Ensure `FLASK_SECRET_KEY` and `ADMIN_TOKEN` are set
220
+
221
+ ### If you get "database is locked" errors:
222
+ - The new retry logic should handle this automatically
223
+ - If it persists, check HuggingFace logs for concurrent request issues
224
+
225
+ ### If you forget your ADMIN_TOKEN:
226
+ 1. Generate a new one: `python -c "import secrets; print(secrets.token_urlsafe(16))"`
227
+ 2. Update it in HuggingFace Spaces secrets
228
+ 3. Update it in your local `.env` file
229
+ 4. Restart the application
230
+
231
+ ### If authentication fails:
232
+ - Ensure you're using the correct ADMIN_TOKEN
233
+ - Check that rate limiting hasn't blocked you (wait 1 hour)
234
+ - Verify the token in HuggingFace secrets matches your local `.env`
235
+
236
+ ---
237
+
238
+ ## Next Steps
239
+
240
+ 1. **Immediate**: Push to HuggingFace using the command above
241
+ 2. **Immediate**: Set up HuggingFace Spaces secrets (FLASK_SECRET_KEY, ADMIN_TOKEN)
242
+ 3. **Immediate**: Update your local `.env` file
243
+ 4. **Soon**: Test the application on HuggingFace to verify all errors are resolved
244
+ 5. **Optional**: Consider merging `feature/sentence-level-categorization` to `main` branch
245
+
246
+ ---
247
+
248
+ ## Files Modified
249
+
250
+ All changes have been committed to git. Here's what was changed:
251
+
252
+ - [app/__init__.py](app/__init__.py) - Secure token generation, secret key validation, rate limiter
253
+ - [app/routes/auth.py](app/routes/auth.py) - Rate limiting on login/generate endpoints
254
+ - [app/routes/admin.py](app/routes/admin.py) - Removed hardcoded ADMIN123 references
255
+ - [requirements.txt](requirements.txt) - Added Flask-Limiter
256
+ - [.env.example](.env.example) - Updated with new structure
257
+ - [Dockerfile](Dockerfile) - Fixed HuggingFace deployment issues
258
+ - [SECURITY.md](SECURITY.md) - New comprehensive security documentation
259
+ - Multiple markdown files - Documentation cleanup
260
+
261
+ ---
262
+
263
+ ## Support
264
+
265
+ For questions or issues:
266
+ - Review [SECURITY.md](SECURITY.md) for detailed security information
267
+ - Check HuggingFace build logs for deployment errors
268
+ - Verify environment variables are set correctly
269
+
270
+ **Remember**: Save your ADMIN_TOKEN securely - it's shown only once when auto-generated!