File size: 2,624 Bytes
63f1d6d
 
 
 
9d562d5
6f41dcc
9d562d5
 
 
 
 
 
6f41dcc
9d562d5
63f1d6d
7ca85b9
63f1d6d
 
7ca85b9
63f1d6d
 
 
 
 
 
 
 
9d562d5
 
 
63f1d6d
 
 
 
 
 
6f41dcc
 
 
 
63f1d6d
 
 
 
 
 
 
 
 
 
 
d81bde6
 
 
63f1d6d
 
 
 
 
 
 
 
 
 
 
 
 
d81bde6
7ca85b9
63f1d6d
d6ffc2c
63f1d6d
 
 
 
 
 
 
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
import gradio as gr
import edge_tts
import asyncio
import tempfile
import re
import emoji

# Функция для очистки текста от нежелательных символов и эмодзи
def clean_text(text):
    # Удаление указанных символов
    text = re.sub(r'[*_~><]', '', text)
    # Удаление эмодзи
    text = emoji.replace_emoji(text, replace='')
    return text

# Get all available voices
async def get_voices():
    voices = await edge_tts.list_voices()
    return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}

# Text-to-speech function
async def text_to_speech(text, voice, rate, pitch):
    if not text.strip():
        return None, gr.Warning("Please enter text to convert.")
    if not voice:
        return None, gr.Warning("Please select a voice.")
    
    # Очистка текста
    text = clean_text(text)
    
    voice_short_name = voice.split(" - ")[0]
    rate_str = f"{rate:+d}%"
    pitch_str = f"{pitch:+d}Hz"
    communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
        tmp_path = tmp_file.name
        try:
            await communicate.save(tmp_path)
        except Exception as e:
            return None, gr.Warning(f"An error occurred during text-to-speech conversion: {str(e)}")
    return tmp_path, None

# Gradio interface function
def tts_interface(text, voice, rate, pitch):
    audio, warning = asyncio.run(text_to_speech(text, voice, rate, pitch))
    return audio, warning

# Create Gradio application
async def create_demo():
    voices = await get_voices()
    
    description = """
    """
    
    demo = gr.Interface(
        fn=tts_interface,
        inputs=[
            gr.Textbox(label="Input Text", lines=5),
            gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
            gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
            gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
        ],
        outputs=[
            gr.Audio(label="Generated Audio", type="filepath"),
            gr.Markdown(label="Warning", visible=False)
        ],
        title="Edge TTS Text-to-Speech",
        description=description,
        article="",
        analytics_enabled=False,
        allow_flagging="manual"
    )
    return demo

# Run the application
if __name__ == "__main__":
    demo = asyncio.run(create_demo())
    demo.launch()