PlayerBPlaytime commited on
Commit
9c72307
1 Parent(s): e2f0632

Update tabs/voice_blender/voice_blender.py

Browse files
Files changed (1) hide show
  1. tabs/voice_blender/voice_blender.py +93 -76
tabs/voice_blender/voice_blender.py CHANGED
@@ -1,40 +1,62 @@
1
- import os, sys
2
  import gradio as gr
3
  import shutil
 
 
 
4
  import requests
5
- from zipfile import ZipFile
6
- from tempfile import TemporaryDirectory
7
 
8
- now_dir = os.getcwd()
9
- sys.path.append(now_dir)
10
 
11
- from assets.i18n.i18n import I18nAuto
12
- from core import run_model_blender_script
13
 
14
- i18n = I18nAuto()
15
 
16
- def download_and_extract_model(url):
17
- temp_dir = TemporaryDirectory()
18
- zip_path = os.path.join(temp_dir.name, "model.zip")
 
 
 
 
 
 
19
 
20
- with requests.get(url, stream=True) as r:
21
- with open(zip_path, 'wb') as f:
22
- shutil.copyfileobj(r.raw, f)
 
 
 
 
23
 
24
- with ZipFile(zip_path, 'r') as zip_ref:
25
- zip_ref.extractall(temp_dir.name)
26
-
27
- pth_files = [os.path.join(temp_dir.name, f) for f in os.listdir(temp_dir.name) if f.endswith('.pth')]
28
 
29
- if pth_files:
30
- return pth_files[0]
31
- else:
32
- raise FileNotFoundError("No se encontró un archivo .pth en el zip descargado.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- def download_and_set_paths(link_a, link_b):
35
- path_a = download_and_extract_model(link_a)
36
- path_b = download_and_extract_model(link_b)
37
- return path_a, path_b
38
 
39
  def voice_blender_tab():
40
  gr.Markdown(i18n("## Voice Blender"))
@@ -52,33 +74,15 @@ def voice_blender_tab():
52
  interactive=True,
53
  placeholder=i18n("Enter model name"),
54
  )
55
- with gr.Row():
56
- with gr.Column():
57
- model_fusion_a_link = gr.Textbox(
58
- label=i18n("Hugging Face Link for Model A"),
59
- value="",
60
- interactive=True,
61
- placeholder=i18n("Enter Hugging Face link for model A"),
62
- )
63
- model_fusion_a_path = gr.Textbox(
64
- label=i18n("Path to Model A"),
65
- value="",
66
- interactive=False,
67
- placeholder=i18n("Path will be set automatically after download"),
68
- )
69
- with gr.Column():
70
- model_fusion_b_link = gr.Textbox(
71
- label=i18n("Hugging Face Link for Model B"),
72
- value="",
73
- interactive=True,
74
- placeholder=i18n("Enter Hugging Face link for model B"),
75
- )
76
- model_fusion_b_path = gr.Textbox(
77
- label=i18n("Path to Model B"),
78
- value="",
79
- interactive=False,
80
- placeholder=i18n("Path will be set automatically after download"),
81
- )
82
  alpha_a = gr.Slider(
83
  minimum=0,
84
  maximum=1,
@@ -89,30 +93,43 @@ def voice_blender_tab():
89
  "Adjusting the position more towards one side or the other will make the model more similar to the first or second."
90
  ),
91
  )
92
- model_fusion_button = gr.Button(i18n("Fusion"), variant="primary")
93
- with gr.Row():
94
- model_fusion_output_info = gr.Textbox(
95
- label=i18n("Output Information"),
96
- info=i18n("The output information will be displayed here."),
97
- value="",
98
- )
99
- model_fusion_pth_output = gr.File(
100
- label=i18n("Download Model"), type="filepath", interactive=False
101
- )
102
-
103
- model_fusion_button.click(
104
- fn=download_and_set_paths,
105
- inputs=[model_fusion_a_link, model_fusion_b_link],
106
- outputs=[model_fusion_a_path, model_fusion_b_path],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  )
108
 
109
- model_fusion_button.click(
110
- fn=run_model_blender_script,
111
- inputs=[
112
- model_fusion_name,
113
- model_fusion_a_path,
114
- model_fusion_b_path,
115
- alpha_a,
116
- ],
117
  outputs=[model_fusion_output_info, model_fusion_pth_output],
118
  )
 
1
+ import os
2
  import gradio as gr
3
  import shutil
4
+ import tempfile
5
+ from core import run_model_blender_script
6
+ from assets.i18n.i18n import I18nAuto
7
  import requests
8
+ import zipfile
 
