phucn001 commited on
Commit
f86c1b9
·
verified ·
1 Parent(s): 3a43e05

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import numpy as np
4
+ from huggingface_hub import snapshot_download
5
+
6
+ # ====== Mapping nhãn ======
7
+ label_map = {
8
+ 0: "Tiêu cực",
9
+ 1: "Trung lập",
10
+ 2: "Tích cực"
11
+ }
12
+
13
+ # ====== Load model ======
14
+ local_dir = snapshot_download(
15
+ repo_id="phucn001/SentimentAnalysisModels",
16
+ local_dir="./Models"
17
+ )
18
+ rf_model = joblib.load(f"{local_dir}/RandomForest/model_random_forest_with_accent.pkl")
19
+ tfidf = joblib.load(f"{local_dir}/RandomForest/tfidf_vectorizer_with_accent.pkl")
20
+
21
+ def predict_rf(text):
22
+ vec = tfidf.transform([text])
23
+ pred = rf_model.predict(vec)[0]
24
+ proba = rf_model.predict_proba(vec)[0]
25
+ return {
26
+ "label": label_map[pred],
27
+ "probabilities": {label_map[i]: float(p) for i, p in enumerate(proba)}
28
+ }
29
+
30
+ demo = gr.Interface(
31
+ fn=predict_rf,
32
+ inputs=gr.Textbox(lines=2, placeholder="Nhập câu bình luận..."),
33
+ outputs="json",
34
+ title="Sentiment Analysis - RandomForest"
35
+ )
36
+
37
+ if __name__ == "__main__":
38
+ demo.launch()