Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from os import getenv
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Optional
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
MODEL_ID = getenv("MODEL_ID", "gpt2") # set by env if you want another model
|
| 8 |
+
GEN_KWARGS = {"max_length": 64, "num_return_sequences": 1}
|
| 9 |
+
|
| 10 |
+
app = FastAPI(title="FastAPI Hugging Face Space")
|
| 11 |
+
|
| 12 |
+
# load pipeline once on startup
|
| 13 |
+
generator = pipeline("text-generation", model=MODEL_ID)
|
| 14 |
+
|
| 15 |
+
class GenerateRequest(BaseModel):
|
| 16 |
+
prompt: str
|
| 17 |
+
max_length: Optional[int] = None
|
| 18 |
+
|
| 19 |
+
@app.get("/health")
|
| 20 |
+
async def health():
|
| 21 |
+
return {"status": "ok", "model": MODEL_ID}
|
| 22 |
+
|
| 23 |
+
@app.post("/generate")
|
| 24 |
+
async def generate(req: GenerateRequest):
|
| 25 |
+
kwargs = GEN_KWARGS.copy()
|
| 26 |
+
if req.max_length:
|
| 27 |
+
kwargs["max_length"] = req.max_length
|
| 28 |
+
out = generator(req.prompt, **kwargs)
|
| 29 |
+
# pipeline returns a list with dicts containing "generated_text"
|
| 30 |
+
return {"generated_text": out[0]["generated_text"]}
|