Spaces:
Sleeping
Sleeping
Create piper_engine.py
Browse files- piper_engine.py +39 -0
piper_engine.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import tempfile
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def piper_tts(
|
| 6 |
+
text,
|
| 7 |
+
model_path,
|
| 8 |
+
config_path,
|
| 9 |
+
speed=1.0,
|
| 10 |
+
pitch=1.0,
|
| 11 |
+
):
|
| 12 |
+
|
| 13 |
+
# Output wav file
|
| 14 |
+
with tempfile.NamedTemporaryFile(
|
| 15 |
+
delete=False,
|
| 16 |
+
suffix=".wav"
|
| 17 |
+
) as f:
|
| 18 |
+
out_path = f.name
|
| 19 |
+
|
| 20 |
+
cmd = [
|
| 21 |
+
"piper",
|
| 22 |
+
"--model", model_path,
|
| 23 |
+
"--config", config_path,
|
| 24 |
+
"--output_file", out_path,
|
| 25 |
+
"--length_scale", str(1 / speed),
|
| 26 |
+
"--pitch_scale", str(pitch),
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
process = subprocess.Popen(
|
| 30 |
+
cmd,
|
| 31 |
+
stdin=subprocess.PIPE,
|
| 32 |
+
stdout=subprocess.PIPE,
|
| 33 |
+
stderr=subprocess.PIPE,
|
| 34 |
+
text=True,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
process.communicate(text)
|
| 38 |
+
|
| 39 |
+
return out_path
|