Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,114 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- en
|
4 |
+
- zh
|
5 |
+
tags:
|
6 |
+
- token-classification
|
7 |
+
- aspect-based-sentiment-analysis
|
8 |
+
- sentiment-analysis
|
9 |
+
- sequence-labeling
|
10 |
+
- transformers
|
11 |
+
- deberta
|
12 |
+
license: mit
|
13 |
+
pipeline_tag: token-classification
|
14 |
+
widget:
|
15 |
+
- text: The user interface is brilliant, but the documentation is a total mess.
|
16 |
+
- text: 这家餐厅的牛排很好吃,但是服务很慢。
|
17 |
+
---
|
18 |
+
|
19 |
+
# DeBERTa-v3-base End-to-End ABSA (Token Classification)
|
20 |
+
|
21 |
+
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.
|
22 |
+
|
23 |
+
## What it does
|
24 |
+
- Detects aspect terms as spans in text
|
25 |
+
- Assigns a sentiment for each detected aspect (Positive/Negative/Neutral)
|
26 |
+
- Returns character-level offsets (`start`, `end`) in the original input
|
27 |
+
|
28 |
+
## How to use (Transformers pipeline)
|
29 |
+
|
30 |
+
```python
|
31 |
+
from transformers import pipeline
|
32 |
+
|
33 |
+
nlp = pipeline(
|
34 |
+
"token-classification",
|
35 |
+
model="your-username/deberta-v3-base-end2end-absa", # replace with your repo id
|
36 |
+
aggregation_strategy="simple", # aggregates sub-tokens into word-level entities
|
37 |
+
)
|
38 |
+
|
39 |
+
text = "The user interface is brilliant, but the documentation is a total mess."
|
40 |
+
preds = nlp(text)
|
41 |
+
print(preds)
|
42 |
+
# Example entity structure:
|
43 |
+
# [{
|
44 |
+
# 'entity_group': 'B-ASP-Positive',
|
45 |
+
# 'word': 'user interface',
|
46 |
+
# 'start': 4,
|
47 |
+
# 'end': 19,
|
48 |
+
# 'score': 0.98
|
49 |
+
# }, {
|
50 |
+
# 'entity_group': 'B-ASP-Negative',
|
51 |
+
# 'word': 'documentation',
|
52 |
+
# 'start': 41,
|
53 |
+
# 'end': 54,
|
54 |
+
# 'score': 0.99
|
55 |
+
# }]
|
56 |
+
```
|
57 |
+
|
58 |
+
### Convert entities to aspect-level results
|
59 |
+
```python
|
60 |
+
def postprocess_entities(entities):
|
61 |
+
aspects = []
|
62 |
+
for ent in entities:
|
63 |
+
label = ent["entity_group"] # e.g. B-ASP-Positive or I-ASP-Positive
|
64 |
+
parts = label.split("-")
|
65 |
+
# Expected formats: O, B-ASP-<SENT>, I-ASP-<SENT>
|
66 |
+
if label == "O":
|
67 |
+
continue
|
68 |
+
prefix, _, sentiment = parts[0], parts[1], parts[2]
|
69 |
+
aspects.append({
|
70 |
+
"aspect": ent["word"],
|
71 |
+
"sentiment": sentiment,
|
72 |
+
"start": int(ent["start"]),
|
73 |
+
"end": int(ent["end"]),
|
74 |
+
"score": float(ent.get("score", 0.0)),
|
75 |
+
})
|
76 |
+
return aspects
|
77 |
+
|
78 |
+
aspects = postprocess_entities(preds)
|
79 |
+
print(aspects)
|
80 |
+
```
|
81 |
+
|
82 |
+
## FastAPI serving (optional)
|
83 |
+
|
84 |
+
You can deploy a simple REST service using FastAPI:
|
85 |
+
|
86 |
+
```bash
|
87 |
+
pip install -r dev/requirements-serving.txt
|
88 |
+
python dev/serve_singlehead_api.py --model your-username/deberta-v3-base-end2end-absa --host 0.0.0.0 --port 8000
|
89 |
+
```
|
90 |
+
|
91 |
+
Predict:
|
92 |
+
```bash
|
93 |
+
curl -X POST http://localhost:8000/predict \
|
94 |
+
-H "Content-Type: application/json" \
|
95 |
+
-d '{"text":"The user interface is brilliant, but the documentation is a total mess."}'
|
96 |
+
```
|
97 |
+
|
98 |
+
Notes:
|
99 |
+
- `start`/`end` are character offsets in the original string.
|
100 |
+
- `aggregation_strategy='simple'` merges sub-tokens into word-level spans. Set to `none|first|average|max` as needed.
|
101 |
+
|
102 |
+
## Model details
|
103 |
+
- Base model: `microsoft/deberta-v3-base`
|
104 |
+
- Task: Token classification with merged labels: `O`, `B-ASP-{Positive|Negative|Neutral}`, `I-ASP-{Positive|Negative|Neutral}`
|
105 |
+
- Training: Fine-tuned with Hugging Face Transformers. The model config includes `id2label/label2id` for native pipeline compatibility and Hub Inference API.
|
106 |
+
|
107 |
+
## Limitations
|
108 |
+
- Long texts are truncated to the maximum sequence length of the model (typically 512). Adjust during training/inference if required.
|
109 |
+
- Sentiments limited to Positive/Negative/Neutral unless retrained with extended schema.
|
110 |
+
|
111 |
+
## License
|
112 |
+
MIT
|
113 |
+
|
114 |
+
|