Spaces:
Running
Running
File size: 1,408 Bytes
e4eb5c5 944dedf 4492d6d 19be65d 4eb15f6 944dedf 19be65d 4236dbe 19be65d a3cf651 944dedf 4236dbe 944dedf 1782e10 4236dbe 1782e10 4236dbe f845b05 944dedf 1782e10 19be65d |
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 |
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':
yield "NSFW content detected. Cannot process."
return
model_path = hf_hub_download(repo_id="aigmixer/speaker_00", filename="speaker_00_model.onnx")
config_path = hf_hub_download(repo_id="aigmixer/speaker_00", filename="speaker_00_model.onnx.json")
voice = PiperVoice.load(model_path, config_path)
# Synthesize speech and stream audio
for audio_chunk in voice.synthesize(text, chunk_size=2048):
yield audio_chunk.tobytes()
# Using Gradio Blocks
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 PiperVoice.")
input_text = gr.Textbox(label="Input Text")
output_audio = gr.Audio(label="Synthesized Speech", type="numpy", streaming=True)
submit_button = gr.Button("Synthesize")
submit_button.click(synthesize_speech, inputs=input_text, outputs=output_audio)
# Run the app
blocks.launch()
|