Võ Minh Trí commited on
Commit ·
994a613
1
Parent(s): 8c87825
Int
Browse files- Dockerfile +12 -0
- app.py +38 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY . .
|
| 10 |
+
|
| 11 |
+
# Chạy uvicorn và bắt buộc mở cổng 7860
|
| 12 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
app = FastAPI(title="PhoBERT Vietnamese Comment Classifier API")
|
| 6 |
+
|
| 7 |
+
# Khởi tạo pipeline phân loại văn bản khi khởi động ứng dụng
|
| 8 |
+
try:
|
| 9 |
+
classifier = pipeline("text-classification", model="vanhai123/phobert-vi-comment-4class")
|
| 10 |
+
except Exception as e:
|
| 11 |
+
classifier = None
|
| 12 |
+
print(f"Lỗi nạp mô hình: {e}")
|
| 13 |
+
|
| 14 |
+
class CommentInput(BaseModel):
|
| 15 |
+
text: str
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def home():
|
| 19 |
+
return {"status": "active", "message": "API phân loại bình luận đang chạy mượt mà!"}
|
| 20 |
+
|
| 21 |
+
@app.post("/predict")
|
| 22 |
+
def predict_sentiment(data: CommentInput):
|
| 23 |
+
if classifier is None:
|
| 24 |
+
raise HTTPException(status_code=500, detail="Mô hình chưa được tải thành công.")
|
| 25 |
+
|
| 26 |
+
if not data.text.strip():
|
| 27 |
+
raise HTTPException(status_code=400, detail="Nội dung văn bản không được để trống.")
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
# Dự đoán nhãn
|
| 31 |
+
prediction = classifier(data.text)[0]
|
| 32 |
+
return {
|
| 33 |
+
"text": data.text,
|
| 34 |
+
"label": prediction["label"],
|
| 35 |
+
"confidence": round(prediction["score"], 4)
|
| 36 |
+
}
|
| 37 |
+
except Exception as e:
|
| 38 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch --index-url https://download.pytorch.org/whl/cpu
|
| 5 |
+
pyvi
|
| 6 |
+
pydantic
|