Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,6 @@ import numpy as np
|
|
4 |
from scipy.io import wavfile
|
5 |
from voice_processing import tts, get_model_names, voice_mapping
|
6 |
from io import BytesIO
|
7 |
-
import json
|
8 |
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
9 |
import asyncio
|
10 |
|
11 |
async def convert_tts(model_name, tts_text, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
|
@@ -36,42 +34,6 @@ async def convert_tts(model_name, tts_text, selected_voice, slang_rate, use_uplo
|
|
36 |
audio_data_uri = f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode('utf-8')}"
|
37 |
return {"info": info}, audio_data_uri
|
38 |
|
39 |
-
def convert_tts_sync(*args):
|
40 |
-
loop = asyncio.new_event_loop()
|
41 |
-
asyncio.set_event_loop(loop)
|
42 |
-
return loop.run_until_complete(convert_tts(*args))
|
43 |
-
|
44 |
-
def batch_convert_tts(json_input):
|
45 |
-
results = []
|
46 |
-
|
47 |
-
try:
|
48 |
-
batch_data = json.loads(json_input)
|
49 |
-
except Exception as e:
|
50 |
-
return {"error": f"Failed to parse JSON input: {str(e)}"}
|
51 |
-
|
52 |
-
with ThreadPoolExecutor() as executor:
|
53 |
-
future_to_entry = {
|
54 |
-
executor.submit(
|
55 |
-
convert_tts_sync,
|
56 |
-
entry.get("model_name"),
|
57 |
-
entry.get("text"),
|
58 |
-
entry.get("voice"),
|
59 |
-
entry.get("slang_rate", 0.5),
|
60 |
-
entry.get("use_uploaded_voice", False),
|
61 |
-
entry.get("voice_upload", None)
|
62 |
-
): entry for entry in batch_data
|
63 |
-
}
|
64 |
-
|
65 |
-
for future in as_completed(future_to_entry):
|
66 |
-
entry = future_to_entry[future]
|
67 |
-
try:
|
68 |
-
result = future.result()
|
69 |
-
results.append({"info": result[0], "audio_uri": result[1]})
|
70 |
-
except Exception as e:
|
71 |
-
results.append({"error": str(e)})
|
72 |
-
|
73 |
-
return json.dumps(results, indent=4)
|
74 |
-
|
75 |
def get_models():
|
76 |
return get_model_names()
|
77 |
|
@@ -79,7 +41,7 @@ def get_voices():
|
|
79 |
return list(voice_mapping.keys())
|
80 |
|
81 |
iface = gr.Interface(
|
82 |
-
fn=
|
83 |
inputs=[
|
84 |
gr.Dropdown(choices=get_models(), label="Model", interactive=True),
|
85 |
gr.Textbox(label="Text", placeholder="Enter text here"),
|
@@ -92,24 +54,9 @@ iface = gr.Interface(
|
|
92 |
gr.JSON(label="Info"),
|
93 |
gr.Textbox(label="Audio URI")
|
94 |
],
|
95 |
-
title="Text-to-Speech Conversion"
|
96 |
-
|
97 |
-
)
|
98 |
-
|
99 |
-
batch_iface = gr.Interface(
|
100 |
-
fn=batch_convert_tts,
|
101 |
-
inputs=gr.Textbox(label="JSON Input", lines=20, placeholder='Paste your JSON input here'),
|
102 |
-
outputs=gr.JSON(label="Batch Results"),
|
103 |
-
title="Batch Text-to-Speech Conversion",
|
104 |
-
allow_flagging="never"
|
105 |
-
)
|
106 |
-
|
107 |
-
app = gr.TabbedInterface(
|
108 |
-
interface_list=[iface, batch_iface],
|
109 |
-
tab_names=["Single Conversion", "Batch Conversion"]
|
110 |
-
)
|
111 |
-
|
112 |
-
app.launch(debug=True)
|
113 |
|
|
|
114 |
|
115 |
|
|
|
4 |
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 |
|
9 |
async def convert_tts(model_name, tts_text, selected_voice, slang_rate, use_uploaded_voice, voice_upload):
|
|
|
34 |
audio_data_uri = f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode('utf-8')}"
|
35 |
return {"info": info}, audio_data_uri
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
def get_models():
|
38 |
return get_model_names()
|
39 |
|
|
|
41 |
return list(voice_mapping.keys())
|
42 |
|
43 |
iface = gr.Interface(
|
44 |
+
fn=convert_tts,
|
45 |
inputs=[
|
46 |
gr.Dropdown(choices=get_models(), label="Model", interactive=True),
|
47 |
gr.Textbox(label="Text", placeholder="Enter text here"),
|
|
|
54 |
gr.JSON(label="Info"),
|
55 |
gr.Textbox(label="Audio URI")
|
56 |
],
|
57 |
+
title="Text-to-Speech Conversion"
|
58 |
+
).queue(default_concurrency_limit=6) # Set concurrency limit to 6 based on your hardware
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
iface.launch()
|
61 |
|
62 |
|