Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4model_id = "mzbac/Phi-3-mini-4k-grammar-correction"
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": "user",
15 "content": "Please correct, polish, or translate the text delimited by triple backticks to standard English.",
16 },
17 {
18 "role": "user",
19 "content": "Text=```neither 经理或员工 has been informed about the meeting```",
20 },
21]
22
23input_ids = tokenizer.apply_chat_template(
24 messages, add_generation_prompt=True, return_tensors="pt"
25).to(model.device)
26
27terminators = [tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|end|>")]
28
29outputs = model.generate(
30 input_ids,
31 max_new_tokens=256,
32 eos_token_id=terminators,
33 do_sample=True,
34 temperature=0.1,
35)
36response = outputs[0]
37print(tokenizer.decode(response))
38
39# <s><|user|> Please correct, polish, or translate the text delimited by triple backticks to standard English.<|end|><|assistant|>
40# <|user|> Text=```neither 经理或员工 has been informed about the meeting```<|end|>
41# <|assistant|> Output=Neither the manager nor the employee has been informed about the meeting.<|end|>