File size: 628 Bytes
cafe5d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline

# Initialize FastAPI and load your Hugging Face model
app = FastAPI()
model = pipeline("text-classification", model="your-username/your-model-name")

# Define request body with Pydantic
class InputData(BaseModel):
    text: str

# API endpoint to receive input and return predictions
@app.post("/predict/")
async def predict(input_data: InputData):
    try:
        result = model(input_data.text)
        return {"output": result}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))