Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,87 +1,24 @@
|
|
1 |
-
import
|
2 |
-
import os
|
3 |
import torch
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
from TTS.api import TTS
|
8 |
-
|
9 |
-
# Check if PyTorch is using the GPU for computations
|
10 |
-
if torch.cuda.is_available():
|
11 |
-
device = torch.device("cuda")
|
12 |
-
print("Using the GPU for computations")
|
13 |
-
else:
|
14 |
-
device = torch.device("cpu")
|
15 |
-
print("Using the CPU for computations")
|
16 |
-
|
17 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
18 |
-
|
19 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
20 |
|
21 |
-
def convert_audio_to_wav(file_path):
|
22 |
-
"""Convert the given audio file to WAV format."""
|
23 |
-
file_name = os.path.basename(file_path)
|
24 |
-
file_ext = os.path.splitext(file_name)[1].lower()
|
25 |
-
|
26 |
-
if file_ext == ".mp3":
|
27 |
-
audio, sr = librosa.load(file_path)
|
28 |
-
librosa.output.write_wav(f"temp_{file_name}", audio, sr)
|
29 |
-
file_path = f"temp_{file_name}"
|
30 |
-
elif file_ext == ".flac":
|
31 |
-
os.system(f"ffmpeg -i {file_path} -acodec pcm_s16le -ar 16000 temp_{file_name}")
|
32 |
-
file_path = f"temp_{file_name}"
|
33 |
-
elif file_ext == ".mp4":
|
34 |
-
clip = VideoFileClip(file_path, audio_codec="aac")
|
35 |
-
audio = clip.audio
|
36 |
-
audio.write_audiofile(f"temp_{file_name}")
|
37 |
-
file_path = f"temp_{file_name}"
|
38 |
-
|
39 |
-
return file_path
|
40 |
-
|
41 |
def clone(text, url, language):
|
42 |
-
"""Generate a voice clone using the given parameters."""
|
43 |
response = requests.get(url)
|
44 |
-
|
45 |
with open("temp.zip", "wb") as f:
|
46 |
f.write(response.content)
|
47 |
-
|
48 |
with zipfile.ZipFile("temp.zip", "r") as zip_ref:
|
49 |
zip_ref.extractall()
|
50 |
-
|
51 |
audio_file = [f for f in os.listdir(".") if f.endswith(".wav")][0]
|
52 |
-
|
53 |
-
# Convert the audio file to WAV format
|
54 |
-
if os.path.splitext(audio_file)[1].lower() not in [".wav", ".flac"]:
|
55 |
-
audio_file = convert_audio_to_wav(audio_file)
|
56 |
-
|
57 |
-
# Check if a GPU is available
|
58 |
-
if torch.cuda.is_available():
|
59 |
-
# Set the device to the GPU
|
60 |
-
device = torch.device("cuda")
|
61 |
-
print("Using the GPU for computations")
|
62 |
-
else:
|
63 |
-
# Set the device to the CPU
|
64 |
-
device = torch.device("cpu")
|
65 |
-
print("Using the CPU for computations")
|
66 |
-
|
67 |
-
# Load the TTS model and move it to the device
|
68 |
-
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
69 |
-
|
70 |
tts.tts_to_file(text=text, speaker_wav=audio_file, language=language, file_path="./output.wav")
|
71 |
-
|
72 |
os.remove(audio_file)
|
73 |
os.remove("temp.zip")
|
74 |
-
|
75 |
return "./output.wav"
|
76 |
|
77 |
-
iface = gr.Interface(fn=clone,
|
78 |
-
inputs=["text", gr.components.Text(label="URL"), gr.Dropdown(choices=["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"], label="Language")],
|
79 |
-
outputs=gr.Audio(type='filepath'),
|
80 |
-
title='Voice Clone',
|
81 |
-
description="""
|
82 |
-
by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and$@$v=v1.16$@$[Tony Assi](https://www.tonyassi.com/ )
|
83 |
-
use this colab with caution <3.
|
84 |
-
""",
|
85 |
-
theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"))
|
86 |
-
|
87 |
iface.launch(share=True)
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
import os
|
4 |
+
import zipfile
|
5 |
+
import requests
|
6 |
from TTS.api import TTS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
8 |
+
device = "cuda"
|
9 |
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def clone(text, url, language):
|
|
|
12 |
response = requests.get(url)
|
|
|
13 |
with open("temp.zip", "wb") as f:
|
14 |
f.write(response.content)
|
|
|
15 |
with zipfile.ZipFile("temp.zip", "r") as zip_ref:
|
16 |
zip_ref.extractall()
|
|
|
17 |
audio_file = [f for f in os.listdir(".") if f.endswith(".wav")][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
tts.tts_to_file(text=text, speaker_wav=audio_file, language=language, file_path="./output.wav")
|
|
|
19 |
os.remove(audio_file)
|
20 |
os.remove("temp.zip")
|
|
|
21 |
return "./output.wav"
|
22 |
|
23 |
+
iface = gr.Interface(fn=clone, inputs=["text", gr.components.Text(label="URL"), gr.Dropdown(choices=["en", "es", "fr", "de", "it", "ja", "zh-CN", "zh-TW"], label="Language")], outputs=gr.Audio(type='filepath'), title='Voice Clone', description=""" by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and [Tony Assi](https://www.tonyassi.com/ ) use this colab with caution <3. """, theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
iface.launch(share=True)
|