DLI-SLQ commited on
Commit
c4a9b80
·
1 Parent(s): 1672a38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -43
app.py CHANGED
@@ -3,86 +3,64 @@ import wave
3
  import numpy as np
4
  from io import BytesIO
5
  from huggingface_hub import hf_hub_download
6
- from piper import PiperVoice
7
  from transformers import pipeline
8
 
9
- # Import necessary libraries:
10
- # gradio for creating the web interface,
11
- # wave for handling WAV audio format,
12
- # numpy for numerical operations,
13
- # BytesIO for in-memory byte handling,
14
- # huggingface_hub for downloading models from the Hugging Face Hub,
15
- # PiperVoice for the text-to-speech functionality,
16
- # pipeline from transformers for the NSFW classifier.
17
-
18
- # Load the NSFW classifier model using Hugging Face's pipeline
19
  nsfw_detector = pipeline("text-classification", model="michellejieli/NSFW_text_classifier")
20
 
21
  def synthesize_speech(text):
22
  # Check for NSFW content using the classifier
23
  nsfw_result = nsfw_detector(text)
24
- # Extract the label and score from the result
25
  label = nsfw_result[0]['label']
26
  score = nsfw_result[0]['score']
27
 
28
- # First, check if the input text contains NSFW content.
29
- #nsfw_result = nsfw_detector(text)
30
  if label == 'NSFW' and score >= 0.95:
31
- # Download and read the error audio file
32
  error_audio_path = hf_hub_download(repo_id="DLI-SLQ/speaker_01234", filename="error_audio.wav")
33
- with open(error_audio_path, 'rb') as error_audio_file:
34
- error_audio = error_audio_file.read()
35
- # Return the error audio and a warning message
36
- return error_audio, "NSFW content detected. Cannot process."
37
 
38
- # If the content is safe, proceed with speech synthesis.
39
- # Download the model and configuration from Hugging Face Hub.
 
 
 
 
 
 
 
 
 
40
  model_path = hf_hub_download(repo_id="DLI-SLQ/speaker_01234", filename="speaker__01234_model.onnx")
41
  config_path = hf_hub_download(repo_id="DLI-SLQ/speaker_01234", filename="speaker__1234_model.onnx.json")
42
-
43
- # Load the PiperVoice model for speech synthesis.
44
  voice = PiperVoice.load(model_path, config_path)
45
-
46
- # Create a BytesIO buffer to hold the synthesized WAV file in memory.
47
  buffer = BytesIO()
48
  with wave.open(buffer, 'wb') as wav_file:
49
- # Set WAV file properties: sample rate, bit depth, and mono channel.
50
  wav_file.setframerate(voice.config.sample_rate)
51
- wav_file.setsampwidth(2) # 16-bit
52
- wav_file.setnchannels(1) # mono
53
-
54
- # Use the PiperVoice model to synthesize speech from the text.
55
  voice.synthesize(text, wav_file)
56
 
57
- # Convert the buffer content to a NumPy array, then to bytes for Gradio output.
58
  buffer.seek(0)
59
  audio_data = np.frombuffer(buffer.read(), dtype=np.int16)
60
  return audio_data.tobytes(), None
61
 
62
- # Set up the Gradio interface.
63
  with gr.Blocks(theme=gr.themes.Base()) as blocks:
64
- # Create a user-friendly markdown title and description.
65
  gr.Markdown("# Text to Speech Synthesizer")
66
  gr.Markdown("Enter text to synthesize it into speech using models from the State Library of Queensland's collection using Piper.")
67
-
68
- # Define Gradio interface components: input textbox, audio output, and output textbox.
69
  input_text = gr.Textbox(label="Input Text")
70
  output_audio = gr.Audio(label="Synthesized Speech", type="numpy")
71
- output_text = gr.Textbox(label="Output Text", visible=True) # Visible for error messages
 
72
 
73
- # Define a function to process the input text and produce outputs.
74
  def process_and_output(text):
75
  audio, message = synthesize_speech(text)
76
  if message:
77
- # If there's a message (e.g., an error message), return None for audio and the message.
78
- return None, message
79
  else:
80
- # Otherwise, return the synthesized audio and None for the message.
81
  return audio, None
82
 
83
- # Link the processing function to the Gradio interface button.
84
- submit_button = gr.Button("Synthesize")
85
  submit_button.click(process_and_output, inputs=input_text, outputs=[output_audio, output_text])
86
 
87
- # Launch the Gradio web application.
88
  blocks.launch()
 
3
  import numpy as np
4
  from io import BytesIO
5
  from huggingface_hub import hf_hub_download
6
+ from piper import PiperVoice
7
  from transformers import pipeline
8
 
9
+ # Load the NSFW classifier model
 
 
 
 
 
 
 
 
 
10
  nsfw_detector = pipeline("text-classification", model="michellejieli/NSFW_text_classifier")
11
 
12
  def synthesize_speech(text):
13
  # Check for NSFW content using the classifier
14
  nsfw_result = nsfw_detector(text)
 
15
  label = nsfw_result[0]['label']
16
  score = nsfw_result[0]['score']
17
 
 
 
18
  if label == 'NSFW' and score >= 0.95:
 
19
  error_audio_path = hf_hub_download(repo_id="DLI-SLQ/speaker_01234", filename="error_audio.wav")
 
 
 
 
20
 
21
+ # Read the error audio file
22
+ try:
23
+ with wave.open(error_audio_path, 'rb') as error_audio_file:
24
+ frames = error_audio_file.readframes(error_audio_file.getnframes())
25
+ error_audio_data = np.frombuffer(frames, dtype=np.int16).tobytes()
26
+ except Exception as e:
27
+ print(f"Error reading audio file: {e}")
28
+ return None, "Error in processing audio file."
29
+
30
+ return error_audio_data, "NSFW content detected. Cannot process."
31
+
32
  model_path = hf_hub_download(repo_id="DLI-SLQ/speaker_01234", filename="speaker__01234_model.onnx")
33
  config_path = hf_hub_download(repo_id="DLI-SLQ/speaker_01234", filename="speaker__1234_model.onnx.json")
34
+
 
35
  voice = PiperVoice.load(model_path, config_path)
36
+
 
37
  buffer = BytesIO()
38
  with wave.open(buffer, 'wb') as wav_file:
 
39
  wav_file.setframerate(voice.config.sample_rate)
40
+ wav_file.setsampwidth(2)
41
+ wav_file.setnchannels(1)
 
 
42
  voice.synthesize(text, wav_file)
43
 
 
44
  buffer.seek(0)
45
  audio_data = np.frombuffer(buffer.read(), dtype=np.int16)
46
  return audio_data.tobytes(), None
47
 
48
+ # Gradio Interface
49
  with gr.Blocks(theme=gr.themes.Base()) as blocks:
 
50
  gr.Markdown("# Text to Speech Synthesizer")
51
  gr.Markdown("Enter text to synthesize it into speech using models from the State Library of Queensland's collection using Piper.")
 
 
52
  input_text = gr.Textbox(label="Input Text")
53
  output_audio = gr.Audio(label="Synthesized Speech", type="numpy")
54
+ output_text = gr.Textbox(label="Output Text", visible=True)
55
+ submit_button = gr.Button("Synthesize")
56
 
 
57
  def process_and_output(text):
58
  audio, message = synthesize_speech(text)
59
  if message:
60
+ return audio, message
 
61
  else:
 
62
  return audio, None
63
 
 
 
64
  submit_button.click(process_and_output, inputs=input_text, outputs=[output_audio, output_text])
65
 
 
66
  blocks.launch()