File size: 1,256 Bytes
b522a6b
 
 
 
 
0ae9d87
b522a6b
 
 
 
 
 
 
 
 
 
8d93856
 
b522a6b
 
8d93856
 
b522a6b
 
 
 
8d93856
0ae9d87
 
9417890
0ae9d87
 
9417890
 
0ae9d87
 
8d93856
 
 
 
 
 
 
 
b522a6b
 
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
import gradio as gr
import uuid
import os
import subprocess
import soundfile as sf
from pydub import AudioSegment

from app_utils import tts_interface, models

borna_model = next(model for model in models if "برنا" in model[0] or "برنا" in model[2])
voice_id = borna_model[2]

STATIC_DIR = "static"
os.makedirs(STATIC_DIR, exist_ok=True)

def tts(text):
    input_path = os.path.join(STATIC_DIR, "input.txt")
    with open(input_path, "w", encoding="utf-8") as f:
        f.write(text.strip())

    subprocess.run(
        ["espeak-ng", "-v", "fa", "-x", "-q", "-f", input_path],
        capture_output=True,
        text=True
    )

    (sr, audio), _ = tts_interface(voice_id, text.strip(), '')
    temp_wav = os.path.join(STATIC_DIR, f"{uuid.uuid4().hex}.wav")
    sf.write(temp_wav, audio, samplerate=sr, subtype="PCM_16")

    out_path = temp_wav.replace(".wav", ".ogg")
    sound = AudioSegment.from_wav(temp_wav)
    sound = sound.set_channels(1)
    sound.export(out_path, format="ogg", codec="libopus")

    os.remove(temp_wav)
    return out_path

demo = gr.Interface(
    fn=tts,
    inputs=gr.Textbox(show_label=False, placeholder="متن فارسی..."),
    outputs=gr.Audio(label="", type="filepath"),
    live=False
)

demo.launch()