Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
-
from TTS.tts.configs.xtts_config import XttsConfig
|
5 |
from TTS.api import TTS
|
6 |
|
7 |
# β
Accept Coqui License Automatically
|
@@ -10,39 +10,46 @@ os.environ["COQUI_TOS_AGREED"] = "1"
|
|
10 |
# β
Allow `XttsConfig` in PyTorch's safe globals
|
11 |
torch.serialization.add_safe_globals([XttsConfig])
|
12 |
|
13 |
-
# β
|
14 |
-
|
15 |
-
return torch.load(model_path, map_location="cpu", weights_only=False) # β
Fix PyTorch 2.6 issue
|
16 |
-
|
17 |
-
# β
Initialize XTTS Model (CPU-only)
|
18 |
-
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2").to("cpu")
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
|
|
|
|
23 |
|
|
|
24 |
def generate_cloned_voice(text, reference_audio, language):
|
25 |
output_path = "output.wav"
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
|
|
|
|
|
|
|
31 |
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
-
# Create the Gradio
|
35 |
interface = gr.Interface(
|
36 |
fn=generate_cloned_voice,
|
37 |
inputs=[
|
38 |
gr.Textbox(label="Enter Translated Text"),
|
39 |
gr.Audio(label="Upload Reference Audio", type="filepath"),
|
40 |
-
gr.Dropdown(
|
41 |
],
|
42 |
outputs=gr.Audio(label="Generated Cloned Voice"),
|
43 |
title="Free Voice Cloning API",
|
44 |
-
description="Upload a sample voice and input text. Select a language, and the system will generate the text in the same voice."
|
|
|
|
|
45 |
)
|
46 |
|
47 |
-
# Launch the Gradio
|
48 |
interface.launch()
|
|
|
1 |
import os
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
+
from TTS.tts.configs.xtts_config import XttsConfig
|
5 |
from TTS.api import TTS
|
6 |
|
7 |
# β
Accept Coqui License Automatically
|
|
|
10 |
# β
Allow `XttsConfig` in PyTorch's safe globals
|
11 |
torch.serialization.add_safe_globals([XttsConfig])
|
12 |
|
13 |
+
# β
Initialize XTTS Model with Streaming Enabled (Faster on CPU)
|
14 |
+
tts = TTS(model_name="tts_models/multilingual/multi-dataset/xtts_v2", stream_inference=True, progress_bar=False).to("cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
print("β
Model loaded successfully!")
|
17 |
+
print(f"β
Supported Languages: {tts.languages.keys()}") # Debugging step
|
18 |
|
19 |
+
# β
Function for Voice Cloning
|
20 |
def generate_cloned_voice(text, reference_audio, language):
|
21 |
output_path = "output.wav"
|
22 |
|
23 |
+
if not text.strip():
|
24 |
+
return "Error: Please enter some text.", None
|
25 |
+
|
26 |
+
if not reference_audio:
|
27 |
+
return "Error: Please upload a reference audio file.", None
|
28 |
|
29 |
+
try:
|
30 |
+
print("π Processing voice cloning...")
|
31 |
+
tts.tts_to_file(text=text, speaker_wav=reference_audio, file_path=output_path, language=language)
|
32 |
+
print("β
Voice cloning complete!")
|
33 |
|
34 |
+
return output_path
|
35 |
+
except Exception as e:
|
36 |
+
print(f"β Error: {str(e)}")
|
37 |
+
return f"Error: {str(e)}", None
|
38 |
|
39 |
+
# β
Create the Gradio Interface
|
40 |
interface = gr.Interface(
|
41 |
fn=generate_cloned_voice,
|
42 |
inputs=[
|
43 |
gr.Textbox(label="Enter Translated Text"),
|
44 |
gr.Audio(label="Upload Reference Audio", type="filepath"),
|
45 |
+
gr.Dropdown(list(tts.languages.keys()), label="Select Target Language", value="en")
|
46 |
],
|
47 |
outputs=gr.Audio(label="Generated Cloned Voice"),
|
48 |
title="Free Voice Cloning API",
|
49 |
+
description="Upload a sample voice and input text. Select a language, and the system will generate the text in the same voice.",
|
50 |
+
allow_flagging="never",
|
51 |
+
concurrency_limit=1 # Prevents multiple processes from running at once
|
52 |
)
|
53 |
|
54 |
+
# β
Launch the Gradio App
|
55 |
interface.launch()
|