saintyboy commited on
Commit
689b8d0
1 Parent(s): 6bee556

Create app.py

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