Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import pipeline
|
4 |
+
import torch
|
5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
6 |
+
|
7 |
+
app = FastAPI(title="Model Inference API")
|
8 |
+
|
9 |
+
# Allow CORS for external frontend
|
10 |
+
app.add_middleware(
|
11 |
+
CORSMiddleware,
|
12 |
+
allow_origins=["*"],
|
13 |
+
allow_methods=["*"],
|
14 |
+
allow_headers=["*"],
|
15 |
+
)
|
16 |
+
|
17 |
+
MODEL_MAP = {
|
18 |
+
"tinny-llama": "Lyon28/Tinny-Llama",
|
19 |
+
"pythia": "Lyon28/Pythia",
|
20 |
+
"bert-tinny": "Lyon28/Bert-Tinny",
|
21 |
+
"albert-base-v2": "Lyon28/Albert-Base-V2",
|
22 |
+
"t5-small": "Lyon28/T5-Small",
|
23 |
+
"gpt-2": "Lyon28/GPT-2",
|
24 |
+
"gpt-neo": "Lyon28/GPT-Neo",
|
25 |
+
"distilbert-base-uncased": "Lyon28/Distilbert-Base-Uncased",
|
26 |
+
"distil-gpt-2": "Lyon28/Distil_GPT-2",
|
27 |
+
"gpt-2-tinny": "Lyon28/GPT-2-Tinny",
|
28 |
+
"electra-small": "Lyon28/Electra-Small"
|
29 |
+
}
|
30 |
+
|
31 |
+
TASK_MAP = {
|
32 |
+
"text-generation": ["gpt-2", "gpt-neo", "distil-gpt-2", "gpt-2-tinny", "tinny-llama", "pythia"],
|
33 |
+
"text-classification": ["bert-tinny", "albert-base-v2", "distilbert-base-uncased", "electra-small"],
|
34 |
+
"text2text-generation": ["t5-small"]
|
35 |
+
}
|
36 |
+
|
37 |
+
class InferenceRequest(BaseModel):
|
38 |
+
text: str
|
39 |
+
max_length: int = 100
|
40 |
+
temperature: float = 0.9
|
41 |
+
|
42 |
+
def get_task(model_id: str):
|
43 |
+
for task, models in TASK_MAP.items():
|
44 |
+
if model_id in models:
|
45 |
+
return task
|
46 |
+
return "text-generation"
|
47 |
+
|
48 |
+
@app.on_event("startup")
|
49 |
+
async def load_models():
|
50 |
+
# Initialize models (optional: pre-load critical models)
|
51 |
+
app.state.pipelines = {}
|
52 |
+
print("Models initialized in memory")
|
53 |
+
|
54 |
+
@app.post("/inference/{model_id}")
|
55 |
+
async def model_inference(model_id: str, request: InferenceRequest):
|
56 |
+
try:
|
57 |
+
if model_id not in MODEL_MAP:
|
58 |
+
raise HTTPException(status_code=404, detail="Model not found")
|
59 |
+
|
60 |
+
task = get_task(model_id)
|
61 |
+
|
62 |
+
# Load pipeline with caching
|
63 |
+
if model_id not in app.state.pipelines:
|
64 |
+
app.state.pipelines[model_id] = pipeline(
|
65 |
+
task=task,
|
66 |
+
model=MODEL_MAP[model_id],
|
67 |
+
device_map="auto",
|
68 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
69 |
+
)
|
70 |
+
|
71 |
+
pipe = app.state.pipelines[model_id]
|
72 |
+
|
73 |
+
# Process based on task
|
74 |
+
if task == "text-generation":
|
75 |
+
result = pipe(
|
76 |
+
request.text,
|
77 |
+
max_length=request.max_length,
|
78 |
+
temperature=request.temperature
|
79 |
+
)[0]['generated_text']
|
80 |
+
|
81 |
+
elif task == "text-classification":
|
82 |
+
output = pipe(request.text)[0]
|
83 |
+
result = {
|
84 |
+
"label": output['label'],
|
85 |
+
"confidence": round(output['score'], 4)
|
86 |
+
}
|
87 |
+
|
88 |
+
elif task == "text2text-generation":
|
89 |
+
result = pipe(request.text)[0]['generated_text']
|
90 |
+
|
91 |
+
return {"result": result}
|
92 |
+
|
93 |
+
except Exception as e:
|
94 |
+
raise HTTPException(status_code=500, detail=str(e))
|
95 |
+
|
96 |
+
@app.get("/models")
|
97 |
+
async def list_models():
|
98 |
+
return {"available_models": list(MODEL_MAP.keys())}
|
99 |
+
|
100 |
+
@app.get("/health")
|
101 |
+
async def health_check():
|
102 |
+
return {"status": "healthy"}
|