Views
No views yet
1from transformers import GPT2LMHeadModel, GPT2Tokenizer
2
3# Load model and tokenizer
4model = GPT2LMHeadModel.from_pretrained("shashanksabhlok/gpt2-instruction-finetuned-ss")
5tokenizer = GPT2Tokenizer.from_pretrained("shashanksabhlok/gpt2-instruction-finetuned-ss")
6
7# Set padding token
8tokenizer.pad_token = tokenizer.eos_token
9
10# Helper function for clean responses
11def ask_model(instruction, input_text=""):
12 if input_text:
13 prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
14
15### Instruction:
16{instruction}
17
18### Input:
19{input_text}
20
21### Response:
22"""
23 else:
24 prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
25
26### Instruction:
27{instruction}
28
29### Response:
30"""
31
32 inputs = tokenizer(prompt, return_tensors="pt")
33 outputs = model.generate(
34 inputs.input_ids,
35 max_length=200,
36 temperature=0.7,
37 do_sample=True,
38 top_k=50,
39 top_p=0.95,
40 pad_token_id=tokenizer.eos_token_id
41 )
42
43 generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
44 response = generated_text[len(prompt):].strip()
45
46 # Clean up instruction markers
47 if "###" in response:
48 response = response.split("###")[0].strip()
49
50 return response
51
52# Use it
53response = ask_model("Translate to French:", "Hello, how are you?")
54print(response)Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
Translate to French:
### Input:
Hello, how are you?
### Response:Bonjour, comment allez-vous?Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
Answer the following question:
### Input:
What is the capital of France?
### Response:The capital of France is Paris.Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
Write a haiku about:
### Input:
mountains
### Response:Peaks touch the sky high
Ancient stones stand proud and still
Snow caps gleam in sunBelow is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
Write a Python function to:
### Input:
Calculate the factorial of a number
### Response:1def factorial(n):
2 if n == 0 or n == 1:
3 return 1
4 return n * factorial(n - 1)Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
Summarize this text:
### Input:
The quick brown fox jumps over the lazy dog. This sentence contains every letter of the alphabet and is commonly used for testing purposes.
### Response:A sentence containing all alphabet letters, used for testing.Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
Convert to past tense:
### Input:
I walk to the store every day.
### Response:I walked to the store every day.