Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,21 +6,23 @@ import zipfile
|
|
6 |
from TTS.api import TTS
|
7 |
from pydub import AudioSegment
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
MODEL_PATH = "tts_models/multilingual/multi-dataset/xtts_v2"
|
12 |
-
LANGUAGES = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"]
|
13 |
AUDIO_FORMATS = [".wav", ".mp3", ".flac", ".mp4"]
|
|
|
14 |
|
|
|
15 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
16 |
print(f"Using device: {device}")
|
17 |
|
18 |
-
|
|
|
19 |
|
20 |
-
def download_audio_file(url):
|
21 |
try:
|
22 |
response = requests.get(url)
|
23 |
file_extension = os.path.splitext(url)[-1].lower()
|
|
|
|
|
24 |
file_name = f"temp{file_extension}"
|
25 |
with open(file_name, "wb") as f:
|
26 |
f.write(response.content)
|
@@ -29,7 +31,7 @@ def download_audio_file(url):
|
|
29 |
print(f"Error downloading audio file: {e}")
|
30 |
return None
|
31 |
|
32 |
-
def extract_zip_file(zip_file):
|
33 |
try:
|
34 |
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
35 |
zip_ref.extractall()
|
@@ -38,52 +40,48 @@ def extract_zip_file(zip_file):
|
|
38 |
print(f"Error extracting zip file: {e}")
|
39 |
return False
|
40 |
|
41 |
-
def
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
|
50 |
-
def synthesize_text(text, input_audio_file, language):
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
def
|
56 |
-
if
|
57 |
-
input_file
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
67 |
else:
|
68 |
-
|
|
|
|
|
69 |
output_file_path = synthesize_text(text, input_audio_file, language)
|
70 |
return output_file_path
|
71 |
|
72 |
iface = gr.Interface(
|
73 |
-
fn=
|
74 |
-
inputs=[
|
75 |
-
|
76 |
-
gr.inputs.File(label="Input File", file_types=[".zip", *AUDIO_FORMATS]),
|
77 |
-
gr.inputs.Dropdown(choices=LANGUAGES, label="Language"),
|
78 |
-
gr.inputs.Textbox(label="URL", lines=1)
|
79 |
-
],
|
80 |
-
outputs=gr.outputs.Audio(type='filepath'),
|
81 |
title='Voice Clone',
|
82 |
-
description="""
|
83 |
-
|
84 |
-
use this colab with caution <3.
|
85 |
-
""",
|
86 |
-
theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate")
|
87 |
)
|
88 |
|
89 |
-
iface.launch(
|
|
|
6 |
from TTS.api import TTS
|
7 |
from pydub import AudioSegment
|
8 |
|
9 |
+
# Constants
|
|
|
|
|
|
|
10 |
AUDIO_FORMATS = [".wav", ".mp3", ".flac", ".mp4"]
|
11 |
+
LANGUAGES = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"]
|
12 |
|
13 |
+
# Device setup
|
14 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
15 |
print(f"Using device: {device}")
|
16 |
|
17 |
+
# TTS model setup
|
18 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
19 |
|
20 |
+
def download_audio_file(url: str) -> str:
|
21 |
try:
|
22 |
response = requests.get(url)
|
23 |
file_extension = os.path.splitext(url)[-1].lower()
|
24 |
+
if file_extension not in AUDIO_FORMATS:
|
25 |
+
raise ValueError(f"Unsupported file extension: {file_extension}")
|
26 |
file_name = f"temp{file_extension}"
|
27 |
with open(file_name, "wb") as f:
|
28 |
f.write(response.content)
|
|
|
31 |
print(f"Error downloading audio file: {e}")
|
32 |
return None
|
33 |
|
34 |
+
def extract_zip_file(zip_file: str) -> bool:
|
35 |
try:
|
36 |
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
37 |
zip_ref.extractall()
|
|
|
40 |
print(f"Error extracting zip file: {e}")
|
41 |
return False
|
42 |
|
43 |
+
def audio_processing:
|
44 |
+
def convert_to_wav(input_audio_file: str) -> str:
|
45 |
+
file_extension = os.path.splitext(input_audio_file)[-1].lower()
|
46 |
+
if file_extension!= ".wav":
|
47 |
+
audio = AudioSegment.from_file(input_audio_file)
|
48 |
+
audio.export("temp.wav", format="wav")
|
49 |
+
os.remove(input_audio_file)
|
50 |
+
return "temp.wav"
|
51 |
+
return input_audio_file
|
52 |
|
53 |
+
def synthesize_text(text: str, input_audio_file: str, language: str) -> str:
|
54 |
+
input_audio_file = convert_to_wav(input_audio_file)
|
55 |
+
tts.tts_to_file(text=text, speaker_wav=input_audio_file, language=language, file_path="./output.wav")
|
56 |
+
return "./output.wav"
|
57 |
|
58 |
+
def voice_clone(text: str, input_file: gr.File, language: str, url: gr.Audio) -> str:
|
59 |
+
if input_file is not None:
|
60 |
+
if input_file.name.endswith(".zip"):
|
61 |
+
if extract_zip_file(input_file):
|
62 |
+
input_audio_file = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith(tuple(AUDIO_FORMATS))]
|
63 |
+
if len(input_audio_file) == 1:
|
64 |
+
input_audio_file = input_audio_file[0]
|
65 |
+
else:
|
66 |
+
return "Error: Please select a single audio file from the extracted files."
|
67 |
+
else:
|
68 |
+
input_audio_file = input_file.name
|
69 |
+
elif url:
|
70 |
+
input_audio_file = url.name
|
71 |
else:
|
72 |
+
return"Error: Please select a file or enter a URL"
|
73 |
+
if language not in LANGUAGES:
|
74 |
+
return "Error: Invalid language selected"
|
75 |
output_file_path = synthesize_text(text, input_audio_file, language)
|
76 |
return output_file_path
|
77 |
|
78 |
iface = gr.Interface(
|
79 |
+
fn=voice_clone,
|
80 |
+
inputs=["text", gr.File(label="Input File", file_types=[".zip", *AUDIO_FORMATS]), gr.Dropdown(choices=LANGUAGES, label="Language"), gr.Audio(label="URL")],
|
81 |
+
outputs=gr.Audio(type='filepath'),
|
|
|
|
|
|
|
|
|
|
|
82 |
title='Voice Clone',
|
83 |
+
description=""" by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and [Tony Assi](https://www.tonyassi.com/ ) use this colab with caution <3. """,
|
84 |
+
theme=gr.themes.Base(primary_hue="green"),
|
|
|
|
|
|
|
85 |
)
|
86 |
|
87 |
+
iface.launch()
|