Implementation of a T5 transformer model for UConn Q&A
from transformers import T5Tokenizer, T5ForConditionalGeneration
# Load the saved model and tokenizer
model_path = "saved_model_t5_normal_complete"
tokenizer = T5Tokenizer.from_pretrained(model_path)
model = T5ForConditionalGeneration.from_pretrained(model_path)
This code imports the necessary libraries from HuggingFace Transformers and loads a pre-trained and fine-tuned T5 model along with its tokenizer from a saved directory.
def generate_response(prompt):
input_text = "question: " + prompt
print(" ")
input_ids = tokenizer.encode(input_text, return_tensors="pt")
# Updated parameters for model.generate()
output = model.generate(
input_ids,
max_length=150,
num_return_sequences=1,
num_beams=5, # Using beam search with 5 beams
temperature=0.7, # Moderately random outputs
top_k=50, # Only consider top 50 tokens for each step
top_p=0.95 # Use nucleus sampling with p=0.95
)
response = tokenizer.decode(output[0], skip_special_tokens=True)
return response
This function takes a user prompt, prepares it for the model by adding a prefix, and then generates a response using the T5 model. The generation parameters are carefully tuned for optimal response quality:
# Test the model with user prompts
while True:
print(" ")
user_input = input("Enter your prompt (or 'quit' to exit): ")
if user_input.lower() == 'quit':
break
generated_response = generate_response(user_input)
print("Model's response:", generated_response)
This simple interactive loop allows users to input questions about UConn. The loop continues until the user types 'quit', with each prompt being processed by the model to generate an informative response.
Enter your prompt (or 'quit' to exit): when was uconn founded?
Model's response: As a UConn tour guide, I would explain that the University of Connecticut was founded in 1881 as the Storrs Agricultural School. The university was officially renamed the University of Connecticut in 1889. Over the years, the university expanded and evolved into the comprehensive research university it is today. UConn was one of the first public universities in the country to offer a wide range of academic programs and research opportunities. The founding of the university in 1881 marked the beginning of UConn's commitment to academic excellence and innovation.