Download api_with_auth.py.example from alkasima/alkvoice: direct link, hf CLI and curl.
- Browser
- Download file 851 Bytes
-
https://huggingface.co/spaces/alkasima/alkvoice/resolve/main/api_with_auth.py.example
- Command line
-
hf download hf://spaces/alkasima/alkvoice/api_with_auth.py.example
-
curl -L -o api_with_auth.py.example https://huggingface.co/spaces/alkasima/alkvoice/resolve/main/api_with_auth.py.example
851 Bytes
| # Add this to api.py for optional API key protection | |
| import os | |
| from fastapi import FastAPI, File, UploadFile, Form, HTTPException, Header | |
| from typing import Optional | |
| # Get API key from environment variable (set in HF Space settings) | |
| API_KEY = os.getenv("API_KEY", None) # Set to None to disable auth | |
| # Add this to your enhance endpoint | |
| async def enhance_audio_endpoint( | |
| file: UploadFile = File(...), | |
| speech_level: float = Form(80.0), | |
| background_level: float = Form(85.0), | |
| x_api_key: Optional[str] = Header(None) # Add this line | |
| ): | |
| # Optional API key check | |
| if API_KEY and x_api_key != API_KEY: | |
| raise HTTPException( | |
| status_code=401, | |
| detail="Invalid or missing API key. Add X-API-Key header." | |
| ) | |
| # ... rest of your existing code | |