Views
No views yet
323e-51002560.015000.89970.65000.79680.80360.56581import torch
2from transformers import AutoTokenizer, AutoModel
3
4# Load model and tokenizer
5repo = "visolex/visobert-absa-restaurant"
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 VLSP2018-ABSA-Restaurant
11aspect_labels = [
12 "AMBIENCE#GENERAL",
13 "DRINKS#PRICES",
14 "DRINKS#QUALITY",
15 "DRINKS#STYLE&OPTIONS",
16 "FOOD#PRICES",
17 "FOOD#QUALITY",
18 "FOOD#STYLE&OPTIONS",
19 "LOCATION#GENERAL",
20 "RESTAURANT#GENERAL",
21 "RESTAURANT#MISCELLANEOUS",
22 "RESTAURANT#PRICES",
23 "SERVICE#GENERAL"
24]
25
26# Sentiment labels
27sentiment_labels = ["POSITIVE", "NEGATIVE", "NEUTRAL"]
28
29# Example review text
30text = "Nhà hàng có không gian đẹp, đồ ăn ngon nhưng giá hơi đắt."
31
32# Tokenize
33inputs = tokenizer(
34 text,
35 return_tensors="pt",
36 padding=True,
37 truncation=True,
38 max_length=256
39)
40inputs.pop("token_type_ids", None)
41
42# Predict
43with torch.no_grad():
44 outputs = model(**inputs)
45
46# Get logits: shape [1, num_aspects, num_sentiments + 1]
47logits = outputs.logits.squeeze(0) # [num_aspects, num_sentiments + 1]
48probs = torch.softmax(logits, dim=-1)
49
50# Predict for each aspect
51none_id = probs.size(-1) - 1 # Index of "none" class
52results = []
53
54for i, aspect in enumerate(aspect_labels):
55 prob_i = probs[i]
56 pred_id = int(prob_i.argmax().item())
57
58 if pred_id != none_id and pred_id < len(sentiment_labels):
59 score = prob_i[pred_id].item()
60 if score >= 0.5: # threshold
61 results.append((aspect, sentiment_labels[pred_id].lower()))
62
63print(f"Text: {text}")
64print(f"Predicted aspects: {results}")
65# Output example: [('aspects', 'positive'), ('aspects', 'positive'), ('aspects', 'negative')]1@misc{visolex_absa_visobert_absa_restaurant,
2 title={ViSoBERT for Vietnamese ABSA for Vietnamese Aspect-based Sentiment Analysis},
3 author={ViSoLex Team},
4 year={2025},
5 url={https://huggingface.co/visolex/visobert-absa-restaurant}
6}