makeitfr commited on
Commit
a9078f6
Β·
verified Β·
1 Parent(s): 13a8ec4

Upload HF_SPACES_SECRETS.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. HF_SPACES_SECRETS.md +226 -0
HF_SPACES_SECRETS.md ADDED
@@ -0,0 +1,226 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # HF Spaces Secrets Configuration Guide
2
+
3
+ ## Overview
4
+ All sensitive credentials (API keys, model names, endpoints) are configured via **Hugging Face Spaces Secrets**, not hardcoded in the code.
5
+
6
+ This document explains how to set up secrets for your HF Space.
7
+
8
+ ## Available Secrets
9
+
10
+ | Secret Name | Purpose | Required | Example Value |
11
+ |---|---|---|---|
12
+ | `QWEN_API_KEY` | Alibaba Qwen VL API Key | Yes | `sk-...` |
13
+ | `QWEN_BASE_URL` | Qwen API Endpoint | No | `https://dashscope-intl.aliyuncs.com/compatible-mode/v1` |
14
+ | `QWEN_MODEL` | Qwen Model Name | No | `qwen-vl-max` |
15
+
16
+ ## Method 1: Web UI (Simple)
17
+
18
+ ### Steps:
19
+ 1. Go to your Space: https://huggingface.co/spaces/YOUR_USERNAME/omniparsev2-ui-detector
20
+ 2. Click **Settings** (βš™οΈ icon) in top right
21
+ 3. Scroll down to **Secrets**
22
+ 4. Click **Add a secret**
23
+ 5. Enter:
24
+ - **Key**: `QWEN_API_KEY`
25
+ - **Value**: `sk-your-api-key-here`
26
+ 6. Replace the value with your actual Qwen API key
27
+ 7. Click **Save**
28
+
29
+ ### Repeat for other secrets (optional):
30
+ - `QWEN_BASE_URL` (usually not needed - uses default)
31
+ - `QWEN_MODEL` (usually not needed - uses `qwen-vl-max`)
32
+
33
+ ## Method 2: API (Programmatic)
34
+
35
+ ### Python Script
36
+ ```python
37
+ import requests
38
+ import json
39
+
40
+ HF_TOKEN = "hf_YOUR_HF_TOKEN_HERE"
41
+ SPACE_ID = "YOUR_USERNAME/omniparsev2-ui-detector"
42
+
43
+ def add_space_secret(key: str, value: str):
44
+ """Add a secret to HF Space via API"""
45
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
46
+
47
+ url = f"https://huggingface.co/api/spaces/{SPACE_ID}/secrets"
48
+ data = {"key": key, "value": value}
49
+
50
+ response = requests.post(url, json=data, headers=headers)
51
+
52
+ if response.status_code == 200:
53
+ print(f"βœ“ Secret '{key}' added successfully")
54
+ else:
55
+ print(f"βœ— Error: {response.status_code} - {response.text}")
56
+
57
+ return response.status_code == 200
58
+
59
+ # Add secrets
60
+ add_space_secret("QWEN_API_KEY", "sk-your-api-key-here")
61
+ add_space_secret("QWEN_BASE_URL", "https://dashscope-intl.aliyuncs.com/compatible-mode/v1")
62
+ add_space_secret("QWEN_MODEL", "qwen-vl-max")
63
+ ```
64
+
65
+ ### Using cURL
66
+ ```bash
67
+ HF_TOKEN="hf_YOUR_HF_TOKEN_HERE"
68
+ SPACE_ID="YOUR_USERNAME/omniparsev2-ui-detector"
69
+
70
+ curl -X POST \
71
+ "https://huggingface.co/api/spaces/${SPACE_ID}/secrets" \
72
+ -H "Authorization: Bearer ${HF_TOKEN}" \
73
+ -H "Content-Type: application/json" \
74
+ -d '{
75
+ "key": "QWEN_API_KEY",
76
+ "value": "sk-your-api-key-here"
77
+ }'
78
+ ```
79
+
80
+ ## Method 3: Bash Script (Automated)
81
+
82
+ ```bash
83
+ #!/bin/bash
84
+
85
+ # Configuration
86
+ HF_TOKEN="hf_YOUR_HF_TOKEN"
87
+ SPACE_ID="YOUR_USERNAME/omniparsev2-ui-detector"
88
+ QWEN_KEY="sk-your-api-key"
89
+
90
+ # Add QWEN_API_KEY
91
+ echo "Adding QWEN_API_KEY..."
92
+ curl -X POST \
93
+ "https://huggingface.co/api/spaces/${SPACE_ID}/secrets" \
94
+ -H "Authorization: Bearer ${HF_TOKEN}" \
95
+ -H "Content-Type: application/json" \
96
+ -d "{\"key\": \"QWEN_API_KEY\", \"value\": \"${QWEN_KEY}\"}" \
97
+ && echo "βœ“ Added" || echo "βœ— Failed"
98
+
99
+ # Add QWEN_BASE_URL
100
+ echo "Adding QWEN_BASE_URL..."
101
+ curl -X POST \
102
+ "https://huggingface.co/api/spaces/${SPACE_ID}/secrets" \
103
+ -H "Authorization: Bearer ${HF_TOKEN}" \
104
+ -H "Content-Type: application/json" \
105
+ -d '{"key": "QWEN_BASE_URL", "value": "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"}' \
106
+ && echo "βœ“ Added" || echo "βœ— Failed"
107
+ ```
108
+
109
+ ## How It Works in the Code
110
+
111
+ ### Reading Secrets
112
+ ```python
113
+ import os
114
+
115
+ # In caption_cropped_images.py
116
+ QWEN_API_KEY = os.environ.get("QWEN_API_KEY")
117
+ QWEN_BASE_URL = os.environ.get("QWEN_BASE_URL", "https://dashscope-intl.aliyuncs.com/compatible-mode/v1")
118
+ QWEN_MODEL = os.environ.get("QWEN_MODEL", "qwen-vl-max")
119
+
120
+ # In app_hf_spaces_server.py
121
+ from caption_cropped_images import qwen_client
122
+ CAPTIONS_ENABLED = qwen_client is not None # Only enabled if API key is set
123
+ ```
124
+
125
+ ### Fallback Behavior
126
+ - If `QWEN_API_KEY` is not set β†’ Captions disabled, system still works
127
+ - If `QWEN_BASE_URL` is not set β†’ Uses default DashScope endpoint
128
+ - If `QWEN_MODEL` is not set β†’ Uses `qwen-vl-max` by default
129
+
130
+ ## Security Best Practices
131
+
132
+ βœ… **DO:**
133
+ - Use HF Spaces Secrets for all API keys
134
+ - Never commit keys to git
135
+ - Rotate keys periodically
136
+ - Use minimal required permissions
137
+
138
+ ❌ **DON'T:**
139
+ - Hardcode API keys in source code
140
+ - Commit `.env` files to git
141
+ - Share secrets in logs or error messages
142
+ - Use personal/test keys in production
143
+
144
+ ## Verification
145
+
146
+ ### Check if Secrets are Set
147
+ ```python
148
+ import os
149
+
150
+ print(f"QWEN_API_KEY: {'SET' if os.environ.get('QWEN_API_KEY') else 'NOT SET'}")
151
+ print(f"QWEN_BASE_URL: {'SET' if os.environ.get('QWEN_BASE_URL') else 'NOT SET'}")
152
+ print(f"QWEN_MODEL: {os.environ.get('QWEN_MODEL', 'DEFAULT (qwen-vl-max)')}")
153
+ ```
154
+
155
+ ### Check Space Logs
156
+ After deploying, check the Space logs to verify secrets are loaded:
157
+ 1. Go to your Space
158
+ 2. Click **Logs** (or view app output)
159
+ 3. Look for: `βœ“ Qwen configured: qwen-vl-max`
160
+
161
+ ## Getting Your Qwen API Key
162
+
163
+ 1. Sign up: https://dashscope.console.aliyun.com/
164
+ 2. Navigate to API Keys
165
+ 3. Copy your API key (format: `sk-...`)
166
+ 4. Keep it safe!
167
+
168
+ ## Local Development (Optional)
169
+
170
+ If you want to test locally with captions:
171
+
172
+ ```bash
173
+ # Set environment variables
174
+ export QWEN_API_KEY="sk-your-key"
175
+ export QWEN_BASE_URL="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
176
+ export QWEN_MODEL="qwen-vl-max"
177
+
178
+ # Run the app
179
+ python app_hf_spaces_server.py
180
+ ```
181
+
182
+ ## Troubleshooting
183
+
184
+ ### Captions show "unavailable"
185
+ Check that `QWEN_API_KEY` is set in Space Secrets:
186
+ 1. Go to Settings β†’ Secrets
187
+ 2. Verify `QWEN_API_KEY` exists
188
+ 3. Restart the Space
189
+
190
+ ### API errors in logs
191
+ Common issues:
192
+ - Invalid API key β†’ Regenerate at dashscope.console.aliyun.com
193
+ - Rate limited β†’ Add delay between requests
194
+ - Endpoint wrong β†’ Use default (leave blank)
195
+
196
+ ### How to update a secret
197
+ 1. Go to Settings β†’ Secrets
198
+ 2. Find the secret
199
+ 3. Click delete (πŸ—‘οΈ)
200
+ 4. Add new one with same name
201
+
202
+ ## Example Configuration for New Space
203
+
204
+ ```bash
205
+ #!/bin/bash
206
+ # Complete setup for new space
207
+
208
+ HF_TOKEN="hf_..."
209
+ SPACE_ID="username/omniparsev2-ui-detector"
210
+
211
+ # Step 1: Add API Key
212
+ curl -X POST "https://huggingface.co/api/spaces/${SPACE_ID}/secrets" \
213
+ -H "Authorization: Bearer ${HF_TOKEN}" \
214
+ -H "Content-Type: application/json" \
215
+ -d '{"key": "QWEN_API_KEY", "value": "sk-your-key-here"}'
216
+
217
+ echo "βœ“ Space secrets configured!"
218
+ echo " Visit: https://huggingface.co/spaces/${SPACE_ID}"
219
+ ```
220
+
221
+ ## Next Steps
222
+
223
+ 1. Get your Qwen API key
224
+ 2. Add it to Space Secrets (via Web UI)
225
+ 3. Deploy the updated code
226
+ 4. Captions will work automatically!