Spaces:
Sleeping
Sleeping
File size: 2,292 Bytes
35275d1 aa33c8d 35275d1 f307059 7275a31 f307059 35275d1 aa33c8d 35275d1 |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
import warnings
import uvicorn
# Tắt warning lặt vặt
warnings.filterwarnings("ignore")
# Load model
MODEL_NAME = "tarudesu/ViHateT5-base-HSD"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
# Khởi tạo FastAPI
app = FastAPI(title="Vietnamese Toxic Comment Detection API")
# Bật CORS cho tất cả domain
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Cho phép tất cả domain
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Schema cho input
class CommentInput(BaseModel):
text: str
prefix: str = "hate-speech-detection"
# Hàm dự đoán
def predict_vihatet5(comment: str, prefix: str = "hate-speech-detection"):
input_text = prefix + ": " + comment
inputs = tokenizer(
input_text,
return_tensors="pt",
truncation=True,
padding=True,
max_length=128
)
output_ids = model.generate(**inputs, max_length=50)
output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
return output
@app.get("/")
async def root():
"""Health check endpoint with comprehensive API documentation"""
return {
"endpoints": {
"POSR https://haiss123-check-comment.hf.space/predict": {
"Resquest body": {
"input": "string",
"prefix": "hate-speech-detection or toxic-speech-detection"
},
"Response": {
"input": "string",
"prefix": "hate-speech-detection or toxic-speech-detection",
"prediction": "offensive,hate,..."
}
}
}
}
# API route
@app.post("/predict")
def predict(input_data: CommentInput):
result = predict_vihatet5(input_data.text, prefix=input_data.prefix)
return {
"input": input_data.text,
"prefix": input_data.prefix,
"prediction": result
}
# Chạy app
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)
|