Views
No views yet

1!pip install transformers ipython
2from transformers import pipeline
3from IPython.display import Audio
4pipe = pipeline("text-to-speech", model="salihfurkaan/VoxPolska-Auralis")
5output = pipe("Cześć, jestem modelem sztucznej inteligencji mówiącym po polsku")
6Audio(output["audio"], rate=output["sampling_rate"])1!pip install --no-deps unsloth==2025.4.1 bitsandbytes unsloth_zoo trl==0.15.2
2!pip install xcodec2==0.1.5 --no-deps
3!pip install vector_quantize_pytorch
4
5from unsloth import FastLanguageModel
6import torch
7from xcodec2.modeling_xcodec2 import XCodec2Model
8import torchaudio
9import soundfile as sf
10from IPython.display import display, Audio
11from transformers import AutoTokenizer, AutoModelForCausalLM
12
13input_text = "Cześć, jestem modelem sztucznej inteligencji mówiącym po polsku."
14XCODEC2_MODEL_NAME = "HKUST-Audio/xcodec2"
15SAMPLE_RATE = 16000
16device = "cuda" if torch.cuda.is_available() else "cpu"
17
18codec_model = XCodec2Model.from_pretrained(XCODEC2_MODEL_NAME)
19codec_model = codec_model.to(device).eval()
20codec_model.to('cpu')
21
22
23tokenizer = AutoTokenizer.from_pretrained("salihfurkaan/VoxPolska-Auralis")
24model = AutoModelForCausalLM.from_pretrained("salihfurkaan/VoxPolska-Auralis")
25
26FastLanguageModel.for_inference(model)
27
28def ids_to_speech_tokens(speech_ids):
29
30 speech_tokens_str = []
31 for speech_id in speech_ids:
32 speech_tokens_str.append(f"<|s_{speech_id}|>")
33 return speech_tokens_str
34
35def extract_speech_ids(speech_tokens_str):
36
37 speech_ids = []
38 for token_str in speech_tokens_str:
39 if token_str.startswith('<|s_') and token_str.endswith('|>'):
40 num_str = token_str[4:-2]
41
42 num = int(num_str)
43 speech_ids.append(num)
44 else:
45 print(f"Unexpected token: {token_str}")
46 return speech_ids
47
48
49with torch.inference_mode():
50 with torch.amp.autocast(device,dtype=model.dtype):
51 formatted_text = f"<|TEXT_UNDERSTANDING_START|>{input_text}<|TEXT_UNDERSTANDING_END|>"
52
53 chat = [
54 {"role": "user", "content": "Convert the text to speech:" + formatted_text},
55 {"role": "assistant", "content": "<|SPEECH_GENERATION_START|>"}
56 ]
57
58 input_ids = tokenizer.apply_chat_template(
59 chat,
60 tokenize=True,
61 return_tensors='pt',
62 continue_final_message=True
63 )
64
65 speech_end_id = tokenizer.convert_tokens_to_ids('<|SPEECH_GENERATION_END|>')
66
67 # Generate the speech autoregressively
68 outputs = model.generate(
69 input_ids,
70 max_length=2048,
71 eos_token_id= speech_end_id ,
72 do_sample=True,
73 top_p=1.2, # Adjusts the diversity of generated content
74 temperature=1.2, # Controls randomness in output
75 )
76
77 generated_ids = outputs[0][input_ids.shape[1]:-1]
78 speech_tokens = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
79 speech_tokens = extract_speech_ids(speech_tokens)
80 speech_tokens = torch.tensor(speech_tokens).cpu().unsqueeze(0).unsqueeze(0)
81 gen_wav = codec_model.decode_code(speech_tokens)
82
83sf.write("output.wav", gen_wav[0, 0, :].cpu().numpy(), 16000)
84
85display(Audio(gen_wav[0, 0, :].cpu().numpy(), rate=16000))
861@misc{
2 title={salihfurkaan/VoxPolska-Auralis},
3 author={Salih Furkan Erik},
4 year={2025},
5 url={https://huggingface.co/salihfurkaan/VoxPolska-Auralis/}
6}