File size: 1,002 Bytes
5ea1723
19a4531
 
 
1514e88
19a4531
1514e88
49784f7
5ea1723
19a4531
 
1514e88
 
19a4531
 
 
49784f7
19a4531
1514e88
 
 
5ea1723
19a4531
 
5ea1723
19a4531
 
 
1514e88
 
 
 
 
 
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
30
31
32
33
34
import os
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Optional
from transformers import pipeline

MODEL_ID = os.getenv("MODEL_ID", "distilgpt2")
CACHE_DIR = os.getenv("HF_HOME", "/app/.cache")
os.makedirs(CACHE_DIR, exist_ok=True)

app = FastAPI(title="FastAPI Hugging Face Space")

generator = pipeline("text-generation", model=MODEL_ID, cache_dir=CACHE_DIR)

class GenerateRequest(BaseModel):
    prompt: str
    max_length: Optional[int] = 64

@app.get("/")
async def root():
    return {"message": "API running. Use POST /generate to generate text."}

@app.get("/health")
async def health():
    return {"status": "ok", "model": MODEL_ID, "cache": CACHE_DIR}

@app.post("/generate")
async def generate(req: GenerateRequest):
    result = generator(req.prompt, max_length=req.max_length, num_return_sequences=1)
    return {"generated_text": result[0]["generated_text"]}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)