Spaces:
Sleeping
Sleeping
drewThomasson
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from TTS.api import TTS
|
4 |
import os
|
|
|
5 |
|
6 |
# Get device
|
7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -9,18 +10,39 @@ device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
9 |
# Initialize TTS model
|
10 |
tts = TTS(model_name="voice_conversion_models/multilingual/vctk/freevc24", progress_bar=False).to(device)
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
15 |
|
16 |
-
def voice_conversion(input_audio, target_voice):
|
17 |
output_path = "output.wav"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Perform voice conversion
|
19 |
-
|
20 |
-
print(f"Target voice is: {target_voice}")
|
21 |
-
tts.voice_conversion_to_file(source_wav=input_audio, target_wav=target_voice, file_path=output_path)
|
22 |
return output_path
|
23 |
|
|
|
|
|
|
|
|
|
24 |
# Define Gradio Interface
|
25 |
with gr.Blocks() as demo:
|
26 |
gr.Markdown("## Voice Conversion using Coqui TTS")
|
@@ -29,11 +51,24 @@ with gr.Blocks() as demo:
|
|
29 |
input_audio = gr.Audio(label="Record or Upload Your Voice", type="filepath")
|
30 |
target_voice = gr.Dropdown(choices=example_files, label="Select Target Voice from Examples",
|
31 |
value=example_files[0], info="Located in Examples/ folder")
|
|
|
32 |
|
|
|
|
|
|
|
|
|
|
|
33 |
convert_button = gr.Button("Convert Voice")
|
34 |
output_audio = gr.Audio(label="Converted Voice", type="filepath")
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Launch with public=True for public URL access and share link
|
39 |
demo.launch(share=True)
|
|
|
2 |
import torch
|
3 |
from TTS.api import TTS
|
4 |
import os
|
5 |
+
import librosa
|
6 |
|
7 |
# Get device
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
10 |
# Initialize TTS model
|
11 |
tts = TTS(model_name="voice_conversion_models/multilingual/vctk/freevc24", progress_bar=False).to(device)
|
12 |
|
13 |
+
def convert_audio_to_wav(file_path):
|
14 |
+
"""Convert any supported format (mp3, etc.) to wav using librosa"""
|
15 |
+
output_path = "temp_input.wav"
|
16 |
+
audio, sr = librosa.load(file_path, sr=None) # Load file (wav, mp3, etc.)
|
17 |
+
librosa.output.write_wav(output_path, audio, sr) # Convert to wav
|
18 |
+
return output_path
|
19 |
|
20 |
+
def voice_conversion(input_audio, target_voice, uploaded_target_voice):
|
21 |
output_path = "output.wav"
|
22 |
+
|
23 |
+
# Check if the user uploaded a target voice, otherwise use selected from examples
|
24 |
+
if uploaded_target_voice is not None:
|
25 |
+
target_voice_path = uploaded_target_voice
|
26 |
+
# Convert uploaded target to wav if necessary
|
27 |
+
if not uploaded_target_voice.endswith(".wav"):
|
28 |
+
target_voice_path = convert_audio_to_wav(uploaded_target_voice)
|
29 |
+
else:
|
30 |
+
target_voice_path = os.path.join("Examples", target_voice)
|
31 |
+
if not os.path.exists(target_voice_path):
|
32 |
+
return "Error: Target voice file not found."
|
33 |
+
|
34 |
+
# Convert input audio to wav if necessary
|
35 |
+
if not input_audio.endswith(".wav"):
|
36 |
+
input_audio = convert_audio_to_wav(input_audio)
|
37 |
+
|
38 |
# Perform voice conversion
|
39 |
+
tts.voice_conversion_to_file(source_wav=input_audio, target_wav=target_voice_path, file_path=output_path)
|
|
|
|
|
40 |
return output_path
|
41 |
|
42 |
+
# Get examples from Examples folder
|
43 |
+
examples_folder = "Examples/"
|
44 |
+
example_files = [f for f in os.listdir(examples_folder) if f.endswith(".wav")]
|
45 |
+
|
46 |
# Define Gradio Interface
|
47 |
with gr.Blocks() as demo:
|
48 |
gr.Markdown("## Voice Conversion using Coqui TTS")
|
|
|
51 |
input_audio = gr.Audio(label="Record or Upload Your Voice", type="filepath")
|
52 |
target_voice = gr.Dropdown(choices=example_files, label="Select Target Voice from Examples",
|
53 |
value=example_files[0], info="Located in Examples/ folder")
|
54 |
+
uploaded_target_voice = gr.Audio(label="Or Upload Your Own Target Voice (Overrides Examples)", type="filepath", optional=True)
|
55 |
|
56 |
+
with gr.Row():
|
57 |
+
play_button = gr.Button("Preview Selected Target Voice")
|
58 |
+
preview_audio = gr.Audio(label="Preview Target Voice", type="filepath")
|
59 |
+
|
60 |
+
# Add convert button and output audio
|
61 |
convert_button = gr.Button("Convert Voice")
|
62 |
output_audio = gr.Audio(label="Converted Voice", type="filepath")
|
63 |
+
|
64 |
+
# Preview button for listening to the selected target voice from examples
|
65 |
+
def preview_target_voice(selected_target_voice):
|
66 |
+
return os.path.join(examples_folder, selected_target_voice)
|
67 |
+
|
68 |
+
play_button.click(preview_target_voice, inputs=[target_voice], outputs=preview_audio)
|
69 |
+
|
70 |
+
# Conversion process
|
71 |
+
convert_button.click(voice_conversion, inputs=[input_audio, target_voice, uploaded_target_voice], outputs=output_audio)
|
72 |
|
73 |
# Launch with public=True for public URL access and share link
|
74 |
demo.launch(share=True)
|