Spaces:
Running
Running
drewThomasson
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,9 @@ import torch
|
|
3 |
from TTS.api import TTS
|
4 |
import os
|
5 |
import librosa
|
|
|
6 |
from datetime import datetime
|
7 |
|
8 |
-
|
9 |
# Get device
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
|
@@ -19,36 +19,61 @@ def convert_audio_to_wav(file_path):
|
|
19 |
librosa.output.write_wav(output_path, audio, sr) # Convert to wav
|
20 |
return output_path
|
21 |
|
22 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
print(datetime.now())
|
24 |
output_path = "output.wav"
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# Check if the user uploaded a target voice, otherwise use selected from examples
|
27 |
if uploaded_target_voice is not None:
|
28 |
target_voice_path = uploaded_target_voice
|
29 |
-
# Convert uploaded target to wav if necessary
|
30 |
if not uploaded_target_voice.endswith(".wav"):
|
31 |
target_voice_path = convert_audio_to_wav(uploaded_target_voice)
|
32 |
else:
|
33 |
target_voice_path = os.path.join("Examples", target_voice)
|
34 |
if not os.path.exists(target_voice_path):
|
35 |
return "Error: Target voice file not found."
|
36 |
-
|
37 |
# Convert input audio to wav if necessary
|
38 |
if not input_audio.endswith(".wav"):
|
39 |
input_audio = convert_audio_to_wav(input_audio)
|
40 |
-
|
41 |
# Perform voice conversion
|
42 |
tts.voice_conversion_to_file(source_wav=input_audio, target_wav=target_voice_path, file_path=output_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
return output_path
|
44 |
|
45 |
# Get examples from Examples folder
|
46 |
examples_folder = "Examples/"
|
47 |
example_files = [f for f in os.listdir(examples_folder) if f.endswith(".wav")]
|
48 |
|
49 |
-
# Define Gradio Interface
|
50 |
with gr.Blocks() as demo:
|
51 |
gr.Markdown("## Voice Conversion using Coqui TTS")
|
|
|
|
|
52 |
|
53 |
with gr.Row():
|
54 |
input_audio = gr.Audio(label="Record or Upload Your Voice", type="filepath")
|
@@ -67,22 +92,21 @@ with gr.Blocks() as demo:
|
|
67 |
play_button = gr.Button("Preview Selected Target Voice")
|
68 |
preview_audio = gr.Audio(label="Preview Target Voice", type="filepath")
|
69 |
|
70 |
-
# Add convert button and output audio
|
71 |
convert_button = gr.Button("Convert Voice")
|
72 |
output_audio = gr.Audio(label="Converted Voice", type="filepath")
|
73 |
-
|
74 |
# Preview button for listening to the selected target voice from examples
|
75 |
def preview_target_voice(selected_target_voice):
|
76 |
return os.path.join(examples_folder, selected_target_voice)
|
77 |
-
|
78 |
play_button.click(preview_target_voice, inputs=[target_voice], outputs=preview_audio)
|
79 |
-
|
80 |
-
# Conversion process
|
81 |
convert_button.click(
|
82 |
voice_conversion,
|
83 |
-
inputs=[input_audio, target_voice, uploaded_target_voice],
|
84 |
outputs=output_audio
|
85 |
)
|
86 |
|
87 |
# Launch with public=True for public URL access and share link
|
88 |
-
demo.launch(share=True)
|
|
|
3 |
from TTS.api import TTS
|
4 |
import os
|
5 |
import librosa
|
6 |
+
import requests
|
7 |
from datetime import datetime
|
8 |
|
|
|
9 |
# Get device
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
|
|
|
19 |
librosa.output.write_wav(output_path, audio, sr) # Convert to wav
|
20 |
return output_path
|
21 |
|
22 |
+
def upload_to_file_io(file_path):
|
23 |
+
"""Uploads a file to file.io and returns the temporary link"""
|
24 |
+
url = "https://file.io"
|
25 |
+
with open(file_path, 'rb') as f:
|
26 |
+
response = requests.post(url, files={"file": f})
|
27 |
+
if response.status_code == 200:
|
28 |
+
temp_link = response.json().get('link')
|
29 |
+
return temp_link
|
30 |
+
return None
|
31 |
+
|
32 |
+
def voice_conversion(input_audio, target_voice, uploaded_target_voice, restrict_duration):
|
33 |
print(datetime.now())
|
34 |
output_path = "output.wav"
|
35 |
+
|
36 |
+
# Check audio duration if the flag is True
|
37 |
+
if restrict_duration:
|
38 |
+
duration = librosa.get_duration(filename=input_audio)
|
39 |
+
if duration > 120:
|
40 |
+
return "Error: Audio file exceeds 2 minutes."
|
41 |
+
|
42 |
# Check if the user uploaded a target voice, otherwise use selected from examples
|
43 |
if uploaded_target_voice is not None:
|
44 |
target_voice_path = uploaded_target_voice
|
|
|
45 |
if not uploaded_target_voice.endswith(".wav"):
|
46 |
target_voice_path = convert_audio_to_wav(uploaded_target_voice)
|
47 |
else:
|
48 |
target_voice_path = os.path.join("Examples", target_voice)
|
49 |
if not os.path.exists(target_voice_path):
|
50 |
return "Error: Target voice file not found."
|
51 |
+
|
52 |
# Convert input audio to wav if necessary
|
53 |
if not input_audio.endswith(".wav"):
|
54 |
input_audio = convert_audio_to_wav(input_audio)
|
55 |
+
|
56 |
# Perform voice conversion
|
57 |
tts.voice_conversion_to_file(source_wav=input_audio, target_wav=target_voice_path, file_path=output_path)
|
58 |
+
|
59 |
+
# Upload converted file to file.io
|
60 |
+
temp_link = upload_to_file_io(output_path)
|
61 |
+
if temp_link:
|
62 |
+
print(f"File uploaded to: {temp_link}") # Log the file link to the terminal
|
63 |
+
else:
|
64 |
+
print("Error uploading the file to file.io")
|
65 |
+
|
66 |
return output_path
|
67 |
|
68 |
# Get examples from Examples folder
|
69 |
examples_folder = "Examples/"
|
70 |
example_files = [f for f in os.listdir(examples_folder) if f.endswith(".wav")]
|
71 |
|
72 |
+
# Define Gradio Interface with Boolean to activate restriction
|
73 |
with gr.Blocks() as demo:
|
74 |
gr.Markdown("## Voice Conversion using Coqui TTS")
|
75 |
+
|
76 |
+
restrict_duration = gr.Checkbox(label="Restrict audio to 2 minutes or less?", value=True)
|
77 |
|
78 |
with gr.Row():
|
79 |
input_audio = gr.Audio(label="Record or Upload Your Voice", type="filepath")
|
|
|
92 |
play_button = gr.Button("Preview Selected Target Voice")
|
93 |
preview_audio = gr.Audio(label="Preview Target Voice", type="filepath")
|
94 |
|
|
|
95 |
convert_button = gr.Button("Convert Voice")
|
96 |
output_audio = gr.Audio(label="Converted Voice", type="filepath")
|
97 |
+
|
98 |
# Preview button for listening to the selected target voice from examples
|
99 |
def preview_target_voice(selected_target_voice):
|
100 |
return os.path.join(examples_folder, selected_target_voice)
|
101 |
+
|
102 |
play_button.click(preview_target_voice, inputs=[target_voice], outputs=preview_audio)
|
103 |
+
|
104 |
+
# Conversion process with duration restriction and file.io upload
|
105 |
convert_button.click(
|
106 |
voice_conversion,
|
107 |
+
inputs=[input_audio, target_voice, uploaded_target_voice, restrict_duration],
|
108 |
outputs=output_audio
|
109 |
)
|
110 |
|
111 |
# Launch with public=True for public URL access and share link
|
112 |
+
demo.launch(share=True)
|