Need help with gpt2 model
I am finetuning gpt2 model to answer questions with given faq.json.Code -import torchfrom torch.utils.data import Dataset, DataLoaderfrom transformers import GPT2LMHeadModel, GPT2Tokenizer, GPT2Configclass FAQDataset(Dataset):definit(self, data_file, tokenizer):self.tokenizer = tokenizerself.inputs =self.targets =with open(data_file, 'r') as file:
        lines = file.readlines()
        
        for i in range(0, len(lines)-1, 2):
            question = lines[i].strip()
            answer = lines[i+1].strip()
            self.inputs.append(question)
            self.targets.append(answer)

def __len__(self):
    return len(self.inputs)

def __getitem__(self, index):
    inputs = self.tokenizer.encode(self.inputs[index], add_special_tokens=True)
    targets = self.tokenizer.encode(self.targets[index], add_special_tokens=True)
    return torch.tensor(inputs), torch.tensor(targets)Load the GPT-2 tokenizer and modeltokenizer = GPT2Tokenizer.from_pretrained(‘gpt2’)model = GPT2LMHeadModel.from_pretrained(‘gpt2’)Load the training datasetdataset = FAQDataset(‘faq.txt’, tokenizer)Define the training parametersbatch_size = 4num_epochs = 3learning_rate = 1e-5Create the data loaderdata_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)Set the model in training modemodel.train()Define the optimizer and the loss functionoptimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate)criterion = torch.nn.CrossEntropyLoss(ignore_index=tokenizer.pad_token_id)Fine-tune the modelfor epoch in range(num_epochs):total_loss = 0for inputs, targets in data_loader:
    optimizer.zero_grad()

    # Forward pass
    outputs = model(inputs, labels=targets)
    loss = criterion(outputs.logits.view(-1, tokenizer.vocab_size), targets.view(-1))

    # Backward pass and optimization
    loss.backward()
    optimizer.step()

    total_loss += loss.item()

avg_loss = total_loss / len(data_loader)
print(f"Epoch {epoch+1}/{num_epochs}, Loss: {avg_loss}")Save the fine-tuned modelmodel.save_pretrained(‘fine-tuned-gpt2’)tokenizer.save_pretrained(‘fine-tuned-gpt2’)prompt-import torchfrom transformers import GPT2LMHeadModel, GPT2TokenizerLoad the fine-tuned model and tokenizermodel = GPT2LMHeadModel.from_pretrained(‘fine-tuned-gpt2’)tokenizer = GPT2Tokenizer.from_pretrained(‘fine-tuned-gpt2’)Set the model to evaluation modemodel.eval()User inputuser_question = “What is your refund policy?”Generate the answer using the fine-tuned modelinput_ids = tokenizer.encode(f"Q: {user_question}\nA:", return_tensors=‘pt’)output = model.generate(input_ids, max_length=100, num_return_sequences=1)generated_answer = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)print(generated_answer)Answer generated is !!! !!!Any help please?