Add local Whisper speech recognition
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env python3
|
||||
"""End-to-end smoke test for Athena's OpenAI-compatible TTS and STT APIs."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
|
||||
BASE_URL = os.environ.get("ROUTER_URL", "http://127.0.0.1:8081/v1").rstrip("/")
|
||||
API_KEY = os.environ.get("ROUTER_API_KEY", "")
|
||||
TEST_TEXT = "Dies ist ein lokaler Test der Spracherkennung auf Athena."
|
||||
|
||||
|
||||
def request(path: str, data: bytes, content_type: str) -> bytes:
|
||||
req = urllib.request.Request(
|
||||
f"{BASE_URL}{path}",
|
||||
data=data,
|
||||
headers={
|
||||
"Authorization": f"Bearer {API_KEY}",
|
||||
"Content-Type": content_type,
|
||||
},
|
||||
method="POST",
|
||||
)
|
||||
with urllib.request.urlopen(req, timeout=360) as response:
|
||||
return response.read()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
if not API_KEY:
|
||||
print("ROUTER_API_KEY is required", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
speech = request(
|
||||
"/audio/speech",
|
||||
json.dumps({
|
||||
"model": "piper",
|
||||
"voice": "alloy",
|
||||
"response_format": "wav",
|
||||
"input": TEST_TEXT,
|
||||
}).encode(),
|
||||
"application/json",
|
||||
)
|
||||
|
||||
boundary = f"speech-{uuid.uuid4().hex}"
|
||||
body = (
|
||||
f"--{boundary}\r\n"
|
||||
'Content-Disposition: form-data; name="model"\r\n\r\n'
|
||||
"whisper-1\r\n"
|
||||
f"--{boundary}\r\n"
|
||||
'Content-Disposition: form-data; name="language"\r\n\r\n'
|
||||
"de\r\n"
|
||||
f"--{boundary}\r\n"
|
||||
'Content-Disposition: form-data; name="file"; filename="test.wav"\r\n'
|
||||
"Content-Type: audio/wav\r\n\r\n"
|
||||
).encode() + speech + f"\r\n--{boundary}--\r\n".encode()
|
||||
|
||||
result = json.loads(request(
|
||||
"/audio/transcriptions",
|
||||
body,
|
||||
f"multipart/form-data; boundary={boundary}",
|
||||
))
|
||||
transcript = result.get("text", "").strip()
|
||||
print(transcript)
|
||||
return 0 if transcript else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user