File size: 4,745 Bytes
d95fbb3
 
 
4fdf3ce
d95fbb3
4fdf3ce
 
 
 
 
 
 
d95fbb3
 
195bbda
4fdf3ce
 
195bbda
 
 
d95fbb3
4fdf3ce
479b63b
4fdf3ce
 
 
 
 
 
 
 
 
479b63b
f116567
4fdf3ce
 
 
195bbda
 
4fdf3ce
 
 
 
f116567
d95fbb3
 
f116567
4fdf3ce
f116567
 
 
4fdf3ce
 
 
 
 
f116567
4fdf3ce
f116567
 
4fdf3ce
 
 
195bbda
8bf81de
4fdf3ce
 
 
 
 
 
 
 
 
 
 
 
195bbda
 
4fdf3ce
 
 
 
 
 
 
195bbda
4fdf3ce
479b63b
 
4fdf3ce
479b63b
195bbda
 
4fdf3ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195bbda
4fdf3ce
 
 
 
195bbda
 
 
4fdf3ce
 
 
 
 
 
 
 
195bbda
 
d95fbb3
 
4fdf3ce
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
import gradio as gr
from TTS.api import TTS
import os
import torch

# 1. Environment & License Agreement
# XTTS requires agreeing to the terms.
os.environ["COQUI_TOS_AGREED"] = "1"

# 2. Load Model
# Check if CUDA is available
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Loading XTTS v2 on {device}...")

try:
    # Load the model with gpu support if available
    tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
except Exception as e:
    print(f"Error loading model: {e}")
    tts = None

def clone_voice(text, audio_filepath, language, speed, temperature, repetition_penalty):
    """
    Generate speech using XTTS v2.
    
    Args:
        text: The text to speak.
        audio_filepath: Path to the reference audio (wav/mp3/etc).
        language: Language code (e.g., "en").
        speed: Speed factor (default 1.0).
        temperature: Sampling temperature (lower = more deterministic).
        repetition_penalty: Penalty to avoid loops.
    """
    print(f"--- NEW REQUEST ---")
    print(f"Text: {text[:50]}...")
    print(f"Reference: {audio_filepath}")
    print(f"Params: Speed={speed}, Temp={temperature}, Rep={repetition_penalty}")

    if not tts:
        return None, "Error: Model failed to load at startup."
    
    if not audio_filepath:
        return None, "Error: No audio file provided. Please record or upload a voice sample."

    output_path = "output.wav"
    
    try:
        # XTTS generation
        tts.tts_to_file(
            text=text,
            file_path=output_path,
            speaker_wav=audio_filepath,
            language=language,
            speed=speed,
            temperature=temperature,
            repetition_penalty=repetition_penalty
        )
        return output_path, f"Success! Generated on {device}."
        
    except Exception as e:
        error_msg = str(e)
        print(f"GENERATION ERROR: {error_msg}")
        return None, f"Error: {error_msg}"

# 3. UI Setup
# Defined languages for XTTS v2
LANGUAGES = [
    "en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "ko", "hu"
]

with gr.Blocks(title="PLOT Voice Engine Pro") as demo:
    gr.Markdown(
        """
        # 🎙️ PLOT Voice Engine (XTTS v2)
        **Advanced Voice Cloning** - improved stability and control.
        """
    )
    
    with gr.Row():
        with gr.Column(scale=1):
            # Input Section
            text_input = gr.Textbox(
                label="Text to Speak", 
                value="Hello! This is a test of the enhanced voice cloning system.",
                lines=3
            )
            
            # Using 'filepath' handles un/packing of audio automatically
            audio_input = gr.Audio(
                label="Reference Voice (Record or Upload)", 
                type="filepath",    # <--- Crucial fix: let Gradio handle files
                sources=["microphone", "upload"]
            )
            
            with gr.Accordion("Advanced Settings", open=True):
                language_dropdown = gr.Dropdown(
                    label="Language", 
                    choices=LANGUAGES, 
                    value="en"
                )
                speed_slider = gr.Slider(
                    label="Speed", 
                    minimum=0.5, 
                    maximum=2.0, 
                    value=1.0, 
                    step=0.1
                )
                temp_slider = gr.Slider(
                    label="Temperature (Stability)", 
                    minimum=0.01, 
                    maximum=1.0, 
                    value=0.75, 
                    step=0.05,
                    info="Lower = more stable, Higher = more expressive."
                )
                rep_slider = gr.Slider(
                    label="Repetition Penalty", 
                    minimum=1.0, 
                    maximum=10.0, 
                    value=5.0, 
                    step=0.1,
                    info="Increase if the model repeats itself."
                )

            submit_btn = gr.Button("🚀 Generate Voice", variant="primary", size="lg")
            
        with gr.Column(scale=1):
            # Output Section
            audio_output = gr.Audio(label="Generated Audio", type="filepath", interactive=False)
            status_output = gr.Textbox(label="Status Log", interactive=False)

    submit_btn.click(
        fn=clone_voice,
        inputs=[
            text_input, 
            audio_input, 
            language_dropdown, 
            speed_slider, 
            temp_slider, 
            rep_slider
        ],
        outputs=[audio_output, status_output]
    )

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