justyoung commited on
Commit
6a80a72
·
verified ·
1 Parent(s): cc9c58f

Rename easyfuncs.py to rvcfunc.py

Browse files
Files changed (1) hide show
  1. easyfuncs.py → rvcfunc.py +194 -242
easyfuncs.py → rvcfunc.py RENAMED
@@ -1,242 +1,194 @@
1
- import os, subprocess
2
- import gradio as gr
3
- import shutil, time, torch, gc
4
- from mega import Mega
5
- from datetime import datetime
6
- import pandas as pd
7
- import os, sys, subprocess, numpy as np
8
- from pydub import AudioSegment
9
- try:
10
- from whisperspeech.pipeline import Pipeline as TTS
11
- whisperspeak_on = True
12
- except:
13
- whisperspeak_on = False
14
-
15
- # Class to handle caching model urls from a spreadsheet
16
- class CachedModels:
17
- def __init__(self):
18
- csv_url = "https://docs.google.com/spreadsheets/d/1tAUaQrEHYgRsm1Lvrnj14HFHDwJWl0Bd9x0QePewNco/export?format=csv&gid=1977693859"
19
- if os.path.exists("spreadsheet.csv"):
20
- self.cached_data = pd.read_csv("spreadsheet.csv")
21
- else:
22
- self.cached_data = pd.read_csv(csv_url)
23
- self.cached_data.to_csv("spreadsheet.csv", index=False)
24
- # Cache model urls
25
- self.models = {}
26
- for _, row in self.cached_data.iterrows():
27
- filename = row['Filename']
28
- url = None
29
- for value in row.values:
30
- if isinstance(value, str) and "huggingface" in value:
31
- url = value
32
- break
33
- if url:
34
- self.models[filename] = url
35
- # Get cached model urls
36
- def get_models(self):
37
- return self.models
38
-
39
- def show(path,ext,on_error=None):
40
- try:
41
- return list(filter(lambda x: x.endswith(ext), os.listdir(path)))
42
- except:
43
- return on_error
44
-
45
- def run_subprocess(command):
46
- try:
47
- subprocess.run(command, check=True)
48
- return True, None
49
- except Exception as e:
50
- return False, e
51
-
52
- def download_from_url(url=None, model=None):
53
- if not url:
54
- try:
55
- url = model[f'{model}']
56
- except:
57
- gr.Warning("Failed")
58
- return ''
59
- if model == '':
60
- try:
61
- model = url.split('/')[-1].split('?')[0]
62
- except:
63
- gr.Warning('Please name the model')
64
- return
65
- model = model.replace('.pth', '').replace('.index', '').replace('.zip', '')
66
- url = url.replace('/blob/main/', '/resolve/main/').strip()
67
-
68
- for directory in ["downloads", "unzips","zip"]:
69
- #shutil.rmtree(directory, ignore_errors=True)
70
- os.makedirs(directory, exist_ok=True)
71
-
72
- try:
73
- if url.endswith('.pth'):
74
- subprocess.run(["wget", url, "-O", f'assets/weights/{model}.pth'])
75
- elif url.endswith('.index'):
76
- os.makedirs(f'logs/{model}', exist_ok=True)
77
- subprocess.run(["wget", url, "-O", f'logs/{model}/added_{model}.index'])
78
- elif url.endswith('.zip'):
79
- subprocess.run(["wget", url, "-O", f'downloads/{model}.zip'])
80
- else:
81
- if "drive.google.com" in url:
82
- url = url.split('/')[0]
83
- subprocess.run(["gdown", url, "--fuzzy", "-O", f'downloads/{model}'])
84
- elif "mega.nz" in url:
85
- Mega().download_url(url, 'downloads')
86
- else:
87
- subprocess.run(["wget", url, "-O", f'downloads/{model}'])
88
-
89
- downloaded_file = next((f for f in os.listdir("downloads")), None)
90
- if downloaded_file:
91
- if downloaded_file.endswith(".zip"):
92
- shutil.unpack_archive(f'downloads/{downloaded_file}', "unzips", 'zip')
93
- for root, _, files in os.walk('unzips'):
94
- for file in files:
95
- file_path = os.path.join(root, file)
96
- if file.endswith(".index"):
97
- os.makedirs(f'logs/{model}', exist_ok=True)
98
- shutil.copy2(file_path, f'logs/{model}')
99
- elif file.endswith(".pth") and "G_" not in file and "D_" not in file:
100
- shutil.copy(file_path, f'assets/weights/{model}.pth')
101
- elif downloaded_file.endswith(".pth"):
102
- shutil.copy(f'downloads/{downloaded_file}', f'assets/weights/{model}.pth')
103
- elif downloaded_file.endswith(".index"):
104
- os.makedirs(f'logs/{model}', exist_ok=True)
105
- shutil.copy(f'downloads/{downloaded_file}', f'logs/{model}/added_{model}.index')
106
- else:
107
- gr.Warning("Failed to download file")
108
- return 'Failed'
109
-
110
- gr.Info("Done")
111
- except Exception as e:
112
- gr.Warning(f"There's been an error: {str(e)}")
113
- finally:
114
- shutil.rmtree("downloads", ignore_errors=True)
115
- shutil.rmtree("unzips", ignore_errors=True)
116
- shutil.rmtree("zip", ignore_errors=True)
117
- return 'Done'
118
-
119
- def speak(audio, text):
120
- print(f"({audio}, {text})")
121
- current_dir = os.getcwd()
122
- os.chdir('./gpt_sovits_demo')
123
- process = subprocess.Popen([
124
- "python", "./zero.py",
125
- "--input_file", audio,
126
- "--audio_lang", "English",
127
- "--text", text,
128
- "--text_lang", "English"
129
- ], stdout=subprocess.PIPE, text=True)
130
-
131
- for line in process.stdout:
132
- line = line.strip()
133
- if "All keys matched successfully" in line:
134
- continue
135
- if line.startswith("(") and line.endswith(")"):
136
- path, finished = line[1:-1].split(", ")
137
- if finished:
138
- os.chdir(current_dir)
139
- return path
140
- os.chdir(current_dir)
141
- return None
142
-
143
- def whisperspeak(text, tts_lang, cps=10.5):
144
- if whisperspeak_on is None: return None
145
- if not "tts_pipe" in locals(): tts_pipe = TTS(t2s_ref='whisperspeech/whisperspeech:t2s-v1.95-small-8lang.model', s2a_ref='whisperspeech/whisperspeech:s2a-v1.95-medium-7lang.model')
146
- from fastprogress.fastprogress import master_bar, progress_bar
147
- master_bar.update = lambda *args, **kwargs: None
148
- progress_bar.update = lambda *args, **kwargs: None
149
-
150
- output = f"audios/tts_audio_{datetime.now().strftime('%Y%m%d_%H%M%S')}.wav"
151
- tts_pipe.generate_to_file(output, text, cps=cps, lang=tts_lang)
152
- return os.path.abspath(output)
153
-
154
- def stereo_process(audio1,audio2,choice):
155
- audio = audio1 if choice == "Input" else audio2
156
- print(audio)
157
- sample_rate, audio_array = audio
158
- if len(audio_array.shape) == 1:
159
- audio_bytes = audio_array.tobytes()
160
- segment = AudioSegment(
161
- data=audio_bytes,
162
- sample_width=audio_array.dtype.itemsize, # 2 bytes for int16
163
- frame_rate=sample_rate, # Use the sample rate from your tuple
164
- channels=1 # Adjust if your audio has more channels
165
- )
166
- samples = np.array(segment.get_array_of_samples())
167
- delay_samples = int(segment.frame_rate * (0.6 / 1000.0))
168
- left_channel = np.zeros_like(samples)
169
- right_channel = samples
170
- left_channel[delay_samples:] = samples[:-delay_samples]
171
- stereo_samples = np.column_stack((left_channel, right_channel))
172
- return (sample_rate, stereo_samples.astype(np.int16))
173
- else:
174
- return audio
175
-
176
- def sr_process(audio1, audio2, choice):
177
- torch.cuda.empty_cache()
178
- gc.collect()
179
- if "tts_pipe" in locals(): del tts_pipe
180
- audio = audio1 if choice == "Input" else audio2
181
- sample_rate, audio_array = audio
182
- audio_segment = AudioSegment(
183
- audio_array.tobytes(),
184
- frame_rate=sample_rate,
185
- sample_width=audio_array.dtype.itemsize,
186
- channels=1 if len(audio_array.shape) == 1 else 2
187
- )
188
- temp_file = os.path.join('TEMP', f'{choice}_{datetime.now().strftime("%Y%m%d_%H%M%S")}.wav')
189
- audio_segment.export(temp_file, format="wav")
190
- output_folder = "SR"
191
- model_name = "speech"
192
- suffix = "_ldm"
193
- guidance_scale = 2.7
194
- ddim_steps = 50
195
- venv_dir = "audiosr"
196
-
197
- def split_audio(input_file, output_folder, chunk_duration=5.12):
198
- os.makedirs(output_folder, exist_ok=True)
199
- ffmpeg_command = f"ffmpeg -i {input_file} -f segment -segment_time {chunk_duration} -c:a pcm_s16le {output_folder}/out%03d.wav"
200
- subprocess.run(ffmpeg_command, shell=True, check=True)
201
-
202
- def create_file_list(output_folder):
203
- file_list = os.path.join(output_folder, "file_list.txt")
204
- with open(file_list, "w") as f:
205
- for filename in sorted(os.listdir(output_folder)):
206
- if filename.endswith(".wav"):
207
- f.write(os.path.join(output_folder, filename) + "\n")
208
- return file_list
209
-
210
- def run_audiosr(file_list, model_name, suffix, guidance_scale, ddim_steps, output_folder, venv_dir):
211
- command = f"{venv_dir}/bin/python -m audiosr --input_file_list {file_list} --model_name {model_name} --suffix {suffix} --guidance_scale {guidance_scale} --ddim_steps {ddim_steps} --save_path {output_folder}"
212
- try:
213
- subprocess.run(command, shell=True, check=True, stderr=subprocess.PIPE)
214
- except subprocess.CalledProcessError as e:
215
- print(f"Error running audiosr: {e.stderr.decode()}")
216
-
217
-
218
- split_audio(temp_file, output_folder)
219
- file_list = create_file_list(output_folder)
220
- run_audiosr(file_list, model_name, suffix, guidance_scale, ddim_steps, output_folder, venv_dir)
221
-
222
- output_file = None
223
- time.sleep(1)
224
- processed_chunks = []
225
- for root, dirs, files in os.walk(output_folder):
226
- for file in sorted(files):
227
- if file.startswith("out") and file.endswith(f"{suffix}.wav"):
228
- chunk_file = os.path.join(root, file)
229
- processed_chunks.append(AudioSegment.from_wav(chunk_file))
230
-
231
- if processed_chunks:
232
- merged_audio = sum(processed_chunks)
233
- output_file = os.path.join(output_folder, f"{choice}_merged{suffix}.wav")
234
- merged_audio.export(output_file, format="wav")
235
-
236
- display_file = AudioSegment.from_file(output_file)
237
- sample_rate = display_file.frame_rate
238
- audio_array = np.array(display_file.get_array_of_samples())
239
- return (sample_rate, audio_array)
240
- else:
241
- print(f"Error: Could not find any processed audio chunks in {output_folder}")
242
- return None
 
