| import gradio as gr |
| import os |
| import tempfile |
| import re |
| from pydub import AudioSegment |
| from openai import OpenAI |
|
|
| |
| DEFAULT_API_KEY = "sk-GINVEtfNbrXNcGQhf3rEUIgzoicNGIApovqZxe0AYJF5PkTV" |
| DEFAULT_BASE_URL = "https://open.keyai.shop" |
|
|
| |
| MAX_CHAR_LIMIT = 140964096 |
|
|
| def clean_text(text): |
| |
| cleaned_text = re.sub(r'\s+', ' ', text.strip()) |
| return cleaned_text |
|
|
| def split_text(text, limit=MAX_CHAR_LIMIT): |
| |
| words = text.split(' ') |
| chunks = [] |
| current_chunk = "" |
| for word in words: |
| if len(current_chunk) + len(word) + 1 <= limit: |
| current_chunk += word + " " |
| else: |
| chunks.append(current_chunk.strip()) |
| current_chunk = word + " " |
| if current_chunk: |
| chunks.append(current_chunk.strip()) |
| return chunks |
|
|
| def tts(text, model, voice, speed): |
| cleaned_text = clean_text(text) |
| chunks = split_text(cleaned_text) |
| |
| audio_segments = [] |
| |
| try: |
| client = OpenAI(api_key=DEFAULT_API_KEY, base_url=DEFAULT_BASE_URL + '/v1') |
| for chunk in chunks: |
| response = client.audio.speech.create( |
| model=model, |
| voice=voice, |
| input=chunk, |
| speed=speed |
| ) |
| with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file: |
| temp_file.write(response.content) |
| temp_file_path = temp_file.name |
| audio_segments.append(AudioSegment.from_mp3(temp_file_path)) |
| except Exception as error: |
| 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.") |
| |
| |
| final_audio = sum(audio_segments) |
| with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as final_temp_file: |
| final_audio.export(final_temp_file.name, format="mp3") |
| final_audio_path = final_temp_file.name |
|
|
| return final_audio_path |
|
|
| |
| custom_css = """ |
| body { |
| background-color: #f0f2f5; |
| font-family: 'Arial', sans-serif; |
| } |
| .title { |
| font-size: 2.5rem; |
| font-weight: bold; |
| text-align: center; |
| margin-bottom: 30px; |
| color: #333; |
| } |
| .container { |
| margin: auto; |
| width: 80%; |
| padding: 20px; |
| } |
| .card { |
| background: white; |
| padding: 20px; |
| border-radius: 8px; |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
| margin-bottom: 20px; |
| } |
| .gradio-container { |
| background: #f0f2f5; |
| } |
| button { |
| background-color: #4CAF50; |
| color: white; |
| border: none; |
| border-radius: 8px; |
| padding: 10px 20px; |
| font-size: 1rem; |
| } |
| button:hover { |
| background-color: #45a049; |
| } |
| """ |
|
|
| with gr.Blocks(css=custom_css) as demo: |
| gr.Markdown("<div class='title'>OpenAI TTS</div>") |
| |
| with gr.Column(elem_classes="container"): |
| with gr.Row(): |
| |
| with gr.Column(elem_classes="card"): |
| 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) |
| char_counter = gr.Markdown("Character count: 0") |
| |
| with gr.Column(elem_classes="card"): |
| model = gr.Dropdown(choices=['tts-1', 'tts-1-hd'], label='Model', value='tts-1') |
| voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy') |
| speed = gr.Slider(minimum=0.5, maximum=2.0, step=0.1, label="Speed", value=1.0) |
| |
| with gr.Row(elem_classes="card"): |
| btn = gr.Button("Text-To-Speech") |
| |
| with gr.Row(elem_classes="card"): |
| output_audio = gr.Audio(label="Kết quả âm thanh") |
| |
| |
| def update_char_counter(text): |
| cleaned_text = clean_text(text) |
| return f"Character count: {len(cleaned_text)}" |
| |
| text.change(fn=update_char_counter, inputs=text, outputs=char_counter) |
| text.submit(fn=tts, inputs=[text, model, voice, speed], outputs=output_audio, api_name="tts_enter_key", concurrency_limit=None) |
| btn.click(fn=tts, inputs=[text, model, voice, speed], outputs=output_audio, api_name="tts_button", concurrency_limit=None) |
|
|
| demo.launch() |
|
|