|
import gradio as gr |
|
import wave |
|
import numpy as np |
|
from io import BytesIO |
|
from huggingface_hub import hf_hub_download |
|
from piper import PiperVoice |
|
|
|
|
|
|
|
def synthesize_speech(text): |
|
|
|
|
|
|
|
model_path = hf_hub_download(repo_id="rhasspy/piper-voices", filename="en_GB-alan-medium.onnx") |
|
config_path = hf_hub_download(repo_id="rhasspy/piper-voices", filename="en_GB-alan-medium.onnx.json") |
|
voice = PiperVoice.load(model_path, config_path) |
|
|
|
|
|
buffer = BytesIO() |
|
with wave.open(buffer, 'wb') as wav_file: |
|
wav_file.setframerate(voice.config.sample_rate) |
|
wav_file.setsampwidth(2) |
|
wav_file.setnchannels(1) |
|
|
|
|
|
voice.synthesize(text, wav_file) |
|
|
|
|
|
buffer.seek(0) |
|
audio_data = np.frombuffer(buffer.read(), dtype=np.int16) |
|
|
|
return audio_data.tobytes() |
|
|
|
|
|
iface = gr.Interface( |
|
fn=synthesize_speech, |
|
inputs=gr.Textbox(label="Input Text"), |
|
outputs=[gr.Audio(label="Synthesized Speech")], |
|
title="Text to Speech Synthesizer", |
|
description="Enter text to synthesize it into speech using PiperVoice.", |
|
allow_flagging="never" |
|
|
|
) |
|
|
|
|
|
iface.launch() |
|
|