Spaces:
Sleeping
Sleeping
Simplify app: Use HF secret for API key and fixed model
Browse files- Remove API key textbox, use QWEN_API_KEY from HF secrets
- Remove model dropdown, fixed to qwen2.5-vl-7b-instruct
- Cleaner, simpler interface for public use
- Add SECRET_SETUP.md with instructions
- SECRET_SETUP.md +57 -0
- app_simple.py +16 -19
SECRET_SETUP.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🔐 Setting Up API Key Secret in Hugging Face Space
|
| 2 |
+
|
| 3 |
+
## How to Add the QWEN_API_KEY Secret
|
| 4 |
+
|
| 5 |
+
1. **Go to your Space Settings**:
|
| 6 |
+
- Navigate to: https://huggingface.co/spaces/Sangmin/conduct-regulation
|
| 7 |
+
- Click on "Settings" tab
|
| 8 |
+
|
| 9 |
+
2. **Add Secret Variable**:
|
| 10 |
+
- Scroll down to "Repository secrets"
|
| 11 |
+
- Click "New secret"
|
| 12 |
+
- Add:
|
| 13 |
+
- **Name**: `QWEN_API_KEY`
|
| 14 |
+
- **Value**: Your Dashscope API key (starts with `sk-`)
|
| 15 |
+
- Click "Add new secret"
|
| 16 |
+
|
| 17 |
+
3. **Verify**:
|
| 18 |
+
- The Space will restart automatically
|
| 19 |
+
- The app will now use your API key securely
|
| 20 |
+
|
| 21 |
+
## Get Your Dashscope API Key
|
| 22 |
+
|
| 23 |
+
1. Go to: https://bailian.console.alibabacloud.com/
|
| 24 |
+
2. Navigate to API Keys section
|
| 25 |
+
3. Create a new API key
|
| 26 |
+
4. Copy the key (starts with `sk-`)
|
| 27 |
+
|
| 28 |
+
## Important Notes
|
| 29 |
+
|
| 30 |
+
- The API key is stored securely and never exposed in logs or UI
|
| 31 |
+
- Only Space owners can view/modify secrets
|
| 32 |
+
- The app will show an error if the secret is not set
|
| 33 |
+
- Users don't need their own API keys - they use yours
|
| 34 |
+
|
| 35 |
+
## Updated App Features
|
| 36 |
+
|
| 37 |
+
✅ **Simplified Interface**:
|
| 38 |
+
- No API key input needed
|
| 39 |
+
- No model selection (uses qwen2.5-vl-7b-instruct)
|
| 40 |
+
- Cleaner, more user-friendly UI
|
| 41 |
+
|
| 42 |
+
✅ **Security**:
|
| 43 |
+
- API key stored as environment variable
|
| 44 |
+
- Not visible to users
|
| 45 |
+
- Secure server-side processing
|
| 46 |
+
|
| 47 |
+
## Testing
|
| 48 |
+
|
| 49 |
+
After setting the secret:
|
| 50 |
+
1. Upload any image with text
|
| 51 |
+
2. Click "Detect Text"
|
| 52 |
+
3. Should see bounding boxes and JSON output
|
| 53 |
+
|
| 54 |
+
If you see "API key not configured" error, check that:
|
| 55 |
+
- Secret name is exactly `QWEN_API_KEY`
|
| 56 |
+
- API key is valid
|
| 57 |
+
- Space has restarted after adding secret
|
app_simple.py
CHANGED
|
@@ -87,14 +87,19 @@ def plot_bounding_boxes(image, bounding_boxes):
|
|
| 87 |
return img
|
| 88 |
|
| 89 |
|
| 90 |
-
def process_image(image,
|
| 91 |
"""Process image using API."""
|
| 92 |
|
| 93 |
if image is None:
|
| 94 |
return None, "Please upload an image"
|
| 95 |
|
|
|
|
|
|
|
| 96 |
if not api_key:
|
| 97 |
-
return None, "Please
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
try:
|
| 100 |
# Save image temporarily
|
|
@@ -149,35 +154,27 @@ def process_image(image, api_key, prompt, model="qwen2.5-vl-7b-instruct"):
|
|
| 149 |
|
| 150 |
|
| 151 |
# Create interface
|
| 152 |
-
with gr.Blocks(title="
|
| 153 |
-
gr.Markdown("#
|
|
|
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
with gr.Column():
|
| 157 |
image_input = gr.Image(type="pil", label="Upload Image")
|
| 158 |
-
api_key_input = gr.Textbox(
|
| 159 |
-
label="API Key",
|
| 160 |
-
type="password",
|
| 161 |
-
placeholder="sk-..."
|
| 162 |
-
)
|
| 163 |
-
model_dropdown = gr.Dropdown(
|
| 164 |
-
choices=["qwen2.5-vl-7b-instruct", "qwen2.5-vl-72b-instruct"],
|
| 165 |
-
value="qwen2.5-vl-7b-instruct",
|
| 166 |
-
label="Model"
|
| 167 |
-
)
|
| 168 |
prompt_input = gr.Textbox(
|
| 169 |
value="Spotting all the text in the image with line-level, and output in JSON format.",
|
| 170 |
-
label="Prompt"
|
|
|
|
| 171 |
)
|
| 172 |
-
submit_btn = gr.Button("Detect Text", variant="primary")
|
| 173 |
|
| 174 |
with gr.Column():
|
| 175 |
-
output_image = gr.Image(label="Result")
|
| 176 |
-
output_text = gr.Textbox(label="JSON Output", lines=
|
| 177 |
|
| 178 |
submit_btn.click(
|
| 179 |
fn=process_image,
|
| 180 |
-
inputs=[image_input,
|
| 181 |
outputs=[output_image, output_text]
|
| 182 |
)
|
| 183 |
|
|
|
|
| 87 |
return img
|
| 88 |
|
| 89 |
|
| 90 |
+
def process_image(image, prompt):
|
| 91 |
"""Process image using API."""
|
| 92 |
|
| 93 |
if image is None:
|
| 94 |
return None, "Please upload an image"
|
| 95 |
|
| 96 |
+
# Get API key from environment variable (HF Secret)
|
| 97 |
+
api_key = os.environ.get("QWEN_API_KEY")
|
| 98 |
if not api_key:
|
| 99 |
+
return None, "API key not configured. Please set QWEN_API_KEY in Space secrets."
|
| 100 |
+
|
| 101 |
+
# Fixed model
|
| 102 |
+
model = "qwen2.5-vl-7b-instruct"
|
| 103 |
|
| 104 |
try:
|
| 105 |
# Save image temporarily
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
# Create interface
|
| 157 |
+
with gr.Blocks(title="行為規制") as demo:
|
| 158 |
+
gr.Markdown("# 行為規制違反の検出")
|
| 159 |
+
gr.Markdown("Upload an image to detect text using Qwen Vision Language Model (qwen2.5-vl-7b-instruct)")
|
| 160 |
|
| 161 |
with gr.Row():
|
| 162 |
with gr.Column():
|
| 163 |
image_input = gr.Image(type="pil", label="Upload Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
prompt_input = gr.Textbox(
|
| 165 |
value="Spotting all the text in the image with line-level, and output in JSON format.",
|
| 166 |
+
label="Prompt",
|
| 167 |
+
lines=2
|
| 168 |
)
|
| 169 |
+
submit_btn = gr.Button("🔍 Detect Text", variant="primary", size="lg")
|
| 170 |
|
| 171 |
with gr.Column():
|
| 172 |
+
output_image = gr.Image(label="Annotated Result")
|
| 173 |
+
output_text = gr.Textbox(label="JSON Output", lines=12)
|
| 174 |
|
| 175 |
submit_btn.click(
|
| 176 |
fn=process_image,
|
| 177 |
+
inputs=[image_input, prompt_input],
|
| 178 |
outputs=[output_image, output_text]
|
| 179 |
)
|
| 180 |
|