kevinwang676 commited on
Commit
3e9f9d7
1 Parent(s): 580d62a

Upload 2 files

Browse files
Files changed (2) hide show
  1. cloning/__init__.py +0 -0
  2. cloning/clonevoice.py +68 -0
cloning/__init__.py ADDED
File without changes
cloning/clonevoice.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from bark.generation import load_codec_model, generate_text_semantic, grab_best_device
2
+ from encodec.utils import convert_audio
3
+ from bark.hubert.hubert_manager import HuBERTManager
4
+ from bark.hubert.pre_kmeans_hubert import CustomHubert
5
+ from bark.hubert.customtokenizer import CustomTokenizer
6
+
7
+ import torchaudio
8
+ import torch
9
+ import os
10
+ import gradio
11
+
12
+
13
+ def clone_voice(audio_filepath, dest_filename, progress=gradio.Progress(track_tqdm=True)):
14
+ # if len(text) < 1:
15
+ # raise gradio.Error('No transcription text entered!')
16
+
17
+ use_gpu = not os.environ.get("BARK_FORCE_CPU", False)
18
+ progress(0, desc="Loading Codec")
19
+ model = load_codec_model(use_gpu=use_gpu)
20
+
21
+ # From https://github.com/gitmylo/bark-voice-cloning-HuBERT-quantizer
22
+ hubert_manager = HuBERTManager()
23
+ hubert_manager.make_sure_hubert_installed()
24
+ hubert_manager.make_sure_tokenizer_installed()
25
+
26
+ # From https://github.com/gitmylo/bark-voice-cloning-HuBERT-quantizer
27
+ # Load HuBERT for semantic tokens
28
+
29
+ # Load the HuBERT model
30
+ device = grab_best_device(use_gpu)
31
+ hubert_model = CustomHubert(checkpoint_path='./models/hubert/hubert.pt').to(device)
32
+
33
+ # Load the CustomTokenizer model
34
+ tokenizer = CustomTokenizer.load_from_checkpoint('./models/hubert/tokenizer.pth').to(device) # Automatically uses the right layers
35
+
36
+ progress(0.25, desc="Converting WAV")
37
+
38
+ # Load and pre-process the audio waveform
39
+ wav, sr = torchaudio.load(audio_filepath)
40
+ if wav.shape[0] == 2: # Stereo to mono if needed
41
+ wav = wav.mean(0, keepdim=True)
42
+
43
+ wav = convert_audio(wav, sr, model.sample_rate, model.channels)
44
+ wav = wav.to(device)
45
+ progress(0.5, desc="Extracting codes")
46
+
47
+ semantic_vectors = hubert_model.forward(wav, input_sample_hz=model.sample_rate)
48
+ semantic_tokens = tokenizer.get_token(semantic_vectors)
49
+
50
+ # Extract discrete codes from EnCodec
51
+ with torch.no_grad():
52
+ encoded_frames = model.encode(wav.unsqueeze(0))
53
+ codes = torch.cat([encoded[0] for encoded in encoded_frames], dim=-1).squeeze() # [n_q, T]
54
+
55
+ # get seconds of audio
56
+ # seconds = wav.shape[-1] / model.sample_rate
57
+ # generate semantic tokens
58
+ # semantic_tokens = generate_text_semantic(text, max_gen_duration_s=seconds, top_k=50, top_p=.95, temp=0.7)
59
+
60
+ # move codes to cpu
61
+ codes = codes.cpu().numpy()
62
+ # move semantic tokens to cpu
63
+ semantic_tokens = semantic_tokens.cpu().numpy()
64
+
65
+ import numpy as np
66
+ output_path = dest_filename + '.npz'
67
+ np.savez(output_path, fine_prompt=codes, coarse_prompt=codes[:2, :], semantic_prompt=semantic_tokens)
68
+ return "Finished"