TheStinger commited on
Commit
3664c30
Β·
verified Β·
1 Parent(s): 0281a51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +209 -207
app.py CHANGED
@@ -1,207 +1,209 @@
1
- import gradio as gr
2
- import requests
3
- import random
4
- import os
5
- import zipfile # built in module for unzipping files (thank god)
6
- import librosa
7
- import time
8
- from infer_rvc_python import BaseLoader
9
- from pydub import AudioSegment
10
-
11
- # fucking dogshit toggle
12
- try:
13
- import spaces
14
- spaces_status = True
15
- except ImportError:
16
- spaces_status = False
17
-
18
- converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None) # <- yeah so like this handles rvc
19
-
20
- global pth_file
21
- global index_file
22
-
23
- pth_file = "model.pth"
24
- index_file = "model.index"
25
-
26
- #CONFIGS
27
- TEMP_DIR = "temp"
28
- MODEL_PREFIX = "model"
29
- PITCH_ALGO_OPT = [
30
- "pm",
31
- "harvest",
32
- "crepe",
33
- "rmvpe",
34
- "rmvpe+",
35
- ]
36
-
37
-
38
- os.makedirs(TEMP_DIR, exist_ok=True)
39
-
40
- def unzip_file(file):
41
- filename = os.path.basename(file).split(".")[0] # converts "model.zip" to "model" so we can do things
42
- with zipfile.ZipFile(file, 'r') as zip_ref:
43
- zip_ref.extractall(os.path.join(TEMP_DIR, filename)) # might not be very ram efficient...
44
- return True
45
-
46
-
47
- def progress_bar(total, current): # best progress bar ever trust me sunglasses emoji 😎
48
- return "[" + "=" * int(current / total * 20) + ">" + " " * (20 - int(current / total * 20)) + "] " + str(int(current / total * 100)) + "%"
49
-
50
- def download_from_url(url, filename=None):
51
- if "huggingface" not in url:
52
- return ["The URL must be from huggingface", "Failed", "Failed"]
53
- if filename is None:
54
- filename = os.path.join(TEMP_DIR, MODEL_PREFIX + str(random.randint(1, 1000)) + ".zip")
55
- response = requests.get(url)
56
- total = int(response.headers.get('content-length', 0)) # bytes to download (length of the file)
57
- if total > 500000000:
58
-
59
- return ["The file is too large. You can only download files up to 500 MB in size.", "Failed", "Failed"]
60
- current = 0
61
- with open(filename, "wb") as f:
62
- for data in response.iter_content(chunk_size=4096): # download in chunks of 4096 bytes (4kb - helps with memory usage and speed)
63
- f.write(data)
64
- current += len(data)
65
- print(progress_bar(total, current), end="\r") # \r is a carriage return, it moves the cursor to the start of the line so its like tqdm sunglasses emoji 😎
66
-
67
- # unzip because the model is in a zip file lel
68
-
69
- try:
70
- unzip_file(filename)
71
- except Exception as e:
72
- return ["Failed to unzip the file", "Failed", "Failed"] # return early if it fails and like tell the user but its dogshit hahahahahahaha 😎 According to all known laws aviation, there is no way a bee should be able to fly.
73
- unzipped_dir = os.path.join(TEMP_DIR, os.path.basename(filename).split(".")[0]) # just do what we did in unzip_file because we need the directory
74
- pth_files = []
75
- index_files = []
76
- for root, dirs, files in os.walk(unzipped_dir): # could be done more efficiently because nobody stores models in subdirectories but like who cares (it's a futureproofing thing lel)
77
- for file in files:
78
- if file.endswith(".pth"):
79
- pth_files.append(os.path.join(root, file))
80
- elif file.endswith(".index"):
81
- index_files.append(os.path.join(root, file))
82
-
83
- print(pth_files, index_files) # debug print because im fucking stupid and i need to see what is going on
84
- global pth_file
85
- global index_file
86
- pth_file = pth_files[0]
87
- index_file = index_files[0]
88
- return ["Downloaded as " + filename, pth_files[0], index_files[0]]
89
-
90
- if spaces_status:
91
- @spaces.GPU()
92
- def convert_now(audio_files, random_tag, converter):
93
- return converter(
94
- audio_files,
95
- random_tag,
96
- overwrite=False,
97
- parallel_workers=8
98
- )
99
- else:
100
- def convert_now(audio_files, random_tag, converter):
101
- return converter(
102
- audio_files,
103
- random_tag,
104
- overwrite=False,
105
- parallel_workers=8
106
- )
107
-
108
- def run(
109
- audio_files,
110
- file_m,
111
- pitch_alg,
112
- pitch_lvl,
113
- file_index,
114
- index_inf,
115
- r_m_f,
116
- e_r,
117
- c_b_p,
118
- ):
119
- if not audio_files:
120
- raise ValueError("The audio pls")
121
-
122
- if isinstance(audio_files, str):
123
- audio_files = [audio_files]
124
-
125
- try:
126
- duration_base = librosa.get_duration(filename=audio_files[0])
127
- print("Duration:", duration_base)
128
- except Exception as e:
129
- print(e)
130
-
131
- random_tag = "USER_"+str(random.randint(10000000, 99999999))
132
-
133
- converter.apply_conf(
134
- tag=random_tag,
135
- file_model=file_m,
136
- pitch_algo=pitch_alg,
137
- pitch_lvl=pitch_lvl,
138
- file_index=file_index,
139
- index_influence=index_inf,
140
- respiration_median_filtering=r_m_f,
141
- envelope_ratio=e_r,
142
- consonant_breath_protection=c_b_p,
143
- resample_sr=44100 if audio_files[0].endswith('.mp3') else 0,
144
- )
145
- time.sleep(0.1)
146
-
147
- result = convert_now(audio_files, random_tag, converter)
148
-
149
- return result[0]
150
-
151
- with gr.Blocks() as demo:
152
- gr.Markdown("## Ilaria RVC πŸ’–")
153
- with gr.Tab("Inference"):
154
- sound_gui = gr.Audio(value=None,type="filepath",autoplay=False,visible=True,)
155
- pth_file_ui = gr.Textbox(label="Model pth file",value=pth_file,visible=False,interactive=False,) # gradio is fucking weird (im with stupid v)
156
- index_file_ui = gr.Textbox(label="Index pth file",value=index_file,visible=False,interactive=False,) # gradio is fucking weird (im with stupid ^)
157
- pitch_algo_conf = gr.Dropdown(PITCH_ALGO_OPT,value=PITCH_ALGO_OPT[4],label="Pitch algorithm",visible=True,interactive=True,)
158
- pitch_lvl_conf = gr.Slider(label="Pitch level",minimum=-24,maximum=24,step=1,value=0,visible=True,interactive=True,)
159
- index_inf_conf = gr.Slider(minimum=0,maximum=1,label="Index influence",value=0.75,)
160
- respiration_filter_conf = gr.Slider(minimum=0,maximum=7,label="Respiration median filtering",value=3,step=1,interactive=True,)
161
- envelope_ratio_conf = gr.Slider(minimum=0,maximum=1,label="Envelope ratio",value=0.25,interactive=True,)
162
- consonant_protec_conf = gr.Slider(minimum=0,maximum=0.5,label="Consonant breath protection",value=0.5,interactive=True,)
163
- button_conf = gr.Button("Convert",variant="primary",)
164
- output_conf = gr.Audio(type="filepath",label="Output",)
165
-
166
- button_conf.click(
167
- run,
168
- inputs=[
169
- sound_gui,
170
- pth_file_ui,
171
- pitch_algo_conf,
172
- pitch_lvl_conf,
173
- index_file_ui, # put a bullet through my head
174
- index_inf_conf,
175
- respiration_filter_conf,
176
- envelope_ratio_conf,
177
- consonant_protec_conf,
178
- ],
179
- outputs=[output_conf],
180
- )
181
-
182
- with gr.Tab("Download Model"):
183
- # markdown
184
- gr.Markdown(
185
- "Download the model from the following URL and upload it here. (Hugginface RVC model)"
186
- )
187
- model = gr.Textbox(lines=1, label="Model URL")
188
- download_button = gr.Button("Download Model")
189
- status = gr.Textbox(lines=1, label="Status", placeholder="Waiting....", interactive=False)
190
- model_pth = gr.Textbox(lines=1, label="Model pth file", placeholder="Waiting....", interactive=False)
191
- index_pth = gr.Textbox(lines=1, label="Index pth file", placeholder="Waiting....", interactive=False)
192
- download_button.click(download_from_url, model, outputs=[status, model_pth, index_pth])
193
- set_model_button = gr.Button("Set Model")
194
- #set_model_button.click(
195
-
196
- with gr.Tab("Credits"):
197
- gr.Markdown(
198
- """
199
- Ilaria RVC made by [Ilaria](https://huggingface.co/TheStinger) suport her on [ko-fi](https://ko-fi.com/ilariaowo)
200
-
201
- The Inference code is made by [r3gm](https://huggingface.co/r3gm) (his module helped form this space πŸ’–)
202
-
203
- made with ❀️ by [mikus](https://github.com/cappuch) - i hacked it up lel
204
- """
205
- )
206
-
207
- demo.queue(api_open=False).launch(show_api=False) # idk ilaria if you want or dont want to
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import random
4
+ import os
5
+ import zipfile # built in module for unzipping files (thank god)
6
+ import librosa
7
+ import time
8
+ from infer_rvc_python import BaseLoader
9
+ from pydub import AudioSegment
10
+
11
+ # fucking dogshit toggle
12
+ try:
13
+ import spaces
14
+ spaces_status = True
15
+ except ImportError:
16
+ spaces_status = False
17
+
18
+ converter = BaseLoader(only_cpu=False, hubert_path=None, rmvpe_path=None) # <- yeah so like this handles rvc
19
+
20
+ global pth_file
21
+ global index_file
22
+
23
+ pth_file = "model.pth"
24
+ index_file = "model.index"
25
+
26
+ #CONFIGS
27
+ TEMP_DIR = "temp"
28
+ MODEL_PREFIX = "model"
29
+ PITCH_ALGO_OPT = [
30
+ "pm",
31
+ "harvest",
32
+ "crepe",
33
+ "rmvpe",
34
+ "rmvpe+",
35
+ ]
36
+
37
+
38
+ os.makedirs(TEMP_DIR, exist_ok=True)
39
+
40
+ def unzip_file(file):
41
+ filename = os.path.basename(file).split(".")[0] # converts "model.zip" to "model" so we can do things
42
+ with zipfile.ZipFile(file, 'r') as zip_ref:
43
+ zip_ref.extractall(os.path.join(TEMP_DIR, filename)) # might not be very ram efficient...
44
+ return True
45
+
46
+
47
+ def progress_bar(total, current): # best progress bar ever trust me sunglasses emoji 😎
48
+ return "[" + "=" * int(current / total * 20) + ">" + " " * (20 - int(current / total * 20)) + "] " + str(int(current / total * 100)) + "%"
49
+
50
+ def download_from_url(url, filename=None):
51
+ if "huggingface" not in url:
52
+ return ["The URL must be from huggingface", "Failed", "Failed"]
53
+ if filename is None:
54
+ filename = os.path.join(TEMP_DIR, MODEL_PREFIX + str(random.randint(1, 1000)) + ".zip")
55
+ response = requests.get(url)
56
+ total = int(response.headers.get('content-length', 0)) # bytes to download (length of the file)
57
+ if total > 500000000:
58
+
59
+ return ["The file is too large. You can only download files up to 500 MB in size.", "Failed", "Failed"]
60
+ current = 0
61
+ with open(filename, "wb") as f:
62
+ for data in response.iter_content(chunk_size=4096): # download in chunks of 4096 bytes (4kb - helps with memory usage and speed)
63
+ f.write(data)
64
+ current += len(data)
65
+ print(progress_bar(total, current), end="\r") # \r is a carriage return, it moves the cursor to the start of the line so its like tqdm sunglasses emoji 😎
66
+
67
+ # unzip because the model is in a zip file lel
68
+
69
+ try:
70
+ unzip_file(filename)
71
+ except Exception as e:
72
+ return ["Failed to unzip the file", "Failed", "Failed"] # return early if it fails and like tell the user but its dogshit hahahahahahaha 😎 According to all known laws aviation, there is no way a bee should be able to fly.
73
+ unzipped_dir = os.path.join(TEMP_DIR, os.path.basename(filename).split(".")[0]) # just do what we did in unzip_file because we need the directory
74
+ pth_files = []
75
+ index_files = []
76
+ for root, dirs, files in os.walk(unzipped_dir): # could be done more efficiently because nobody stores models in subdirectories but like who cares (it's a futureproofing thing lel)
77
+ for file in files:
78
+ if file.endswith(".pth"):
79
+ pth_files.append(os.path.join(root, file))
80
+ elif file.endswith(".index"):
81
+ index_files.append(os.path.join(root, file))
82
+
83
+ print(pth_files, index_files) # debug print because im fucking stupid and i need to see what is going on
84
+ global pth_file
85
+ global index_file
86
+ pth_file = pth_files[0]
87
+ index_file = index_files[0]
88
+ return ["Downloaded as " + filename, pth_files[0], index_files[0]]
89
+
90
+ if spaces_status:
91
+ @spaces.GPU()
92
+ def convert_now(audio_files, random_tag, converter):
93
+ return converter(
94
+ audio_files,
95
+ random_tag,
96
+ overwrite=False,
97
+ parallel_workers=8
98
+ )
99
+ else:
100
+ def convert_now(audio_files, random_tag, converter):
101
+ return converter(
102
+ audio_files,
103
+ random_tag,
104
+ overwrite=False,
105
+ parallel_workers=8
106
+ )
107
+
108
+ def run(
109
+ audio_files,
110
+ file_m,
111
+ pitch_alg,
112
+ pitch_lvl,
113
+ file_index,
114
+ index_inf,
115
+ r_m_f,
116
+ e_r,
117
+ c_b_p,
118
+ ):
119
+ if not audio_files:
120
+ raise ValueError("The audio pls")
121
+
122
+ if isinstance(audio_files, str):
123
+ audio_files = [audio_files]
124
+
125
+ try:
126
+ duration_base = librosa.get_duration(filename=audio_files[0])
127
+ print("Duration:", duration_base)
128
+ except Exception as e:
129
+ print(e)
130
+
131
+ random_tag = "USER_"+str(random.randint(10000000, 99999999))
132
+
133
+ converter.apply_conf(
134
+ tag=random_tag,
135
+ file_model=file_m,
136
+ pitch_algo=pitch_alg,
137
+ pitch_lvl=pitch_lvl,
138
+ file_index=file_index,
139
+ index_influence=index_inf,
140
+ respiration_median_filtering=r_m_f,
141
+ envelope_ratio=e_r,
142
+ consonant_breath_protection=c_b_p,
143
+ resample_sr=44100 if audio_files[0].endswith('.mp3') else 0,
144
+ )
145
+ time.sleep(0.1)
146
+
147
+ result = convert_now(audio_files, random_tag, converter)
148
+
149
+ return result[0]
150
+
151
+ with gr.Blocks() as demo:
152
+ gr.Markdown("## Ilaria RVC πŸ’–")
153
+ with gr.Tab("Inference"):
154
+ sound_gui = gr.Audio(value=None,type="filepath",autoplay=False,visible=True,)
155
+ pth_file_ui = gr.Textbox(label="Model pth file",value=pth_file,visible=False,interactive=False,) # gradio is fucking weird (im with stupid v)
156
+ index_file_ui = gr.Textbox(label="Index pth file",value=index_file,visible=False,interactive=False,) # gradio is fucking weird (im with stupid ^)
157
+ pitch_algo_conf = gr.Dropdown(PITCH_ALGO_OPT,value=PITCH_ALGO_OPT[4],label="Pitch algorithm",visible=True,interactive=True,)
158
+ pitch_lvl_conf = gr.Slider(label="Pitch level",minimum=-24,maximum=24,step=1,value=0,visible=True,interactive=True,)
159
+ index_inf_conf = gr.Slider(minimum=0,maximum=1,label="Index influence",value=0.75,)
160
+ respiration_filter_conf = gr.Slider(minimum=0,maximum=7,label="Respiration median filtering",value=3,step=1,interactive=True,)
161
+ envelope_ratio_conf = gr.Slider(minimum=0,maximum=1,label="Envelope ratio",value=0.25,interactive=True,)
162
+ consonant_protec_conf = gr.Slider(minimum=0,maximum=0.5,label="Consonant breath protection",value=0.5,interactive=True,)
163
+ button_conf = gr.Button("Convert",variant="primary",)
164
+ output_conf = gr.Audio(type="filepath",label="Output",)
165
+
166
+ button_conf.click(
167
+ run,
168
+ inputs=[
169
+ sound_gui,
170
+ pth_file_ui,
171
+ pitch_algo_conf,
172
+ pitch_lvl_conf,
173
+ index_file_ui, # put a bullet through my head
174
+ index_inf_conf,
175
+ respiration_filter_conf,
176
+ envelope_ratio_conf,
177
+ consonant_protec_conf,
178
+ ],
179
+ outputs=[output_conf],
180
+ )
181
+
182
+ with gr.Tab("Download Model"):
183
+ # markdown
184
+ gr.Markdown(
185
+ "Download the model from the following URL and upload it here. (Hugginface RVC model)"
186
+ )
187
+ model = gr.Textbox(lines=1, label="Model URL")
188
+ download_button = gr.Button("Download Model")
189
+ status = gr.Textbox(lines=1, label="Status", placeholder="Waiting....", interactive=False)
190
+ model_pth = gr.Textbox(lines=1, label="Model pth file", placeholder="Waiting....", interactive=False)
191
+ index_pth = gr.Textbox(lines=1, label="Index pth file", placeholder="Waiting....", interactive=False)
192
+ download_button.click(download_from_url, model, outputs=[status, model_pth, index_pth])
193
+ set_model_button = gr.Button("Set Model")
194
+ #set_model_button.click(
195
+
196
+ with gr.Tab("Credits"):
197
+ gr.Markdown(
198
+ """
199
+ Ilaria RVC made by [Ilaria](https://huggingface.co/TheStinger) suport her on [ko-fi](https://ko-fi.com/ilariaowo)
200
+
201
+ The Inference code is made by [r3gm](https://huggingface.co/r3gm) (his module helped form this space πŸ’–)
202
+
203
+ made with ❀️ by [mikus](https://github.com/cappuch) - i hacked it up lel
204
+
205
+ ### **In loving memory of JLabDX** πŸ•ŠοΈ
206
+ """
207
+ )
208
+
209
+ demo.queue(api_open=False).launch(show_api=False) # idk ilaria if you want or dont want to