Spaces:
Running
Running
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() |