76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
import os
|
|
import tempfile
|
|
import subprocess
|
|
import json
|
|
from flask import Flask, request, jsonify
|
|
from werkzeug.utils import secure_filename
|
|
|
|
|
|
# Test that the kokoro-tts system works correctly
|
|
def test_kokoro_tts_integration():
|
|
"""Test that kokoro-tts can be called from Python"""
|
|
|
|
# Create a simple Flask app for testing
|
|
app = Flask(__name__)
|
|
|
|
OUTPUT_DIR = "output"
|
|
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
|
|
|
VOICES = ["bm_fable", "bm_lewis", "bm_george"]
|
|
|
|
def generate_wav_file(text, title, voice):
|
|
"""Generate WAV file using kokoro-tts"""
|
|
filename = f"{secure_filename(title)}.wav"
|
|
filepath = os.path.join(OUTPUT_DIR, filename)
|
|
|
|
# Create temporary input file with the text
|
|
with tempfile.NamedTemporaryFile(
|
|
mode="w", suffix=".txt", delete=False
|
|
) as temp_file:
|
|
temp_file.write(text)
|
|
temp_input_path = temp_file.name
|
|
|
|
try:
|
|
# Run the kokoro-tts command
|
|
cmd = ["kokoro-tts", temp_input_path, filepath, "--voice", voice]
|
|
result = subprocess.run(cmd, check=True, capture_output=True, text=True)
|
|
|
|
# Clean up temporary file
|
|
os.unlink(temp_input_path)
|
|
|
|
return filepath
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
try:
|
|
os.unlink(temp_input_path)
|
|
except:
|
|
pass
|
|
return None
|
|
|
|
# Test the function directly
|
|
test_text = "This is a test of the kokoro text-to-speech system."
|
|
test_title = "test_output"
|
|
|
|
# Pin a deterministic voice for reproducible testing
|
|
voice = VOICES[0]
|
|
|
|
print(f"Using voice: {voice}")
|
|
|
|
# Generate the WAV file
|
|
filepath = generate_wav_file(test_text, test_title, voice)
|
|
|
|
if filepath and os.path.exists(filepath):
|
|
print(f"SUCCESS: WAV file created at {filepath}")
|
|
return True
|
|
else:
|
|
print("FAILED: Could not create WAV file")
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
success = test_kokoro_tts_integration()
|
|
if success:
|
|
print("Integration test passed!")
|
|
else:
|
|
print("Integration test failed!")
|