Views
No views yet
1import torch
2from peft import PeftModel
3from transformers import AutoModelForCausalLM, AutoProcessor
4
5device = "cuda" if torch.cuda.is_available() else "cpu"
6
7processor = AutoProcessor.from_pretrained("google/gemma-3n-E2B-it", device_map="auto")
8base_model = AutoModelForCausalLM.from_pretrained("google/gemma-3n-E2B-it")
9model = PeftModel.from_pretrained(
10 base_model, "JacobLinCool/gemma-3n-E2B-transcribe-zh-tw-1"
11).to(device)
12
13
14def trascribe(model, processor, audio):
15 messages = [
16 {
17 "role": "system",
18 "content": [
19 {
20 "type": "text",
21 "text": "You are an assistant that transcribes speech accurately.",
22 }
23 ],
24 },
25 {
26 "role": "user",
27 "content": [
28 {"type": "audio", "audio": audio},
29 {"type": "text", "text": "Transcribe this audio."},
30 ],
31 },
32 ]
33
34 input_ids = processor.apply_chat_template(
35 messages,
36 add_generation_prompt=True,
37 tokenize=True,
38 return_dict=True,
39 return_tensors="pt",
40 )
41 input_ids = input_ids.to(device, dtype=model.dtype)
42
43 model.eval()
44 with torch.no_grad():
45 outputs = model.generate(**input_ids, max_new_tokens=128)
46
47 prediction = processor.batch_decode(
48 outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False
49 )[0]
50 prediction = prediction.split("\nmodel\n")[-1].strip()
51 return prediction
52
53
54if __name__ == "__main__":
55 prediction = trascribe(model, processor, "/workspace/audio.mp3")
56 print(prediction)
571@misc{vonwerra2022trl,
2 title = {{TRL: Transformer Reinforcement Learning}},
3 author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
4 year = 2020,
5 journal = {GitHub repository},
6 publisher = {GitHub},
7 howpublished = {\url{https://github.com/huggingface/trl}}
8}