2024-10-24 03:12:23 +00:00
|
|
|
import uvicorn
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from pydantic import BaseModel
|
|
|
|
import classification
|
|
|
|
|
|
|
|
app = FastAPI()
|
|
|
|
|
2024-10-24 05:07:11 +00:00
|
|
|
|
2024-10-24 03:12:23 +00:00
|
|
|
class TextClassificationRequest(BaseModel):
|
|
|
|
text: str
|
|
|
|
labels: list[str]
|
|
|
|
|
2024-10-24 05:07:11 +00:00
|
|
|
|
2024-10-24 03:12:23 +00:00
|
|
|
class TextClassificationResponse(BaseModel):
|
|
|
|
prediction: str
|
2024-10-24 05:07:11 +00:00
|
|
|
ranks: list[str]
|
2024-10-24 03:12:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.post("/classify")
|
|
|
|
def classify(req: TextClassificationRequest) -> TextClassificationResponse:
|
|
|
|
result = classification.classify(req.text, req.labels)
|
|
|
|
|
2024-10-24 05:07:11 +00:00
|
|
|
return TextClassificationResponse(prediction=result.prediction, ranks=result.rank)
|
2024-10-24 03:12:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-10-24 05:07:11 +00:00
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|