Views
No views yet
1from transformers import GPT2Tokenizer, GPT2LMHeadModel
2import torch
3
4model_name = "CodeferSystem/GPT2-Hacker-password-generator"
5
6# Load the pre-trained GPT-2 model and tokenizer from the specified directory
7tokenizer = GPT2Tokenizer.from_pretrained(model_name) # Load standard GPT-2 tokenizer
8model = GPT2LMHeadModel.from_pretrained(model_name) # Load fine-tuned GPT-2 model
9
10# Function to generate an answer based on a given question
11def generate_answer(question):
12 # Create a prompt by formatting the question for the model
13 prompt = f"Question: {question}\nAnswer:"
14
15 # Encode the prompt into input token IDs suitable for the model
16 input_ids = tokenizer.encode(prompt, return_tensors="pt")
17
18 # Set the model to evaluation mode
19 model.eval()
20
21 # Generate the output without calculating gradients (for efficiency)
22 with torch.no_grad():
23 output = model.generate(
24 input_ids, # Provide the input tokens
25 max_length=50, # Set the maximum length of the generated text
26 num_return_sequences=1, # Only return one sequence of text
27 no_repeat_ngram_size=2, # Prevent repeating n-grams (sequences of n words)
28 do_sample=True, # Enable sampling (randomized generation)
29 top_k=50, # Limit the model's choices to the top 50 probable words
30 top_p=0.95, # Use nucleus sampling (the cumulative probability distribution)
31 temperature=2.0, # Control the randomness/creativity of the output
32 pad_token_id=tokenizer.eos_token_id # Specify the padding token ID (EOS token in this case)
33 )
34
35 # Decode the generated token IDs back to a string and strip any special tokens
36 generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
37
38 # Extract the part after "Answer:" to get the model's generated answer
39 answer = generated_text.split("Answer:")[-1].strip()
40
41 return answer
42
43# Example usage
44question = "generate password."
45print(generate_answer(question)) # Print the generated password