File size: 3,084 Bytes
e54c498
50139e9
 
 
 
e54c498
50139e9
 
 
 
 
e54c498
50139e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163df47
50139e9
 
 
 
 
e54c498
50139e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e54c498
50139e9
 
 
 
e54c498
50139e9
 
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
import gradio as gr
import edge_tts
import asyncio
import tempfile
import os

# دریافت و فیلتر صداهای فارسی (fa-IR)
async def get_voices():
    voices = await edge_tts.list_voices()
    persian_voices = [v for v in voices if v['Locale'] == 'fa-IR']
    return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in persian_voices}

# تبدیل متن به گفتار به زبان فارسی
async def text_to_speech(text, voice, rate, pitch):
    if not text.strip():
        return None, "لطفاً متنی جهت تبدیل وارد کنید."
    if not voice:
        return None, "لطفاً یک صدا انتخاب کنید."
    
    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
        await communicate.save(tmp_path)
    return tmp_path, None

async def tts_interface(text, voice, rate, pitch):
    audio, warning = await text_to_speech(text, voice, rate, pitch)
    if warning:
        return audio, gr.Warning(warning)
    return audio, None

async def create_demo():
    voices = await get_voices()
    
    description = """
    متن فارسی خود را وارد کنید تا به صدای طبیعی تبدیل شود.
    این برنامه از Microsoft Edge TTS برای تبدیل متن به گفتار فارسی استفاده می‌کند.
    سرعت و تن صدا قابل تنظیم است: مقدار 0 پیش‌فرض است؛ مقادیر مثبت سرعت یا تن را افزایش و مقادیر منفی کاهش می‌دهند.
    توجه: این برنامه تنها از صداهای فارسی پشتیبانی می‌کند.
    """
    
    demo = gr.Interface(
        fn=tts_interface,
        inputs=[
            gr.Textbox(label="متن ورودی (فارسی)", lines=5),
            gr.Dropdown(choices=[""] + list(voices.keys()), label="انتخاب صدا", value=""),
            gr.Slider(minimum=-50, maximum=50, value=0, label="تنظیم سرعت گفتار (%)", step=1),
            gr.Slider(minimum=-20, maximum=20, value=0, label="تنظیم تن صدا (Hz)", step=1)
        ],
        outputs=[
            gr.Audio(label="صدای تولید شده", type="filepath"),
            gr.Markdown(label="هشدار", visible=False)
        ],
        title="تبدیل متن به گفتار فارسی (Microsoft Edge TTS)",
        description=description,
        article="این برنامه از Microsoft Edge TTS برای تبدیل متن فارسی به گفتار استفاده می‌کند.",
        analytics_enabled=False,
        allow_flagging="manual",
        api_name=None
    )
    return demo

async def main():
    demo = await create_demo()
    demo.queue(default_concurrency_limit=5)
    demo.launch(show_api=False)

if __name__ == "__main__":
    asyncio.run(main())