-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f54e98e
commit 1166e56
Showing
6 changed files
with
183 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.5.3 | ||
1.5.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
openapi: "3.0.0" | ||
info: | ||
title: 'Wyoming Wake' | ||
version: '1.0.0' | ||
description: 'API for Wake Word Detection' | ||
paths: | ||
/api/info: | ||
get: | ||
summary: 'Get service information' | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
/api/detect-wake-word: | ||
post: | ||
summary: 'Transcribe WAV data to text' | ||
requestBody: | ||
description: 'WAV data (16-bit 16Khz mono preferred)' | ||
required: true | ||
content: | ||
audio/wav: | ||
schema: | ||
type: string | ||
format: binary | ||
parameters: | ||
- in: query | ||
name: uri | ||
description: 'URI of Wyoming ASR service' | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: OK | ||
content: | ||
application/json: | ||
schema: | ||
type: object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Shared code for HTTP servers.""" | ||
import argparse | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
from flask import Flask, jsonify, redirect, request | ||
from swagger_ui import flask_api_doc # pylint: disable=no-name-in-module | ||
|
||
from wyoming.client import AsyncClient | ||
from wyoming.info import Describe, Info | ||
|
||
|
||
def get_argument_parser() -> argparse.ArgumentParser: | ||
"""Create argument parser with shared arguments.""" | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--host", default="0.0.0.0") | ||
parser.add_argument("--port", type=int, default=5000) | ||
parser.add_argument("--uri", help="URI of Wyoming service") | ||
parser.add_argument( | ||
"--debug", action="store_true", help="Print DEBUG logs to console" | ||
) | ||
return parser | ||
|
||
|
||
def get_app( | ||
name: str, openapi_config_path: Union[str, Path], args: argparse.Namespace | ||
) -> Flask: | ||
"""Create Flask app with default endpoints.""" | ||
|
||
app = Flask(name) | ||
|
||
@app.route("/") | ||
def redirect_to_api(): | ||
return redirect("/api") | ||
|
||
@app.route("/api/info", methods=["GET"]) | ||
async def api_info(): | ||
uri = request.args.get("uri", args.uri) | ||
if not uri: | ||
raise ValueError("URI is required") | ||
|
||
async with AsyncClient.from_uri(uri) as client: | ||
await client.write_event(Describe().event()) | ||
|
||
while True: | ||
event = await client.read_event() | ||
if event is None: | ||
raise RuntimeError("Client disconnected") | ||
|
||
if Info.is_type(event.type): | ||
info = Info.from_event(event) | ||
return jsonify(info.to_dict()) | ||
|
||
@app.errorhandler(Exception) | ||
async def handle_error(err): | ||
"""Return error as text.""" | ||
return (f"{err.__class__.__name__}: {err}", 500) | ||
|
||
flask_api_doc( | ||
app, config_path=str(openapi_config_path), url_prefix="/api", title="API doc" | ||
) | ||
|
||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""HTTP server for wake word detection.""" | ||
import io | ||
import logging | ||
import wave | ||
from pathlib import Path | ||
|
||
from flask import Response, jsonify, request | ||
|
||
from wyoming.audio import wav_to_chunks | ||
from wyoming.client import AsyncClient | ||
from wyoming.error import Error | ||
from wyoming.wake import Detection, NotDetected | ||
|
||
from .shared import get_app, get_argument_parser | ||
|
||
_DIR = Path(__file__).parent | ||
CONF_PATH = _DIR / "conf" / "wake.yaml" | ||
|
||
|
||
def main(): | ||
parser = get_argument_parser() | ||
parser.add_argument("--samples-per-chunk", type=int, default=1024) | ||
args = parser.parse_args() | ||
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO) | ||
|
||
app = get_app("wake", CONF_PATH, args) | ||
|
||
@app.route("/api/detect-wake-word", methods=["POST", "GET"]) | ||
async def api_wake() -> Response: | ||
uri = request.args.get("uri", args.uri) | ||
if not uri: | ||
raise ValueError("URI is required") | ||
|
||
async with AsyncClient.from_uri(uri) as client: | ||
with io.BytesIO(request.data) as wav_io: | ||
with wave.open(wav_io, "rb") as wav_file: | ||
chunks = wav_to_chunks( | ||
wav_file, | ||
samples_per_chunk=args.samples_per_chunk, | ||
start_event=True, | ||
stop_event=True, | ||
) | ||
for chunk in chunks: | ||
await client.write_event(chunk.event()) | ||
|
||
while True: | ||
event = await client.read_event() | ||
if event is None: | ||
raise RuntimeError("Client disconnected") | ||
|
||
if Detection.is_type(event.type) or NotDetected.is_type(event.type): | ||
return jsonify(event.to_dict()) | ||
|
||
if Error.is_type(event.type): | ||
error = Error.from_event(event) | ||
raise RuntimeError( | ||
f"Unexpected error from client: code={error.code}, text={error.text}" | ||
) | ||
|
||
app.run(args.host, args.port) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |