Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
try:
|
| 8 |
-
# Running the command and capturing the output
|
| 9 |
-
result = subprocess.run(command, shell=True, text=True, capture_output=True, check=True)
|
| 10 |
-
return result.stdout
|
| 11 |
-
except subprocess.CalledProcessError as e:
|
| 12 |
-
return e.stderr
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
iface = gr.Interface(
|
| 15 |
-
fn=
|
| 16 |
-
inputs=
|
| 17 |
-
outputs=
|
| 18 |
-
title="
|
| 19 |
-
description="Enter
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
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 # Adjust import as per your project structure
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
#file_path = hf_hub_download("rhasspy/piper-voices", "en_GB-alan-medium.onnx")
|
| 9 |
+
|
| 10 |
+
def synthesize_speech(text):
|
| 11 |
+
# Load the PiperVoice model and configuration
|
| 12 |
+
# model_path = "en_GB-alan-medium.onnx" # this is for loading local model
|
| 13 |
+
# config_path = "en_GB-alan-medium.onnx.json" # for loading local json
|
| 14 |
+
model_path = hf_hub_download(repo_id="rhasspy/piper-voices", filename="en_GB-alan-medium.onnx")
|
| 15 |
+
config_path = hf_hub_download(repo_id="rhasspy/piper-voices", filename="en_GB-alan-medium.onnx.json")
|
| 16 |
+
voice = PiperVoice.load(model_path, config_path)
|
| 17 |
+
|
| 18 |
+
# Create an in-memory buffer for the WAV file
|
| 19 |
+
buffer = BytesIO()
|
| 20 |
+
with wave.open(buffer, 'wb') as wav_file:
|
| 21 |
+
wav_file.setframerate(voice.config.sample_rate)
|
| 22 |
+
wav_file.setsampwidth(2) # 16-bit
|
| 23 |
+
wav_file.setnchannels(1) # mono
|
| 24 |
+
|
| 25 |
+
# Synthesize speech
|
| 26 |
+
voice.synthesize(text, wav_file)
|
| 27 |
+
|
| 28 |
+
# Convert buffer to NumPy array for Gradio output
|
| 29 |
+
buffer.seek(0)
|
| 30 |
+
audio_data = np.frombuffer(buffer.read(), dtype=np.int16)
|
| 31 |
+
|
| 32 |
+
return audio_data.tobytes()
|
| 33 |
+
|
| 34 |
+
# Create a Gradio interface with labels
|
| 35 |
iface = gr.Interface(
|
| 36 |
+
fn=synthesize_speech,
|
| 37 |
+
inputs=gr.Textbox(label="Input Text"),
|
| 38 |
+
outputs=[gr.Audio(label="Synthesized Speech")],
|
| 39 |
+
title="Text to Speech Synthesizer",
|
| 40 |
+
description="Enter text to synthesize it into speech using PiperVoice.",
|
| 41 |
+
allow_flagging="never"
|
| 42 |
+
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Run the app
|
| 46 |
iface.launch()
|