1
+ import os, subprocess
2
+ import gradio as gr
3
+ import shutil, time, torch, gc
4
+ from mega import Mega
5
+ from datetime import datetime
6
+ import pandas as pd
7
+ import os, sys, subprocess, numpy as np
8
+ from pydub import AudioSegment
9
+
10
+ # Class to handle caching model urls from a spreadsheet
11
+ class CachedModels:
12
+ def __init__(self):
13
+ csv_url = "https://docs.google.com/spreadsheets/d/1tAUaQrEHYgRsm1Lvrnj14HFHDwJWl0Bd9x0QePewNco/export?format=csv&gid=1977693859"
14
+ if os.path.exists("spreadsheet.csv"):
15
+ self.cached_data = pd.read_csv("spreadsheet.csv")
16
+ else:
17
+ self.cached_data = pd.read_csv(csv_url)
18
+ self.cached_data.to_csv("spreadsheet.csv", index=False)
19
+ # Cache model urls
20
+ self.models = {}
21
+ for _, row in self.cached_data.iterrows():
22
+ filename = row['Filename']
23
+ url = None
24
+ for value in row.values:
25
+ if isinstance(value, str) and "huggingface" in value:
26
+ url = value
27
+ break
28
+ if url:
29
+ self.models[filename] = url
30
+ # Get cached model urls
31
+ def get_models(self):
32
+ return self.models
33
+
34
+ def show(path,ext,on_error=None):
35
+ try:
36
+ return list(filter(lambda x: x.endswith(ext), os.listdir(path)))
37
+ except:
38
+ return on_error
39
+
40
+ def run_subprocess(command):
41
+ try:
42
+ subprocess.run(command, check=True)
43
+ return True, None
44
+ except Exception as e:
45
+ return False, e
46
+
47
+ def download_from_url(url=None, model=None):
48
+ if not url:
49
+ try:
50
+ url = model[f'{model}']
51
+ except:
52
+ gr.Warning("Failed")
53
+ return ''
54
+ if model == '':
55
+ try:
56
+ model = url.split('/')[-1].split('?')[0]
57
+ except:
58
+ gr.Warning('Please name the model')
59
+ return
60
+ model = model.replace('.pth', '').replace('.index', '').replace('.zip', '')
61
+ url = url.replace('/blob/main/', '/resolve/main/').strip()
62
+
63
+ for directory in ["downloads", "unzips","zip"]:
64
+ #shutil.rmtree(directory, ignore_errors=True)
65
+ os.makedirs(directory, exist_ok=True)
66
+
67
+ try:
68
+ if url.endswith('.pth'):
69
+ subprocess.run(["wget", url, "-O", f'assets/weights/{model}.pth'])
70
+ elif url.endswith('.index'):
71
+ os.makedirs(f'logs/{model}', exist_ok=True)
72
+ subprocess.run(["wget", url, "-O", f'logs/{model}/added_{model}.index'])
73
+ elif url.endswith('.zip'):
74
+ subprocess.run(["wget", url, "-O", f'downloads/{model}.zip'])
75
+ else:
76
+ if "drive.google.com" in url:
77
+ url = url.split('/')[0]
78
+ subprocess.run(["gdown", url, "--fuzzy", "-O", f'downloads/{model}'])
79
+ elif "mega.nz" in url:
80
+ Mega().download_url(url, 'downloads')
81
+ else:
82
+ subprocess.run(["wget", url, "-O", f'downloads/{model}'])
83
+
84
+ downloaded_file = next((f for f in os.listdir("downloads")), None)
85
+ if downloaded_file:
86
+ if downloaded_file.endswith(".zip"):
87
+ shutil.unpack_archive(f'downloads/{downloaded_file}', "unzips", 'zip')
88
+ for root, _, files in os.walk('unzips'):
89
+ for file in files:
90
+ file_path = os.path.join(root, file)
91
+ if file.endswith(".index"):
92
+ os.makedirs(f'logs/{model}', exist_ok=True)
93
+ shutil.copy2(file_path, f'logs/{model}')
94
+ elif file.endswith(".pth") and "G_" not in file and "D_" not in file:
95
+ shutil.copy(file_path, f'assets/weights/{model}.pth')
96
+ elif downloaded_file.endswith(".pth"):
97
+ shutil.copy(f'downloads/{downloaded_file}', f'assets/weights/{model}.pth')
98
+ elif downloaded_file.endswith(".index"):
99
+ os.makedirs(f'logs/{model}', exist_ok=True)
100
+ shutil.copy(f'downloads/{downloaded_file}', f'logs/{model}/added_{model}.index')
101
+ else:
102
+ gr.Warning("Failed to download file")
103
+ return 'Failed'
104
+
105
+ gr.Info("Done")
106
+ except Exception as e:
107
+ gr.Warning(f"There's been an error: {str(e)}")
108
+ finally:
109
+ shutil.rmtree("downloads", ignore_errors=True)
110
+ shutil.rmtree("unzips", ignore_errors=True)
111
+ shutil.rmtree("zip", ignore_errors=True)
112
+ return 'Done'
113
+
114
+ def speak(audio, text):
115
+ print(f"({audio}, {text})")
116
+ current_dir = os.getcwd()
117
+ os.chdir('./gpt_sovits_demo')
118
+ process = subprocess.Popen([
119
+ "python", "./zero.py",
120
+ "--input_file", audio,
121
+ "--audio_lang", "English",
122
+ "--text", text,
123
+ "--text_lang", "English"
124
+ ], stdout=subprocess.PIPE, text=True)
125
+
126
+ for line in process.stdout:
127
+ line = line.strip()
128
+ if "All keys matched successfully" in line:
129
+ continue
130
+ if line.startswith("(") and line.endswith(")"):
131
+ path, finished = line[1:-1].split(", ")
132
+ if finished:
133
+ os.chdir(current_dir)
134
+ return path
135
+ os.chdir(current_dir)
136
+ return None
137
+
138
+
139
+ def stereo_process(audio1,audio2,choice):
140
+ audio = audio1 if choice == "Input" else audio2
141
+ print(audio)
142
+ sample_rate, audio_array = audio
143
+ if len(audio_array.shape) == 1:
144
+ audio_bytes = audio_array.tobytes()
145
+ segment = AudioSegment(
146
+ data=audio_bytes,
147
+ sample_width=audio_array.dtype.itemsize, # 2 bytes for int16
148
+ frame_rate=sample_rate, # Use the sample rate from your tuple
149
+ channels=1 # Adjust if your audio has more channels
150
+ )
151
+ samples = np.array(segment.get_array_of_samples())
152
+ delay_samples = int(segment.frame_rate * (0.6 / 1000.0))
153
+ left_channel = np.zeros_like(samples)
154
+ right_channel = samples
155
+ left_channel[delay_samples:] = samples[:-delay_samples]
156
+ stereo_samples = np.column_stack((left_channel, right_channel))
157
+ return (sample_rate, stereo_samples.astype(np.int16))
158
+ else:
159
+ return audio
160
+
161
+ def sr_process(audio1, audio2, choice):
162
+ torch.cuda.empty_cache()
163
+ gc.collect()
164
+ if "tts_pipe" in locals(): del tts_pipe
165
+ audio = audio1 if choice == "Input" else audio2
166
+ sample_rate, audio_array = audio
167
+ audio_segment = AudioSegment(
168
+ audio_array.tobytes(),
169
+ frame_rate=sample_rate,
170
+ sample_width=audio_array.dtype.itemsize,
171
+ channels=1 if len(audio_array.shape) == 1 else 2
172
+ )
173
+ temp_file = os.path.join('TEMP', f'{choice}_{datetime.now().strftime("%Y%m%d_%H%M%S")}.wav')
174
+ audio_segment.export(temp_file, format="wav")
175
+ output_folder = "SR"
176
+ model_name = "speech"
177
+ suffix = "_ldm"
178
+ guidance_scale = 2.7
179
+ ddim_steps = 50
180
+ venv_dir = "audiosr"
181
+
182
+ def split_audio(input_file, output_folder, chunk_duration=5.12):
183
+ os.makedirs(output_folder, exist_ok=True)
184
+ ffmpeg_command = f"ffmpeg -i {input_file} -f segment -segment_time {chunk_duration} -c:a pcm_s16le {output_folder}/out%03d.wav"
185
+ subprocess.run(ffmpeg_command, shell=True, check=True)
186
+
187
+ def create_file_list(output_folder):
188
+ file_list = os.path.join(output_folder, "file_list.txt")
189
+ with open(file_list, "w") as f:
190
+ for filename in sorted(os.listdir(output_folder)):
191
+ if filename.endswith(".wav"):
192
+ f.write(os.path.join(output_folder, filename) + "\n")
193
+ return file_list
194
+