Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import re
|
|
| 5 |
from pydub import AudioSegment # Library to combine audio files
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
-
# Default
|
| 9 |
DEFAULT_API_KEY = "sk-GINVEtfNbrXNcGQhf3rEUIgzoicNGIApovqZxe0AYJF5PkTV"
|
| 10 |
DEFAULT_BASE_URL = "https://open.keyai.shop"
|
| 11 |
|
|
@@ -13,27 +13,23 @@ DEFAULT_BASE_URL = "https://open.keyai.shop"
|
|
| 13 |
MAX_CHAR_LIMIT = 140964096
|
| 14 |
|
| 15 |
def clean_text(text):
|
| 16 |
-
#
|
| 17 |
cleaned_text = re.sub(r'\s+', ' ', text.strip())
|
| 18 |
return cleaned_text
|
| 19 |
|
| 20 |
def split_text(text, limit=MAX_CHAR_LIMIT):
|
| 21 |
-
#
|
| 22 |
words = text.split(' ')
|
| 23 |
chunks = []
|
| 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):
|
|
@@ -43,57 +39,97 @@ def tts(text, model, voice, speed):
|
|
| 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, #
|
| 53 |
-
voice=voice, #
|
| 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
|
| 62 |
audio_segments.append(AudioSegment.from_mp3(temp_file_path))
|
| 63 |
-
|
| 64 |
except Exception as error:
|
| 65 |
-
raise gr.Error("
|
| 66 |
|
| 67 |
-
#
|
| 68 |
final_audio = sum(audio_segments)
|
| 69 |
-
|
| 70 |
-
# Save the concatenated audio to a final file
|
| 71 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as final_temp_file:
|
| 72 |
final_audio.export(final_temp_file.name, format="mp3")
|
| 73 |
final_audio_path = final_temp_file.name
|
| 74 |
|
| 75 |
return final_audio_path
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
with gr.Blocks() as demo:
|
| 79 |
-
gr.Markdown("
|
| 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 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
def update_char_counter(text):
|
| 91 |
-
cleaned_text = clean_text(text)
|
| 92 |
return f"Character count: {len(cleaned_text)}"
|
| 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 |
|
|
|
|
| 5 |
from pydub import AudioSegment # Library to combine audio files
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
+
# Default API key and endpoint
|
| 9 |
DEFAULT_API_KEY = "sk-GINVEtfNbrXNcGQhf3rEUIgzoicNGIApovqZxe0AYJF5PkTV"
|
| 10 |
DEFAULT_BASE_URL = "https://open.keyai.shop"
|
| 11 |
|
|
|
|
| 13 |
MAX_CHAR_LIMIT = 140964096
|
| 14 |
|
| 15 |
def clean_text(text):
|
| 16 |
+
# Loại bỏ khoảng trắng thừa và xuống dòng
|
| 17 |
cleaned_text = re.sub(r'\s+', ' ', text.strip())
|
| 18 |
return cleaned_text
|
| 19 |
|
| 20 |
def split_text(text, limit=MAX_CHAR_LIMIT):
|
| 21 |
+
# Tách văn bản thành các đoạn không vượt quá giới hạn ký tự
|
| 22 |
words = text.split(' ')
|
| 23 |
chunks = []
|
| 24 |
current_chunk = ""
|
|
|
|
| 25 |
for word in words:
|
|
|
|
| 26 |
if len(current_chunk) + len(word) + 1 <= limit:
|
| 27 |
current_chunk += word + " "
|
| 28 |
else:
|
| 29 |
chunks.append(current_chunk.strip())
|
| 30 |
current_chunk = word + " "
|
|
|
|
| 31 |
if current_chunk:
|
| 32 |
chunks.append(current_chunk.strip())
|
|
|
|
| 33 |
return chunks
|
| 34 |
|
| 35 |
def tts(text, model, voice, speed):
|
|
|
|
| 39 |
audio_segments = []
|
| 40 |
|
| 41 |
try:
|
|
|
|
| 42 |
client = OpenAI(api_key=DEFAULT_API_KEY, base_url=DEFAULT_BASE_URL + '/v1')
|
|
|
|
|
|
|
| 43 |
for chunk in chunks:
|
| 44 |
response = client.audio.speech.create(
|
| 45 |
+
model=model, # Các lựa chọn: "tts-1", "tts-1-hd"
|
| 46 |
+
voice=voice, # Các lựa chọn: 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
|
| 47 |
input=chunk,
|
| 48 |
speed=speed
|
| 49 |
)
|
|
|
|
|
|
|
| 50 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
|
| 51 |
temp_file.write(response.content)
|
| 52 |
temp_file_path = temp_file.name
|
| 53 |
audio_segments.append(AudioSegment.from_mp3(temp_file_path))
|
|
|
|
| 54 |
except Exception as error:
|
| 55 |
+
raise gr.Error("Đã xảy ra lỗi khi tạo giọng nói. Vui lòng kiểm tra API key hoặc thử lại sau.")
|
| 56 |
|
| 57 |
+
# Nối các đoạn âm thanh lại thành một file duy nhất
|
| 58 |
final_audio = sum(audio_segments)
|
|
|
|
|
|
|
| 59 |
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as final_temp_file:
|
| 60 |
final_audio.export(final_temp_file.name, format="mp3")
|
| 61 |
final_audio_path = final_temp_file.name
|
| 62 |
|
| 63 |
return final_audio_path
|
| 64 |
|
| 65 |
+
# CSS tùy chỉnh cho giao diện
|
| 66 |
+
custom_css = """
|
| 67 |
+
body {
|
| 68 |
+
background-color: #f0f2f5;
|
| 69 |
+
font-family: 'Arial', sans-serif;
|
| 70 |
+
}
|
| 71 |
+
.title {
|
| 72 |
+
font-size: 2.5rem;
|
| 73 |
+
font-weight: bold;
|
| 74 |
+
text-align: center;
|
| 75 |
+
margin-bottom: 30px;
|
| 76 |
+
color: #333;
|
| 77 |
+
}
|
| 78 |
+
.container {
|
| 79 |
+
margin: auto;
|
| 80 |
+
width: 80%;
|
| 81 |
+
padding: 20px;
|
| 82 |
+
}
|
| 83 |
+
.card {
|
| 84 |
+
background: white;
|
| 85 |
+
padding: 20px;
|
| 86 |
+
border-radius: 8px;
|
| 87 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 88 |
+
margin-bottom: 20px;
|
| 89 |
+
}
|
| 90 |
+
.gradio-container {
|
| 91 |
+
background: #f0f2f5;
|
| 92 |
+
}
|
| 93 |
+
button {
|
| 94 |
+
background-color: #4CAF50;
|
| 95 |
+
color: white;
|
| 96 |
+
border: none;
|
| 97 |
+
border-radius: 8px;
|
| 98 |
+
padding: 10px 20px;
|
| 99 |
+
font-size: 1rem;
|
| 100 |
+
}
|
| 101 |
+
button:hover {
|
| 102 |
+
background-color: #45a049;
|
| 103 |
+
}
|
| 104 |
+
"""
|
| 105 |
|
| 106 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 107 |
+
gr.Markdown("<div class='title'>OpenAI TTS</div>")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
with gr.Column(elem_classes="container"):
|
| 110 |
+
with gr.Row():
|
| 111 |
+
# Cột nhập văn bản
|
| 112 |
+
with gr.Column(elem_classes="card"):
|
| 113 |
+
text = gr.Textbox(label="Văn bản đầu vào", placeholder="Nhập văn bản cần chuyển giọng nói...", lines=10)
|
| 114 |
+
char_counter = gr.Markdown("Character count: 0")
|
| 115 |
+
# Cột cài đặt tham số TTS
|
| 116 |
+
with gr.Column(elem_classes="card"):
|
| 117 |
+
model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model', value='tts-1')
|
| 118 |
+
voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy')
|
| 119 |
+
speed = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Speed", value=1.0)
|
| 120 |
+
|
| 121 |
+
with gr.Row(elem_classes="card"):
|
| 122 |
+
btn = gr.Button("Text-To-Speech")
|
| 123 |
+
|
| 124 |
+
with gr.Row(elem_classes="card"):
|
| 125 |
+
output_audio = gr.Audio(label="Kết quả âm thanh")
|
| 126 |
+
|
| 127 |
+
# Hàm cập nhật số lượng ký tự
|
| 128 |
def update_char_counter(text):
|
| 129 |
+
cleaned_text = clean_text(text)
|
| 130 |
return f"Character count: {len(cleaned_text)}"
|
| 131 |
+
|
| 132 |
text.change(fn=update_char_counter, inputs=text, outputs=char_counter)
|
|
|
|
|
|
|
| 133 |
text.submit(fn=tts, inputs=[text, model, voice, speed], outputs=output_audio, api_name="tts_enter_key", concurrency_limit=None)
|
| 134 |
btn.click(fn=tts, inputs=[text, model, voice, speed], outputs=output_audio, api_name="tts_button", concurrency_limit=None)
|
| 135 |
|