Spaces:
Sleeping
Sleeping
chore: remove all API keys and enforce credentials only via secrets
Browse files- README.md +1 -1
- main.py +4 -1
- test_app.py +3 -2
README.md
CHANGED
|
@@ -16,7 +16,7 @@ A high-speed code evaluation backend designed for Hugging Face Spaces. External
|
|
| 16 |
|
| 17 |
To prevent unauthorized public access and ensure only your applications can send code:
|
| 18 |
1. Go to **Settings** → **Variables and secrets** in your Hugging Face Space.
|
| 19 |
-
2. Add a Secret named `SERVICE_API_KEY` (
|
| 20 |
3. External requests must provide this key in the `X-API-Key` header (or `Authorization: Bearer <key>`). Unauthorized requests are rejected with `401 Unauthorized`.
|
| 21 |
|
| 22 |
## Architecture
|
|
|
|
| 16 |
|
| 17 |
To prevent unauthorized public access and ensure only your applications can send code:
|
| 18 |
1. Go to **Settings** → **Variables and secrets** in your Hugging Face Space.
|
| 19 |
+
2. Add a Secret named `SERVICE_API_KEY` (set this to any secret token you choose).
|
| 20 |
3. External requests must provide this key in the `X-API-Key` header (or `Authorization: Bearer <key>`). Unauthorized requests are rejected with `401 Unauthorized`.
|
| 21 |
|
| 22 |
## Architecture
|
main.py
CHANGED
|
@@ -210,7 +210,10 @@ async def evaluate_code(request: Request) -> dict:
|
|
| 210 |
active_model = model_override or get_active_model()
|
| 211 |
|
| 212 |
if not active_key:
|
| 213 |
-
raise HTTPException(
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
# Auto-route Google Gemini keys if base_url wasn't specified
|
| 216 |
if active_key.startswith("AIza") and not active_base_url:
|
|
|
|
| 210 |
active_model = model_override or get_active_model()
|
| 211 |
|
| 212 |
if not active_key:
|
| 213 |
+
raise HTTPException(
|
| 214 |
+
status_code=400,
|
| 215 |
+
detail="Missing AI API key. Please configure OPENAI_API_KEY in Hugging Face Secrets or include 'api_key' in the request.",
|
| 216 |
+
)
|
| 217 |
|
| 218 |
# Auto-route Google Gemini keys if base_url wasn't specified
|
| 219 |
if active_key.startswith("AIza") and not active_base_url:
|
test_app.py
CHANGED
|
@@ -75,7 +75,7 @@ def test_evaluate_mocked_json():
|
|
| 75 |
res = client.post("/evaluate", json={
|
| 76 |
"question": "Implement quicksort",
|
| 77 |
"code": "def quicksort(arr): return arr",
|
| 78 |
-
"api_key": "
|
| 79 |
"base_url": "https://api.openai.com/v1"
|
| 80 |
})
|
| 81 |
assert res.status_code == 200
|
|
@@ -100,7 +100,8 @@ def test_evaluate_mocked_form():
|
|
| 100 |
res = client.post("/evaluate", data={
|
| 101 |
"question": "Double n",
|
| 102 |
"code": "def double(n): return n * 2",
|
| 103 |
-
"api_key": "
|
|
|
|
| 104 |
})
|
| 105 |
assert res.status_code == 200
|
| 106 |
data = res.json()
|
|
|
|
| 75 |
res = client.post("/evaluate", json={
|
| 76 |
"question": "Implement quicksort",
|
| 77 |
"code": "def quicksort(arr): return arr",
|
| 78 |
+
"api_key": "mock-key",
|
| 79 |
"base_url": "https://api.openai.com/v1"
|
| 80 |
})
|
| 81 |
assert res.status_code == 200
|
|
|
|
| 100 |
res = client.post("/evaluate", data={
|
| 101 |
"question": "Double n",
|
| 102 |
"code": "def double(n): return n * 2",
|
| 103 |
+
"api_key": "mock-key",
|
| 104 |
+
"base_url": "https://api.openai.com/v1"
|
| 105 |
})
|
| 106 |
assert res.status_code == 200
|
| 107 |
data = res.json()
|