Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(model): add hugging face transformer textattack/albert-base-v2-ag-news example #1783

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions example/transformer/ag_news/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Evaluate Hugging Face Transformer `textattack/albert-base-v2-ag-news` on AG News dataset
---

This example requires swcli version being above 0.4.0

```bash
$ python3 -m pip install starwhale>=0.4.0
$ git clone https://github.com/star-whale/starwhale.git
$ cd starwhale/example/text_cls_AG_NEWS
$ swcli dataset build .
$ cd ../transformer/ag_news/code
$ python3 download_model.py
$ cd ..
$ python3 -m pip install -r requirements-sw-lock.txt
$ swcli -vvv model eval --dataset ag_news/version/latest .
```
Empty file.
8 changes: 8 additions & 0 deletions example/transformer/ag_news/code/download_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained("textattack/albert-base-v2-ag-news")
model = AutoModelForSequenceClassification.from_pretrained(
"textattack/albert-base-v2-ag-news"
)
tokenizer.save_pretrained("./ag_news/models")
model.save_pretrained("./ag_news/models")
39 changes: 39 additions & 0 deletions example/transformer/ag_news/code/evaluator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from pathlib import Path

from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification

from starwhale import PipelineHandler, multi_classification

ROOTDIR = Path(__file__).parent.parent
_LABEL_NAMES = ["LABEL_0", "LABEL_1", "LABEL_2", "LABEL_3"]


class TextClassificationHandler(PipelineHandler):
def __init__(self) -> None:
super().__init__()
tokenizer = AutoTokenizer.from_pretrained(str(ROOTDIR / "models"))
model = AutoModelForSequenceClassification.from_pretrained(
str(ROOTDIR / "models")
)
self.mode = pipeline(
task="text-classification", model=model, tokenizer=tokenizer
)

def ppl(self, data: dict, **kw):
_r = self.mode(data["text"])
return _LABEL_NAMES.index(_r[0]["label"])

@multi_classification(
confusion_matrix_normalize="all",
show_hamming_loss=True,
show_cohen_kappa_score=True,
show_roc_auc=False,
all_labels=[i for i in range(0, 4)],
)
def cmp(self, ppl_result):
result, label = [], []
for _data in ppl_result:
label.append(_data["ds_data"]["label"])
result.append(_data["result"])

return label, result
15 changes: 15 additions & 0 deletions example/transformer/ag_news/model.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 1.0
name: ag_news_hg

model:
- models/config.json
- models/pytorch_model.bin
- models/special_tokens_map.json
- models/spiece.model
- models/tokenizer.json
- models/tokenizer_config.json

run:
handler: code.evaluator:TextClassificationHandler

desc: TextClassification by huggingface transformer
4 changes: 4 additions & 0 deletions example/transformer/ag_news/requirements-sw-lock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
transformers
torch
sentencepiece
protobuf==3.19.0
8 changes: 8 additions & 0 deletions example/transformer/ag_news/runtime.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
api_version: 1.1
dependencies:
- requirements-sw-lock.txt
environment:
arch: noarch
os: ubuntu:20.04
mode: venv
name: hg-textattack/albert-base-v2-ag-news