Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,62 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
-
import torch.nn.functional as F
|
| 5 |
-
|
| 6 |
-
# ==============================
|
| 7 |
-
# 1. Load Model
|
| 8 |
-
# ==============================
|
| 9 |
-
MODEL_ID = "anhgf/visec-phobert-sentiment-vi"
|
| 10 |
-
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
-
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 13 |
-
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 14 |
-
model.to(DEVICE)
|
| 15 |
-
model.eval()
|
| 16 |
-
|
| 17 |
-
LABELS = ["negative", "positive"]
|
| 18 |
-
|
| 19 |
-
# ==============================
|
| 20 |
-
# 2. Inference Function
|
| 21 |
-
# ==============================
|
| 22 |
-
def classify_sentiment(text):
|
| 23 |
-
if not text or text.strip() == "":
|
| 24 |
-
return {"label": "empty input", "probabilities": {}}
|
| 25 |
-
|
| 26 |
-
# Tokenize
|
| 27 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 28 |
-
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 29 |
-
|
| 30 |
-
# Model forward
|
| 31 |
-
with torch.no_grad():
|
| 32 |
-
logits = model(**inputs).logits
|
| 33 |
-
softmax = F.softmax(logits, dim=-1)[0].cpu().tolist()
|
| 34 |
-
|
| 35 |
-
# Because model only has 2 classes: [neg, pos]
|
| 36 |
-
neg_p = softmax[0]
|
| 37 |
-
pos_p = softmax[1]
|
| 38 |
-
|
| 39 |
-
# Predict label
|
| 40 |
-
label = "negative" if neg_p > pos_p else "positive"
|
| 41 |
-
|
| 42 |
-
return {
|
| 43 |
-
"label": label,
|
| 44 |
-
"probabilities": {
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
}
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
# ==============================
|
| 51 |
-
# 3. Gradio UI
|
| 52 |
-
# ==============================
|
| 53 |
-
app = gr.Interface(
|
| 54 |
-
fn=classify_sentiment,
|
| 55 |
-
inputs=gr.Textbox(lines=3, label="Nhập văn bản tiếng Việt"),
|
| 56 |
-
outputs=gr.JSON(label="Kết quả phân tích cảm xúc (POS / NEG)"),
|
| 57 |
-
title="Vietnamese Sentiment Classification (PhoBERT)",
|
| 58 |
-
description="Model này chỉ có 2 lớp: Positive và Negative."
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
if __name__ == "__main__":
|
| 62 |
-
app.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
# ==============================
|
| 7 |
+
# 1. Load Model
|
| 8 |
+
# ==============================
|
| 9 |
+
MODEL_ID = "anhgf/visec-phobert-sentiment-vi"
|
| 10 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 13 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 14 |
+
model.to(DEVICE)
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
LABELS = ["negative", "positive"]
|
| 18 |
+
|
| 19 |
+
# ==============================
|
| 20 |
+
# 2. Inference Function
|
| 21 |
+
# ==============================
|
| 22 |
+
def classify_sentiment(text):
|
| 23 |
+
if not text or text.strip() == "":
|
| 24 |
+
return {"label": "empty input", "probabilities": {}}
|
| 25 |
+
|
| 26 |
+
# Tokenize
|
| 27 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 28 |
+
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 29 |
+
|
| 30 |
+
# Model forward
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
logits = model(**inputs).logits
|
| 33 |
+
softmax = F.softmax(logits, dim=-1)[0].cpu().tolist()
|
| 34 |
+
|
| 35 |
+
# Because model only has 2 classes: [neg, pos]
|
| 36 |
+
neg_p = softmax[0]
|
| 37 |
+
pos_p = softmax[1]
|
| 38 |
+
|
| 39 |
+
# Predict label
|
| 40 |
+
label = "negative" if neg_p > pos_p else "positive"
|
| 41 |
+
|
| 42 |
+
return {
|
| 43 |
+
"label": label,
|
| 44 |
+
# "probabilities": {
|
| 45 |
+
# "negative": float(neg_p),
|
| 46 |
+
# "positive": float(pos_p)
|
| 47 |
+
# }
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
# ==============================
|
| 51 |
+
# 3. Gradio UI
|
| 52 |
+
# ==============================
|
| 53 |
+
app = gr.Interface(
|
| 54 |
+
fn=classify_sentiment,
|
| 55 |
+
inputs=gr.Textbox(lines=3, label="Nhập văn bản tiếng Việt"),
|
| 56 |
+
outputs=gr.JSON(label="Kết quả phân tích cảm xúc (POS / NEG)"),
|
| 57 |
+
title="Vietnamese Sentiment Classification (PhoBERT)",
|
| 58 |
+
description="Model này chỉ có 2 lớp: Positive và Negative."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
app.launch()
|