ThanhNguyen1811 commited on
Commit
70df48c
·
verified ·
1 Parent(s): 027bb4f

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +31 -0
  2. best_model_state.bin +3 -0
  3. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+
5
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6
+
7
+ MODEL_NAME = "vinai/phobert-base-v2"
8
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=3)
9
+ model.load_state_dict(torch.load("best_model_state.bin", map_location=device))
10
+ model.to(device)
11
+ model.eval()
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
14
+ label_map = {0: "An toàn", 1: "Tiêu cực", 2: "Nguy cơ bạo lực"}
15
+
16
+ def predict(text):
17
+ inputs = tokenizer.encode_plus(
18
+ text.lower(), return_tensors="pt", max_length=128, padding="max_length", truncation=True
19
+ )
20
+ input_ids = inputs["input_ids"].to(device)
21
+ attention_mask = inputs["attention_mask"].to(device)
22
+
23
+ with torch.no_grad():
24
+ outputs = model(input_ids=input_ids, attention_mask=attention_mask)
25
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
26
+ pred = torch.argmax(probs, dim=1).item()
27
+
28
+ return f"{label_map[pred]} (độ tin cậy: {probs[0][pred]:.2f})"
29
+
30
+ demo = gr.Interface(fn=predict, inputs="text", outputs="text", title="PhoBERT - Phân tích cảm xúc học sinh")
31
+ demo.launch()
best_model_state.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:40fa85d61d0837ebd176638652e12fcc74562c07d30f9cdb558c29b591950415
3
+ size 540087061
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ transformers
3
+ gradio