Views
No views yet

1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4model_id = "kmaurinjones/grammar-llama-3.2-1B"
5tokenizer = AutoTokenizer.from_pretrained(model_id)
6model = AutoModelForCausalLM.from_pretrained(
7 model_id,
8 torch_dtype=torch.bfloat16,
9 device_map="auto"
10)
11
12messages = [
13 {
14 "role": "system",
15 "content": "",
16 },
17 {
18 "role": "user",
19 "content": """
20 # Instructions
21 - Revise the following transcript in the ways outlined below
22 - Return exactly the revised text and nothing else
23 - Use the custom vocabulary of terms to inform your revision
24
25 # Revisions
26 - Correct grammar
27 - Correct punctuation
28 - Correct spelling
29 - Correct word choice
30
31 # Vocabulary
32 {custom vocabulary terms, separated by newline}
33
34 # Transcript
35 {transcript}
36 """
37 }
38]
39
40input_ids = tokenizer.apply_chat_template(
41 messages,
42 add_generation_prompt=True,
43 return_tensors="pt"
44).to(model.device)
45
46terminators = [
47 tokenizer.eos_token_id,
48 tokenizer.convert_tokens_to_ids("<|eot_id|>")
49]
50
51outputs = model.generate(
52 input_ids,
53 # max_new_tokens=256,
54 eos_token_id=terminators,
55 do_sample=True,
56 temperature=0.1,
57)
58response = outputs[0]
59print(tokenizer.decode(response))
60
61# Example of Model Input
62"""
63<|begin_of_text|><|start_header_id|>system<|end_header_id|>
64
65Cutting Knowledge Date: December 2023
66Today Date: 26 July 2024
67
68<|eot_id|><|start_header_id|>user<|end_header_id|>
69
70# Instructions
71- Revise the following transcript in the ways outlined below
72- Return exactly the revised text and nothing else
73- Use the custom vocabulary of terms to inform your revision
74
75# Revisions
76- Correct grammar
77- Correct punctuation
78- Correct spelling
79- Correct word choice
80
81# Vocabulary
82Seljukid
83Turbessel
84Gaziantep
85Ahlat
86
87# Transcript
88in 1111, he was invited to participate in a seljookid campaign. with his troops he joined the main seljookid army. but during the siege of turbessle he died in august 1111. his coffin was sent to ahlat.<|eot_id|><|start_header_id|>assistant<|end_header_id|>
89"""
90
91# Example of Model Output
92"""
93In 1111, he was invited to participate in a Seljukid campaign. With his troops he joined the main Seljukid army. But during the siege of Turbessel he died in August 1111. His coffin was sent to Ahlat.<|eot_id|>
94"""