Spaces:
Running
Running
File size: 1,051 Bytes
f86c1b9 | 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 | import gradio as gr
import joblib
import numpy as np
from huggingface_hub import snapshot_download
# ====== Mapping nhãn ======
label_map = {
0: "Tiêu cực",
1: "Trung lập",
2: "Tích cực"
}
# ====== Load model ======
local_dir = snapshot_download(
repo_id="phucn001/SentimentAnalysisModels",
local_dir="./Models"
)
rf_model = joblib.load(f"{local_dir}/RandomForest/model_random_forest_with_accent.pkl")
tfidf = joblib.load(f"{local_dir}/RandomForest/tfidf_vectorizer_with_accent.pkl")
def predict_rf(text):
vec = tfidf.transform([text])
pred = rf_model.predict(vec)[0]
proba = rf_model.predict_proba(vec)[0]
return {
"label": label_map[pred],
"probabilities": {label_map[i]: float(p) for i, p in enumerate(proba)}
}
demo = gr.Interface(
fn=predict_rf,
inputs=gr.Textbox(lines=2, placeholder="Nhập câu bình luận..."),
outputs="json",
title="Sentiment Analysis - RandomForest"
)
if __name__ == "__main__":
demo.launch()
|