Views
No views yet
1import torch
2from transformers import CsmForConditionalGeneration, AutoProcessor
3import soundfile as sf
4from peft import PeftModel
5
6
7model_id = "unsloth/csm-1b"
8device = "cuda" if torch.cuda.is_available() else "cpu"
9
10
11processor = AutoProcessor.from_pretrained(model_id)
12base_model = CsmForConditionalGeneration.from_pretrained(model_id, device_map=device)
13
14model = PeftModel.from_pretrained(base_model, "khazarai/Medical-TTS")
15
16text = "Mild dorsal angulation of the distal radius reflective of the fracture."
17
18speaker_id = 0
19
20conversation = [
21 {"role": str(speaker_id), "content": [{"type": "text", "text": text}]},
22]
23audio_values = model.generate(
24 **processor.apply_chat_template(
25 conversation,
26 tokenize=True,
27 return_dict=True,
28 ).to("cuda"),
29 max_new_tokens=650,
30 # play with these parameters to tweak results
31 # depth_decoder_top_k=0,
32 # depth_decoder_top_p=0.9,
33 # depth_decoder_do_sample=True,
34 # depth_decoder_temperature=0.9,
35 # top_k=0,
36 # top_p=1.0,
37 # temperature=0.9,
38 # do_sample=True,
39 #########################################################
40 output_audio=True
41)
42audio = audio_values[0].to(torch.float32).cpu().numpy()
43sf.write("example.wav", audio, 24000)
44