sjrhuschlee
commited on
Commit
路
578108e
1
Parent(s):
290992f
Update README.md
Browse files
README.md
CHANGED
@@ -182,20 +182,21 @@ res = nlp(qa_input)
|
|
182 |
# {'score': 0.984, 'start': 30, 'end': 37, 'answer': ' London'}
|
183 |
|
184 |
# b) Load model & tokenizer
|
185 |
-
model = AutoModelForQuestionAnswering.from_pretrained(
|
|
|
|
|
186 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
187 |
|
188 |
question = f'{tokenizer.cls_token}Where do I live?' # '<cls>Where do I live?'
|
189 |
context = 'My name is Sarah and I live in London'
|
190 |
encoding = tokenizer(question, context, return_tensors="pt")
|
191 |
-
|
192 |
encoding["input_ids"],
|
193 |
-
attention_mask=encoding["attention_mask"]
|
194 |
-
return_dict=False
|
195 |
)
|
196 |
|
197 |
all_tokens = tokenizer.convert_ids_to_tokens(encoding["input_ids"][0].tolist())
|
198 |
-
answer_tokens = all_tokens[torch.argmax(
|
199 |
answer = tokenizer.decode(tokenizer.convert_tokens_to_ids(answer_tokens))
|
200 |
# 'London'
|
201 |
```
|
|
|
182 |
# {'score': 0.984, 'start': 30, 'end': 37, 'answer': ' London'}
|
183 |
|
184 |
# b) Load model & tokenizer
|
185 |
+
model = AutoModelForQuestionAnswering.from_pretrained(
|
186 |
+
model_name, trust_remote_code=True
|
187 |
+
)
|
188 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
189 |
|
190 |
question = f'{tokenizer.cls_token}Where do I live?' # '<cls>Where do I live?'
|
191 |
context = 'My name is Sarah and I live in London'
|
192 |
encoding = tokenizer(question, context, return_tensors="pt")
|
193 |
+
output = model(
|
194 |
encoding["input_ids"],
|
195 |
+
attention_mask=encoding["attention_mask"]
|
|
|
196 |
)
|
197 |
|
198 |
all_tokens = tokenizer.convert_ids_to_tokens(encoding["input_ids"][0].tolist())
|
199 |
+
answer_tokens = all_tokens[torch.argmax(output["start_logits"]):torch.argmax(output["end_logits"]) + 1]
|
200 |
answer = tokenizer.decode(tokenizer.convert_tokens_to_ids(answer_tokens))
|
201 |
# 'London'
|
202 |
```
|