ameerazam08 commited on
Commit
2d03482
1 Parent(s): 1925fef

Create test.py

Browse files
Files changed (1) hide show
  1. test.py +29 -0
test.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Load the trained model and tokenizer
2
+ import torch
3
+ from transformers import GPT2Tokenizer, GPT2LMHeadModel,GPT2Model
4
+ from transformers import TFAutoModel
5
+
6
+ model_path = "./trained_gpt2_jokes/5/"
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+ model = GPT2LMHeadModel.from_pretrained(model_path, from_tf=True).to(device)
9
+ tokenizer = GPT2Tokenizer.from_pretrained(model_path)
10
+ # Put the model in eval mode
11
+ model.eval()
12
+ # Define a function for generating text
13
+ def generate_text(prompt_text):
14
+ # Encode the prompt text and move to GPU
15
+ input_ids = tokenizer.encode(prompt_text, return_tensors="pt").to(device)
16
+ # Generate text
17
+ with torch.no_grad():
18
+ output = model.generate(input_ids, max_length=100, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id, temperature=0.5)
19
+ # Decode the generated text
20
+ decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
21
+ return decoded_output
22
+ # Testing the model with a sample prompt
23
+ prompt = "Why man are "
24
+ generated_joke = generate_text(prompt)
25
+ print(generated_joke)
26
+
27
+ #result getting --->Why man are so upset about the new $20 bill? Because it's only worth $15 more than $20.
28
+
29
+