<jupyter_start><jupyter_text><jupyter_code>!pip install transformers
import torch
from transformers import BertForQuestionAnswering
from transformers import BertTokenizer

#Model
model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')

#Tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')
#Example question and paragraph related to it
question = '''Who owns Chelsea?"'''

paragraph = '''Chelsea Football Club are an English professional football club based in Fulham, London. Founded in 1905, the club competes in the Premier League, the top division of English football. Chelsea are among England most successful clubs, having won over thirty competitive honours, including six league titles and six European trophies. Their home ground is Stamford Bridge. Chelsea won their first major honour, the League Championship, in 1955. The club won the FA Cup for the first time in 1970 and their first European honour, the UEFA Cup Winners Cup, in 1971. After a period of decline in the late 1970s and 1980s, the club enjoyed a revival in the 1990s and had more success in cup competitions. The past two decades have been the most successful in Chelsea history: they won five of its six league titles and the UEFA Champions League during this period. Chelsea are one of five clubs to have won all three of UEFA's main club competitions, and the only London club to have won the Champions League. Chelsea home kit colours are royal blue shirts and shorts with white socks. The club crest features a ceremonial lion rampant regardant holding a staff. The club has rivalries with neighbouring teams Arsenal and Tottenham Hotspur, and a historic rivalry with Leeds United. Based on attendance figures, the club has the sixth-largest fanbase in England. In terms of club value, Chelsea are the sixth most valuable football club in the world, worth £2.13 billion, and are the eighth highest-earning football club in the world. Since 2003, Chelsea have been owned by Russian billionaire <NAME>.'''
            
encoding = tokenizer.encode_plus(text=question,text_pair=paragraph)

inputs = encoding['input_ids']  #Token embeddings
sentence_embedding = encoding['token_type_ids']  #Segment embeddings
tokens = tokenizer.convert_ids_to_tokens(inputs) #input tokens
#predict answer from model
start_scores, end_scores = model(input_ids=torch.tensor([inputs]), token_type_ids=torch.tensor([sentence_embedding]))
start_index = torch.argmax(start_scores)

end_index = torch.argmax(end_scores)

answer = ' '.join(tokens[start_index:end_index+1])
answer
# in cases where certain words are filled with special characters do to BERT's internal encoding, this is just removing those characters
corrected_answer = ''

for word in answer.split():
    
    #If it's a subword token
    if word[0:2] == '##':
        corrected_answer += word[2:]
    else:
        corrected_answer += ' ' + word
corrected_answer<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06258278145695365, 'fraction_numerical': 0.012913907284768211, 'mean_word_length': 5.228865979381443, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '373605', 'license': 'Unlicense', 'n_tokens_mistral': 825, 'n_tokens_neox': 760, 'n_words': 365, 'path': 'BERT_QA_Bot_(1).ipynb', 'repo_name': 'dheerajjoshim/machinelearningcourse', 'size': '3030'}