Views
No views yet
1from transformers import GPT2LMHeadModel, GPT2Tokenizer
2
3# Load model and tokenizer
4model = GPT2LMHeadModel.from_pretrained("AshishKJain/gpt2-large-alpaca-sft")
5tokenizer = GPT2Tokenizer.from_pretrained("AshishKJain/gpt2-large-alpaca-sft")
6
7# Prepare prompt in Alpaca format
8prompt = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
9
10### Instruction:
11Give three tips for staying healthy.
12### Response:
13"""
14
15# Generate response
16inputs = tokenizer(prompt, return_tensors="pt")
17outputs = model.generate(
18 inputs['input_ids'],
19 max_new_tokens=256,
20 temperature=0.7,
21 top_k=50,
22 do_sample=True,
23 pad_token_id=tokenizer.eos_token_id,
24)
25response = tokenizer.decode(outputs[0], skip_special_tokens=True)
26print(response)Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
[Your instruction here]
### Response:Below is an instruction that describes a task. Write a response that appropriately completes the request.
### Instruction:
[Your instruction here]
### Input:
[Additional context]
### Response:1@misc{gpt2-large-alpaca,
2 author = {Ashish K Jain},
3 title = {GPT2-Large Fine-tuned on Alpaca Instructions},
4 year = {2025},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/AshishKJain/gpt2-large-alpaca-sft}}
7}