vanhai123 commited on
Commit
252173a
·
verified ·
1 Parent(s): 03297dc

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +48 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import pickle
4
+ import os
5
+ from keras.utils import pad_sequences
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ # Tên repo trên HF
9
+ REPO_ID = "vanhai123/vietnamese-emotion-lstm"
10
+
11
+ # Tải model .h5 từ repo
12
+ model_path = hf_hub_download(repo_id=REPO_ID, filename="emotion_lstm_model.h5")
13
+ model = tf.keras.models.load_model(model_path)
14
+
15
+ # Tải tokenizer.pkl
16
+ tokenizer_path = hf_hub_download(repo_id=REPO_ID, filename="tokenizer.pkl")
17
+ with open(tokenizer_path, "rb") as f:
18
+ tokenizer = pickle.load(f)
19
+
20
+ # Tải max_length.txt
21
+ maxlen_path = hf_hub_download(repo_id=REPO_ID, filename="max_length.txt")
22
+ with open(maxlen_path, "r") as f:
23
+ max_length = int(f.read())
24
+
25
+ # Làm sạch văn bản
26
+ def clean_text(text):
27
+ return text.lower().strip()
28
+
29
+ # Hàm dự đoán cảm xúc
30
+ def predict_emotion(text):
31
+ cleaned = clean_text(text)
32
+ seq = tokenizer.texts_to_sequences([cleaned])
33
+ pad = pad_sequences(seq, maxlen=max_length, padding='post', truncating='post')
34
+ pred = model.predict(pad)
35
+ label = int(tf.argmax(pred, axis=1)[0])
36
+ return {0: "😠 Tiêu cực", 1: "😐 Trung lập", 2: "😊 Tích cực"}.get(label, "Không xác định")
37
+
38
+ # Giao diện Gradio
39
+ interface = gr.Interface(
40
+ fn=predict_emotion,
41
+ inputs=gr.Textbox(lines=4, placeholder="Nhập một câu tiếng Việt..."),
42
+ outputs="text",
43
+ title="Phân loại cảm xúc tiếng Việt",
44
+ description="Mô hình LSTM phân loại cảm xúc (Tích cực / Trung lập / Tiêu cực) dựa trên văn bản đầu vào."
45
+ )
46
+
47
+ if __name__ == "__main__":
48
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ tensorflow
3
+ scikit-learn
4
+ huggingface_hub