Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Header, HTTPException
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
API_KEY = "
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
@@ -11,10 +13,16 @@ generator = pipeline(
|
|
| 11 |
max_new_tokens=256
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.post("/generate")
|
| 15 |
-
def generate(
|
|
|
|
|
|
|
|
|
|
| 16 |
if x_api_key != API_KEY:
|
| 17 |
raise HTTPException(status_code=401, detail="Invalid API Key")
|
| 18 |
|
| 19 |
-
|
| 20 |
-
return {"response":
|
|
|
|
| 1 |
+
import os
|
| 2 |
from fastapi import FastAPI, Header, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
API_KEY = os.getenv("API_KEY")
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 13 |
max_new_tokens=256
|
| 14 |
)
|
| 15 |
|
| 16 |
+
class PromptRequest(BaseModel):
|
| 17 |
+
prompt: str
|
| 18 |
+
|
| 19 |
@app.post("/generate")
|
| 20 |
+
def generate(data: PromptRequest, x_api_key: str = Header(None)):
|
| 21 |
+
if API_KEY is None:
|
| 22 |
+
raise HTTPException(status_code=500, detail="API key not configured")
|
| 23 |
+
|
| 24 |
if x_api_key != API_KEY:
|
| 25 |
raise HTTPException(status_code=401, detail="Invalid API Key")
|
| 26 |
|
| 27 |
+
result = generator(data.prompt)
|
| 28 |
+
return {"response": result[0]["generated_text"]}
|