akshansh36 commited on
Commit
93f8aa2
1 Parent(s): 088dce9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +360 -360
app.py CHANGED
@@ -1,360 +1,360 @@
1
- import os
2
- import gradio as gr
3
- import spaces
4
- from infer_rvc_python import BaseLoader
5
- import random
6
- import logging
7
- import time
8
- import soundfile as sf
9
- from infer_rvc_python.main import download_manager
10
- import zipfile
11
- import edge_tts
12
- import asyncio
13
- import librosa
14
- import traceback
15
- import soundfile as sf
16
- from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter
17
- from pedalboard.io import AudioFile
18
- from pydub import AudioSegment
19
- import noisereduce as nr
20
- import numpy as np
21
- import urllib.request
22
- import shutil
23
- import threading
24
-
25
- logging.getLogger("infer_rvc_python").setLevel(logging.ERROR)
26
-
27
- converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None)
28
-
29
- title = "<center><strong><font size='7'>Vodex AI</font></strong></center>"
30
- theme = "aliabid94/new-theme"
31
-
32
- def find_files(directory):
33
- file_paths = []
34
- for filename in os.listdir(directory):
35
- if filename.endswith('.pth') or filename.endswith('.zip') or filename.endswith('.index'):
36
- file_paths.append(os.path.join(directory, filename))
37
- return file_paths
38
-
39
- def unzip_in_folder(my_zip, my_dir):
40
- with zipfile.ZipFile(my_zip) as zip:
41
- for zip_info in zip.infolist():
42
- if zip_info.is_dir():
43
- continue
44
- zip_info.filename = os.path.basename(zip_info.filename)
45
- zip.extract(zip_info, my_dir)
46
-
47
- def find_my_model(a_, b_):
48
- if a_ is None or a_.endswith(".pth"):
49
- return a_, b_
50
-
51
- txt_files = []
52
- for base_file in [a_, b_]:
53
- if base_file is not None and base_file.endswith(".txt"):
54
- txt_files.append(base_file)
55
-
56
- directory = os.path.dirname(a_)
57
-
58
- for txt in txt_files:
59
- with open(txt, 'r') as file:
60
- first_line = file.readline()
61
-
62
- download_manager(
63
- url=first_line.strip(),
64
- path=directory,
65
- extension="",
66
- )
67
-
68
- for f in find_files(directory):
69
- if f.endswith(".zip"):
70
- unzip_in_folder(f, directory)
71
-
72
- model = None
73
- index = None
74
- end_files = find_files(directory)
75
-
76
- for ff in end_files:
77
- if ff.endswith(".pth"):
78
- model = os.path.join(directory, ff)
79
- gr.Info(f"Model found: {ff}")
80
- if ff.endswith(".index"):
81
- index = os.path.join(directory, ff)
82
- gr.Info(f"Index found: {ff}")
83
-
84
- if not model:
85
- gr.Error(f"Model not found in: {end_files}")
86
-
87
- if not index:
88
- gr.Warning("Index not found")
89
-
90
- return model, index
91
-
92
- def get_file_size(url):
93
- if "huggingface" not in url:
94
- raise ValueError("Only downloads from Hugging Face are allowed")
95
-
96
- try:
97
- with urllib.request.urlopen(url) as response:
98
- info = response.info()
99
- content_length = info.get("Content-Length")
100
-
101
- file_size = int(content_length)
102
- if file_size > 500000000:
103
- raise ValueError("The file is too large. You can only download files up to 500 MB in size.")
104
-
105
- except Exception as e:
106
- raise e
107
-
108
- def clear_files(directory):
109
- time.sleep(15)
110
- print(f"Clearing files: {directory}.")
111
- shutil.rmtree(directory)
112
-
113
- def get_my_model(url_data):
114
- if not url_data:
115
- return None, None
116
-
117
- if "," in url_data:
118
- a_, b_ = url_data.split()
119
- a_, b_ = a_.strip().replace("/blob/", "/resolve/"), b_.strip().replace("/blob/", "/resolve/")
120
- else:
121
- a_, b_ = url_data.strip().replace("/blob/", "/resolve/"), None
122
-
123
- out_dir = "downloads"
124
- folder_download = str(random.randint(1000, 9999))
125
- directory = os.path.join(out_dir, folder_download)
126
- os.makedirs(directory, exist_ok=True)
127
-
128
- try:
129
- get_file_size(a_)
130
- if b_:
131
- get_file_size(b_)
132
-
133
- valid_url = [a_] if not b_ else [a_, b_]
134
- for link in valid_url:
135
- download_manager(
136
- url=link,
137
- path=directory,
138
- extension="",
139
- )
140
-
141
- for f in find_files(directory):
142
- if f.endswith(".zip"):
143
- unzip_in_folder(f, directory)
144
-
145
- model = None
146
- index = None
147
- end_files = find_files(directory)
148
-
149
- for ff in end_files:
150
- if ff.endswith(".pth"):
151
- model = ff
152
- gr.Info(f"Model found: {ff}")
153
- if ff.endswith(".index"):
154
- index = ff
155
- gr.Info(f"Index found: {ff}")
156
-
157
- if not model:
158
- raise ValueError(f"Model not found in: {end_files}")
159
-
160
- if not index:
161
- gr.Warning("Index not found")
162
- else:
163
- index = os.path.abspath(index)
164
-
165
- return os.path.abspath(model), index
166
-
167
- except Exception as e:
168
- raise e
169
- finally:
170
- t = threading.Thread(target=clear_files, args=(directory,))
171
- t.start()
172
-
173
- def convert_now(audio_files, random_tag, converter):
174
- return converter(
175
- audio_files,
176
- random_tag,
177
- overwrite=False,
178
- parallel_workers=8
179
- )
180
-
181
- def apply_noisereduce(audio_list):
182
- print("Applying noise reduction")
183
-
184
- result = []
185
- for audio_path in audio_list:
186
- out_path = f'{os.path.splitext(audio_path)[0]}_noisereduce.wav'
187
-
188
- try:
189
- # Load audio file
190
- audio = AudioSegment.from_file(audio_path)
191
-
192
- # Convert audio to numpy array
193
- samples = np.array(audio.get_array_of_samples())
194
-
195
- # Reduce noise
196
- reduced_noise = nr.reduce_noise(y=samples, sr=audio.frame_rate, prop_decrease=0.6)
197
-
198
- # Convert reduced noise signal back to audio
199
- reduced_audio = AudioSegment(
200
- reduced_noise.tobytes(),
201
- frame_rate=audio.frame_rate,
202
- sample_width=audio.sample_width,
203
- channels=audio.channels
204
- )
205
-
206
- # Save reduced audio to file
207
- reduced_audio.export(out_path, format="wav")
208
- result.append(out_path)
209
-
210
- except Exception as e:
211
- traceback.print_exc()
212
- print(f"Error in noise reduction: {str(e)}")
213
- result.append(audio_path)
214
-
215
- return result
216
-
217
- def run(audio_files, file_m, file_index):
218
- if not audio_files:
219
- raise ValueError("Please provide an audio file.")
220
-
221
- if isinstance(audio_files, str):
222
- audio_files = [audio_files]
223
-
224
- try:
225
- duration_base = librosa.get_duration(filename=audio_files[0])
226
- print("Duration:", duration_base)
227
- except Exception as e:
228
- print(e)
229
-
230
- if file_m is not None and file_m.endswith(".txt"):
231
- file_m, file_index = find_my_model(file_m, file_index)
232
- print(file_m, file_index)
233
-
234
- random_tag = "USER_" + str(random.randint(10000000, 99999999))
235
-
236
- # Hardcoding pitch algorithm and other parameters
237
- pitch_alg = "rmvpe+"
238
- pitch_lvl = 0
239
- index_inf = 0.75
240
- r_m_f = 3
241
- e_r = 0.25
242
- c_b_p = 0.5
243
-
244
- converter.apply_conf(
245
- tag=random_tag,
246
- file_model=file_m,
247
- pitch_algo=pitch_alg,
248
- pitch_lvl=pitch_lvl,
249
- file_index=file_index,
250
- index_influence=index_inf,
251
- respiration_median_filtering=r_m_f,
252
- envelope_ratio=e_r,
253
- consonant_breath_protection=c_b_p,
254
- resample_sr=44100 if audio_files[0].endswith('.mp3') else 0,
255
- )
256
- time.sleep(0.1)
257
-
258
- result = convert_now(audio_files, random_tag, converter)
259
-
260
- # Always apply noise reduction
261
- result = apply_noisereduce(result)
262
-
263
- return result
264
-
265
- def model_conf():
266
- model_files = [f for f in os.listdir("models") if f.endswith(".pth")]
267
- return gr.Dropdown(
268
- label="Select Model File",
269
- choices=model_files,
270
- value=model_files[0] if model_files else None,
271
- interactive=True,
272
- )
273
-
274
- def index_conf():
275
- index_files = [f for f in os.listdir("models") if f.endswith(".index")]
276
- return gr.Dropdown(
277
- label="Select Index File",
278
- choices=index_files,
279
- value=index_files[0] if index_files else None,
280
- interactive=True,
281
- )
282
-
283
- def audio_conf():
284
- return gr.File(
285
- label="Audio files",
286
- file_count="multiple",
287
- type="filepath",
288
- container=True,
289
- )
290
-
291
- def button_conf():
292
- return gr.Button(
293
- "Inference",
294
- variant="primary",
295
- )
296
-
297
- def output_conf():
298
- return gr.File(
299
- label="Result",
300
- file_count="multiple",
301
- interactive=False,
302
- )
303
-
304
- def get_gui(theme):
305
- with gr.Blocks(theme=theme, delete_cache=(3200, 3200)) as app:
306
- gr.Markdown(title)
307
-
308
- aud = audio_conf()
309
-
310
- model = model_conf()
311
- indx = index_conf()
312
- button_base = button_conf()
313
- output_base = output_conf()
314
-
315
- button_base.click(
316
- run,
317
- inputs=[
318
- aud,
319
- model,
320
- indx,
321
- ],
322
- outputs=[output_base],
323
- )
324
-
325
- gr.Examples(
326
- examples=[
327
- [
328
- ["./test.ogg"],
329
- "./model.pth",
330
- "./model.index",
331
- ],
332
- [
333
- ["./example2/test2.ogg"],
334
- "./example2/model.pth",
335
- "./example2/model.index",
336
- ],
337
- ],
338
- fn=run,
339
- inputs=[
340
- aud,
341
- model,
342
- indx,
343
- ],
344
- outputs=[output_base],
345
- cache_examples=False,
346
- )
347
-
348
- return app
349
-
350
- if __name__ == "__main__":
351
- app = get_gui(theme)
352
- app.queue(default_concurrency_limit=40)
353
- app.launch(
354
- max_threads=40,
355
- share=False,
356
- show_error=True,
357
- quiet=False,
358
- debug=False,
359
- allowed_paths=["./downloads/"],
360
- )
 
