Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
3 |
-
import
|
4 |
import torch
|
5 |
import zipfile
|
6 |
from TTS.api import TTS
|
@@ -15,75 +14,39 @@ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
15 |
print(f"Using device: {device}")
|
16 |
|
17 |
# TTS model setup
|
|
|
18 |
MODEL_PATH = "tts_models/multilingual/multi-dataset/xtts_v2"
|
19 |
tts = TTS(MODEL_PATH).to(device)
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
def convert_to_wav(self, input_audio_file: str) -> str:
|
26 |
-
file_extension = os.path.splitext(input_audio_file)[-1].lower()
|
27 |
-
if file_extension!= ".wav":
|
28 |
-
audio = AudioSegment.from_file(input_audio_file)
|
29 |
-
audio.export("temp.wav", format="wav")
|
30 |
-
os.remove(input_audio_file)
|
31 |
-
return "temp.wav"
|
32 |
-
return input_audio_file
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
tts.tts_to_file(text=text, speaker_wav=input_audio_file, language=language, file_path="./output.wav")
|
37 |
-
return "./output.wav"
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
file_extension = os.path.splitext(url)[-1].lower()
|
43 |
-
if file_extension not in AUDIO_FORMATS:
|
44 |
-
raise ValueError(f"Unsupported file extension: {file_extension}")
|
45 |
-
file_name = f"temp{file_extension}"
|
46 |
-
with open(file_name, "wb") as f:
|
47 |
-
f.write(response.content)
|
48 |
-
return file_name
|
49 |
-
except requests.exceptions.RequestException as e:
|
50 |
-
print(f"Error downloading audio file: {e}")
|
51 |
-
return None
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
zip_ref.extractall()
|
57 |
-
return True
|
58 |
-
except zipfile.BadZipfile as e:
|
59 |
-
print(f"Error extracting zip file: {e}")
|
60 |
-
return False
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
if input_file is None:
|
65 |
-
return None
|
66 |
-
if input_file.name.endswith(".zip"):
|
67 |
-
if extract_zip_file(input_file):
|
68 |
-
input_audio_file = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith(tuple(AUDIO_FORMATS))]
|
69 |
-
if len(input_audio_file) == 1:
|
70 |
-
input_audio_file = input_audio_file[0]
|
71 |
-
else:
|
72 |
-
return "Error: Please select a single audio file from the extracted files."
|
73 |
-
else:
|
74 |
-
input_audio_file = input_file.name
|
75 |
-
output_file_path = audio_processor.synthesize_text(text, input_audio_file, language)
|
76 |
-
return output_file_path
|
77 |
|
78 |
iface = gr.Interface(
|
79 |
-
|
80 |
-
inputs=["text",
|
81 |
-
outputs=
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
87 |
)
|
88 |
|
89 |
iface.launch()
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
import torch
|
4 |
import zipfile
|
5 |
from TTS.api import TTS
|
|
|
14 |
print(f"Using device: {device}")
|
15 |
|
16 |
# TTS model setup
|
17 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
18 |
MODEL_PATH = "tts_models/multilingual/multi-dataset/xtts_v2"
|
19 |
tts = TTS(MODEL_PATH).to(device)
|
20 |
|
21 |
+
def generate_audio(text, language, speed, pitch, volume):
|
22 |
+
# Prepare input
|
23 |
+
input_text = {"text": text, "language": language}
|
24 |
+
tts.prepare_input(input_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Generate audio
|
27 |
+
audio = tts.generate_audio(input_text, speed=speed, pitch=pitch, volume=volume)
|
|
|
|
|
28 |
|
29 |
+
# Save audio
|
30 |
+
audio_path = "output.wav"
|
31 |
+
tts.save_audio(audio_path, audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
# Convert to mp3
|
34 |
+
audio_segment = AudioSegment.from_wav(audio_path)
|
35 |
+
audio_segment.export(audio_path[:-4] + ".mp3", format="mp3")
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
# Return audio path
|
38 |
+
return audio_path[:-4] + ".mp3"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
iface = gr.Interface(
|
41 |
+
generate_audio,
|
42 |
+
inputs=["text", "language", "speed", "pitch", "volume"],
|
43 |
+
outputs="audio",
|
44 |
+
audio=True,
|
45 |
+
audio_format="mp3",
|
46 |
+
title="Text-to-Speech",
|
47 |
+
description="Convert text to speech in multiple languages.",
|
48 |
+
allow_flagging=False,
|
49 |
+
cache_examples=False,
|
50 |
)
|
51 |
|
52 |
iface.launch()
|