MMD-Coder/persian-tts-piper
1
1import gradio as gr2import wave3import numpy as np4from io import BytesIO5from huggingface_hub import hf_hub_download6from piper import PiperVoice 7from transformers import pipeline8import typing9 10model_path = hf_hub_download(repo_id="gyroing/Persian-Piper-Model-gyro", filename="fa_IR-gyro-medium.onnx")11config_path = hf_hub_download(repo_id="gyroing/Persian-Piper-Model-gyro", filename="fa_IR-gyro-medium.onnx.json")12voice = PiperVoice.load(model_path, config_path)13 14 15def synthesize_speech(text):16 17 18 # Create an in-memory buffer for the WAV file19 buffer = BytesIO()20 with wave.open(buffer, 'wb') as wav_file:21 wav_file.setframerate(voice.config.sample_rate)22 wav_file.setsampwidth(2) # 16-bit23 wav_file.setnchannels(1) # mono24 25 # Synthesize speech26 # eztext = preprocess_text(text)27 voice.synthesize(text, wav_file)28 29 # Convert buffer to NumPy array for Gradio output30 buffer.seek(0)31 audio_data = np.frombuffer(buffer.read(), dtype=np.int16)32 33 return audio_data.tobytes(), None34 35# Using Gradio Blocks36with gr.Blocks(theme=gr.themes.Base()) as blocks:37 gr.Markdown("# Persian Text to Speech Synthesizer")38 gr.Markdown("Enter text to synthesize it into speech using Piper With Persian gyro Model :")39 input_text = gr.Textbox(label=" ", rtl=True , text_align="right" )40 output_audio = gr.Audio(label="Synthesized Speech", type="numpy")41 output_text = gr.Textbox(label="Output Text", visible=False, rtl=True) # This is the new text output component42 submit_button = gr.Button("Synthesize")43 44 submit_button.click(synthesize_speech, inputs=input_text, outputs=[output_audio, output_text])45# Run the app46blocks.launch()