Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,21 @@ from TTS.api import TTS
|
|
4 |
import os
|
5 |
import spaces
|
6 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
9 |
|
@@ -15,43 +30,26 @@ def load_tts_model():
|
|
15 |
|
16 |
tts = load_tts_model()
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
"Darth Vader": "./voices/Darth Vader.mp3",
|
28 |
-
"Homer Simpson": "./voices/Homer Simpson.mp3",
|
29 |
-
"Mario": "./voices/Mario.mp3",
|
30 |
-
"PewDiePie": "./voices/PewDiePie.mp3",
|
31 |
-
"Pokimane": "./voices/Pokimane.mp3",
|
32 |
-
"Ninja": "./voices/Ninja.mp3",
|
33 |
-
"Shroud": "./voices/Shroud.mp3",
|
34 |
-
"Tfue": "./voices/Tfue.mp3",
|
35 |
-
"Barack Obama": "./voices/Barack Obama.mp3",
|
36 |
-
"Donald Trump": "./voices/Donald Trump.mp3",
|
37 |
-
"Angela Merkel": "./voices/Angela Merkel.mp3",
|
38 |
-
"Justin Trudeau": "./voices/Justin Trudeau.mp3",
|
39 |
-
"Emmanuel Macron": "./voices/Emmanuel Macron.mp3",
|
40 |
-
"Serena Williams": "./voices/Serena Williams.mp3",
|
41 |
-
"Michael Jordan": "./voices/Michael Jordan.mp3",
|
42 |
-
"Lionel Messi": "./voices/Lionel Messi.mp3",
|
43 |
-
"LeBron James": "./voices/LeBron James.mp3",
|
44 |
-
"Usain Bolt": "./voices/Usain.mp3" # Note: The file is named "Usain.mp3"
|
45 |
-
}
|
46 |
|
47 |
def check_voice_files():
|
48 |
"""
|
49 |
-
Checks if all voice files exist in the
|
50 |
Returns a message listing missing files or confirming all files are present.
|
51 |
"""
|
52 |
missing = []
|
53 |
for voice, path in celebrity_voices.items():
|
54 |
-
|
|
|
|
|
55 |
missing.append(f"{voice}: {path}")
|
56 |
if missing:
|
57 |
return "**Missing Voice Files:**\n" + "\n".join(missing)
|
@@ -63,9 +61,11 @@ def tts_generate(text, voice, language):
|
|
63 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
|
64 |
temp_audio_path = temp_audio.name
|
65 |
|
|
|
|
|
66 |
tts.tts_to_file(
|
67 |
text=text,
|
68 |
-
speaker_wav=
|
69 |
language=language,
|
70 |
file_path=temp_audio_path
|
71 |
)
|
@@ -86,10 +86,6 @@ def clone_voice(text, audio_file, language):
|
|
86 |
|
87 |
return temp_audio_path
|
88 |
|
89 |
-
# Placeholder function for Talking Image tab
|
90 |
-
def talking_image_placeholder():
|
91 |
-
return "Talking Image functionality not implemented yet."
|
92 |
-
|
93 |
# Define Gradio interface
|
94 |
with gr.Blocks() as demo:
|
95 |
gr.Markdown("# Advanced Voice Synthesis")
|
@@ -103,7 +99,7 @@ with gr.Blocks() as demo:
|
|
103 |
with gr.Row():
|
104 |
tts_text = gr.Textbox(label="Text to speak")
|
105 |
tts_voice = gr.Dropdown(choices=list(celebrity_voices.keys()), label="Celebrity Voice")
|
106 |
-
tts_language = gr.Dropdown(["en", "es", "fr", "de", "it","ar"], label="Language", value="en")
|
107 |
tts_generate_btn = gr.Button("Generate")
|
108 |
tts_output = gr.Audio(label="Generated Audio")
|
109 |
|
@@ -113,14 +109,11 @@ with gr.Blocks() as demo:
|
|
113 |
outputs=tts_output
|
114 |
)
|
115 |
|
116 |
-
with gr.TabItem("Talking Image"):
|
117 |
-
gr.Markdown("Talking Image functionality coming soon!")
|
118 |
-
|
119 |
with gr.TabItem("Clone Voice"):
|
120 |
with gr.Row():
|
121 |
clone_text = gr.Textbox(label="Text to speak")
|
122 |
clone_audio = gr.Audio(label="Voice reference audio file", type="filepath")
|
123 |
-
clone_language = gr.Dropdown(["en", "es", "fr", "de", "it","ar"], label="Language", value="en")
|
124 |
clone_generate_btn = gr.Button("Generate")
|
125 |
clone_output = gr.Audio(label="Generated Audio")
|
126 |
|
@@ -130,21 +123,10 @@ with gr.Blocks() as demo:
|
|
130 |
outputs=clone_output
|
131 |
)
|
132 |
|
133 |
-
js_func = """
|
134 |
-
function refresh() {
|
135 |
-
const url = new URL(window.location);
|
136 |
-
|
137 |
-
if (url.searchParams.get('__theme') !== 'dark') {
|
138 |
-
url.searchParams.set('__theme', 'dark');
|
139 |
-
window.location.href = url.href;
|
140 |
-
}
|
141 |
-
}
|
142 |
-
"""
|
143 |
-
|
144 |
# Launch the interface
|
145 |
demo.launch()
|
146 |
|
147 |
# Clean up temporary files (this will run after the Gradio server is closed)
|
148 |
for file in os.listdir():
|
149 |
if file.endswith('.wav') and file.startswith('tmp'):
|
150 |
-
os.remove(file)
|
|
|
4 |
import os
|
5 |
import spaces
|
6 |
import tempfile
|
7 |
+
from pymongo import MongoClient
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
from huggingface_hub import hf_hub_download
|
10 |
+
|
11 |
+
# Load environment variables
|
12 |
+
load_dotenv()
|
13 |
+
|
14 |
+
# Get MongoDB URI and Hugging Face token from .env file
|
15 |
+
mongodb_uri = os.getenv('MONGODB_URI')
|
16 |
+
hf_token = os.getenv('HF_TOKEN')
|
17 |
+
|
18 |
+
# Connect to MongoDB
|
19 |
+
client = MongoClient(mongodb_uri)
|
20 |
+
db = client['mitra-voices']
|
21 |
+
voices_collection = db['voices']
|
22 |
|
23 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
24 |
|
|
|
30 |
|
31 |
tts = load_tts_model()
|
32 |
|
33 |
+
# Fetch celebrity voices from MongoDB
|
34 |
+
def get_celebrity_voices():
|
35 |
+
voices = {}
|
36 |
+
for category in voices_collection.find():
|
37 |
+
for voice in category['voices']:
|
38 |
+
voices[voice['name']] = f"voices/{voice['name']}.mp3"
|
39 |
+
return voices
|
40 |
+
|
41 |
+
celebrity_voices = get_celebrity_voices()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
def check_voice_files():
|
44 |
"""
|
45 |
+
Checks if all voice files exist in the Hugging Face repository.
|
46 |
Returns a message listing missing files or confirming all files are present.
|
47 |
"""
|
48 |
missing = []
|
49 |
for voice, path in celebrity_voices.items():
|
50 |
+
try:
|
51 |
+
hf_hub_download(repo_id="nikkmitra/clone", filename=path, repo_type="space", token=hf_token)
|
52 |
+
except Exception:
|
53 |
missing.append(f"{voice}: {path}")
|
54 |
if missing:
|
55 |
return "**Missing Voice Files:**\n" + "\n".join(missing)
|
|
|
61 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
|
62 |
temp_audio_path = temp_audio.name
|
63 |
|
64 |
+
voice_file = hf_hub_download(repo_id="nikkmitra/clone", filename=celebrity_voices[voice], repo_type="space", token=hf_token)
|
65 |
+
|
66 |
tts.tts_to_file(
|
67 |
text=text,
|
68 |
+
speaker_wav=voice_file,
|
69 |
language=language,
|
70 |
file_path=temp_audio_path
|
71 |
)
|
|
|
86 |
|
87 |
return temp_audio_path
|
88 |
|
|
|
|
|
|
|
|
|
89 |
# Define Gradio interface
|
90 |
with gr.Blocks() as demo:
|
91 |
gr.Markdown("# Advanced Voice Synthesis")
|
|
|
99 |
with gr.Row():
|
100 |
tts_text = gr.Textbox(label="Text to speak")
|
101 |
tts_voice = gr.Dropdown(choices=list(celebrity_voices.keys()), label="Celebrity Voice")
|
102 |
+
tts_language = gr.Dropdown(["en", "es", "fr", "de", "it", "ar"], label="Language", value="en")
|
103 |
tts_generate_btn = gr.Button("Generate")
|
104 |
tts_output = gr.Audio(label="Generated Audio")
|
105 |
|
|
|
109 |
outputs=tts_output
|
110 |
)
|
111 |
|
|
|
|
|
|
|
112 |
with gr.TabItem("Clone Voice"):
|
113 |
with gr.Row():
|
114 |
clone_text = gr.Textbox(label="Text to speak")
|
115 |
clone_audio = gr.Audio(label="Voice reference audio file", type="filepath")
|
116 |
+
clone_language = gr.Dropdown(["en", "es", "fr", "de", "it", "ar"], label="Language", value="en")
|
117 |
clone_generate_btn = gr.Button("Generate")
|
118 |
clone_output = gr.Audio(label="Generated Audio")
|
119 |
|
|
|
123 |
outputs=clone_output
|
124 |
)
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
# Launch the interface
|
127 |
demo.launch()
|
128 |
|
129 |
# Clean up temporary files (this will run after the Gradio server is closed)
|
130 |
for file in os.listdir():
|
131 |
if file.endswith('.wav') and file.startswith('tmp'):
|
132 |
+
os.remove(file)
|