time-period-api / app.py
DelaliScratchwerk's picture
Update app.py
2317360 verified
raw
history blame contribute delete
951 Bytes
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline
app = FastAPI()
clf = pipeline(
"text-classification",
model="DelaliScratchwerk/time-period-classifier-bert",
# If your pipeline supports it, you can also set device here later
)
class PredictRequest(BaseModel):
inputs: str
@app.post("/predict")
def predict(req: PredictRequest):
text = (req.inputs or "").strip()
if not text:
raise HTTPException(status_code=400, detail="inputs is empty")
# Hard guard against huge payloads (prevents space crashes)
if len(text) > 8000:
text = text[:8000]
try:
# Force truncation so the model never errors on long inputs
result = clf(text, truncation=True, max_length=512)
return result
except Exception as e:
# Return a 400 with the error instead of 500
raise HTTPException(status_code=400, detail=str(e))