File size: 1,670 Bytes
f845b05
944dedf
 
 
 
 
4eb15f6
944dedf
 
 
 
 
 
791253b
 
944dedf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
418b839
944dedf
 
 
 
 
 
 
f8bf4b8
f845b05
944dedf
21c111d
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
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  # Adjust import as per your project structure

#file_path = hf_hub_download("rhasspy/piper-voices", "en_GB-alan-medium.onnx")

def synthesize_speech(text):
    # Load the PiperVoice model and configuration
  #  model_path = "en_GB-alan-medium.onnx" # this is for loading local model
  #  config_path = "en_GB-alan-medium.onnx.json" # for loading local json
    model_path = hf_hub_download(repo_id="rhasspy/piper-voices", filename="en/en_GB/alan/medium/en_GB-alan-medium.onnx")
    config_path = hf_hub_download(repo_id="rhasspy/piper-voices", filename="en/en_GB/alan/medium/en_GB-alan-medium.onnx.json")
    voice = PiperVoice.load(model_path, config_path)

    # Create an in-memory buffer for the WAV file
    buffer = BytesIO()
    with wave.open(buffer, 'wb') as wav_file:
        wav_file.setframerate(voice.config.sample_rate)
        wav_file.setsampwidth(2)  # 16-bit
        wav_file.setnchannels(1)  # mono

        # Synthesize speech
        voice.synthesize(text, wav_file)

    # Convert buffer to NumPy array for Gradio output
    buffer.seek(0)
    audio_data = np.frombuffer(buffer.read(), dtype=np.int16)

    return audio_data.tobytes()

# Create a Gradio interface with labels
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"
    
)

# Run the app
iface.launch()