1
+ import os
2
+ import gradio as gr
3
+ import spaces
4
+ from infer_rvc_python import BaseLoader
5
+ import random
6
+ import logging
7
+ import time
8
+ import soundfile as sf
9
+ from infer_rvc_python.main import download_manager
10
+ import zipfile
11
+ import edge_tts
12
+ import asyncio
13
+ import librosa
14
+ import traceback
15
+ import soundfile as sf
16
+ from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter
17
+ from pedalboard.io import AudioFile
18
+ from pydub import AudioSegment
19
+ import noisereduce as nr
20
+ import numpy as np
21
+ import urllib.request
22
+ import shutil
23
+ import threading
24
+
25
+ logging.getLogger("infer_rvc_python").setLevel(logging.ERROR)
26
+
27
+ converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None)
28
+
29
+ title = "<center><strong><font size='7'>Vodex AI</font></strong></center>"
30
+ theme = "aliabid94/new-theme"
31
+
32
+ def find_files(directory):
33
+ file_paths = []
34
+ for filename in os.listdir(directory):
35
+ if filename.endswith('.pth') or filename.endswith('.zip') or filename.endswith('.index'):
36
+ file_paths.append(os.path.join(directory, filename))
37
+ return file_paths
38
+
39
+ def unzip_in_folder(my_zip, my_dir):
40
+ with zipfile.ZipFile(my_zip) as zip:
41
+ for zip_info in zip.infolist():
42
+ if zip_info.is_dir():
43
+ continue
44
+ zip_info.filename = os.path.basename(zip_info.filename)
45
+ zip.extract(zip_info, my_dir)
46
+
47
+ def find_my_model(a_, b_):
48
+ if a_ is None or a_.endswith(".pth"):
49
+ return a_, b_
50
+
51
+ txt_files = []
52
+ for base_file in [a_, b_]:
53
+ if base_file is not None and base_file.endswith(".txt"):
54
+ txt_files.append(base_file)
55
+
56
+ directory = os.path.dirname(a_)
57
+
58
+ for txt in txt_files:
59
+ with open(txt, 'r') as file:
60
+ first_line = file.readline()
61
+
62
+ download_manager(
63
+ url=first_line.strip(),
64
+ path=directory,
65
+ extension="",
66
+ )
67
+
68
+ for f in find_files(directory):
69
+ if f.endswith(".zip"):
70
+ unzip_in_folder(f, directory)
71
+
72
+ model = None
73
+ index = None
74
+ end_files = find_files(directory)
75
+
76
+ for ff in end_files:
77
+ if ff.endswith(".pth"):
78
+ model = os.path.join(directory, ff)
79
+ gr.Info(f"Model found: {ff}")
80
+ if ff.endswith(".index"):
81
+ index = os.path.join(directory, ff)
82
+ gr.Info(f"Index found: {ff}")
83
+
84
+ if not model:
85
+ gr.Error(f"Model not found in: {end_files}")
86
+
87
+ if not index:
88
+ gr.Warning("Index not found")
89
+
90
+ return model, index
91
+
92
+ def get_file_size(url):
93
+ if "huggingface" not in url:
94
+ raise ValueError("Only downloads from Hugging Face are allowed")
95
+
96
+ try:
97
+ with urllib.request.urlopen(url) as response:
98
+ info = response.info()
99
+ content_length = info.get("Content-Length")
100
+
101
+ file_size = int(content_length)
102
+ if file_size > 500000000:
103
+ raise ValueError("The file is too large. You can only download files up to 500 MB in size.")
104
+
105
+ except Exception as e:
106
+ raise e
107
+
108
+ def clear_files(directory):
109
+ time.sleep(15)
110
+ print(f"Clearing files: {directory}.")
111
+ shutil.rmtree(directory)
112
+
113
+ def get_my_model(url_data):
114
+ if not url_data:
115
+ return None, None
116
+
117
+ if "," in url_data:
118
+ a_, b_ = url_data.split()
119
+ a_, b_ = a_.strip().replace("/blob/", "/resolve/"), b_.strip().replace("/blob/", "/resolve/")
120
+ else:
121
+ a_, b_ = url_data.strip().replace("/blob/", "/resolve/"), None
122
+
123
+ out_dir = "downloads"
124
+ folder_download = str(random.randint(1000, 9999))
125
+ directory = os.path.join(out_dir, folder_download)
126
+ os.makedirs(directory, exist_ok=True)
127
+
128
+ try:
129
+ get_file_size(a_)
130
+ if b_:
131
+ get_file_size(b_)
132
+
133
+ valid_url = [a_] if not b_ else [a_, b_]
134
+ for link in valid_url:
135
+ download_manager(
136
+ url=link,
137
+ path=directory,
138
+ extension="",
139
+ )
140
+
141
+ for f in find_files(directory):
142
+ if f.endswith(".zip"):
143
+ unzip_in_folder(f, directory)
144
+
145
+ model = None
146
+ index = None
147
+ end_files = find_files(directory)
148
+
149
+ for ff in end_files:
150
+ if ff.endswith(".pth"):
151
+ model = ff
152
+ gr.Info(f"Model found: {ff}")
153
+ if ff.endswith(".index"):
154
+ index = ff
155
+ gr.Info(f"Index found: {ff}")
156
+
157
+ if not model:
158
+ raise ValueError(f"Model not found in: {end_files}")
159
+
160
+ if not index:
161
+ gr.Warning("Index not found")
162
+ else:
163
+ index = os.path.abspath(index)
164
+
165
+ return os.path.abspath(model), index
166
+
167
+ except Exception as e:
168
+ raise e
169
+ finally:
170
+ t = threading.Thread(target=clear_files, args=(directory,))
171
+ t.start()
172
+
173
+ def convert_now(audio_files, random_tag, converter):
174
+ return converter(
175
+ audio_files,
176
+ random_tag,
177
+ overwrite=False,
178
+ parallel_workers=8
179
+ )
180
+
181
+ def apply_noisereduce(audio_list):
182
+ print("Applying noise reduction")
183
+
184
+ result = []
185
+ for audio_path in audio_list:
186
+ out_path = f'{os.path.splitext(audio_path)[0]}_noisereduce.wav'
187
+
188
+ try:
189
+ # Load audio file
190
+ audio = AudioSegment.from_file(audio_path)
191
+
192
+ # Convert audio to numpy array
193
+ samples = np.array(audio.get_array_of_samples())
194
+
195
+ # Reduce noise
196
+ reduced_noise = nr.reduce_noise(y=samples, sr=audio.frame_rate, prop_decrease=0.6)
197
+
198
+ # Convert reduced noise signal back to audio
199
+ reduced_audio = AudioSegment(
200
+ reduced_noise.tobytes(),
201
+ frame_rate=audio.frame_rate,
202
+ sample_width=audio.sample_width,
203
+ channels=audio.channels
204
+ )
205
+
206
+ # Save reduced audio to file
207
+ reduced_audio.export(out_path, format="wav")
208
+ result.append(out_path)
209
+
210
+ except Exception as e:
211
+ traceback.print_exc()
212
+ print(f"Error in noise reduction: {str(e)}")
213
+ result.append(audio_path)
214
+
215
+ return result
216
+
217
+ def run(audio_files, file_m, file_index):
218
+ if not audio_files:
219
+ raise ValueError("Please provide an audio file.")
220
+
221
+ if isinstance(audio_files, str):
222
+ audio_files = [audio_files]
223
+
224
+ try:
225
+ duration_base = librosa.get_duration(filename=audio_files[0])
226
+ print("Duration:", duration_base)
227
+ except Exception as e:
228
+ print(e)
229
+
230
+ if file_m is not None and file_m.endswith(".txt"):
231
+ file_m, file_index = find_my_model(file_m, file_index)
232
+ print(file_m, file_index)
233
+
234
+ random_tag = "USER_" + str(random.randint(10000000, 99999999))
235
+
236
+ # Hardcoding pitch algorithm and other parameters
237
+ pitch_alg = "rmvpe+"
238
+ pitch_lvl = 0
239
+ index_inf = 0.75
240
+ r_m_f = 3
241
+ e_r = 0.25
242
+ c_b_p = 0.5
243
+
244
+ converter.apply_conf(
245
+ tag=random_tag,
246
+ file_model=file_m,
247
+ pitch_algo=pitch_alg,
248
+ pitch_lvl=pitch_lvl,
249
+ file_index=file_index,
250
+ index_influence=index_inf,
251
+ respiration_median_filtering=r_m_f,
252
+ envelope_ratio=e_r,
253
+ consonant_breath_protection=c_b_p,
254
+ resample_sr=44100 if audio_files[0].endswith('.mp3') else 0,
255
+ )
256
+ time.sleep(0.1)
257
+
258
+ result = convert_now(audio_files, random_tag, converter)
259
+
260
+ # # Always apply noise reduction
261
+ # result = apply_noisereduce(result)
262
+
263
+ return result
264
+
265
+ def model_conf():
266
+ model_files = [f for f in os.listdir("models") if f.endswith(".pth")]
267
+ return gr.Dropdown(
268
+ label="Select Model File",
269
+ choices=model_files,
270
+ value=model_files[0] if model_files else None,
271
+ interactive=True,
272
+ )
273
+
274
+ def index_conf():
275
+ index_files = [f for f in os.listdir("models") if f.endswith(".index")]
276
+ return gr.Dropdown(
277
+ label="Select Index File",
278
+ choices=index_files,
279
+ value=index_files[0] if index_files else None,
280
+ interactive=True,
281
+ )
282
+
283
+ def audio_conf():
284
+ return gr.File(
285
+ label="Audio files",
286
+ file_count="multiple",
287
+ type="filepath",
288
+ container=True,
289
+ )
290
+
291
+ def button_conf():
292
+ return gr.Button(
293
+ "Inference",
294
+ variant="primary",
295
+ )
296
+
297
+ def output_conf():
298
+ return gr.File(
299
+ label="Result",
300
+ file_count="multiple",
301
+ interactive=False,
302
+ )
303
+
304
+ def get_gui(theme):
305
+ with gr.Blocks(theme=theme, delete_cache=(3200, 3200)) as app:
306
+ gr.Markdown(title)
307
+
308
+ aud = audio_conf()
309
+
310
+ model = model_conf()
311
+ indx = index_conf()
312
+ button_base = button_conf()
313
+ output_base = output_conf()
314
+
315
+ button_base.click(
316
+ run,
317
+ inputs=[
318
+ aud,
319
+ model,
320
+ indx,
321
+ ],
322
+ outputs=[output_base],
323
+ )
324
+
325
+ gr.Examples(
326
+ examples=[
327
+ [
328
+ ["./test.ogg"],
329
+ "./model.pth",
330
+ "./model.index",
331
+ ],
332
+ [
333
+ ["./example2/test2.ogg"],
334
+ "./example2/model.pth",
335
+ "./example2/model.index",
336
+ ],
337
+ ],
338
+ fn=run,
339
+ inputs=[
340
+ aud,
341
+ model,
342
+ indx,
343
+ ],
344
+ outputs=[output_base],
345
+ cache_examples=False,
346
+ )
347
+
348
+ return app
349
+
350
+ if __name__ == "__main__":
351
+ app = get_gui(theme)
352
+ app.queue(default_concurrency_limit=40)
353
+ app.launch(
354
+ max_threads=40,
355
+ share=False,
356
+ show_error=True,
357
+ quiet=False,
358
+ debug=False,
359
+ allowed_paths=["./downloads/"],
360
+ )