Spaces:
Build error
Build error
Aseem Gupta
commited on
Commit
·
22fdf85
1
Parent(s):
c879c20
test8 test7 worked but limited
Browse files- app.py +50 -7
- requirements.txt +3 -0
app.py
CHANGED
@@ -1,24 +1,55 @@
|
|
1 |
import gradio as gr
|
2 |
from TTS.api import TTS
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
|
|
5 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
6 |
|
7 |
-
# Load the model
|
8 |
-
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2")
|
9 |
tts.to("cpu")
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Function to generate voice
|
12 |
-
def generate_voice(text, speaker_audio):
|
13 |
-
output_path = "
|
|
|
14 |
tts.tts_to_file(
|
15 |
text=text,
|
16 |
speaker_wav=speaker_audio,
|
17 |
file_path=output_path,
|
18 |
-
language="en"
|
19 |
)
|
20 |
return output_path
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# Gradio interface
|
23 |
with gr.Blocks() as demo:
|
24 |
gr.Markdown("# 🗣️ Voice Cloning with Coqui XTTS-v2")
|
@@ -26,16 +57,28 @@ with gr.Blocks() as demo:
|
|
26 |
with gr.Row():
|
27 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type the text you want to synthesize...")
|
28 |
speaker_audio_input = gr.Audio(label="Upload Speaker Audio (WAV)", type="filepath")
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
output_audio = gr.Audio(label="Generated Voice", type="filepath")
|
31 |
|
32 |
generate_button = gr.Button("Generate Voice")
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
generate_button.click(
|
35 |
fn=generate_voice,
|
36 |
-
inputs=[text_input, speaker_audio_input],
|
37 |
outputs=output_audio
|
38 |
)
|
39 |
|
40 |
# Launch the app
|
41 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from TTS.api import TTS
|
3 |
import os
|
4 |
+
import tempfile
|
5 |
+
import sounddevice as sd
|
6 |
+
from scipy.io.wavfile import write
|
7 |
|
8 |
+
# Agree to Coqui's terms
|
9 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
10 |
|
11 |
+
# Load the model and optimize CPU usage
|
12 |
+
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
13 |
tts.to("cpu")
|
14 |
|
15 |
+
# Supported languages by the model
|
16 |
+
LANGUAGES = {
|
17 |
+
"English": "en",
|
18 |
+
"Spanish": "es",
|
19 |
+
"German": "de",
|
20 |
+
"French": "fr",
|
21 |
+
"Italian": "it",
|
22 |
+
"Hindi" : "hi",
|
23 |
+
"Russian": "ru",
|
24 |
+
"Spanish": "es",
|
25 |
+
"Turkish": "tr",
|
26 |
+
"Japanese": "ja",
|
27 |
+
"Korean": "ko",
|
28 |
+
"Hungarian": "hu"
|
29 |
+
}
|
30 |
+
|
31 |
# Function to generate voice
|
32 |
+
def generate_voice(text, speaker_audio, language):
|
33 |
+
output_path = tempfile.mktemp(suffix=".wav")
|
34 |
+
|
35 |
tts.tts_to_file(
|
36 |
text=text,
|
37 |
speaker_wav=speaker_audio,
|
38 |
file_path=output_path,
|
39 |
+
language=LANGUAGES.get(language, "en")
|
40 |
)
|
41 |
return output_path
|
42 |
|
43 |
+
# Function to record audio from the mic
|
44 |
+
def record_audio(duration=10, filename="mic_input.wav"):
|
45 |
+
fs = 44100 # Sample rate
|
46 |
+
print("Recording...")
|
47 |
+
audio_data = sd.rec(int(duration * fs), samplerate=fs, channels=1)
|
48 |
+
sd.wait() # Wait until recording is finished
|
49 |
+
write(filename, fs, audio_data)
|
50 |
+
print(f"Recording saved as {filename}")
|
51 |
+
return filename
|
52 |
+
|
53 |
# Gradio interface
|
54 |
with gr.Blocks() as demo:
|
55 |
gr.Markdown("# 🗣️ Voice Cloning with Coqui XTTS-v2")
|
|
|
57 |
with gr.Row():
|
58 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type the text you want to synthesize...")
|
59 |
speaker_audio_input = gr.Audio(label="Upload Speaker Audio (WAV)", type="filepath")
|
60 |
+
language_dropdown = gr.Dropdown(
|
61 |
+
label="Select Output Language",
|
62 |
+
choices=list(LANGUAGES.keys()),
|
63 |
+
value="English"
|
64 |
+
)
|
65 |
+
mic_button = gr.Button("Record from Mic")
|
66 |
|
67 |
output_audio = gr.Audio(label="Generated Voice", type="filepath")
|
68 |
|
69 |
generate_button = gr.Button("Generate Voice")
|
70 |
|
71 |
+
mic_button.click(
|
72 |
+
fn=lambda: record_audio(duration=10),
|
73 |
+
inputs=[],
|
74 |
+
outputs=speaker_audio_input,
|
75 |
+
)
|
76 |
+
|
77 |
generate_button.click(
|
78 |
fn=generate_voice,
|
79 |
+
inputs=[text_input, speaker_audio_input, language_dropdown],
|
80 |
outputs=output_audio
|
81 |
)
|
82 |
|
83 |
# Launch the app
|
84 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
requirements.txt
CHANGED
@@ -2,3 +2,6 @@ gradio
|
|
2 |
torch
|
3 |
TTS
|
4 |
soundfile
|
|
|
|
|
|
|
|
2 |
torch
|
3 |
TTS
|
4 |
soundfile
|
5 |
+
coqui-tts
|
6 |
+
sounddevice
|
7 |
+
scipy
|