Views
No views yet
323e-51002560.015000.95030.66240.76100.73680.60391import torch
2from transformers import AutoTokenizer, AutoModel
3
4# Load model and tokenizer
5repo = "visolex/phobert-v1-absa-smartphone"
6tokenizer = AutoTokenizer.from_pretrained(repo, trust_remote_code=True)
7model = AutoModel.from_pretrained(repo, trust_remote_code=True)
8model.eval()
9
10# Aspect labels for ViSFD
11aspect_labels = [
12 "BATTERY",
13 "CAMERA",
14 "DESIGN",
15 "FEATURES",
16 "GENERAL",
17 "PERFORMANCE",
18 "PRICE",
19 "SCREEN",
20 "SER&ACC",
21 "STORAGE"
22]
23
24# Sentiment labels
25sentiment_labels = ["POSITIVE", "NEGATIVE", "NEUTRAL"]
26
27# Example review text
28text = "Pin rất tốt, camera đẹp nhưng giá hơi cao."
29
30# Tokenize
31inputs = tokenizer(
32 text,
33 return_tensors="pt",
34 padding=True,
35 truncation=True,
36 max_length=256
37)
38inputs.pop("token_type_ids", None)
39
40# Predict
41with torch.no_grad():
42 outputs = model(**inputs)
43
44# Get logits: shape [1, num_aspects, num_sentiments + 1]
45logits = outputs.logits.squeeze(0) # [num_aspects, num_sentiments + 1]
46probs = torch.softmax(logits, dim=-1)
47
48# Predict for each aspect
49none_id = probs.size(-1) - 1 # Index of "none" class
50results = []
51
52for i, aspect in enumerate(aspect_labels):
53 prob_i = probs[i]
54 pred_id = int(prob_i.argmax().item())
55
56 if pred_id != none_id and pred_id < len(sentiment_labels):
57 score = prob_i[pred_id].item()
58 if score >= 0.5: # threshold
59 results.append((aspect, sentiment_labels[pred_id].lower()))
60
61print(f"Text: {text}")
62print(f"Predicted aspects: {results}")
63# Output example: [('aspects', 'positive'), ('aspects', 'positive'), ('aspects', 'negative')]1@misc{visolex_absa_phobert_v1_absa_smartphone,
2 title={PhoBERT-v1 for Vietnamese ABSA for Vietnamese Aspect-based Sentiment Analysis},
3 author={ViSoLex Team},
4 year={2025},
5 url={https://huggingface.co/visolex/phobert-v1-absa-smartphone}
6}