Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,10 +8,7 @@ import tarfile
|
|
8 |
from pathlib import Path
|
9 |
import soundfile as sf
|
10 |
import sherpa_onnx
|
11 |
-
from deep_translator import GoogleTranslator
|
12 |
import numpy as np
|
13 |
-
from iso639 import Lang
|
14 |
-
import pycountry
|
15 |
|
16 |
|
17 |
models = [
|
@@ -21,9 +18,9 @@ models = [
|
|
21 |
['vits-piper-fa-gyro-medium','https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-fa_IR-gyro-medium.tar.bz2'],
|
22 |
['piper-fa-amir-medium','https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-fa_IR-amir-medium.tar.bz2'],
|
23 |
['vits-mimic3-fa-haaniye_low','https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-mimic3-fa-haaniye_low.tar.bz2'],
|
24 |
-
['',''],
|
25 |
]
|
26 |
-
|
27 |
def download_and_extract_model(url, destination):
|
28 |
"""Download and extract the model files."""
|
29 |
print(f"Downloading from URL: {url}")
|
@@ -176,6 +173,9 @@ def dl_espeak_data():
|
|
176 |
print(" Subdirectories:", dirs)
|
177 |
if files:
|
178 |
print(" Files:", files)
|
|
|
|
|
|
|
179 |
def find_model_files(model_dir):
|
180 |
"""Find model files in the given directory and its subdirectories."""
|
181 |
model_files = {}
|
@@ -212,15 +212,18 @@ def find_model_files(model_dir):
|
|
212 |
def generate_audio(text, model_info):
|
213 |
"""Generate audio from text using the specified model."""
|
214 |
try:
|
215 |
-
model_dir = os.path.join("./models", model_info
|
216 |
|
217 |
print(f"\nLooking for model in: {model_dir}")
|
218 |
|
219 |
# Download model if it doesn't exist
|
220 |
if not os.path.exists(model_dir):
|
221 |
-
print(f"Model directory doesn't exist, downloading {model_info
|
222 |
os.makedirs(model_dir, exist_ok=True)
|
223 |
-
|
|
|
|
|
|
|
224 |
|
225 |
print(f"Contents of {model_dir}:")
|
226 |
for item in os.listdir(model_dir):
|
@@ -267,7 +270,7 @@ def generate_audio(text, model_info):
|
|
267 |
|
268 |
# Set data dir if it exists
|
269 |
espeak_data = os.path.join(os.path.dirname(model_files['model']), 'espeak-ng-data')
|
270 |
-
data_dir = espeak_data if os.path.exists(espeak_data) else ''
|
271 |
|
272 |
# Get lexicon path if it exists
|
273 |
lexicon = model_files.get('lexicon', '') if os.path.exists(model_files.get('lexicon', '')) else ''
|
@@ -328,28 +331,22 @@ def tts_interface(selected_model, text, status_output):
|
|
328 |
if not text.strip():
|
329 |
return None, "Please enter some text"
|
330 |
|
331 |
-
|
332 |
-
model_id =
|
333 |
-
if not model_id or model_id not in models:
|
334 |
-
return None, "Please select a model"
|
335 |
-
|
336 |
-
|
337 |
# Store original text for status message
|
338 |
original_text = text
|
339 |
|
340 |
|
341 |
try:
|
342 |
# Update status with language info
|
343 |
-
|
344 |
-
|
345 |
-
voice_name = model_info.get('name', model_id)
|
346 |
-
status = f"Generating speech using {voice_name} ({lang_name})..."
|
347 |
|
348 |
# Generate audio
|
349 |
-
audio_data, sample_rate = generate_audio(text,
|
350 |
|
351 |
# Include translation info in final status if text was actually translated
|
352 |
-
final_status = f"Generated speech using {voice_name}
|
353 |
final_status += f"\nText: '{text}'"
|
354 |
|
355 |
return (sample_rate, audio_data), final_status
|
|
|
8 |
from pathlib import Path
|
9 |
import soundfile as sf
|
10 |
import sherpa_onnx
|
|
|
11 |
import numpy as np
|
|
|
|
|
12 |
|
13 |
|
14 |
models = [
|
|
|
18 |
['vits-piper-fa-gyro-medium','https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-fa_IR-gyro-medium.tar.bz2'],
|
19 |
['piper-fa-amir-medium','https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-fa_IR-amir-medium.tar.bz2'],
|
20 |
['vits-mimic3-fa-haaniye_low','https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-mimic3-fa-haaniye_low.tar.bz2'],
|
21 |
+
# ['',''],
|
22 |
]
|
23 |
+
dropdown_choices = list([i[0] for i in models])
|
24 |
def download_and_extract_model(url, destination):
|
25 |
"""Download and extract the model files."""
|
26 |
print(f"Downloading from URL: {url}")
|
|
|
173 |
print(" Subdirectories:", dirs)
|
174 |
if files:
|
175 |
print(" Files:", files)
|
176 |
+
|
177 |
+
dl_espeak_data()
|
178 |
+
|
179 |
def find_model_files(model_dir):
|
180 |
"""Find model files in the given directory and its subdirectories."""
|
181 |
model_files = {}
|
|
|
212 |
def generate_audio(text, model_info):
|
213 |
"""Generate audio from text using the specified model."""
|
214 |
try:
|
215 |
+
model_dir = os.path.join("./models", model_info)
|
216 |
|
217 |
print(f"\nLooking for model in: {model_dir}")
|
218 |
|
219 |
# Download model if it doesn't exist
|
220 |
if not os.path.exists(model_dir):
|
221 |
+
print(f"Model directory doesn't exist, downloading {model_info}...")
|
222 |
os.makedirs(model_dir, exist_ok=True)
|
223 |
+
for i in models:
|
224 |
+
if model_info == i[0]:
|
225 |
+
model_url=i[1]
|
226 |
+
download_and_extract_model(model_url, model_dir)
|
227 |
|
228 |
print(f"Contents of {model_dir}:")
|
229 |
for item in os.listdir(model_dir):
|
|
|
270 |
|
271 |
# Set data dir if it exists
|
272 |
espeak_data = os.path.join(os.path.dirname(model_files['model']), 'espeak-ng-data')
|
273 |
+
data_dir = espeak_data if os.path.exists(espeak_data) else 'espeak-ng-data'
|
274 |
|
275 |
# Get lexicon path if it exists
|
276 |
lexicon = model_files.get('lexicon', '') if os.path.exists(model_files.get('lexicon', '')) else ''
|
|
|
331 |
if not text.strip():
|
332 |
return None, "Please enter some text"
|
333 |
|
334 |
+
|
335 |
+
model_id = selected_model
|
|
|
|
|
|
|
|
|
336 |
# Store original text for status message
|
337 |
original_text = text
|
338 |
|
339 |
|
340 |
try:
|
341 |
# Update status with language info
|
342 |
+
voice_name = model_id
|
343 |
+
status = f"Generating speech using {voice_name} ..."
|
|
|
|
|
344 |
|
345 |
# Generate audio
|
346 |
+
audio_data, sample_rate = generate_audio(text, model_id)
|
347 |
|
348 |
# Include translation info in final status if text was actually translated
|
349 |
+
final_status = f"Generated speech using {voice_name}"
|
350 |
final_status += f"\nText: '{text}'"
|
351 |
|
352 |
return (sample_rate, audio_data), final_status
|