Update app.py
Browse files
app.py
CHANGED
@@ -5,12 +5,10 @@ from scipy.io import wavfile
|
|
5 |
from voice_processing import tts, get_model_names, voice_mapping
|
6 |
from io import BytesIO
|
7 |
import asyncio
|
8 |
-
import json
|
9 |
-
import uuid
|
10 |
|
11 |
# Constants for limits
|
12 |
-
MAX_TEXT_FILES = 20 #
|
13 |
-
MAX_WORDS = 5000 #
|
14 |
BATCH_SIZE = 5 # Number of texts to process in parallel
|
15 |
|
16 |
async def process_tts_request(model_name, tts_text, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
|
@@ -40,20 +38,19 @@ async def process_tts_request(model_name, tts_text, selected_voice, slang_rate,
|
|
40 |
else:
|
41 |
audio_bytes = audio_output
|
42 |
|
43 |
-
|
44 |
-
return {"info": info}, audio_data_uri
|
45 |
|
46 |
async def convert_tts(model_name, tts_texts, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
|
47 |
# Enforce limits
|
48 |
if len(tts_texts) > MAX_TEXT_FILES:
|
49 |
-
return
|
50 |
|
51 |
word_count = sum(len(tts_text.split()) for tts_text in tts_texts)
|
52 |
if word_count > MAX_WORDS:
|
53 |
-
return
|
54 |
|
55 |
# Process texts in batches
|
56 |
-
|
57 |
for i in range(0, len(tts_texts), BATCH_SIZE):
|
58 |
batch_texts = tts_texts[i:i+BATCH_SIZE]
|
59 |
tasks = [
|
@@ -61,15 +58,12 @@ async def convert_tts(model_name, tts_texts, selected_voice, slang_rate, use_upl
|
|
61 |
for tts_text in batch_texts
|
62 |
]
|
63 |
batch_results = await asyncio.gather(*tasks)
|
64 |
-
|
65 |
-
info, audio_uri = result
|
66 |
-
json_content = {"info": info, "audio_uri": audio_uri}
|
67 |
-
json_filename = f"{uuid.uuid4()}.json"
|
68 |
-
with open(json_filename, 'w') as json_file:
|
69 |
-
json.dump(json_content, json_file)
|
70 |
-
json_files.append(json_filename)
|
71 |
|
72 |
-
|
|
|
|
|
|
|
73 |
|
74 |
def get_models():
|
75 |
return get_model_names()
|
@@ -88,7 +82,8 @@ iface = gr.Interface(
|
|
88 |
gr.File(label="Voice File")
|
89 |
],
|
90 |
outputs=[
|
91 |
-
gr.
|
|
|
92 |
],
|
93 |
title="Text-to-Speech Conversion"
|
94 |
)
|
@@ -96,3 +91,4 @@ iface = gr.Interface(
|
|
96 |
iface.launch()
|
97 |
|
98 |
|
|
|
|
5 |
from voice_processing import tts, get_model_names, voice_mapping
|
6 |
from io import BytesIO
|
7 |
import asyncio
|
|
|
|
|
8 |
|
9 |
# Constants for limits
|
10 |
+
MAX_TEXT_FILES = 20 # Maximum number of text files processed concurrently
|
11 |
+
MAX_WORDS = 5000 # Maximum number of words processed concurrently
|
12 |
BATCH_SIZE = 5 # Number of texts to process in parallel
|
13 |
|
14 |
async def process_tts_request(model_name, tts_text, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
|
|
|
38 |
else:
|
39 |
audio_bytes = audio_output
|
40 |
|
41 |
+
return {"info": info}, audio_bytes
|
|
|
42 |
|
43 |
async def convert_tts(model_name, tts_texts, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
|
44 |
# Enforce limits
|
45 |
if len(tts_texts) > MAX_TEXT_FILES:
|
46 |
+
return {"error": f"Number of text files should not exceed {MAX_TEXT_FILES}."}, None
|
47 |
|
48 |
word_count = sum(len(tts_text.split()) for tts_text in tts_texts)
|
49 |
if word_count > MAX_WORDS:
|
50 |
+
return {"error": f"Total number of words should not exceed {MAX_WORDS}."}, None
|
51 |
|
52 |
# Process texts in batches
|
53 |
+
results = []
|
54 |
for i in range(0, len(tts_texts), BATCH_SIZE):
|
55 |
batch_texts = tts_texts[i:i+BATCH_SIZE]
|
56 |
tasks = [
|
|
|
58 |
for tts_text in batch_texts
|
59 |
]
|
60 |
batch_results = await asyncio.gather(*tasks)
|
61 |
+
results.extend(batch_results)
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
info_list = [{"info": info} for info, _ in results]
|
64 |
+
audio_uris = [f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode('utf-8')}" for _, audio_bytes in results]
|
65 |
+
|
66 |
+
return info_list, audio_uris
|
67 |
|
68 |
def get_models():
|
69 |
return get_model_names()
|
|
|
82 |
gr.File(label="Voice File")
|
83 |
],
|
84 |
outputs=[
|
85 |
+
gr.JSON(label="Info"),
|
86 |
+
gr.JSON(label="Audio URIs")
|
87 |
],
|
88 |
title="Text-to-Speech Conversion"
|
89 |
)
|
|
|
91 |
iface.launch()
|
92 |
|
93 |
|
94 |
+
|