Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,21 +4,48 @@ import os
|
|
4 |
import zipfile
|
5 |
import requests
|
6 |
from TTS.api import TTS
|
|
|
|
|
7 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
iface.launch(share=True)
|
|
|
4 |
import zipfile
|
5 |
import requests
|
6 |
from TTS.api import TTS
|
7 |
+
|
8 |
+
# Set environment variable
|
9 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
10 |
+
|
11 |
+
# Define constants
|
12 |
+
MODEL_PATH = "tts_models/multilingual/multi-dataset/xtts_v2"
|
13 |
+
LANGUAGES = ["en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko", "hi"]
|
14 |
+
|
15 |
+
# Automatically detect and use GPU if available
|
16 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
17 |
+
print(f"Using device: {device}")
|
18 |
+
|
19 |
+
# Load TTS model
|
20 |
+
tts = TTS(MODEL_PATH).to(device)
|
21 |
|
22 |
def clone(text, url, language):
|
23 |
+
# Download and extract audio file
|
24 |
response = requests.get(url)
|
25 |
with open("temp.zip", "wb") as f:
|
26 |
f.write(response.content)
|
27 |
with zipfile.ZipFile("temp.zip", "r") as zip_ref:
|
28 |
zip_ref.extractall()
|
29 |
audio_file = [f for f in os.listdir(".") if f.endswith(".wav")][0]
|
30 |
+
|
31 |
+
# Generate audio using TTS model
|
32 |
tts.tts_to_file(text=text, speaker_wav=audio_file, language=language, file_path="./output.wav")
|
33 |
+
|
34 |
+
# Clean up
|
35 |
os.remove(audio_file)
|
36 |
os.remove("temp.zip")
|
37 |
+
|
38 |
return "./output.wav"
|
39 |
|
40 |
+
# Create Gradio interface
|
41 |
+
iface = gr.Interface(
|
42 |
+
fn=clone,
|
43 |
+
inputs=["text", gr.components.Text(label="URL"), gr.Dropdown(choices=LANGUAGES, label="Language")],
|
44 |
+
outputs=gr.Audio(type='filepath'),
|
45 |
+
title='Voice Clone',
|
46 |
+
description=""" by [Angetyde](https://youtube.com/@Angetyde?si=7nusP31nTumIkPTF) and [Tony Assi](https://www.tonyassi.com/ ) use this colab with caution <3. """,
|
47 |
+
theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate")
|
48 |
+
)
|
49 |
+
|
50 |
+
# Launch Gradio interface
|
51 |
iface.launch(share=True)
|