Hello, I have a code, I do not know what else I need to configure so that I can get answers to my questions. the answers are wrong. please help me to make the model answer the questions I have trained. the end goal is to run this as a tech support service so that the trained model takes care of the easy stuff.
!pip3 install transformers
import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel, AdamW
Define the questions and answers
qa_pairs = [
[‘Q: How to delete the account A: IOS: Go to settings on the app, click on Edit, Click on Logout, Delete the Account Android: Go to settings, Click on the three dots, Click on Logout, Delete the Account.’],
[‘Q: How to deactivate my account A: IOS: Go to settings on the app, click on Edit, Click on Logout, Delete the Account Android: Go to settings, Click on the three dots, Click on Logout, Delete the Account.’],
[‘Q: How i can remove my nmbr A: IOS: Go to settings on the app, click on Edit, Click on Logout, Delete the Account Android: Go to settings, Click on the three dots, Click on Logout, Delete the Account.’],
[‘Q: How to delete an account A: IOS: Go to settings on the app, click on Edit, Click on Logout, Delete the Account Android: Go to settings, Click on the three dots, Click on Logout, Delete the Account.’]
]
Load the pre-trained GPT-2 model and tokenizer
model = GPT2LMHeadModel.from_pretrained(‘gpt2’)
tokenizer = GPT2Tokenizer.from_pretrained(‘gpt2’)
text = “”
for question in qa_pairs:
text += f"{question} {tokenizer.eos_token}"
Encode the text using the tokenizer
input_ids = tokenizer.encode(text, add_special_tokens=True, return_tensors=‘pt’)
Define the optimizer and learning rate scheduler
optimizer = AdamW(model.parameters(), lr=5e-5)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.9)
Fine-tune the model
device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)
model.to(device)
model.train()
for epoch in range(15):
optimizer.zero_grad()
loss = model(input_ids.to(device), labels=input_ids.to(device))[0]
loss.backward()
optimizer.step()
scheduler.step()
if epoch % 100 == 0:
print(f"Epoch {epoch}, Loss {loss.item()}")
Generate responses to new questions
model.eval()
def generate_response(question):
# Encode the question using the tokenizer
input_ids = tokenizer.encode(question + " <|question|>", add_special_tokens=False, return_tensors=‘pt’).to(device)
# Generate the answer using the model
sample_output = model.generate(input_ids, do_sample=True, max_length=100, top_k=20, top_p=1.0)
# Decode the generated answer using the tokenizer
answer = tokenizer.decode(sample_output[0], skip_special_tokens=True)
# Split the generated answer into individual sentences
sentences = answer.split('. ')
# Look for the sentence that contains the answer to the question
for sentence in sentences:
if question in sentence:
return sentence
# If no sentence contains the answer, return the full generated answer
return answer
Example usage
question = “How to delete an account”
response = generate_response(question)
print(f"{question}\n {response}")
Answer: How to delete an account
How to delete an account <|question|>
This tool allows you to:
Remove an account
Delete an account by entering an invalid password
Delete an account by entering an invalid password
Unblock an account
Unblock an account by entering an invalid password
Unblock an account by entering an invalid password
Unblock an account by entering an invalid password
Unblock an account by entering an invalid password or by entering an invalid password