Spaces:
Runtime error
Runtime error
File size: 821 Bytes
689b8d0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from fastapi import FastAPI, HTTPException, Request
from transformers import pipeline
import os
app = FastAPI()
# Load your model
classifier = pipeline('sentiment-analysis')
# Define your API key (in a real application, use environment variables or a secret manager)
API_KEY = os.getenv("API_KEY", "your-default-api-key")
@app.middleware("http")
async def api_key_middleware(request: Request, call_next):
api_key = request.headers.get("X-API-KEY")
if api_key != API_KEY:
raise HTTPException(status_code=403, detail="Could not validate API key")
response = await call_next(request)
return response
@app.post("/predict")
async def predict(text: str):
result = classifier(text)
return result
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
|