Views
No views yet
1%%capture
2import os
3if "COLAB_" not in "".join(os.environ.keys()):
4 !pip install unsloth
5else:
6 !pip install --no-deps bitsandbytes accelerate xformers==0.0.29.post3 peft trl==0.15.2 triton cut_cross_entropy unsloth_zoo
7 !pip install sentencepiece protobuf datasets huggingface_hub hf_transfer
8 !pip install --no-deps unsloth
9!pip install snac
10!pip install wandb
11!pip install -U datasets1from unsloth import FastLanguageModel
2import torch
3
4dtype = None
5load_in_4bit = False # Use 4bit quantization to reduce memory usage. Can be False.
6
7model, tokenizer = FastLanguageModel.from_pretrained(
8 model_name = "datatab/aida-parla-16bit-v1",
9 max_seq_length=32768,
10 dtype=dtype,
11 load_in_4bit=load_in_4bit,
12)1def generate_audio_for_prompts(prompts, chosen_voice=None, model=None, tokenizer=None):
2 import locale
3 import torch
4 from snac import SNAC
5 from IPython.display import display, Audio
6
7 locale.getpreferredencoding = lambda: "UTF-8"
8
9 if model is None or tokenizer is None:
10 raise ValueError("You must pass both model and tokenizer.")
11
12 snac_model = SNAC.from_pretrained("hubertsiuzdak/snac_24khz").to("cuda")
13
14 # Enable fast inference
15 FastLanguageModel.for_inference(model)
16
17 # Move SNAC model to CPU for decoding
18 snac_model = snac_model.to("cpu")
19
20 for prompt in prompts:
21 print(f"\n🗣 Prompt: {prompt}")
22
23 full_prompt = f"{chosen_voice}: {prompt}" if chosen_voice else prompt
24 input_ids = tokenizer(full_prompt, return_tensors="pt").input_ids
25
26 start_token = torch.tensor([[128259]], dtype=torch.int64)
27 end_tokens = torch.tensor([[128009, 128260]], dtype=torch.int64)
28
29 modified_input_ids = torch.cat([start_token, input_ids, end_tokens], dim=1)
30
31 padding = 0
32 max_len = modified_input_ids.shape[1]
33 padded_tensor = modified_input_ids
34 attention_mask = torch.ones((1, max_len), dtype=torch.int64)
35
36 input_ids = padded_tensor.to("cuda")
37 attention_mask = attention_mask.to("cuda")
38
39 generated_ids = model.generate(
40 input_ids=input_ids,
41 attention_mask=attention_mask,
42 max_new_tokens=4096,
43 do_sample=True,
44 temperature=0.2,
45 top_p=0.95,
46 repetition_penalty=1.6,
47 num_return_sequences=1,
48 eos_token_id=128258,
49 use_cache=True
50 )
51
52 token_to_find = 128257
53 token_to_remove = 128258
54
55 token_indices = (generated_ids == token_to_find).nonzero(as_tuple=True)
56 if len(token_indices[1]) > 0:
57 last_idx = token_indices[1][-1].item()
58 cropped = generated_ids[:, last_idx + 1:]
59 else:
60 cropped = generated_ids
61
62 cropped = cropped[cropped != token_to_remove]
63
64 row_length = cropped.size(0)
65 new_length = (row_length // 7) * 7
66 trimmed_row = cropped[:new_length]
67 trimmed_row = [t - 128266 for t in trimmed_row]
68
69 def redistribute_codes(code_list):
70 layer_1 = []
71 layer_2 = []
72 layer_3 = []
73 for i in range((len(code_list)+1)//7):
74 layer_1.append(code_list[7*i])
75 layer_2.append(code_list[7*i+1]-4096)
76 layer_3.append(code_list[7*i+2]-(2*4096))
77 layer_3.append(code_list[7*i+3]-(3*4096))
78 layer_2.append(code_list[7*i+4]-(4*4096))
79 layer_3.append(code_list[7*i+5]-(5*4096))
80 layer_3.append(code_list[7*i+6]-(6*4096))
81 codes = [torch.tensor(layer_1).unsqueeze(0),
82 torch.tensor(layer_2).unsqueeze(0),
83 torch.tensor(layer_3).unsqueeze(0)]
84
85 # codes = [c.to("cuda") for c in codes]
86 audio_hat = snac_model.decode(codes)
87 return audio_hat
88
89 try:
90 samples = redistribute_codes(trimmed_row)
91 display(Audio(samples.detach().squeeze().to("cpu").numpy(), rate=24000))
92 except Exception as e:
93 print("❌ Error decoding audio:", str(e))
94
95 # Clean up
96 del samples1prompts = [
2 "Novi Sad je Evropska prestonica kulture,posle Beograda,drugi grad u Srbiji po broju stanovnika.",
3 "Kragujevac je gradsko naselje i sedište istoimene teritorijalne jedinice u Srbiji.",
4 "Gost emisije Među nama bio je Robert Kozma iz Zeleno levog fronta.",
5 "On je čovek brojnih zanimanja, kojima je zajednička kreativnost. On je i satiričar i glumac i muzičar, imitator.",
6]
7
8chosen_voice = "alek" # glasovi: "mila", "senka", "judita", "saska", "goga", "alek", "arsa", "janko", "bora", "zoki"
9generate_audio_for_prompts(prompts, chosen_voice, model=model, tokenizer=tokenizer)