Views
No views yet
transformers library from Hugging Face:1from transformers import BertForQuestionAnswering, BertTokenizer
2
3# Load the tokenizer and model
4tokenizer = BertTokenizer.from_pretrained("Abdo36/Bert-SquAD-QA")
5model = BertForQuestionAnswering.from_pretrained("Abdo36/Bert-SquAD-QA")
6
7context = "BERT is a method of pre-training language representations."
8question = "What is BERT?"
9
10inputs = tokenizer.encode_plus(question, context, return_tensors="pt")
11
12# Perform inference
13outputs = model(**inputs)
14start_scores = outputs.start_logits
15end_scores = outputs.end_logits
16
17# Extract answer
18start_index = start_scores.argmax()
19end_index = end_scores.argmax()
20answer = tokenizer.decode(inputs["input_ids"][0][start_index:end_index + 1])
21
22print("Answer:", answer)1@article{devlin2018bert,
2 title={BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding},
3 author={Devlin, Jacob and Chang, Ming-Wei and Lee, Kenton and Toutanova, Kristina},
4 journal={arXiv preprint arXiv:1810.04805},
5 year={2018}
6}