|
|
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", |
|
|
|
|
|
) |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
if len(text) > 8000: |
|
|
text = text[:8000] |
|
|
|
|
|
try: |
|
|
|
|
|
result = clf(text, truncation=True, max_length=512) |
|
|
return result |
|
|
except Exception as e: |
|
|
|
|
|
raise HTTPException(status_code=400, detail=str(e)) |
|
|
|