Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,16 @@ import re
|
|
| 5 |
from pydub import AudioSegment # Library to combine audio files
|
| 6 |
from openai import OpenAI
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# Max character limit per API request
|
| 9 |
MAX_CHAR_LIMIT = 140964096
|
| 10 |
|
| 11 |
def clean_text(text):
|
| 12 |
# Replace newlines with spaces and multiple spaces with a single space
|
| 13 |
-
cleaned_text = re.sub(r'\s+', ' ', text.strip())
|
| 14 |
return cleaned_text
|
| 15 |
|
| 16 |
def split_text(text, limit=MAX_CHAR_LIMIT):
|
|
@@ -20,40 +24,38 @@ def split_text(text, limit=MAX_CHAR_LIMIT):
|
|
| 20 |
current_chunk = ""
|
| 21 |
|
| 22 |
for word in words:
|
| 23 |
-
# Add words to the current chunk without exceeding the character limit
|
| 24 |
-
if len(current_chunk) + len(word) + 1 <= limit:
|
| 25 |
current_chunk += word + " "
|
| 26 |
else:
|
| 27 |
-
chunks.append(current_chunk.strip())
|
| 28 |
-
current_chunk = word + " "
|
| 29 |
|
| 30 |
if current_chunk:
|
| 31 |
-
chunks.append(current_chunk.strip())
|
| 32 |
|
| 33 |
return chunks
|
| 34 |
|
| 35 |
-
def tts(text, model, voice, speed
|
| 36 |
-
if api_key == '':
|
| 37 |
-
raise gr.Error('Please enter your Key')
|
| 38 |
-
|
| 39 |
cleaned_text = clean_text(text)
|
| 40 |
chunks = split_text(cleaned_text)
|
| 41 |
|
| 42 |
audio_segments = []
|
| 43 |
-
|
| 44 |
try:
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
# Process each chunk of text
|
| 48 |
for chunk in chunks:
|
| 49 |
response = client.audio.speech.create(
|
| 50 |
-
model=model,
|
| 51 |
-
voice=voice,
|
| 52 |
input=chunk,
|
| 53 |
speed=speed
|
| 54 |
)
|
| 55 |
|
| 56 |
-
# Create a
|
| 57 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
|
| 58 |
temp_file.write(response.content)
|
| 59 |
temp_file_path = temp_file.name
|
|
@@ -76,13 +78,10 @@ def tts(text, model, voice, speed, api_key, base_url):
|
|
| 76 |
with gr.Blocks() as demo:
|
| 77 |
gr.Markdown("# <center> OpenAI TTS </center>")
|
| 78 |
with gr.Row(variant='panel'):
|
| 79 |
-
api_key = gr.Textbox(type='password', label='OpenAI API Key', placeholder='sk-sAqNNgs8VZyi8DHY4a37D44eBc3d408e96D40116Da679376')
|
| 80 |
model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model', value='tts-1')
|
| 81 |
voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy')
|
| 82 |
speed = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Speed", value=1.0)
|
| 83 |
-
|
| 84 |
-
base_url = gr.Dropdown(choices=['https://open.keyai.shop','https://api.openai.com' ], label="API Endpoint", value='https://open.keyai.shop')
|
| 85 |
-
|
| 86 |
text = gr.Textbox(label="Input text", placeholder="Enter your text and then click on the 'Text-To-Speech' button, or simply press the Enter key.")
|
| 87 |
char_counter = gr.Markdown("Character count: 0")
|
| 88 |
btn = gr.Button("Text-To-Speech")
|
|
@@ -94,7 +93,8 @@ with gr.Blocks() as demo:
|
|
| 94 |
|
| 95 |
text.change(fn=update_char_counter, inputs=text, outputs=char_counter)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
demo.launch()
|
|
|
|
| 5 |
from pydub import AudioSegment # Library to combine audio files
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
+
# Default constants
|
| 9 |
+
DEFAULT_API_KEY = "sk-GINVEtfNbrXNcGQhf3rEUIgzoicNGIApovqZxe0AYJF5PkTV"
|
| 10 |
+
DEFAULT_BASE_URL = "https://open.keyai.shop"
|
| 11 |
+
|
| 12 |
# Max character limit per API request
|
| 13 |
MAX_CHAR_LIMIT = 140964096
|
| 14 |
|
| 15 |
def clean_text(text):
|
| 16 |
# Replace newlines with spaces and multiple spaces with a single space
|
| 17 |
+
cleaned_text = re.sub(r'\s+', ' ', text.strip())
|
| 18 |
return cleaned_text
|
| 19 |
|
| 20 |
def split_text(text, limit=MAX_CHAR_LIMIT):
|
|
|
|
| 24 |
current_chunk = ""
|
| 25 |
|
| 26 |
for word in words:
|
| 27 |
+
# Add words to the current chunk without exceeding the character limit (+1 for space)
|
| 28 |
+
if len(current_chunk) + len(word) + 1 <= limit:
|
| 29 |
current_chunk += word + " "
|
| 30 |
else:
|
| 31 |
+
chunks.append(current_chunk.strip())
|
| 32 |
+
current_chunk = word + " "
|
| 33 |
|
| 34 |
if current_chunk:
|
| 35 |
+
chunks.append(current_chunk.strip())
|
| 36 |
|
| 37 |
return chunks
|
| 38 |
|
| 39 |
+
def tts(text, model, voice, speed):
|
|
|
|
|
|
|
|
|
|
| 40 |
cleaned_text = clean_text(text)
|
| 41 |
chunks = split_text(cleaned_text)
|
| 42 |
|
| 43 |
audio_segments = []
|
| 44 |
+
|
| 45 |
try:
|
| 46 |
+
# Use default API key and endpoint
|
| 47 |
+
client = OpenAI(api_key=DEFAULT_API_KEY, base_url=DEFAULT_BASE_URL + '/v1')
|
| 48 |
|
| 49 |
# Process each chunk of text
|
| 50 |
for chunk in chunks:
|
| 51 |
response = client.audio.speech.create(
|
| 52 |
+
model=model, # Options: "tts-1", "tts-1-hd"
|
| 53 |
+
voice=voice, # Options: 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
|
| 54 |
input=chunk,
|
| 55 |
speed=speed
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# Create a temporary file for each audio chunk
|
| 59 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
|
| 60 |
temp_file.write(response.content)
|
| 61 |
temp_file_path = temp_file.name
|
|
|
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
gr.Markdown("# <center> OpenAI TTS </center>")
|
| 80 |
with gr.Row(variant='panel'):
|
|
|
|
| 81 |
model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model', value='tts-1')
|
| 82 |
voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy')
|
| 83 |
speed = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Speed", value=1.0)
|
| 84 |
+
|
|
|
|
|
|
|
| 85 |
text = gr.Textbox(label="Input text", placeholder="Enter your text and then click on the 'Text-To-Speech' button, or simply press the Enter key.")
|
| 86 |
char_counter = gr.Markdown("Character count: 0")
|
| 87 |
btn = gr.Button("Text-To-Speech")
|
|
|
|
| 93 |
|
| 94 |
text.change(fn=update_char_counter, inputs=text, outputs=char_counter)
|
| 95 |
|
| 96 |
+
# Removed API key and endpoint inputs from both submit and button click actions
|
| 97 |
+
text.submit(fn=tts, inputs=[text, model, voice, speed], outputs=output_audio, api_name="tts_enter_key", concurrency_limit=None)
|
| 98 |
+
btn.click(fn=tts, inputs=[text, model, voice, speed], outputs=output_audio, api_name="tts_button", concurrency_limit=None)
|
| 99 |
|
| 100 |
demo.launch()
|