from snac import SNAC
import torch
import torch
from transformers import AutoModelForCausalLM, Trainer, TrainingArguments, AutoTokenizer
import numpy as np
import soundfile as sf
import IPython.display as ipd
import librosa
from ipywebrtc import AudioRecorder, Audio
from IPython.display import display
import ipywidgets as widgets
from huggingface_hub import snapshot_download
import torchaudio.transforms as T
import librosa
import torch
from IPython.display import Audio, display
device = "cuda" if torch.cuda.is_available() else "mps" #or cpu if you aren't on an M type mac
print(device)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)
model.to(device)
CHANGE THIS TO YOUR OWN FILE AND TEXT
my_wav_file_is = "/content/drive/MyDrive/Colab Notebooks/short15s_sezen_aksu.wav"
and_the_transcript_is = "Ayşeciğin filmi var zeynep değirmencioğlunun hafızasını kaybediyor yolda birileri buluyorlar "
the_model_should_say = [
"Hayat, her gün karşımıza yeni fırsatlar ve zorluklar çıkarır. Önemli olan, bu anları nasıl değerlendirdiğimizdir. Bazen küçük bir adım bile büyük değişimlerin başlangıcı olabilir. Her sabah yeni bir başlangıçtır; dünü geride bırakıp bugünü en iyi şekilde değerlendirmek elimizde. İnsan, hedeflerine ulaşmak için kararlılıkla ilerlemeli ve karşılaştığı engellerden yılmadan yoluna devam etmelidir."
]
#@title Tokenising your stuff for the prompt
''' Here we tokenise the prompt you gave us, we also tokenise the prompts you want the model to say
The template is:
start_of_human, start_of_text, text, end_of_text, start_of_ai, start_of_speech, speech, end_of_speech, end_of_ai, start_of_human, text, end_of_human and then generate from here
with torch.inference_mode():
codes = snac_model.encode(waveform)
all_codes = []
for i in range(codes[0].shape[1]):
all_codes.append(codes[0][0][i].item()+128266)
all_codes.append(codes[1][0][2i].item()+128266+4096)
all_codes.append(codes[2][0][4i].item()+128266+(24096))
all_codes.append(codes[2][0][(4i)+1].item()+128266+(34096))
all_codes.append(codes[1][0][(2i)+1].item()+128266+(44096))
all_codes.append(codes[2][0][(4i)+2].item()+128266+(54096))
all_codes.append(codes[2][0][(4i)+3].item()+128266+(6*4096))
mask = cropped_tensor != token_to_remove
processed_rows = []
for row in cropped_tensor:
# Apply the mask to each row
masked_row = row[row != token_to_remove]
processed_rows.append(masked_row)
code_lists = []
for row in processed_rows:
# row is a 1D tensor with its own length
row_length = row.size(0)
new_length = (row_length // 7) * 7 # largest multiple of 7 that fits in this row
trimmed_row = row[:new_length]
trimmed_row = [t - 128266 for t in trimmed_row]
code_lists.append(trimmed_row)