File size: 2,374 Bytes
f7c2514
 
1ae6559
 
 
 
f7c2514
 
1ae6559
 
b8e7134
1ae6559
 
 
 
 
 
 
 
 
3ce0c32
1ae6559
 
 
 
 
 
 
 
 
 
 
 
 
fd9e32a
 
1ae6559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
library_name: transformers
tags:
  - japanese
  - ner
  - medical
---

# Model Card for `Tomohiro/MedTXTNER`

**本モデルは、日本語医療テキストの NER(固有表現抽出)タスク向けに `cl-tohoku/bert-base-japanese-v3` をファインチューニングしたモデルです。**

## モデル詳細

### 説明
- ベースに `cl-tohoku/bert-base-japanese-v3`を使用  
- 奈良先端大で作成された日本語医療テキストのアノテーション付きデータ(症例報告、読影レポート、看護記録)でファインチューニングを実施

| 項目                    | 詳細                                   |
|-------------------------|----------------------------------------|
| **Developed by**        | NAIST ソーシャルコンピューティング研究室       |
| **Model type**          | Token classification                  |
| **Language(s)**         | Japanese                               |
| **Finetuned from**      | cl-tohoku/bert-base-japanese-v3        |

### モデルソース
- **Hub リポジトリ**: https://huggingface.co/Tomohiro/MedTXTNER

## 利用方法

```python
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification

model_dir = "Tomohiro/MedTXTNER"
model     = AutoModelForTokenClassification.from_pretrained(model_dir)
tokenizer = AutoTokenizer.from_pretrained(checkpoint_dir, use_fast=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()

def predict_text(text: str):
    enc = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        padding="max_length",
        max_length=512,
        is_split_into_words=False
    ).to(device)

    with torch.no_grad():
        outputs = model(**enc)
        logits  = outputs.logits

    pred_ids = torch.argmax(logits, dim=-1)[0].cpu().tolist()
    tokens = tokenizer.convert_ids_to_tokens(enc["input_ids"][0])
    id2label = model.config.id2label

    result = []
    for tok, pid in zip(tokens, pred_ids):
        if tok in tokenizer.all_special_tokens:
            continue
        result.append((tok, id2label[pid]))
    return result

sample = "症例】53歳女性。発熱と嘔気を認め、プレドニゾロンを中断しました。"
for tok, lab in predict_text(sample):
    print(f"{tok}\t{lab}")