File size: 2,208 Bytes
4e63be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e9ef73
 
4e63be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5300a9
99fb0c5
 
4e63be7
 
f618670
4e63be7
 
a5300a9
4e63be7
 
99fb0c5
 
 
 
 
 
 
 
 
 
4e63be7
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
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 
from transformers import pipeline

# Load the NSFW classifier model
nsfw_detector = pipeline("text-classification", model="michellejieli/NSFW_text_classifier")

def synthesize_speech(text):
    # Check for NSFW content
    nsfw_result = nsfw_detector(text)
    if nsfw_result[0]['label'] == 'NSFW':
        return "NSFW content detected. Cannot process.", None

    model_path = hf_hub_download(repo_id="DLI-SLQ/speaker_01234", filename="speaker__01234_model.onnx")
    config_path = hf_hub_download(repo_id="DLI-SLQ/speaker_01234", filename="speaker__01234_model.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(), None
    

# Gradio Interface
with gr.Blocks(theme=gr.themes.Base()) as blocks:
    gr.Markdown("# Text to Speech Synthesizer")
    gr.Markdown("Enter text to synthesize it into speech using models from the State Library of Queensland's collection using Piper.")
    input_text = gr.Textbox(label="Input Text")
    output_audio = gr.Audio(label="Synthesized Speech", type="numpy")
    output_text = gr.Textbox(label="Output Text", visible=True)  # Make this visible for error messages
    submit_button = gr.Button("Synthesize")

    def process_and_output(text):
        audio, message = synthesize_speech(text)
        if message:
            output_text.update(message)
            output_audio.update(None)
        else:
            output_audio.update(audio)
            output_text.update(None)

    submit_button.click(process_and_output, inputs=input_text, outputs=[output_audio, output_text])

# Run the app
blocks.launch()