File size: 3,997 Bytes
112539c
cd58124
 
112539c
 
071ff53
cd58124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20bc263
cd58124
 
 
 
20bc263
cd58124
 
 
 
 
 
20bc263
cd58124
 
 
 
20bc263
cd58124
 
 
 
5130a91
cbac426
cd58124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import gradio as gr
import edge_tts
import io
import os
import time
import asyncio
from docx import Document

AUDIO_DIR = 'audio_files'  # Thư mục để lưu tệp âm thanh
MAX_FILE_AGE = 24 * 60 * 60  # Thời gian lưu trữ tệp âm thanh (24 giờ)

# Hàm để lấy tất cả các giọng nói có sẵn
async def get_voices():
    voices = await edge_tts.list_voices()
    return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}

async def text_to_speech(text, lang):
    tts = edge_tts.Communicate(text, voice=lang)
    
    # Đường dẫn cho tệp âm thanh
    os.makedirs(AUDIO_DIR, exist_ok=True)  # Tạo thư mục nếu chưa tồn tại
    audio_file_name = f"{time.time()}.mp3"
    audio_file_path = os.path.join(AUDIO_DIR, audio_file_name)

    # Lưu âm thanh vào tệp
    await tts.save(audio_file_path)  # Lưu trực tiếp vào đường dẫn hợp lệ
    delete_old_audio_files()  # Xóa các tệp âm thanh cũ
    return audio_file_path, audio_file_path  # Trả về đường dẫn tệp âm thanh

def delete_old_audio_files():
    now = time.time()
    for file_name in os.listdir(AUDIO_DIR):
        file_path = os.path.join(AUDIO_DIR, file_name)
        if now - os.path.getmtime(file_path) > MAX_FILE_AGE:
            os.remove(file_path)

async def txt_to_speech(file, lang):
    with open(file.name, 'r') as f:
        text = f.read()
    return await text_to_speech(text, lang)

async def docx_to_speech(file, lang):
    doc = Document(file.name)
    text = "\n".join([para.text for para in doc.paragraphs])  # Lấy tất cả văn bản từ các đoạn
    return await text_to_speech(text, lang)

# Tạo giao diện Gradio
async def create_interface():
    voices = await get_voices()  # Lấy danh sách giọng nói

    with gr.Blocks() as iface:
        with gr.Tab("Text to Speech"):
            gr.Markdown("### Convert text to speech")
            text_input = gr.Textbox(lines=10, label="Enter your text here:")
            lang_input = gr.Dropdown(choices=list(voices.keys()), label="Select language:")  # Cập nhật dropdown giọng nói
            
            audio_output, file_output = gr.Audio(label="Audio"), gr.File(label="Audio File")
            gr.Button("Convert").click(fn=lambda text, lang: asyncio.run(text_to_speech(text, voices[lang])),
                                        inputs=[text_input, lang_input], 
                                        outputs=[audio_output, file_output])

        with gr.Tab("TXT to Speech"):
            gr.Markdown("### Convert .txt file to speech")
            file_input = gr.File(label="Upload your .txt file")
            lang_input_file = gr.Dropdown(choices=list(voices.keys()), label="Select language:")  # Cập nhật dropdown giọng nói
            
            audio_output_file, file_output_file = gr.Audio(label="Audio"), gr.File(label="Audio File")
            gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(txt_to_speech(file, voices[lang])),
                                        inputs=[file_input, lang_input_file], 
                                        outputs=[audio_output_file, file_output_file])

        with gr.Tab("DOCX to Speech"):
            gr.Markdown("### Convert .docx file to speech")
            docx_file_input = gr.File(label="Upload your .docx file")
            lang_input_docx = gr.Dropdown(choices=list(voices.keys()), label="Select language:")  # Cập nhật dropdown giọng nói
            
            audio_output_docx, file_output_docx = gr.Audio(label="Audio"), gr.File(label="Audio File")
            gr.Button("Convert").click(fn=lambda file, lang: asyncio.run(docx_to_speech(file, voices[lang])),
                                        inputs=[docx_file_input, lang_input_docx], 
                                        outputs=[audio_output_docx, file_output_docx])

    iface.launch(enable_queue=True)

# Chạy ứng dụng
asyncio.run(create_interface())