PatnaikAshish commited on
Commit
159c62c
·
verified ·
1 Parent(s): b72a5dc

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from core.cloner import KokoClone
4
+
5
+ # 1. Initialize the cloner globally so models load only once when the server starts
6
+ print("Loading KokoClone models for the Web UI...")
7
+ cloner = KokoClone()
8
+
9
+ def clone_voice(text, lang, ref_audio_path):
10
+ """Gradio prediction function."""
11
+ if not text or not text.strip():
12
+ raise gr.Error("Please enter some text.")
13
+ if not ref_audio_path:
14
+ raise gr.Error("Please upload or record a reference audio file.")
15
+
16
+ output_file = "gradio_output.wav"
17
+
18
+ try:
19
+ # Call the core engine
20
+ cloner.generate(
21
+ text=text,
22
+ lang=lang,
23
+ reference_audio=ref_audio_path,
24
+ output_path=output_file
25
+ )
26
+ return output_file
27
+ except Exception as e:
28
+ raise gr.Error(f"An error occurred during generation: {str(e)}")
29
+
30
+ # 2. Build the Gradio UI using Blocks
31
+ # Gradio 6.0 fix: Removed theme from here
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown(
34
+ """
35
+ <div style="text-align: center;">
36
+ <h1>🎧 KokoClone</h1>
37
+ <p>Voice Cloning, Now Inside Kokoro.<br>
38
+ Generate natural multilingual speech and clone any target voice with ease.<br>
39
+ <i>Built on Kokoro TTS.</i></p>
40
+ </div>
41
+ """
42
+ )
43
+
44
+ with gr.Row():
45
+ # LEFT COLUMN: Inputs
46
+ with gr.Column(scale=1):
47
+ text_input = gr.Textbox(
48
+ label="1. Text to Synthesize",
49
+ lines=4,
50
+ placeholder="Enter the text you want spoken..."
51
+ )
52
+
53
+ lang_input = gr.Dropdown(
54
+ label="2. Language",
55
+ choices=[
56
+ ("English", "en"),
57
+ ("Hindi", "hi"),
58
+ ("French", "fr"),
59
+ ("Japanese", "ja"),
60
+ ("Chinese", "zh"),
61
+ ("Italian", "it"),
62
+ ("Spanish", "es"),
63
+ ("Portuguese", "pt")
64
+ ],
65
+ value="en"
66
+ )
67
+
68
+ # Using type="filepath" passes the temp file path directly to our cloner
69
+ ref_audio_input = gr.Audio(
70
+ label="3. Reference Voice (Upload or Record)",
71
+ type="filepath"
72
+ )
73
+
74
+ submit_btn = gr.Button("🚀 Generate Clone", variant="primary")
75
+
76
+ # RIGHT COLUMN: Outputs and Instructions
77
+ with gr.Column(scale=1):
78
+ output_audio = gr.Audio(
79
+ label="Generated Cloned Audio",
80
+ interactive=False,
81
+ autoplay=False
82
+ )
83
+
84
+ gr.Markdown(
85
+ """
86
+ <br>
87
+
88
+ ### 💡 Tips for Best Results:
89
+ * **Clean Audio:** Use a reference audio clip without background noise or music.
90
+ * **Length:** A reference clip of 3 to 10 seconds is usually the sweet spot.
91
+ * **Language Match:** Make sure the selected language matches the text you typed!
92
+ * **First Run:** The very first generation might take a few extra seconds while the models allocate memory.
93
+ """
94
+ )
95
+
96
+ # 3. Wire the button to the function
97
+ submit_btn.click(
98
+ fn=clone_voice,
99
+ inputs=[text_input, lang_input, ref_audio_input],
100
+ outputs=output_audio
101
+ )
102
+
103
+ # 4. Launch the app
104
+ if __name__ == "__main__":
105
+ # Gradio 6.0 fix: Moved theme here and removed show_api
106
+ demo.launch(inbrowser=True, theme=gr.themes.Soft())