alkvoice / api_with_auth.py.example
alkasima's picture
Upload 16 files
93f5fa9 verified
Raw History Blame Contribute Delete
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
@app.post("/enhance")
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