Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,59 @@ import numpy as np
|
|
| 4 |
import soundfile as sf
|
| 5 |
import tempfile
|
| 6 |
import os
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
"""
|
| 12 |
try:
|
| 13 |
if not reference_audio:
|
|
@@ -16,67 +65,274 @@ def voice_clone_demo(reference_audio, input_text):
|
|
| 16 |
if not input_text or not input_text.strip():
|
| 17 |
return None, "โ Please enter text to convert!"
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
except Exception as e:
|
| 25 |
-
return None, f"โ Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34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 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
lines=4
|
| 53 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
clone_button.click(
|
| 76 |
-
fn=voice_clone_demo,
|
| 77 |
-
inputs=[reference_audio, text_input],
|
| 78 |
-
outputs=[audio_output, status_output]
|
| 79 |
-
)
|
| 80 |
|
|
|
|
| 81 |
if __name__ == "__main__":
|
| 82 |
-
demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import soundfile as sf
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
+
from scipy.io import wavfile
|
| 8 |
+
import librosa
|
| 9 |
|
| 10 |
+
def extract_audio_features(audio_path):
|
| 11 |
+
"""Extract features from audio for voice cloning"""
|
| 12 |
+
try:
|
| 13 |
+
# Load audio file
|
| 14 |
+
audio, sr = librosa.load(audio_path, sr=16000)
|
| 15 |
+
return audio, sr
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Error processing audio: {e}")
|
| 18 |
+
return None, None
|
| 19 |
+
|
| 20 |
+
def voice_clone_with_audio(reference_audio, input_audio, enhance_quality=True):
|
| 21 |
+
"""
|
| 22 |
+
Voice-to-Voice cloning: Clone reference voice using input audio
|
| 23 |
+
"""
|
| 24 |
+
try:
|
| 25 |
+
if not reference_audio:
|
| 26 |
+
return None, "โ Please upload reference audio!"
|
| 27 |
+
|
| 28 |
+
if not input_audio:
|
| 29 |
+
return None, "โ Please upload input audio to transform!"
|
| 30 |
+
|
| 31 |
+
# Process reference audio
|
| 32 |
+
ref_audio, ref_sr = extract_audio_features(reference_audio)
|
| 33 |
+
if ref_audio is None:
|
| 34 |
+
return None, "โ Error processing reference audio!"
|
| 35 |
+
|
| 36 |
+
# Process input audio
|
| 37 |
+
input_audio_data, input_sr = extract_audio_features(input_audio)
|
| 38 |
+
if input_audio_data is None:
|
| 39 |
+
return None, "โ Error processing input audio!"
|
| 40 |
+
|
| 41 |
+
# For demo: Apply simple voice transformation
|
| 42 |
+
# In production, this would use actual voice cloning models
|
| 43 |
+
transformed_audio = apply_voice_transformation(
|
| 44 |
+
reference_audio=ref_audio,
|
| 45 |
+
input_audio=input_audio_data,
|
| 46 |
+
enhance_quality=enhance_quality
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Save output audio
|
| 50 |
+
output_path = save_audio_output(transformed_audio, ref_sr)
|
| 51 |
+
|
| 52 |
+
return output_path, f"โ
Voice cloning complete!\n๐ต Transformed {len(input_audio_data)/input_sr:.1f}s of audio using reference voice"
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return None, f"โ Error in voice cloning: {str(e)}"
|
| 56 |
+
|
| 57 |
+
def voice_clone_with_text(reference_audio, input_text, language="en", speed=1.0):
|
| 58 |
"""
|
| 59 |
+
Text-to-Voice cloning: Generate speech from text using reference voice
|
| 60 |
"""
|
| 61 |
try:
|
| 62 |
if not reference_audio:
|
|
|
|
| 65 |
if not input_text or not input_text.strip():
|
| 66 |
return None, "โ Please enter text to convert!"
|
| 67 |
|
| 68 |
+
# Process reference audio
|
| 69 |
+
ref_audio, ref_sr = extract_audio_features(reference_audio)
|
| 70 |
+
if ref_audio is None:
|
| 71 |
+
return None, "โ Error processing reference audio!"
|
| 72 |
|
| 73 |
+
# Generate speech from text (demo implementation)
|
| 74 |
+
generated_audio = text_to_speech_with_voice(
|
| 75 |
+
text=input_text,
|
| 76 |
+
reference_voice=ref_audio,
|
| 77 |
+
language=language,
|
| 78 |
+
speed=speed
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Save output audio
|
| 82 |
+
output_path = save_audio_output(generated_audio, ref_sr)
|
| 83 |
+
|
| 84 |
+
return output_path, f"โ
Text-to-speech complete!\n๐ Generated speech for: '{input_text[:100]}{'...' if len(input_text) > 100 else ''}'"
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
+
return None, f"โ Error in text-to-speech: {str(e)}"
|
| 88 |
+
|
| 89 |
+
def apply_voice_transformation(reference_audio, input_audio, enhance_quality=True):
|
| 90 |
+
"""
|
| 91 |
+
Apply voice transformation (demo implementation)
|
| 92 |
+
In production, this would use models like XTTS, OpenVoice, etc.
|
| 93 |
+
"""
|
| 94 |
+
# Demo: Simple pitch and tone adjustment
|
| 95 |
+
# This is a placeholder - replace with actual voice cloning model
|
| 96 |
+
|
| 97 |
+
# Normalize audio lengths
|
| 98 |
+
min_length = min(len(reference_audio), len(input_audio))
|
| 99 |
+
if min_length > 0:
|
| 100 |
+
# Simple blending for demo (not real voice cloning)
|
| 101 |
+
alpha = 0.7 # Weight for input audio
|
| 102 |
+
beta = 0.3 # Weight for reference characteristics
|
| 103 |
+
|
| 104 |
+
# Resize to same length
|
| 105 |
+
ref_segment = reference_audio[:min_length]
|
| 106 |
+
input_segment = input_audio[:min_length]
|
| 107 |
+
|
| 108 |
+
# Simple transformation (placeholder)
|
| 109 |
+
transformed = alpha * input_segment + beta * ref_segment
|
| 110 |
+
|
| 111 |
+
# Apply enhancement if requested
|
| 112 |
+
if enhance_quality:
|
| 113 |
+
transformed = enhance_audio_quality(transformed)
|
| 114 |
+
|
| 115 |
+
return transformed
|
| 116 |
+
else:
|
| 117 |
+
return input_audio
|
| 118 |
|
| 119 |
+
def text_to_speech_with_voice(text, reference_voice, language="en", speed=1.0):
|
| 120 |
+
"""
|
| 121 |
+
Generate speech from text using reference voice characteristics
|
| 122 |
+
In production, this would use TTS models with voice cloning
|
| 123 |
+
"""
|
| 124 |
+
# Demo: Generate simple synthetic speech
|
| 125 |
+
# This is a placeholder - replace with actual TTS model
|
| 126 |
|
| 127 |
+
duration = len(text) * 0.1 * speed # Rough duration estimate
|
| 128 |
+
sr = 16000
|
| 129 |
+
samples = int(duration * sr)
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
# Generate simple sine wave pattern (placeholder)
|
| 132 |
+
t = np.linspace(0, duration, samples)
|
| 133 |
+
frequency = 200 + np.mean(np.abs(reference_voice)) * 100 # Use ref voice characteristics
|
| 134 |
+
|
| 135 |
+
synthetic_speech = 0.3 * np.sin(2 * np.pi * frequency * t)
|
| 136 |
+
|
| 137 |
+
# Add some variation based on text length
|
| 138 |
+
for i, char in enumerate(text[:10]):
|
| 139 |
+
freq_mod = 200 + ord(char) % 100
|
| 140 |
+
synthetic_speech += 0.1 * np.sin(2 * np.pi * freq_mod * t)
|
| 141 |
+
|
| 142 |
+
return synthetic_speech[:samples]
|
| 143 |
+
|
| 144 |
+
def enhance_audio_quality(audio):
|
| 145 |
+
"""Apply audio enhancement"""
|
| 146 |
+
# Simple noise reduction and normalization
|
| 147 |
+
audio = audio / np.max(np.abs(audio)) # Normalize
|
| 148 |
+
audio = audio * 0.8 # Reduce volume slightly
|
| 149 |
+
return audio
|
| 150 |
+
|
| 151 |
+
def save_audio_output(audio_data, sample_rate):
|
| 152 |
+
"""Save audio data to temporary file"""
|
| 153 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 154 |
+
output_path = tmp_file.name
|
| 155 |
+
|
| 156 |
+
# Ensure audio is in correct format
|
| 157 |
+
audio_data = np.array(audio_data, dtype=np.float32)
|
| 158 |
+
|
| 159 |
+
# Save using soundfile
|
| 160 |
+
sf.write(output_path, audio_data, sample_rate)
|
| 161 |
+
|
| 162 |
+
return output_path
|
| 163 |
+
|
| 164 |
+
# Create Gradio interface with tabs
|
| 165 |
+
def create_interface():
|
| 166 |
+
with gr.Blocks(
|
| 167 |
+
title="๐ญ Voice Cloning Studio",
|
| 168 |
+
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="green")
|
| 169 |
+
) as demo:
|
| 170 |
+
|
| 171 |
+
# Header
|
| 172 |
+
gr.HTML("""
|
| 173 |
+
<div style="text-align: center; padding: 20px;">
|
| 174 |
+
<h1 style="color: #2E86AB; margin-bottom: 10px;">๐ญ AI Voice Cloning Studio</h1>
|
| 175 |
+
<p style="color: #666; font-size: 18px;">Clone any voice with AI technology - Support for both Audio and Text input</p>
|
| 176 |
+
</div>
|
| 177 |
+
""")
|
| 178 |
+
|
| 179 |
+
with gr.Row():
|
| 180 |
+
with gr.Column(scale=1):
|
| 181 |
+
# Reference Voice Section
|
| 182 |
+
gr.HTML("<h3 style='color: #2E86AB;'>๐ค Upload Reference Voice</h3>")
|
| 183 |
+
reference_audio = gr.Audio(
|
| 184 |
+
label="Reference Audio (10+ seconds recommended)",
|
| 185 |
+
type="filepath",
|
| 186 |
+
sources=["upload", "microphone"]
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
gr.HTML("<p style='color: #666; font-size: 14px;'>This is the voice you want to clone. Upload clear, high-quality audio.</p>")
|
| 190 |
+
|
| 191 |
+
with gr.Column(scale=1):
|
| 192 |
+
# Input Method Selection
|
| 193 |
+
gr.HTML("<h3 style='color: #2E86AB;'>๐ฅ Choose Input Method</h3>")
|
| 194 |
+
|
| 195 |
+
with gr.Tabs():
|
| 196 |
+
with gr.TabItem("๐ต Audio Input"):
|
| 197 |
+
gr.HTML("<p>Upload audio to transform into the reference voice</p>")
|
| 198 |
+
input_audio = gr.Audio(
|
| 199 |
+
label="Input Audio to Transform",
|
| 200 |
+
type="filepath",
|
| 201 |
+
sources=["upload", "microphone"]
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
enhance_audio = gr.Checkbox(
|
| 205 |
+
label="๐๏ธ Enhance Audio Quality",
|
| 206 |
+
value=True
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
audio_clone_btn = gr.Button(
|
| 210 |
+
"๐ค Clone Voice from Audio",
|
| 211 |
+
variant="primary",
|
| 212 |
+
size="lg"
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
with gr.TabItem("๐ Text Input"):
|
| 216 |
+
gr.HTML("<p>Enter text to speak in the reference voice</p>")
|
| 217 |
+
text_input = gr.Textbox(
|
| 218 |
+
label="Text to Convert",
|
| 219 |
+
placeholder="Enter the text you want to speak in the cloned voice...",
|
| 220 |
+
lines=4,
|
| 221 |
+
max_lines=6
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
with gr.Row():
|
| 225 |
+
language_select = gr.Dropdown(
|
| 226 |
+
choices=[
|
| 227 |
+
("๐บ๐ธ English", "en"),
|
| 228 |
+
("๐ช๐ธ Spanish", "es"),
|
| 229 |
+
("๐ซ๐ท French", "fr"),
|
| 230 |
+
("๐ฉ๐ช German", "de"),
|
| 231 |
+
("๐ฎ๐น Italian", "it"),
|
| 232 |
+
("๐ง๐ท Portuguese", "pt"),
|
| 233 |
+
("๐จ๐ณ Chinese", "zh"),
|
| 234 |
+
("๐ฏ๐ต Japanese", "ja")
|
| 235 |
+
],
|
| 236 |
+
value="en",
|
| 237 |
+
label="Language"
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
speed_control = gr.Slider(
|
| 241 |
+
minimum=0.5,
|
| 242 |
+
maximum=2.0,
|
| 243 |
+
step=0.1,
|
| 244 |
+
value=1.0,
|
| 245 |
+
label="Speech Speed"
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
text_clone_btn = gr.Button(
|
| 249 |
+
"๐ Generate Speech from Text",
|
| 250 |
+
variant="secondary",
|
| 251 |
+
size="lg"
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
# Output Section
|
| 255 |
+
with gr.Row():
|
| 256 |
+
with gr.Column():
|
| 257 |
+
gr.HTML("<h3 style='color: #2E86AB;'>๐ต Cloned Voice Output</h3>")
|
| 258 |
+
audio_output = gr.Audio(
|
| 259 |
+
label="Generated Audio",
|
| 260 |
+
type="filepath"
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
status_output = gr.Textbox(
|
| 264 |
+
label="Status",
|
| 265 |
+
lines=3,
|
| 266 |
+
interactive=False
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
# Examples Section
|
| 270 |
+
with gr.Accordion("๐ก Example Texts", open=False):
|
| 271 |
+
examples = [
|
| 272 |
+
"Hello, this is a demonstration of AI voice cloning technology.",
|
| 273 |
+
"Welcome to the future of artificial intelligence and speech synthesis.",
|
| 274 |
+
"This voice was generated using advanced machine learning models.",
|
| 275 |
+
"Experience the power of AI-driven voice generation with natural speech patterns."
|
| 276 |
+
]
|
| 277 |
|
| 278 |
+
gr.Examples(
|
| 279 |
+
examples=examples,
|
| 280 |
+
inputs=text_input,
|
| 281 |
+
label="Click to try these examples:"
|
|
|
|
| 282 |
)
|
| 283 |
+
|
| 284 |
+
# How it works section
|
| 285 |
+
with gr.Accordion("๐ How Voice Cloning Works", open=False):
|
| 286 |
+
gr.Markdown("""
|
| 287 |
+
### Voice-to-Voice Cloning Process
|
| 288 |
+
1. **๐ค Reference Voice**: Upload 10+ seconds of clear speech
|
| 289 |
+
2. **๐ฅ Input Audio**: Upload audio you want to transform
|
| 290 |
+
3. **๐ง AI Analysis**: Extract voice characteristics and features
|
| 291 |
+
4. **๐ต Voice Synthesis**: Apply reference voice to input content
|
| 292 |
+
|
| 293 |
+
### Text-to-Speech Process
|
| 294 |
+
1. **๐ค Reference Voice**: Upload voice sample to clone
|
| 295 |
+
2. **๐ Text Input**: Enter text to convert to speech
|
| 296 |
+
3. **๐ฃ๏ธ Speech Generation**: Generate speech in the cloned voice
|
| 297 |
+
4. **๐ต Audio Output**: Download your cloned speech
|
| 298 |
|
| 299 |
+
### Tips for Best Results
|
| 300 |
+
- **Reference Audio**: Use 10+ seconds of clear, single-speaker audio
|
| 301 |
+
- **Input Audio**: Ensure good quality with minimal background noise
|
| 302 |
+
- **Language**: Match reference voice language when possible
|
| 303 |
+
- **Length**: Shorter inputs (under 30 seconds) work better
|
| 304 |
+
""")
|
| 305 |
|
| 306 |
+
# Event handlers
|
| 307 |
+
audio_clone_btn.click(
|
| 308 |
+
fn=voice_clone_with_audio,
|
| 309 |
+
inputs=[reference_audio, input_audio, enhance_audio],
|
| 310 |
+
outputs=[audio_output, status_output],
|
| 311 |
+
show_progress=True
|
| 312 |
+
)
|
| 313 |
+
|
| 314 |
+
text_clone_btn.click(
|
| 315 |
+
fn=voice_clone_with_text,
|
| 316 |
+
inputs=[reference_audio, text_input, language_select, speed_control],
|
| 317 |
+
outputs=[audio_output, status_output],
|
| 318 |
+
show_progress=True
|
| 319 |
+
)
|
| 320 |
+
|
| 321 |
+
# Auto-generate on Enter for text
|
| 322 |
+
text_input.submit(
|
| 323 |
+
fn=voice_clone_with_text,
|
| 324 |
+
inputs=[reference_audio, text_input, language_select, speed_control],
|
| 325 |
+
outputs=[audio_output, status_output],
|
| 326 |
+
show_progress=True
|
| 327 |
+
)
|
| 328 |
|
| 329 |
+
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
+
# Launch the app
|
| 332 |
if __name__ == "__main__":
|
| 333 |
+
demo = create_interface()
|
| 334 |
+
demo.launch(
|
| 335 |
+
server_name="0.0.0.0",
|
| 336 |
+
server_port=7860,
|
| 337 |
+
share=False
|
| 338 |
+
)
|