phucn001 commited on
Commit
317e2d2
·
verified ·
1 Parent(s): 307ebe3

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+ import numpy as np
4
+ from tensorflow.keras.models import load_model
5
+ from huggingface_hub import snapshot_download
6
+ import unicodedata
7
+
8
+ # ====== Mapping nhãn ======
9
+ label_map = {
10
+ 0: "Tiêu cực",
11
+ 1: "Trung lập",
12
+ 2: "Tích cực"
13
+ }
14
+
15
+ MAX_LEN = 200
16
+
17
+ # ====== Load model ======
18
+ local_dir = snapshot_download(
19
+ repo_id="phucn001/SentimentAnalysisModels",
20
+ local_dir="./Models"
21
+ )
22
+ cnn_lstm_model = load_model(f"{local_dir}/CNNLSTM/best_model.h5", compile=False)
23
+ cnn_lstm_tokenizer = joblib.load(f"{local_dir}/CNNLSTM/tokenizer.pkl")
24
+ cnn_label_encoder = joblib.load(f"{local_dir}/CNNLSTM/label_encoder.pkl")
25
+
26
+ def normalize_text(text):
27
+ return unicodedata.normalize("NFC", text).strip().lower()
28
+
29
+ reverse_label_map = {normalize_text(v): k for k, v in label_map.items()}
30
+
31
+ def predict_cnn_lstm(text):
32
+ seq = cnn_lstm_tokenizer.texts_to_sequences([text])
33
+ padded = np.zeros((1, MAX_LEN))
34
+ padded[0, -len(seq[0]):] = seq[0][:MAX_LEN]
35
+
36
+ probs = cnn_lstm_model.predict(padded, verbose=0)[0]
37
+
38
+ # reorder theo label_map
39
+ probs_reordered = np.zeros_like(probs)
40
+ for i, label in enumerate(cnn_label_encoder.classes_):
41
+ norm_label = normalize_text(label)
42
+ new_index = reverse_label_map[norm_label]
43
+ probs_reordered[new_index] = probs[i]
44
+
45
+ pred = int(np.argmax(probs_reordered))
46
+ return {
47
+ "label": label_map[pred],
48
+ "probabilities": {label_map[i]: float(probs_reordered[i]) for i in range(len(probs_reordered))}
49
+ }
50
+
51
+ demo = gr.Interface(
52
+ fn=predict_cnn_lstm,
53
+ inputs=gr.Textbox(lines=2, placeholder="Nhập câu bình luận..."),
54
+ outputs="json",
55
+ title="Sentiment Analysis - CNN-LSTM"
56
+ )
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()