File size: 7,123 Bytes
8dae870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import soundfile as sf
from core.cloner import KokoClone

# 1. Initialize the cloner globally so models load only once when the server starts
print("Loading KokoClone models for the Web UI...")
cloner = KokoClone()
def clone_voice(text, lang, ref_audio_path):
    """Gradio handler: text + reference audio β†’ cloned speech."""
    if not text or not text.strip():
        raise gr.Error("Please enter some text.")
    if not ref_audio_path:
        raise gr.Error("Please upload or record a reference audio file.")

    output_file = "gradio_output.wav"

    try:
        cloner.generate(
            text=text,
            lang=lang,
            reference_audio=ref_audio_path,
            output_path=output_file
        )
        return output_file
    except Exception as e:
        raise gr.Error(f"An error occurred during generation: {str(e)}")


def convert_voice(source_audio_path, ref_audio_path):
    """Gradio handler: source audio + reference audio β†’ re-voiced speech."""
    if not source_audio_path:
        raise gr.Error("Please upload or record a source audio file.")
    if not ref_audio_path:
        raise gr.Error("Please upload or record a reference audio file.")

    output_file = "gradio_convert_output.wav"

    try:
        cloner.convert(
            source_audio=source_audio_path,
            reference_audio=ref_audio_path,
            output_path=output_file
        )
        return output_file
    except Exception as e:
        raise gr.Error(f"An error occurred during conversion: {str(e)}")

# 2. Build the Gradio UI using Blocks
with gr.Blocks() as demo:
    gr.Markdown(
        """

        <div style="text-align: center;">

            <h1>🎧 KokoClone</h1>

            <p>Voice Cloning, Now Inside Kokoro.<br>

            Generate natural multilingual speech and clone any target voice with ease.<br>

            <i>Built on Kokoro TTS.</i></p>

        </div>

        """
    )

    with gr.Tabs():
        # ── Tab 1: Text β†’ Cloned Speech ─────────────────────────────────────
        with gr.Tab("🎀 Text β†’ Clone"):
            with gr.Row():
                with gr.Column(scale=1):
                    text_input = gr.Textbox(
                        label="1. Text to Synthesize",
                        lines=4,
                        placeholder="Enter the text you want spoken..."
                    )

                    lang_input = gr.Dropdown(
                        label="2. Language",
                        choices=[
                            ("English", "en"),
                            ("Hindi", "hi"),
                            ("French", "fr"),
                            ("Japanese", "ja"),
                            ("Chinese", "zh"),
                            ("Italian", "it"),
                            ("Spanish", "es"),
                            ("Portuguese", "pt")
                        ],
                        value="en"
                    )

                    ref_audio_input = gr.Audio(
                        label="3. Reference Voice (Upload or Record)",
                        type="filepath"
                    )

                    submit_btn = gr.Button("πŸš€ Generate Clone", variant="primary")

                with gr.Column(scale=1):
                    output_audio = gr.Audio(
                        label="Generated Cloned Audio",
                        interactive=False,
                        autoplay=False
                    )

                    gr.Markdown(
                        """

                        <br>



                        ### πŸ’‘ Tips for Best Results:

                        * **Clean Audio:** Use a reference audio clip without background noise or music.

                        * **Length:** A reference clip of 3 to 10 seconds is usually the sweet spot.

                        * **Language Match:** Make sure the selected language matches the text you typed!

                        * **First Run:** The very first generation might take a few extra seconds while the models allocate memory.

                        """
                    )

            submit_btn.click(
                fn=lambda: gr.update(value="βŒ› Generating...", interactive=False),
                outputs=submit_btn
            ).then(
                fn=clone_voice,
                inputs=[text_input, lang_input, ref_audio_input],
                outputs=output_audio
            ).then(
                fn=lambda: gr.update(value="πŸš€ Generate Clone", interactive=True),
                outputs=submit_btn
            )

        # ── Tab 2: Audio β†’ Re-voiced Speech ─────────────────────────────────
        with gr.Tab("πŸ” Audio β†’ Clone"):
            with gr.Row():
                with gr.Column(scale=1):
                    source_audio_input = gr.Audio(
                        label="1. Source Audio (speech to re-voice)",
                        type="filepath"
                    )

                    ref_audio_convert_input = gr.Audio(
                        label="2. Reference Voice (target speaker)",
                        type="filepath"
                    )

                    convert_btn = gr.Button("πŸ” Convert Voice", variant="primary")

                with gr.Column(scale=1):
                    convert_output_audio = gr.Audio(
                        label="Converted Audio",
                        interactive=False,
                        autoplay=False
                    )

                    gr.Markdown(
                        """

                        <br>



                        ### πŸ’‘ How it works:

                        * Upload any speech recording as the **source**.

                        * Upload a short clip of the **target speaker** as the reference.

                        * KokoClone re-voices the source speech to sound like the reference β€” no transcription needed.



                        ### Tips:

                        * Clean, noise-free audio works best for both inputs.

                        * Reference clips of 3–10 seconds give the best voice transfer.

                        """
                    )

            convert_btn.click(
                fn=lambda: gr.update(value="βŒ› Converting...", interactive=False),
                outputs=convert_btn
            ).then(
                fn=convert_voice,
                inputs=[source_audio_input, ref_audio_convert_input],
                outputs=convert_output_audio
            ).then(
                fn=lambda: gr.update(value="πŸ” Convert Voice", interactive=True),
                outputs=convert_btn
            )

# 4. Launch the app
if __name__ == "__main__":
    # Gradio 6.0 fix: Moved theme here and removed show_api
    demo.launch(server_name="0.0.0.0")