Aspect-based Sentiment Analysis
Collection
6 items
•
Updated
•
5
This model performs end-to-end Aspect-Based Sentiment Analysis (ABSA) by jointly extracting aspect terms and their sentiments via a single token-classification head. Labels are merged as IOB-with-sentiment, e.g. B-ASP-Positive
, I-ASP-Negative
, or O
for non-aspect tokens.
start
, end
) in the original inputfrom transformers import pipeline
nlp = pipeline(
"token-classification",
model="yangheng/deberta-v3-base-end2end-absa", # replace with your repo id
aggregation_strategy="simple", # aggregates sub-tokens into word-level entities
)
text = "The user interface is brilliant, but the documentation is a total mess."
preds = nlp(text)
print(preds)
# Example entity structure:
# [{
# 'entity_group': 'B-ASP-Positive',
# 'word': 'user interface',
# 'start': 4,
# 'end': 19,
# 'score': 0.98
# }, {
# 'entity_group': 'B-ASP-Negative',
# 'word': 'documentation',
# 'start': 41,
# 'end': 54,
# 'score': 0.99
# }]
def postprocess_entities(entities):
aspects = []
for ent in entities:
label = ent["entity_group"] # e.g. B-ASP-Positive or I-ASP-Positive
parts = label.split("-")
# Expected formats: O, B-ASP-<SENT>, I-ASP-<SENT>
if label == "O":
continue
prefix, _, sentiment = parts[0], parts[1], parts[2]
aspects.append({
"aspect": ent["word"],
"sentiment": sentiment,
"start": int(ent["start"]),
"end": int(ent["end"]),
"score": float(ent.get("score", 0.0)),
})
return aspects
aspects = postprocess_entities(preds)
print(aspects)
The aspect sentiment analysis performance can be improved by the joint aspect term extraction and aspect sentiment classification. Find the example here
You can deploy a simple REST service using FastAPI:
python serve_singlehead_api.py --model yangheng/deberta-v3-base-end2end-absa --host 0.0.0.0 --port 8000
Predict:
curl -X POST http://localhost:8000/predict \
-H "Content-Type: application/json" \
-d '{"text":"The user interface is brilliant, but the documentation is a total mess."}'
Notes:
start
/end
are character offsets in the original string.aggregation_strategy='simple'
merges sub-tokens into word-level spans. Set to none|first|average|max
as needed.microsoft/deberta-v3-base
O
, B-ASP-{Positive|Negative|Neutral}
, I-ASP-{Positive|Negative|Neutral}
id2label/label2id
for native pipeline compatibility and Hub Inference API.MIT