9
 
10
+ i18n = I18nAuto()
 
11
 
12
+ # Global variables to store the downloaded model paths
13
+ model_paths = {"model_a": None, "model_b": None}
14
 
 
15
 
16
+ def download_and_extract(url, extract_to):
17
+ try:
18
+ with requests.get(url, stream=True) as r:
19
+ r.raise_for_status()
20
+ with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
21
+ tmp_file.write(r.content)
22
+
23
+ with zipfile.ZipFile(tmp_file.name, 'r') as zip_ref:
24
+ zip_ref.extractall(extract_to)
25
 
26
+ pth_files = [os.path.join(extract_to, f) for f in os.listdir(extract_to) if f.endswith('.pth')]
27
+ if len(pth_files) == 0:
28
+ raise FileNotFoundError(f"No .pth files found in {extract_to}")
29
+
30
+ return pth_files[0] # Assuming there's one .pth file per model
31
+ except Exception as e:
32
+ raise RuntimeError(f"Failed to download or extract model from {url}. Error: {str(e)}")
33
 
 
 
 
 
34
 
35
+ def download_models(model_url_1, model_url_2):
36
+ try:
37
+ # Create temporary directories for model extraction
38
+ temp_dir_1 = tempfile.mkdtemp()
39
+ temp_dir_2 = tempfile.mkdtemp()
40
+
41
+ # Download and extract models
42
+ model_paths["model_a"] = download_and_extract(model_url_1, temp_dir_1)
43
+ model_paths["model_b"] = download_and_extract(model_url_2, temp_dir_2)
44
+
45
+ return "Models downloaded successfully!", model_paths["model_a"], model_paths["model_b"]
46
+ except Exception as e:
47
+ return f"An error occurred during download: {str(e)}", None, None
48
+
49
+
50
+ def blend_models(model_name, alpha_a):
51
+ try:
52
+ if not model_paths["model_a"] or not model_paths["model_b"]:
53
+ raise FileNotFoundError("Models not found. Please download the models first.")
54
+
55
+ message, model_blended = run_model_blender_script(model_name, model_paths["model_a"], model_paths["model_b"], alpha_a)
56
+ return message, model_blended
57
+ except Exception as e:
58
+ return f"An error occurred blending the models: {str(e)}", None
59
 
 
 
 
 
60
 
61
  def voice_blender_tab():
62
  gr.Markdown(i18n("## Voice Blender"))
 
74
  interactive=True,
75
  placeholder=i18n("Enter model name"),
76
  )
77
+ model_fusion_a_url = gr.Textbox(
78
+ label=i18n("Model A URL"),
79
+ placeholder=i18n("Enter Hugging Face URL for Model A"),
80
+ )
81
+ model_fusion_b_url = gr.Textbox(
82
+ label=i18n("Model B URL"),
83
+ placeholder=i18n("Enter Hugging Face URL for Model B"),
84
+ )
85
+ download_button = gr.Button(i18n("Download Models"), variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  alpha_a = gr.Slider(
87
  minimum=0,
88
  maximum=1,
 
93
  "Adjusting the position more towards one side or the other will make the model more similar to the first or second."
94
  ),
95
  )
96
+ blend_button = gr.Button(i18n("Blend Models"), variant="primary")
97
+
98
+ # Outputs
99
+ download_info = gr.Textbox(
100
+ label=i18n("Download Information"),
101
+ value="",
102
+ interactive=False,
103
+ )
104
+ model_fusion_a_path = gr.Textbox(
105
+ label=i18n("Model A Path"),
106
+ value="",
107
+ interactive=False,
108
+ )
109
+ model_fusion_b_path = gr.Textbox(
110
+ label=i18n("Model B Path"),
111
+ value="",
112
+ interactive=False,
113
+ )
114
+ model_fusion_output_info = gr.Textbox(
115
+ label=i18n("Output Information"),
116
+ value="",
117
+ interactive=False,
118
+ )
119
+ model_fusion_pth_output = gr.File(
120
+ label=i18n("Download Blended Model"),
121
+ type="filepath",
122
+ interactive=False,
123
+ )
124
+
125
+ download_button.click(
126
+ fn=download_models,
127
+ inputs=[model_fusion_a_url, model_fusion_b_url],
128
+ outputs=[download_info, model_fusion_a_path, model_fusion_b_path],
129
  )
130
 
131
+ blend_button.click(
132
+ fn=blend_models,
133
+ inputs=[model_fusion_name, alpha_a],
 
 
 
 
 
134
  outputs=[model_fusion_output_info, model_fusion_pth_output],
135
  )