File size: 4,452 Bytes
25b4156
502a2a0
 
 
 
 
 
1b86984
502a2a0
 
25b4156
b434646
25b4156
99ec6fb
 
25b4156
502a2a0
25b4156
502a2a0
25b4156
502a2a0
538c4bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6e3348
538c4bd
 
 
 
 
 
d6e3348
538c4bd
 
 
 
 
 
 
 
 
 
 
 
 
d6e3348
9112429
 
 
2c51ca9
 
9112429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---
license: mit
language:
- en
base_model:
- google-t5/t5-base
datasets:
- li2017dailydialog/daily_dialog
metrics:
- rouge
---
# T5-base-ddg

This model is a fine-tuned version of `T5` for open eneded dialog generation. It was finetuned on the Daily Dialog dataset for 35 epochs using 
Cyclic attention and custom loss.

## Model Usage

Below is an example of how to load and use this model for summarization:

```python
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration

# Set the device (use GPU if available)
device = 'cuda' if torch.cuda.is_available() else 'cpu'

# Load the model and tokenizer from Hugging Face
tokenizer = T5Tokenizer.from_pretrained("Vijayendra/T5-base-ddg")
model = T5ForConditionalGeneration.from_pretrained("Vijayendra/T5-base-ddg").to(device)

# Define your prompts
input_prompts = [
    "I am having a bad day at work",
    "What should I do about my stress?",
    "How can I improve my productivity?",
    "I'm feeling very anxious today",
    "What is the best way to learn new skills?",
    "How do I deal with failure?",
    "What do you think about the future of technology?",
    "I want to improve my communication skills",
    "How can I stay motivated at work?",
    "What is the meaning of life?"
]

# Generate responses
generated_responses = {}
for prompt in input_prompts:
    inputs = tokenizer(prompt, return_tensors="pt", max_length=40, truncation=True, padding="max_length").to(device)
    
    model.eval()
    with torch.no_grad():
        generated_ids = model.generate(
            input_ids=inputs['input_ids'],
            attention_mask=inputs['attention_mask'],
            max_length=100,
            num_beams=7,
            repetition_penalty=2.5,
            length_penalty=2.0,
            early_stopping=True
        )

    # Decode the generated response
    generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
    generated_responses[prompt] = generated_text

# Display the input prompts and the generated responses
for prompt, response in generated_responses.items():
    print(f"Prompt: {prompt}")
    print(f"Response: {response}\n")





from transformers import T5Tokenizer, T5ForConditionalGeneration
from datasets import load_dataset
import torch

# Set the device (use GPU if available)
device = 'cuda' if torch.cuda.is_available() else 'cpu'

# Load your T5 model and tokenizer
tokenizer = T5Tokenizer.from_pretrained("Vijayendra/T5-base-ddg")
model = T5ForConditionalGeneration.from_pretrained("Vijayendra/T5-base-ddg").to(device)

# Load the dataset - Replace with your dataset name
dataset = load_dataset('daily_dialog', split='test')

# Generate 10 responses from the test set
def generate_responses(dataset, num_responses=50):
    responses = []
    for i, data in enumerate(dataset):
        if i >= num_responses:
            break

        # Get the input prompt and reference response
        input_text = data['dialog'][0]  # Assuming the first dialog is the input prompt
        reference_text = data['dialog'][1]  # Assuming the second dialog is the expected response
        
        # Tokenize and generate response
        inputs = tokenizer(input_text, return_tensors="pt", max_length=40, truncation=True, padding="max_length").to(device)
        model.eval()
        with torch.no_grad():
            generated_ids = model.generate(
                input_ids=inputs['input_ids'],
                attention_mask=inputs['attention_mask'],
                max_length=40,
                num_beams=7,
                repetition_penalty=2.5,
                length_penalty=2.0,
                early_stopping=True
            )
        
        # Decode generated response
        generated_text = tokenizer.decode(generated_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
        
        # Append input, generated response, and reference
        responses.append({
            "Input Prompt": input_text,
            "Generated Response": generated_text,
            "Reference Response": reference_text
        })

    return responses

# Get the responses
responses = generate_responses(dataset)

# Print the results
for idx, response in enumerate(responses):
    print(f"Prompt {idx+1}: {response['Input Prompt']}")
    print(f"T5 Model Response: {response['Generated Response']}")
    print(f"Reference Response: {response['Reference Response']}\n")