File size: 4,627 Bytes
9c72307
a7fc05c
 
9c72307
 
 
e6792b8
9c72307
a7fc05c
9c72307
a7fc05c
9c72307
 
a7fc05c
 
9c72307
 
 
 
 
 
 
 
 
a7fc05c
9c72307
 
 
 
 
 
 
a7fc05c
e6792b8
9c72307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e49c838
9c72307
 
 
e6792b8
a7fc05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c72307
 
 
 
 
 
 
 
 
a7fc05c
 
 
 
 
 
 
 
 
 
9c72307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6792b8
 
9c72307
 
 
a7fc05c
e6792b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import gradio as gr
import shutil
import tempfile
from core import run_model_blender_script
from assets.i18n.i18n import I18nAuto
import requests
import zipfile

i18n = I18nAuto()

# Global variables to store the downloaded model paths
model_paths = {"model_a": None, "model_b": None}


def download_and_extract(url, extract_to):
    try:
        with requests.get(url, stream=True) as r:
            r.raise_for_status()
            with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
                tmp_file.write(r.content)
        
        with zipfile.ZipFile(tmp_file.name, 'r') as zip_ref:
            zip_ref.extractall(extract_to)

        pth_files = [os.path.join(extract_to, f) for f in os.listdir(extract_to) if f.endswith('.pth')]
        if len(pth_files) == 0:
            raise FileNotFoundError(f"No .pth files found in {extract_to}")
        
        return pth_files[0]  # Assuming there's one .pth file per model
    except Exception as e:
        raise RuntimeError(f"Failed to download or extract model from {url}. Error: {str(e)}")


def download_models(model_url_1, model_url_2):
    try:
        # Create temporary directories for model extraction
        temp_dir_1 = tempfile.mkdtemp()
        temp_dir_2 = tempfile.mkdtemp()

        # Download and extract models
        model_paths["model_a"] = download_and_extract(model_url_1, temp_dir_1)
        model_paths["model_b"] = download_and_extract(model_url_2, temp_dir_2)

        return "Models downloaded successfully!", model_paths["model_a"], model_paths["model_b"]
    except Exception as e:
        return f"An error occurred during download: {str(e)}", None, None


def blend_models(model_name, alpha_a):
    try:
        if not model_paths["model_a"] or not model_paths["model_b"]:
            raise FileNotFoundError("Models not found. Please download the models first.")
        
        message, model_blended = run_model_blender_script(model_name, model_paths["model_b"], model_paths["model_a"], alpha_a)
        return message, model_blended
    except Exception as e:
        return f"An error occurred blending the models: {str(e)}", None


def voice_blender_tab():
    gr.Markdown(i18n("## Voice Blender"))
    gr.Markdown(
        i18n(
            "Select two voice models, set your desired blend percentage, and blend them into an entirely new voice."
        )
    )
    with gr.Column():
        model_fusion_name = gr.Textbox(
            label=i18n("Model Name"),
            info=i18n("Name of the new model."),
            value="",
            max_lines=1,
            interactive=True,
            placeholder=i18n("Enter model name"),
        )
        model_fusion_a_url = gr.Textbox(
            label=i18n("Model A URL"),
            placeholder=i18n("Enter Hugging Face URL for Model A"),
        )
        model_fusion_b_url = gr.Textbox(
            label=i18n("Model B URL"),
            placeholder=i18n("Enter Hugging Face URL for Model B"),
        )
        download_button = gr.Button(i18n("Download Models"), variant="primary")
        alpha_a = gr.Slider(
            minimum=0,
            maximum=1,
            label=i18n("Blend Ratio"),
            value=0.5,
            interactive=True,
            info=i18n(
                "Adjusting the position more towards one side or the other will make the model more similar to the first or second."
            ),
        )
        blend_button = gr.Button(i18n("Blend Models"), variant="primary")

        # Outputs
        download_info = gr.Textbox(
            label=i18n("Download Information"),
            value="",
            interactive=False,
        )
        model_fusion_a_path = gr.Textbox(
            label=i18n("Model A Path"),
            value="",
            interactive=False,
        )
        model_fusion_b_path = gr.Textbox(
            label=i18n("Model B Path"),
            value="",
            interactive=False,
        )
        model_fusion_output_info = gr.Textbox(
            label=i18n("Output Information"),
            value="",
            interactive=False,
        )
        model_fusion_pth_output = gr.File(
            label=i18n("Download Blended Model"),
            type="filepath",
            interactive=False,
        )

    download_button.click(
        fn=download_models,
        inputs=[model_fusion_a_url, model_fusion_b_url],
        outputs=[download_info, model_fusion_a_path, model_fusion_b_path],
    )

    blend_button.click(
        fn=blend_models,
        inputs=[model_fusion_name, alpha_a],
        outputs=[model_fusion_output_info, model_fusion_pth_output],
    )