Add Python inference script
Browse files- inference.py +126 -0
inference.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Standalone inference script for Saudi MSA Piper TTS model
|
| 4 |
+
Works on any computer with the ONNX model files
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import argparse
|
| 8 |
+
import sys
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
from piper import PiperVoice
|
| 13 |
+
except ImportError:
|
| 14 |
+
print("Error: piper-tts not installed")
|
| 15 |
+
print("Install with: pip install piper-tts")
|
| 16 |
+
sys.exit(1)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def synthesize_text(text, model_path, output_path, config_path=None):
|
| 20 |
+
"""
|
| 21 |
+
Synthesize Arabic text to speech
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
text: Arabic text to synthesize
|
| 25 |
+
model_path: Path to ONNX model file
|
| 26 |
+
output_path: Path to save output WAV file
|
| 27 |
+
config_path: Optional path to config JSON file
|
| 28 |
+
"""
|
| 29 |
+
model_path = Path(model_path)
|
| 30 |
+
|
| 31 |
+
# Auto-detect config file if not provided
|
| 32 |
+
if config_path is None:
|
| 33 |
+
config_path = model_path.with_suffix('.onnx.json')
|
| 34 |
+
if not config_path.exists():
|
| 35 |
+
config_path = model_path.parent / 'config.json'
|
| 36 |
+
|
| 37 |
+
# Check if model exists
|
| 38 |
+
if not model_path.exists():
|
| 39 |
+
print(f"Error: Model file not found: {model_path}")
|
| 40 |
+
sys.exit(1)
|
| 41 |
+
|
| 42 |
+
# Load voice model
|
| 43 |
+
print(f"Loading model: {model_path}")
|
| 44 |
+
try:
|
| 45 |
+
voice = PiperVoice.load(str(model_path))
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Error loading model: {e}")
|
| 48 |
+
sys.exit(1)
|
| 49 |
+
|
| 50 |
+
# Synthesize
|
| 51 |
+
print(f"Synthesizing: {text}")
|
| 52 |
+
try:
|
| 53 |
+
with open(output_path, 'wb') as f:
|
| 54 |
+
voice.synthesize_stream_raw(text, f)
|
| 55 |
+
print(f"✓ Audio saved to: {output_path}")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Error during synthesis: {e}")
|
| 58 |
+
sys.exit(1)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def main():
|
| 62 |
+
parser = argparse.ArgumentParser(
|
| 63 |
+
description='Saudi MSA Piper TTS - Text to Speech Inference',
|
| 64 |
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
| 65 |
+
epilog="""
|
| 66 |
+
Examples:
|
| 67 |
+
# Synthesize from command line
|
| 68 |
+
python3 inference.py -t "مرحبا بك" -o output.wav
|
| 69 |
+
|
| 70 |
+
# Synthesize from stdin
|
| 71 |
+
echo "مرحبا بك" | python3 inference.py -o output.wav
|
| 72 |
+
|
| 73 |
+
# Specify custom model path
|
| 74 |
+
python3 inference.py -t "مرحبا بك" -m /path/to/model.onnx -o output.wav
|
| 75 |
+
|
| 76 |
+
# Read from file
|
| 77 |
+
cat text.txt | python3 inference.py -o output.wav
|
| 78 |
+
"""
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
parser.add_argument(
|
| 82 |
+
'-t', '--text',
|
| 83 |
+
type=str,
|
| 84 |
+
help='Arabic text to synthesize (if not provided, reads from stdin)'
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
parser.add_argument(
|
| 88 |
+
'-m', '--model',
|
| 89 |
+
type=str,
|
| 90 |
+
default='saudi_msa_epoch455.onnx',
|
| 91 |
+
help='Path to ONNX model file (default: saudi_msa_epoch455.onnx)'
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
parser.add_argument(
|
| 95 |
+
'-o', '--output',
|
| 96 |
+
type=str,
|
| 97 |
+
required=True,
|
| 98 |
+
help='Output WAV file path'
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
parser.add_argument(
|
| 102 |
+
'-c', '--config',
|
| 103 |
+
type=str,
|
| 104 |
+
help='Path to config JSON file (auto-detected if not provided)'
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
args = parser.parse_args()
|
| 108 |
+
|
| 109 |
+
# Get text from argument or stdin
|
| 110 |
+
if args.text:
|
| 111 |
+
text = args.text
|
| 112 |
+
else:
|
| 113 |
+
print("Reading text from stdin...")
|
| 114 |
+
text = sys.stdin.read().strip()
|
| 115 |
+
|
| 116 |
+
if not text:
|
| 117 |
+
print("Error: No text provided")
|
| 118 |
+
parser.print_help()
|
| 119 |
+
sys.exit(1)
|
| 120 |
+
|
| 121 |
+
# Synthesize
|
| 122 |
+
synthesize_text(text, args.model, args.output, args.config)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
if __name__ == '__main__':
|
| 126 |
+
main()
|