Finished working version of our AI Question Answering Chat Bot:
import torch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
# Model and tokenizer initialization for a question answering task using RoBERTa
model_name = "deepset/roberta-base-squad2" # Example of a RoBERTa model fine-tuned on SQuAD v2
student_model = AutoModelForQuestionAnswering.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Device configuration and evaluation mode
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
student_model.to(device)
student_model.eval()
# Context text for the model to reference
text = """
The early vacuum tube computers, also known as first-generation computers, used a variety of memory technologies before settling on magnetic-core memory. The Atanasoff-Berry computer of 1942 stored numerical values as binary numbers in a revolving mechanical drum, with a special circuit to refresh this "dynamic" memory on every revolution. The war-time ENIAC, developed in 1946, could store 20 numbers, but the vacuum-tube registers used were too expensive to build to store more than a few numbers. A stored-program computer was out of reach until an economical form of memory could be developed.
The core memory used on the 1103 had an access time of 10 microseconds. The 1950s saw the evolution of the electronic computer from a research project to a commercial product, with common designs and multiple copies made, thereby starting a major new industry. The early commercial machines used vacuum tubes and a variety of memory technologies, converging on magnetic core by the end of the decade.
Magnetic-Core Memory
Magnetic-core memory was a significant development in computer memory technology. It was first used in the Whirlwind computer in 1949 and later in the IBM 705, a vacuum tube-based computer delivered in 1955. In 1976, 95% of all computer main memories consisted of ferrite cores, with 20-30 billion of them being produced yearly worldwide.
Transition from Vacuum Tubes to Transistors
Vacuum tubes were vital components of early computers, with the ENIAC containing an impressive 17,500 vacuum tubes. However, in 1954, Bell Labs built the first computer that didn't use vacuum tubes, the transistorized "TRADIC" computer for the U.S. Air Force. This marked the beginning of the transition from vacuum tubes to transistors in computer technology.
Role of Vacuum Tubes
Vacuum tubes were extensively used in early, first-generation computers for logical calculations and as a way to store computer memory. They were used in many electronic devices, including radios, telephone networks, sound recording/amplification & reproduction, radar, televisions, and computers.
Replacement of Magnetic Drums and Vacuum Tube Storage
Core memory, which could only be made by hand while looking through a microscope, replaced magnetic drums and volatile vacuum tube storage in the 1960s when it became cheap enough to manufacture.
In summary, early vacuum tube computers used a variety of memory technologies before settling on magnetic-core memory, which played a crucial role in the development of computer memory technology. The transition from vacuum tubes to transistors marked a significant shift in computer technology.
"""
# Function to answer questions based on the provided context
def answer_question(question, context):
# Encoding the question and context, maxing out the context for the model's limits
model_max_length = tokenizer.model_max_length
inputs = tokenizer.encode_plus(
question, context,
add_special_tokens=True,
return_tensors="pt",
truncation=True,
max_length=model_max_length # Using the max length that the model can handle
)
inputs = {key: val.to(device) for key, val in inputs.items()}
# Performing inference
with torch.no_grad():
outputs = student_model(**inputs)
# Extracting the scores for the start and end of the answer
answer_start_scores, answer_end_scores = outputs.start_logits, outputs.end_logits
# Identifying the tokens with the highest start and end scores
answer_start = torch.argmax(answer_start_scores)
answer_end = torch.argmax(answer_end_scores) + 1
# Ensuring valid answer span
if answer_end <= answer_start:
return "The answer span is invalid."
# Decoding the tokens into a string
answer = tokenizer.convert_tokens_to_string(
tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][answer_start:answer_end])
)
return answer
# Interactive loop for user to ask questions
while True:
user_input = input("Ask a question (type 'exit' to quit): ")
if user_input.lower() == 'exit':
break
try:
# Getting the answer to the question from the model
response = answer_question(user_input, text)
print("Answer:", response)
except Exception as e:
# If an error occurs, print the error message
print("Error:", e)