Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,47 +3,49 @@ import torch
|
|
| 3 |
import torchaudio
|
| 4 |
import tempfile
|
| 5 |
import os
|
| 6 |
-
import logging
|
| 7 |
-
|
| 8 |
-
# Setup logging
|
| 9 |
-
logging.basicConfig(level=logging.INFO)
|
| 10 |
-
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
# Device detection
|
| 13 |
-
DEVICE = "cpu"
|
| 14 |
-
|
| 15 |
-
DEVICE = "cuda"
|
| 16 |
-
logger.info("๐ Running on CUDA GPU")
|
| 17 |
-
else:
|
| 18 |
-
logger.info("๐ Running on CPU")
|
| 19 |
-
|
| 20 |
-
print(f"๐ Running on device: {DEVICE}")
|
| 21 |
|
| 22 |
-
# Global
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
"""Load
|
| 28 |
-
global
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
def
|
| 44 |
"""
|
| 45 |
-
๐ค VOICE-TO-VOICE CLONING
|
| 46 |
-
|
| 47 |
"""
|
| 48 |
try:
|
| 49 |
if not reference_audio:
|
|
@@ -52,64 +54,50 @@ def voice_to_voice_cloning(reference_audio, input_audio, language="en", exaggera
|
|
| 52 |
if not input_audio:
|
| 53 |
return None, "โ Please upload input audio (content to transform)!"
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Step 1: Extract text from input audio using Whisper
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
whisper_model = whisper.load_model("base")
|
| 62 |
-
result = whisper_model.transcribe(input_audio)
|
| 63 |
extracted_text = result["text"]
|
| 64 |
-
print(f"
|
| 65 |
-
|
| 66 |
-
print(f"โ ๏ธ Whisper failed: {e}")
|
| 67 |
extracted_text = "Voice cloning demonstration using uploaded audio content."
|
|
|
|
| 68 |
|
| 69 |
-
# Step 2:
|
| 70 |
-
|
| 71 |
-
if not load_chatterbox_models():
|
| 72 |
-
return None, "โ Chatterbox models failed to load!"
|
| 73 |
|
| 74 |
-
# Step 3: Generate voice using Chatterbox
|
| 75 |
-
print("๐ญ Generating cloned voice...")
|
| 76 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 77 |
output_path = tmp_file.name
|
| 78 |
|
| 79 |
-
# Use
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
cfg=cfg
|
| 87 |
-
)
|
| 88 |
-
else:
|
| 89 |
-
model = MULTILINGUAL_MODEL
|
| 90 |
-
wav = model.generate(
|
| 91 |
-
extracted_text,
|
| 92 |
-
audio_prompt_path=reference_audio,
|
| 93 |
-
language_id=language,
|
| 94 |
-
exaggeration=exaggeration,
|
| 95 |
-
cfg=cfg
|
| 96 |
-
)
|
| 97 |
-
|
| 98 |
-
# Step 4: Save generated audio
|
| 99 |
-
torchaudio.save(output_path, wav.cpu(), model.sr)
|
| 100 |
|
|
|
|
| 101 |
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 102 |
-
return output_path, f"โ
Voice-to-Voice Cloning Complete!\n๐ค
|
| 103 |
else:
|
| 104 |
return None, "โ Generated audio file is empty!"
|
| 105 |
|
| 106 |
except Exception as e:
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
def
|
| 110 |
"""
|
| 111 |
-
๐ TEXT-TO-VOICE CLONING
|
| 112 |
-
Generates speech from text using reference voice
|
| 113 |
"""
|
| 114 |
try:
|
| 115 |
if not reference_audio:
|
|
@@ -118,99 +106,79 @@ def text_to_voice_cloning(reference_audio, input_text, language="en", exaggerati
|
|
| 118 |
if not input_text or not input_text.strip():
|
| 119 |
return None, "โ Please enter text to convert!"
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
if ENGLISH_MODEL is None or MULTILINGUAL_MODEL is None:
|
| 126 |
-
if not load_chatterbox_models():
|
| 127 |
-
return None, "โ Chatterbox models failed to load!"
|
| 128 |
|
| 129 |
-
# Generate speech using Chatterbox
|
| 130 |
-
print("๐ญ Generating speech...")
|
| 131 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 132 |
output_path = tmp_file.name
|
| 133 |
|
| 134 |
-
#
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
cfg=cfg
|
| 142 |
-
)
|
| 143 |
-
else:
|
| 144 |
-
model = MULTILINGUAL_MODEL
|
| 145 |
-
wav = model.generate(
|
| 146 |
-
input_text,
|
| 147 |
-
audio_prompt_path=reference_audio,
|
| 148 |
-
language_id=language,
|
| 149 |
-
exaggeration=exaggeration,
|
| 150 |
-
cfg=cfg
|
| 151 |
-
)
|
| 152 |
-
|
| 153 |
-
# Save generated audio
|
| 154 |
-
torchaudio.save(output_path, wav.cpu(), model.sr)
|
| 155 |
|
|
|
|
| 156 |
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 157 |
-
return output_path, f"โ
Text-to-Voice Complete!\n๐ Generated
|
| 158 |
else:
|
| 159 |
return None, "โ Generated audio file is empty!"
|
| 160 |
|
| 161 |
except Exception as e:
|
| 162 |
-
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
# Try
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
startup_message = "โ
Chatterbox Models Ready!" if models_loaded else "โ ๏ธ Models will load on first use"
|
| 168 |
-
except Exception as e:
|
| 169 |
-
models_loaded = False
|
| 170 |
-
startup_message = f"โ ๏ธ Model loading will be attempted on first use: {str(e)}"
|
| 171 |
|
| 172 |
-
# Create Gradio interface with
|
| 173 |
with gr.Blocks(
|
| 174 |
-
title="๐ญ
|
| 175 |
-
theme=gr.themes.Soft(primary_hue="
|
| 176 |
) as demo:
|
| 177 |
|
| 178 |
-
# Header
|
| 179 |
gr.HTML("""
|
| 180 |
<div style="text-align: center; padding: 20px;">
|
| 181 |
-
<h1 style="color: #
|
| 182 |
-
<p style="color: #666; font-size: 18px;">Voice-to-Voice & Text-to-Speech
|
| 183 |
-
<p style="color: #888; font-size: 14px;">
|
| 184 |
</div>
|
| 185 |
""")
|
| 186 |
|
| 187 |
-
#
|
|
|
|
| 188 |
gr.HTML(f"""
|
| 189 |
-
<div style="text-align: center; padding: 15px; background:
|
| 190 |
-
<strong>๐ค
|
| 191 |
</div>
|
| 192 |
""")
|
| 193 |
|
| 194 |
-
# Reference Voice (shared
|
| 195 |
-
gr.HTML("<h3 style='color: #
|
| 196 |
reference_audio = gr.Audio(
|
| 197 |
-
label="Upload Reference Audio (
|
| 198 |
type="filepath",
|
| 199 |
sources=["upload", "microphone"]
|
| 200 |
)
|
| 201 |
-
gr.HTML("<p style='color: #666; text-align: center; margin-bottom: 20px;'>๐ This voice will be cloned and applied to your content</p>")
|
| 202 |
|
| 203 |
-
# Tabs for different
|
| 204 |
with gr.Tabs():
|
| 205 |
-
#
|
| 206 |
with gr.TabItem("๐ต Voice-to-Voice Cloning"):
|
| 207 |
gr.HTML("""
|
| 208 |
-
<div style="padding: 15px; background: #
|
| 209 |
-
<h4
|
| 210 |
-
<p
|
| 211 |
-
2
|
| 212 |
-
3
|
| 213 |
-
4
|
| 214 |
</div>
|
| 215 |
""")
|
| 216 |
|
|
@@ -222,162 +190,109 @@ with gr.Blocks(
|
|
| 222 |
sources=["upload", "microphone"]
|
| 223 |
)
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
)
|
| 242 |
-
|
| 243 |
-
voice_exaggeration = gr.Slider(
|
| 244 |
-
minimum=0.0,
|
| 245 |
-
maximum=2.0,
|
| 246 |
-
step=0.1,
|
| 247 |
-
value=0.5,
|
| 248 |
-
label="๐ญ Emotion Exaggeration"
|
| 249 |
-
)
|
| 250 |
-
|
| 251 |
-
voice_cfg = gr.Slider(
|
| 252 |
-
minimum=0.1,
|
| 253 |
-
maximum=1.0,
|
| 254 |
-
step=0.1,
|
| 255 |
-
value=0.5,
|
| 256 |
-
label="๐๏ธ CFG Scale (Accuracy)"
|
| 257 |
-
)
|
| 258 |
|
| 259 |
-
|
| 260 |
"๐ค Transform Voice (Audio โ Cloned Audio)",
|
| 261 |
variant="primary",
|
| 262 |
size="lg"
|
| 263 |
)
|
| 264 |
|
| 265 |
with gr.Column():
|
| 266 |
-
|
| 267 |
-
label="Voice-to-Voice Result",
|
| 268 |
-
type="filepath"
|
| 269 |
-
)
|
| 270 |
-
|
| 271 |
voice_status = gr.Textbox(
|
| 272 |
label="Voice-to-Voice Status",
|
| 273 |
lines=6,
|
| 274 |
interactive=False
|
| 275 |
)
|
| 276 |
|
| 277 |
-
#
|
| 278 |
with gr.TabItem("๐ Text-to-Speech Cloning"):
|
| 279 |
gr.HTML("""
|
| 280 |
<div style="padding: 15px; background: #f0fff0; border-radius: 10px; margin-bottom: 15px;">
|
| 281 |
-
<h4
|
| 282 |
-
<p
|
| 283 |
-
2
|
| 284 |
-
3
|
| 285 |
-
4
|
| 286 |
</div>
|
| 287 |
""")
|
| 288 |
|
| 289 |
with gr.Row():
|
| 290 |
with gr.Column():
|
| 291 |
text_input = gr.Textbox(
|
| 292 |
-
label="Text to Convert
|
| 293 |
-
placeholder="Enter
|
| 294 |
-
lines=5
|
| 295 |
-
max_lines=8
|
| 296 |
)
|
| 297 |
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
)
|
| 313 |
-
|
| 314 |
-
text_exaggeration = gr.Slider(
|
| 315 |
-
minimum=0.0,
|
| 316 |
-
maximum=2.0,
|
| 317 |
-
step=0.1,
|
| 318 |
-
value=0.5,
|
| 319 |
-
label="๐ญ Emotion Exaggeration"
|
| 320 |
-
)
|
| 321 |
-
|
| 322 |
-
text_cfg = gr.Slider(
|
| 323 |
-
minimum=0.1,
|
| 324 |
-
maximum=1.0,
|
| 325 |
-
step=0.1,
|
| 326 |
-
value=0.5,
|
| 327 |
-
label="๐๏ธ CFG Scale (Accuracy)"
|
| 328 |
-
)
|
| 329 |
|
| 330 |
-
|
| 331 |
"๐ Generate Speech (Text โ Cloned Audio)",
|
| 332 |
variant="secondary",
|
| 333 |
size="lg"
|
| 334 |
)
|
| 335 |
|
| 336 |
with gr.Column():
|
| 337 |
-
|
| 338 |
-
label="Text-to-Speech Result",
|
| 339 |
-
type="filepath"
|
| 340 |
-
)
|
| 341 |
-
|
| 342 |
text_status = gr.Textbox(
|
| 343 |
label="Text-to-Speech Status",
|
| 344 |
lines=6,
|
| 345 |
interactive=False
|
| 346 |
)
|
| 347 |
|
| 348 |
-
# Examples
|
| 349 |
with gr.Accordion("๐ก Example Texts", open=False):
|
| 350 |
examples = [
|
| 351 |
-
"Hello, this is a demonstration of AI voice cloning
|
| 352 |
-
"The weather is beautiful
|
| 353 |
-
"Artificial intelligence
|
| 354 |
-
"This advanced voice cloning system can generate natural speech in multiple languages."
|
| 355 |
]
|
| 356 |
-
|
| 357 |
-
gr.Examples(
|
| 358 |
-
examples=examples,
|
| 359 |
-
inputs=text_input,
|
| 360 |
-
label="Click to use these example texts:"
|
| 361 |
-
)
|
| 362 |
|
| 363 |
-
#
|
| 364 |
-
|
| 365 |
-
fn=
|
| 366 |
-
inputs=[reference_audio, input_audio,
|
| 367 |
-
outputs=[
|
| 368 |
show_progress=True
|
| 369 |
)
|
| 370 |
|
| 371 |
-
|
| 372 |
-
fn=
|
| 373 |
-
inputs=[reference_audio, text_input,
|
| 374 |
-
outputs=[
|
| 375 |
show_progress=True
|
| 376 |
)
|
| 377 |
|
| 378 |
if __name__ == "__main__":
|
| 379 |
-
demo.launch(
|
| 380 |
-
server_name="0.0.0.0",
|
| 381 |
-
server_port=7860,
|
| 382 |
-
share=False
|
| 383 |
-
)
|
|
|
|
| 3 |
import torchaudio
|
| 4 |
import tempfile
|
| 5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Device detection
|
| 8 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
print(f"๐ Using device: {DEVICE}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Global models
|
| 12 |
+
TTS_MODEL = None
|
| 13 |
+
WHISPER_MODEL = None
|
| 14 |
|
| 15 |
+
def load_models():
|
| 16 |
+
"""Load TTS models with proper error handling"""
|
| 17 |
+
global TTS_MODEL, WHISPER_MODEL
|
| 18 |
|
| 19 |
+
print("๐ Loading models...")
|
| 20 |
+
|
| 21 |
+
# Load XTTS-v2 (most reliable for voice cloning)
|
| 22 |
+
if TTS_MODEL is None:
|
| 23 |
+
try:
|
| 24 |
+
from TTS.api import TTS
|
| 25 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 26 |
+
print("๐ฆ Loading XTTS-v2...")
|
| 27 |
+
TTS_MODEL = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(DEVICE)
|
| 28 |
+
print("โ
XTTS-v2 loaded successfully!")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"โ XTTS-v2 failed: {e}")
|
| 31 |
+
return False
|
| 32 |
+
|
| 33 |
+
# Load Whisper for voice-to-voice
|
| 34 |
+
if WHISPER_MODEL is None:
|
| 35 |
+
try:
|
| 36 |
+
import whisper
|
| 37 |
+
print("๐ฆ Loading Whisper...")
|
| 38 |
+
WHISPER_MODEL = whisper.load_model("base")
|
| 39 |
+
print("โ
Whisper loaded successfully!")
|
| 40 |
+
except Exception as e:
|
| 41 |
+
print(f"โ Whisper failed: {e}")
|
| 42 |
+
|
| 43 |
+
return TTS_MODEL is not None
|
| 44 |
|
| 45 |
+
def voice_to_voice_clone(reference_audio, input_audio, language="en"):
|
| 46 |
"""
|
| 47 |
+
๐ค VOICE-TO-VOICE CLONING - Real Implementation
|
| 48 |
+
Transform input audio content using reference voice characteristics
|
| 49 |
"""
|
| 50 |
try:
|
| 51 |
if not reference_audio:
|
|
|
|
| 54 |
if not input_audio:
|
| 55 |
return None, "โ Please upload input audio (content to transform)!"
|
| 56 |
|
| 57 |
+
# Load models
|
| 58 |
+
if not load_models():
|
| 59 |
+
return None, "โ XTTS-v2 model failed to load!"
|
| 60 |
+
|
| 61 |
+
print("๐ค Starting Voice-to-Voice Cloning...")
|
| 62 |
|
| 63 |
# Step 1: Extract text from input audio using Whisper
|
| 64 |
+
if WHISPER_MODEL:
|
| 65 |
+
print("๐ Transcribing input audio...")
|
| 66 |
+
result = WHISPER_MODEL.transcribe(input_audio)
|
|
|
|
|
|
|
| 67 |
extracted_text = result["text"]
|
| 68 |
+
print(f"โ
Extracted: {extracted_text[:100]}...")
|
| 69 |
+
else:
|
|
|
|
| 70 |
extracted_text = "Voice cloning demonstration using uploaded audio content."
|
| 71 |
+
print("โ ๏ธ Using fallback text")
|
| 72 |
|
| 73 |
+
# Step 2: Generate new audio with reference voice using XTTS-v2
|
| 74 |
+
print("๐ญ Generating speech with cloned voice...")
|
|
|
|
|
|
|
| 75 |
|
|
|
|
|
|
|
| 76 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 77 |
output_path = tmp_file.name
|
| 78 |
|
| 79 |
+
# Use XTTS-v2 for voice cloning
|
| 80 |
+
TTS_MODEL.tts_to_file(
|
| 81 |
+
text=extracted_text,
|
| 82 |
+
speaker_wav=reference_audio,
|
| 83 |
+
language=language,
|
| 84 |
+
file_path=output_path
|
| 85 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
+
# Verify output
|
| 88 |
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 89 |
+
return output_path, f"โ
Voice-to-Voice Cloning Complete!\n๐ค Original content: '{extracted_text[:100]}...'\n๐ญ Applied reference voice characteristics\n๐ Language: {language}\n๐ค Model: XTTS-v2"
|
| 90 |
else:
|
| 91 |
return None, "โ Generated audio file is empty!"
|
| 92 |
|
| 93 |
except Exception as e:
|
| 94 |
+
error_msg = f"โ Voice-to-Voice Error: {str(e)}"
|
| 95 |
+
print(error_msg)
|
| 96 |
+
return None, error_msg
|
| 97 |
|
| 98 |
+
def text_to_voice_clone(reference_audio, input_text, language="en"):
|
| 99 |
"""
|
| 100 |
+
๐ TEXT-TO-VOICE CLONING - Real Implementation
|
|
|
|
| 101 |
"""
|
| 102 |
try:
|
| 103 |
if not reference_audio:
|
|
|
|
| 106 |
if not input_text or not input_text.strip():
|
| 107 |
return None, "โ Please enter text to convert!"
|
| 108 |
|
| 109 |
+
# Load models
|
| 110 |
+
if not load_models():
|
| 111 |
+
return None, "โ XTTS-v2 model failed to load!"
|
| 112 |
|
| 113 |
+
print("๐ Starting Text-to-Voice Cloning...")
|
|
|
|
|
|
|
|
|
|
| 114 |
|
|
|
|
|
|
|
| 115 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 116 |
output_path = tmp_file.name
|
| 117 |
|
| 118 |
+
# Generate speech using XTTS-v2
|
| 119 |
+
TTS_MODEL.tts_to_file(
|
| 120 |
+
text=input_text,
|
| 121 |
+
speaker_wav=reference_audio,
|
| 122 |
+
language=language,
|
| 123 |
+
file_path=output_path
|
| 124 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
# Verify output
|
| 127 |
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
| 128 |
+
return output_path, f"โ
Text-to-Voice Complete!\n๐ Generated: '{input_text[:100]}...'\n๐ญ Using reference voice characteristics\n๐ Language: {language}\n๐ค Model: XTTS-v2"
|
| 129 |
else:
|
| 130 |
return None, "โ Generated audio file is empty!"
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
+
error_msg = f"โ Text-to-Voice Error: {str(e)}"
|
| 134 |
+
print(error_msg)
|
| 135 |
+
return None, error_msg
|
| 136 |
|
| 137 |
+
# Try loading models at startup
|
| 138 |
+
startup_success = load_models()
|
| 139 |
+
startup_msg = "โ
XTTS-v2 Ready for Voice Cloning!" if startup_success else "โ ๏ธ Models will load on first use"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# Create Gradio interface with BOTH functionalities
|
| 142 |
with gr.Blocks(
|
| 143 |
+
title="๐ญ Voice Cloning Studio - XTTS-v2",
|
| 144 |
+
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="green")
|
| 145 |
) as demo:
|
| 146 |
|
|
|
|
| 147 |
gr.HTML("""
|
| 148 |
<div style="text-align: center; padding: 20px;">
|
| 149 |
+
<h1 style="color: #2E86AB;">๐ญ Voice Cloning Studio</h1>
|
| 150 |
+
<p style="color: #666; font-size: 18px;">Real Voice-to-Voice & Text-to-Speech Cloning</p>
|
| 151 |
+
<p style="color: #888; font-size: 14px;">Powered by XTTS-v2 - Production Ready Open Source Model</p>
|
| 152 |
</div>
|
| 153 |
""")
|
| 154 |
|
| 155 |
+
# Status
|
| 156 |
+
status_color = "#d4edda" if startup_success else "#fff3cd"
|
| 157 |
gr.HTML(f"""
|
| 158 |
+
<div style="text-align: center; padding: 15px; background: {status_color}; border-radius: 10px; margin-bottom: 20px;">
|
| 159 |
+
<strong>๐ค Model Status:</strong> {startup_msg}
|
| 160 |
</div>
|
| 161 |
""")
|
| 162 |
|
| 163 |
+
# Reference Voice (shared)
|
| 164 |
+
gr.HTML("<h3 style='color: #2E86AB; text-align: center;'>๐ค Reference Voice (Voice to Clone)</h3>")
|
| 165 |
reference_audio = gr.Audio(
|
| 166 |
+
label="Upload Reference Audio (6+ seconds recommended)",
|
| 167 |
type="filepath",
|
| 168 |
sources=["upload", "microphone"]
|
| 169 |
)
|
|
|
|
| 170 |
|
| 171 |
+
# Tabs for different modes
|
| 172 |
with gr.Tabs():
|
| 173 |
+
# VOICE-TO-VOICE CLONING TAB
|
| 174 |
with gr.TabItem("๐ต Voice-to-Voice Cloning"):
|
| 175 |
gr.HTML("""
|
| 176 |
+
<div style="padding: 15px; background: #e8f4fd; border-radius: 10px; margin-bottom: 15px;">
|
| 177 |
+
<h4>๐ค Voice-to-Voice Process:</h4>
|
| 178 |
+
<p><strong>1.</strong> Upload reference voice (person to clone)<br>
|
| 179 |
+
<strong>2.</strong> Upload input audio (speech content to transform)<br>
|
| 180 |
+
<strong>3.</strong> AI extracts text from input audio using Whisper<br>
|
| 181 |
+
<strong>4.</strong> XTTS-v2 generates new audio with reference voice + extracted content</p>
|
| 182 |
</div>
|
| 183 |
""")
|
| 184 |
|
|
|
|
| 190 |
sources=["upload", "microphone"]
|
| 191 |
)
|
| 192 |
|
| 193 |
+
voice_lang = gr.Dropdown(
|
| 194 |
+
choices=[
|
| 195 |
+
("๐บ๐ธ English", "en"),
|
| 196 |
+
("๐ช๐ธ Spanish", "es"),
|
| 197 |
+
("๐ซ๐ท French", "fr"),
|
| 198 |
+
("๐ฉ๐ช German", "de"),
|
| 199 |
+
("๐ฎ๐น Italian", "it"),
|
| 200 |
+
("๐ง๐ท Portuguese", "pt"),
|
| 201 |
+
("๐จ๐ณ Chinese", "zh"),
|
| 202 |
+
("๐ฏ๐ต Japanese", "ja"),
|
| 203 |
+
("๐ฐ๐ท Korean", "ko"),
|
| 204 |
+
("๐ท๐บ Russian", "ru")
|
| 205 |
+
],
|
| 206 |
+
value="en",
|
| 207 |
+
label="Language"
|
| 208 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
voice_btn = gr.Button(
|
| 211 |
"๐ค Transform Voice (Audio โ Cloned Audio)",
|
| 212 |
variant="primary",
|
| 213 |
size="lg"
|
| 214 |
)
|
| 215 |
|
| 216 |
with gr.Column():
|
| 217 |
+
voice_output = gr.Audio(label="Voice-to-Voice Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
voice_status = gr.Textbox(
|
| 219 |
label="Voice-to-Voice Status",
|
| 220 |
lines=6,
|
| 221 |
interactive=False
|
| 222 |
)
|
| 223 |
|
| 224 |
+
# TEXT-TO-VOICE CLONING TAB
|
| 225 |
with gr.TabItem("๐ Text-to-Speech Cloning"):
|
| 226 |
gr.HTML("""
|
| 227 |
<div style="padding: 15px; background: #f0fff0; border-radius: 10px; margin-bottom: 15px;">
|
| 228 |
+
<h4>๐ Text-to-Speech Process:</h4>
|
| 229 |
+
<p><strong>1.</strong> Upload reference voice (person to clone)<br>
|
| 230 |
+
<strong>2.</strong> Enter text to convert to speech<br>
|
| 231 |
+
<strong>3.</strong> XTTS-v2 generates speech directly in the cloned voice<br>
|
| 232 |
+
<strong>4.</strong> Download high-quality result</p>
|
| 233 |
</div>
|
| 234 |
""")
|
| 235 |
|
| 236 |
with gr.Row():
|
| 237 |
with gr.Column():
|
| 238 |
text_input = gr.Textbox(
|
| 239 |
+
label="Text to Convert",
|
| 240 |
+
placeholder="Enter text to speak in the cloned voice...",
|
| 241 |
+
lines=5
|
|
|
|
| 242 |
)
|
| 243 |
|
| 244 |
+
text_lang = gr.Dropdown(
|
| 245 |
+
choices=[
|
| 246 |
+
("๐บ๐ธ English", "en"),
|
| 247 |
+
("๐ช๐ธ Spanish", "es"),
|
| 248 |
+
("๐ซ๐ท French", "fr"),
|
| 249 |
+
("๐ฉ๐ช German", "de"),
|
| 250 |
+
("๐ฎ๐น Italian", "it"),
|
| 251 |
+
("๐ง๐ท Portuguese", "pt"),
|
| 252 |
+
("๐จ๐ณ Chinese", "zh"),
|
| 253 |
+
("๐ฏ๐ต Japanese", "ja")
|
| 254 |
+
],
|
| 255 |
+
value="en",
|
| 256 |
+
label="Language"
|
| 257 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
+
text_btn = gr.Button(
|
| 260 |
"๐ Generate Speech (Text โ Cloned Audio)",
|
| 261 |
variant="secondary",
|
| 262 |
size="lg"
|
| 263 |
)
|
| 264 |
|
| 265 |
with gr.Column():
|
| 266 |
+
text_output = gr.Audio(label="Text-to-Speech Result")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
text_status = gr.Textbox(
|
| 268 |
label="Text-to-Speech Status",
|
| 269 |
lines=6,
|
| 270 |
interactive=False
|
| 271 |
)
|
| 272 |
|
| 273 |
+
# Examples
|
| 274 |
with gr.Accordion("๐ก Example Texts", open=False):
|
| 275 |
examples = [
|
| 276 |
+
"Hello, this is a demonstration of AI voice cloning using XTTS-v2.",
|
| 277 |
+
"The weather today is absolutely beautiful, perfect for a walk in the park.",
|
| 278 |
+
"Artificial intelligence continues to revolutionize how we create and share content."
|
|
|
|
| 279 |
]
|
| 280 |
+
gr.Examples(examples=examples, inputs=text_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
+
# Connect both functions - VOICE-TO-VOICE AND TEXT-TO-SPEECH
|
| 283 |
+
voice_btn.click(
|
| 284 |
+
fn=voice_to_voice_clone,
|
| 285 |
+
inputs=[reference_audio, input_audio, voice_lang],
|
| 286 |
+
outputs=[voice_output, voice_status],
|
| 287 |
show_progress=True
|
| 288 |
)
|
| 289 |
|
| 290 |
+
text_btn.click(
|
| 291 |
+
fn=text_to_voice_clone,
|
| 292 |
+
inputs=[reference_audio, text_input, text_lang],
|
| 293 |
+
outputs=[text_output, text_status],
|
| 294 |
show_progress=True
|
| 295 |
)
|
| 296 |
|
| 297 |
if __name__ == "__main__":
|
| 298 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|