Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import zipfile
|
4 |
+
import glob
|
5 |
+
from huggingface_hub import login, HfApi, create_repo
|
6 |
+
|
7 |
+
def export_model_to_hf(hftoken, experiment_name, manual_epoch_number, logs_path, repoid, create_new_repo):
|
8 |
+
num_epochs = int(manual_epoch_number) if manual_epoch_number.isdigit() else None
|
9 |
+
|
10 |
+
# Construct the weights path based on the provided number of epochs
|
11 |
+
if num_epochs is not None:
|
12 |
+
weights_path = f"/content/RVC/assets/weights/{experiment_name}_e{num_epochs}*"
|
13 |
+
else:
|
14 |
+
potential = f"/content/RVC/assets/weights/{experiment_name}.pth"
|
15 |
+
if os.path.exists(potential):
|
16 |
+
weights_path = f"/content/RVC/assets/weights/{experiment_name}"
|
17 |
+
else:
|
18 |
+
currentMax = 0
|
19 |
+
for r, _, f in os.walk("/content/RVC/assets/weights/"):
|
20 |
+
for name in f:
|
21 |
+
if(name.endswith(".pth") and (name != experiment_name + ".pth")):
|
22 |
+
if(name.find(experiment_name) == -1):
|
23 |
+
continue
|
24 |
+
pot = name.split('_')
|
25 |
+
ep = pot[len(pot) - 2][1:]
|
26 |
+
if not ep.isdecimal():
|
27 |
+
continue
|
28 |
+
ep = int(ep)
|
29 |
+
if ep > currentMax:
|
30 |
+
currentMax = ep
|
31 |
+
num_epochs = currentMax
|
32 |
+
weights_path = f"/content/RVC/assets/weights/{experiment_name}_e{num_epochs}*"
|
33 |
+
|
34 |
+
weights_files = glob.glob(weights_path + ".pth")
|
35 |
+
if weights_files and any(glob_result := glob.glob(logs_path)):
|
36 |
+
log_file = glob_result[0]
|
37 |
+
output_folder = "/content/toHF"
|
38 |
+
os.makedirs(output_folder, exist_ok=True)
|
39 |
+
output_zip_path = f"{output_folder}/{experiment_name}.zip"
|
40 |
+
with zipfile.ZipFile(output_zip_path, 'w') as zipf:
|
41 |
+
for weights_file in weights_files:
|
42 |
+
zipf.write(weights_file, os.path.basename(weights_file))
|
43 |
+
zipf.write(log_file, os.path.basename(log_file))
|
44 |
+
|
45 |
+
login(token=hftoken)
|
46 |
+
if create_new_repo:
|
47 |
+
create_repo(repoid)
|
48 |
+
|
49 |
+
api = HfApi()
|
50 |
+
api.upload_folder(folder_path=output_folder, repo_id=repoid, repo_type="model")
|
51 |
+
|
52 |
+
return f"Model uploaded successfully to {repoid}"
|
53 |
+
else:
|
54 |
+
return "Couldn't find your model files. Check the found file results above. (Did you run Index Training?)"
|
55 |
+
|
56 |
+
with gr.Blocks() as demo:
|
57 |
+
gr.Markdown("<small>Export Finished Model to HuggingFace<br>[click this to get HF token](https://huggingface.co/settings/tokens)</small>")
|
58 |
+
|
59 |
+
hftoken = gr.Textbox(label="HuggingFace Token (set Role to 'write')", type="password")
|
60 |
+
experiment_name = gr.Textbox(label="Experiment Name", value="rewrite")
|
61 |
+
manual_epoch_number = gr.Textbox(label="Manual Epoch Number (leave blank for auto-detect)", value="")
|
62 |
+
logs_path = gr.Textbox(label="Logs Path", value="/content/RVC/logs/rewrite/added_IVF37_Flat_nprobe_1_rewrite_v2.index")
|
63 |
+
repoid = gr.Textbox(label="HuggingFace Repository ID", value="Hev832/rewrite-sonic")
|
64 |
+
create_new_repo = gr.Checkbox(label="Create New Repository", value=True)
|
65 |
+
|
66 |
+
output = gr.Textbox(label="Output")
|
67 |
+
|
68 |
+
btn = gr.Button("Export Model")
|
69 |
+
btn.click(export_model_to_hf, inputs=[hftoken, experiment_name, manual_epoch_number, logs_path, repoid, create_new_repo], outputs=output)
|
70 |
+
|
71 |
+
demo.launch()
|