Views
No views yet
T5 for open eneded dialog generation. It was finetuned on the Daily Dialog dataset for 35 epochs using
Cyclic attention and custom loss.1import torch
2from transformers import T5Tokenizer, T5ForConditionalGeneration
3from datasets import load_dataset # Added import
4
5# Set the device (use GPU if available)
6device = 'cuda' if torch.cuda.is_available() else 'cpu'
7
8# Load the model and tokenizer from Hugging Face
9tokenizer = T5Tokenizer.from_pretrained("Vijayendra/T5-base-ddg")
10model = T5ForConditionalGeneration.from_pretrained("Vijayendra/T5-base-ddg").to(device)
11
12# Define your prompts
13input_prompts = [
14 "I am having a bad day at work",
15 "What should I do about my stress?",
16 "How can I improve my productivity?",
17 "I'm feeling very anxious today",
18 "What is the best way to learn new skills?",
19 "How do I deal with failure?",
20 "What do you think about the future of technology?",
21 "I want to improve my communication skills",
22 "How can I stay motivated at work?",
23 "What is the meaning of life?"
24]
25
26# Generate responses
27generated_responses = {}
28for prompt in input_prompts:
29 inputs = tokenizer(prompt, return_tensors="pt", max_length=40, truncation=True, padding="max_length").to(device)
30
31 model.eval()
32 with torch.no_grad():
33 generated_ids = model.generate(
34 input_ids=inputs['input_ids'],
35 attention_mask=inputs['attention_mask'],
36 max_length=100,
37 num_beams=7,
38 repetition_penalty=2.5,
39 length_penalty=2.0,
40 early_stopping=True
41 )
42
43 # Decode the generated response
44 generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
45 generated_responses[prompt] = generated_text
46
47# Display the input prompts and the generated responses
48for prompt, response in generated_responses.items():
49 print(f"Prompt: {prompt}")
50 print(f"Response: {response}\n")
51
52
53# Load the dataset - Replace with your dataset name
54dataset = load_dataset('daily_dialog', split='test',trust_remote_code=True)
55
56# Generate 10 responses from the test set
57def generate_responses(dataset, num_responses=50):
58 responses = []
59 for i, data in enumerate(dataset):
60 if i >= num_responses:
61 break
62
63 # Get the input prompt and reference response
64 input_text = data['dialog'][0] # Assuming the first dialog is the input prompt
65 reference_text = data['dialog'][1] # Assuming the second dialog is the expected response
66
67 # Tokenize and generate response
68 inputs = tokenizer(input_text, return_tensors="pt", max_length=40, truncation=True, padding="max_length").to(device)
69 model.eval()
70 with torch.no_grad():
71 generated_ids = model.generate(
72 input_ids=inputs['input_ids'],
73 attention_mask=inputs['attention_mask'],
74 max_length=40,
75 num_beams=7,
76 repetition_penalty=2.5,
77 length_penalty=2.0,
78 early_stopping=True
79 )
80
81 # Decode generated response
82 generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
83
84 # Append input, generated response, and reference
85 responses.append({
86 "Input Prompt": input_text,
87 "Generated Response": generated_text,
88 "Reference Response": reference_text
89 })
90
91 return responses
92
93# Get the responses
94responses = generate_responses(dataset)
95
96# Print the results
97for idx, response in enumerate(responses):
98 print(f"Prompt {idx+1}: {response['Input Prompt']}")
99 print(f"T5 Model Response: {response['Generated Response']}")
100 print(f"Reference Response: {response['Reference Response']}\n")
101
102