Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,14 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
import re
|
| 5 |
-
from pydub import AudioSegment #
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
-
#
|
| 9 |
DEFAULT_API_KEY = "sk-GINVEtfNbrXNcGQhf3rEUIgzoicNGIApovqZxe0AYJF5PkTV"
|
| 10 |
DEFAULT_BASE_URL = "https://open.keyai.shop"
|
| 11 |
|
| 12 |
-
#
|
| 13 |
MAX_CHAR_LIMIT = 140964096
|
| 14 |
|
| 15 |
def clean_text(text):
|
|
@@ -62,10 +62,13 @@ def tts(text, model, voice, speed):
|
|
| 62 |
|
| 63 |
return final_audio_path
|
| 64 |
|
| 65 |
-
# CSS tùy chỉnh
|
| 66 |
custom_css = """
|
|
|
|
|
|
|
| 67 |
body {
|
| 68 |
-
background-color: #
|
|
|
|
| 69 |
font-family: 'Arial', sans-serif;
|
| 70 |
}
|
| 71 |
.title {
|
|
@@ -73,22 +76,22 @@ body {
|
|
| 73 |
font-weight: bold;
|
| 74 |
text-align: center;
|
| 75 |
margin-bottom: 30px;
|
| 76 |
-
color: #
|
| 77 |
}
|
| 78 |
.container {
|
| 79 |
margin: auto;
|
| 80 |
-
width:
|
| 81 |
padding: 20px;
|
| 82 |
}
|
| 83 |
.card {
|
| 84 |
-
background:
|
| 85 |
padding: 20px;
|
| 86 |
border-radius: 8px;
|
| 87 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.
|
| 88 |
margin-bottom: 20px;
|
| 89 |
}
|
| 90 |
.gradio-container {
|
| 91 |
-
background: #
|
| 92 |
}
|
| 93 |
button {
|
| 94 |
background-color: #4CAF50;
|
|
@@ -97,30 +100,48 @@ button {
|
|
| 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 |
-
|
|
|
|
| 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=
|
| 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='
|
| 119 |
-
speed = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="
|
| 120 |
|
|
|
|
| 121 |
with gr.Row(elem_classes="card"):
|
| 122 |
-
btn = gr.Button("
|
| 123 |
|
|
|
|
| 124 |
with gr.Row(elem_classes="card"):
|
| 125 |
output_audio = gr.Audio(label="Kết quả âm thanh")
|
| 126 |
|
|
|
|
| 2 |
import os
|
| 3 |
import tempfile
|
| 4 |
import re
|
| 5 |
+
from pydub import AudioSegment # Thư viện để kết hợp các file âm thanh
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
+
# API key và endpoint mặc định
|
| 9 |
DEFAULT_API_KEY = "sk-GINVEtfNbrXNcGQhf3rEUIgzoicNGIApovqZxe0AYJF5PkTV"
|
| 10 |
DEFAULT_BASE_URL = "https://open.keyai.shop"
|
| 11 |
|
| 12 |
+
# Giới hạn ký tự tối đa mỗi yêu cầu API
|
| 13 |
MAX_CHAR_LIMIT = 140964096
|
| 14 |
|
| 15 |
def clean_text(text):
|
|
|
|
| 62 |
|
| 63 |
return final_audio_path
|
| 64 |
|
| 65 |
+
# CSS tùy chỉnh với giao diện tối và tích hợp FontAwesome cho icon
|
| 66 |
custom_css = """
|
| 67 |
+
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css');
|
| 68 |
+
|
| 69 |
body {
|
| 70 |
+
background-color: #1e1e2f;
|
| 71 |
+
color: #e0e0e0;
|
| 72 |
font-family: 'Arial', sans-serif;
|
| 73 |
}
|
| 74 |
.title {
|
|
|
|
| 76 |
font-weight: bold;
|
| 77 |
text-align: center;
|
| 78 |
margin-bottom: 30px;
|
| 79 |
+
color: #f1f1f1;
|
| 80 |
}
|
| 81 |
.container {
|
| 82 |
margin: auto;
|
| 83 |
+
width: 90%;
|
| 84 |
padding: 20px;
|
| 85 |
}
|
| 86 |
.card {
|
| 87 |
+
background: #2e2e42;
|
| 88 |
padding: 20px;
|
| 89 |
border-radius: 8px;
|
| 90 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.5);
|
| 91 |
margin-bottom: 20px;
|
| 92 |
}
|
| 93 |
.gradio-container {
|
| 94 |
+
background: #1e1e2f;
|
| 95 |
}
|
| 96 |
button {
|
| 97 |
background-color: #4CAF50;
|
|
|
|
| 100 |
border-radius: 8px;
|
| 101 |
padding: 10px 20px;
|
| 102 |
font-size: 1rem;
|
| 103 |
+
cursor: pointer;
|
| 104 |
}
|
| 105 |
button:hover {
|
| 106 |
background-color: #45a049;
|
| 107 |
}
|
| 108 |
+
input, textarea, select {
|
| 109 |
+
background: #3a3a50;
|
| 110 |
+
border: 1px solid #555;
|
| 111 |
+
color: #e0e0e0;
|
| 112 |
+
padding: 8px;
|
| 113 |
+
border-radius: 4px;
|
| 114 |
+
}
|
| 115 |
+
select {
|
| 116 |
+
padding: 8px 10px;
|
| 117 |
+
}
|
| 118 |
+
label {
|
| 119 |
+
font-weight: bold;
|
| 120 |
+
margin-bottom: 5px;
|
| 121 |
+
}
|
| 122 |
"""
|
| 123 |
|
| 124 |
with gr.Blocks(css=custom_css) as demo:
|
| 125 |
+
# Tiêu đề với icon Microphone
|
| 126 |
+
gr.Markdown("<div class='title'><i class='fas fa-microphone-alt'></i> OpenAI TTS</div>")
|
| 127 |
|
| 128 |
with gr.Column(elem_classes="container"):
|
| 129 |
with gr.Row():
|
| 130 |
+
# Cột nhập văn bản (mở rộng với 15 dòng)
|
| 131 |
with gr.Column(elem_classes="card"):
|
| 132 |
+
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=15)
|
| 133 |
char_counter = gr.Markdown("Character count: 0")
|
| 134 |
+
# Cột cài đặt tham số TTS với menu được cải tiến
|
| 135 |
with gr.Column(elem_classes="card"):
|
| 136 |
+
model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Chọn Model', value='tts-1')
|
| 137 |
+
voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Chọn Giọng', value='alloy')
|
| 138 |
+
speed = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Tốc độ", value=1.0)
|
| 139 |
|
| 140 |
+
# Nút chuyển đổi với icon Play
|
| 141 |
with gr.Row(elem_classes="card"):
|
| 142 |
+
btn = gr.Button("<i class='fas fa-play'></i> Chuyển Giọng")
|
| 143 |
|
| 144 |
+
# Kết quả âm thanh
|
| 145 |
with gr.Row(elem_classes="card"):
|
| 146 |
output_audio = gr.Audio(label="Kết quả âm thanh")
|
| 147 |
|