File size: 801 Bytes
f817340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
import sys
import os

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from src.predict import HateSpeechPredictor

app = FastAPI()
print("--> Đang khởi động Server...")
predictor = HateSpeechPredictor()


class Item(BaseModel):
    text: str


@app.post("/predict")
def predict(item: Item):
    # Gọi hàm predict thông minh (đã xử lý đoạn văn)
    result = predictor.predict(item.text)

    return {
        "text": item.text,
        "prediction": result['label'],
        "confidence": f"{result['confidence']:.2%}",
        "is_toxic": result['is_toxic'],
        "flagged_sentence": result['flagged_sentence']
    }


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)