File size: 5,486 Bytes
eb87c7d
 
695751c
eb87c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4513e9
 
eb87c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18b9202
 
0f20885
18b9202
 
eb87c7d
cdbbd0a
eb87c7d
695751c
 
 
 
 
 
eb87c7d
 
18b9202
eb87c7d
 
18b9202
 
eb87c7d
18b9202
 
 
eb87c7d
 
 
695751c
 
 
 
 
eb87c7d
695751c
 
eb87c7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144db4c
 
eb87c7d
 
 
144db4c
eb87c7d
 
144db4c
eb87c7d
144db4c
eb87c7d
 
18b9202
eb87c7d
 
 
 
 
 
 
18b9202
 
 
 
eb87c7d
 
 
 
 
 
 
18b9202
eb87c7d
 
 
 
a4513e9
eb87c7d
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import sys
import traceback
import uuid
from types import ModuleType

# Keep compatibility with dependencies that import lzma in this environment.
class MockLZMA(ModuleType):
    LZMAError = Exception
    FORMAT_AUTO = 0
    FORMAT_XZ = 1
    FORMAT_ALONE = 2
    FORMAT_RAW = 3
    CHECK_NONE = 0
    CHECK_CRC32 = 1
    CHECK_CRC64 = 4
    CHECK_SHA256 = 12

    class LZMAFile:
        def __init__(self, *args, **kwargs):
            raise NotImplementedError("lzma is not supported in this environment")

        def read(self, *args, **kwargs):
            pass

        def write(self, *args, **kwargs):
            pass

        def seek(self, *args, **kwargs):
            pass

        def tell(self, *args, **kwargs):
            pass

    class LZMACompressor:
        def __init__(self, *args, **kwargs):
            raise NotImplementedError("lzma is not supported in this environment")

    class LZMADecompressor:
        def __init__(self, *args, **kwargs):
            raise NotImplementedError("lzma is not supported in this environment")

    def open(self, *args, **kwargs):
        raise NotImplementedError("lzma is not supported in this environment")

    def compress(self, *args, **kwargs):
        raise NotImplementedError("lzma is not supported in this environment")

    def decompress(self, *args, **kwargs):
        raise NotImplementedError("lzma is not supported in this environment")

    def is_check_supported(self, *args, **kwargs):
        return False


sys.modules["lzma"] = MockLZMA("lzma")

import gradio as gr

# Try importing spaces for Hugging Face ZeroGPU.
try:
    import spaces
except ImportError:
    class spaces:
        @staticmethod
        def GPU(fn=None, duration=None):
            if fn is None:
                return lambda wrapped: wrapped
            return fn

os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"

import torch

if hasattr(torch, "backends") and hasattr(torch.backends, "mps"):
    torch.backends.mps.is_available = lambda: False

from rvc_engine import RVCEngine

print("Initializing RVC engine...")
rvc_engine = RVCEngine()

TEMP_DIR = "temp_audio"
os.makedirs(TEMP_DIR, exist_ok=True)

VOICE_PRESETS = {
    "Adam": ("GOTHMOMMY.pth", -10),
    "MM": ("BetterMommyGirlfriend.pth", 3),
}


@spaces.GPU(duration=300)
def convert_voice_gpu(input_audio, output_path, model_name, pitch_shift):
    try:
        rvc_engine.convert(input_audio, output_path, model_name, pitch_shift, in_process=True)
    except Exception as e:
        traceback.print_exc()
        return f"{type(e).__name__}: {e}"
    return None


def rvc_handler(input_audio, voice_preset):
    if not input_audio:
        raise gr.Error("Please upload an audio file to convert.")
    if voice_preset not in VOICE_PRESETS:
        raise gr.Error("Please select a voice.")

    model_name, pitch_shift = VOICE_PRESETS[voice_preset]
    if model_name not in rvc_engine.get_available_models():
        raise gr.Error(f"The model for {voice_preset} is not available.")

    output_path = os.path.join(TEMP_DIR, f"rvc_output_{uuid.uuid4().hex}.wav")
    try:
        conversion_error = convert_voice_gpu(
            input_audio, output_path, model_name, pitch_shift
        )
        if conversion_error:
            raise gr.Error(f"RVC conversion failed: {conversion_error}")
        return output_path
    except gr.Error:
        raise
    except Exception as e:
        raise gr.Error(f"RVC conversion failed: {e}") from e


custom_css = """
.container {
    max-width: 1000px;
    margin: 0 auto;
    padding: 20px;
}
.header {
    text-align: center;
    margin-bottom: 30px;
    background: linear-gradient(135deg, #4f46e5, #0891b2);
    padding: 30px;
    border-radius: 12px;
    color: white;
    box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}
.header h1 {
    font-size: 2.5rem;
    font-weight: 800;
    margin-bottom: 8px;
    color: white !important;
}
.header p {
    font-size: 1.1rem;
    opacity: 0.95;
    margin: 0;
}
"""

theme = gr.themes.Soft(
    primary_hue="stone",
    secondary_hue="yellow",
    font=[gr.themes.GoogleFont("Outfit"), "ui-sans-serif", "system-ui", "sans-serif"],
)

with gr.Blocks(title="The Voice of Mr. Adam", theme=theme, css=custom_css) as demo:
    with gr.Column(elem_classes="container"):
        with gr.Column(elem_classes="header"):
            gr.Markdown("# ☢️ The Voice of Mr. Adam")
            gr.Markdown(
                "<p>If I sent you here from my resume, it is to prove the work is my own despite obvious differences in presentation.</p>"
            )

        gr.Markdown("### Choose an input audio file and a voice.")
        with gr.Row():
            with gr.Column(scale=1):
                rvc_audio = gr.Audio(
                    sources=["upload", "microphone"],
                    type="filepath",
                    label="Input Voice Audio",
                )
                rvc_voice = gr.Radio(
                    choices=["Adam", "MM"],
                    value="Adam",
                    label="Voice",
                )
                rvc_btn = gr.Button("Convert Voice", variant="primary")
            with gr.Column(scale=1):
                rvc_output = gr.Audio(label="Converted Audio Output", type="filepath")

        rvc_btn.click(
            fn=rvc_handler,
            inputs=[rvc_audio, rvc_voice],
            outputs=[rvc_output],
        )

demo.queue()

if __name__ == "__main__":
    demo.launch()