1
1
"""HTTP server for automated speech recognition (ASR)."""
2
-
3
- import argparse
4
2
import io
3
+ import logging
5
4
import wave
6
5
from pathlib import Path
7
6
8
- from flask import Flask , Response , jsonify , redirect , request
9
- from swagger_ui import flask_api_doc # pylint: disable=no-name-in-module
7
+ from flask import Response , jsonify , request
10
8
11
9
from wyoming .asr import Transcribe , Transcript
12
10
from wyoming .audio import wav_to_chunks
13
11
from wyoming .client import AsyncClient
14
12
from wyoming .error import Error
15
- from wyoming .info import Describe , Info
13
+
14
+ from .shared import get_app , get_argument_parser
16
15
17
16
_DIR = Path (__file__ ).parent
18
17
CONF_PATH = _DIR / "conf" / "asr.yaml"
19
18
20
19
21
20
def main ():
22
- parser = argparse .ArgumentParser ()
23
- parser .add_argument ("--host" , default = "0.0.0.0" )
24
- parser .add_argument ("--port" , type = int , default = 5000 )
25
- parser .add_argument ("--uri" , help = "URI of Wyoming ASR service" )
21
+ parser = get_argument_parser ()
26
22
parser .add_argument ("--model" , help = "Default model name for transcription" )
27
23
parser .add_argument ("--language" , help = "Default language for transcription" )
28
24
parser .add_argument ("--samples-per-chunk" , type = int , default = 1024 )
29
25
args = parser .parse_args ()
26
+ logging .basicConfig (level = logging .DEBUG if args .debug else logging .INFO )
30
27
31
- app = Flask ("asr" )
32
-
33
- @app .route ("/" )
34
- def redirect_to_api ():
35
- return redirect ("/api" )
28
+ app = get_app ("asr" , CONF_PATH , args )
36
29
37
30
@app .route ("/api/speech-to-text" , methods = ["POST" ])
38
31
async def api_stt () -> Response :
@@ -52,7 +45,7 @@ async def api_stt() -> Response:
52
45
with wave .open (wav_io , "rb" ) as wav_file :
53
46
chunks = wav_to_chunks (
54
47
wav_file ,
55
- samples_per_chunk = 1024 ,
48
+ samples_per_chunk = args . samples_per_chunk ,
56
49
start_event = True ,
57
50
stop_event = True ,
58
51
)
@@ -74,25 +67,6 @@ async def api_stt() -> Response:
74
67
f"Unexpected error from client: code={ error .code } , text={ error .text } "
75
68
)
76
69
77
- @app .route ("/api/info" , methods = ["GET" ])
78
- async def api_info ():
79
- uri = request .args .get ("uri" , args .uri )
80
- if not uri :
81
- raise ValueError ("URI is required" )
82
-
83
- async with AsyncClient .from_uri (uri ) as client :
84
- await client .write_event (Describe ().event ())
85
-
86
- while True :
87
- event = await client .read_event ()
88
- if event is None :
89
- raise RuntimeError ("Client disconnected" )
90
-
91
- if Info .is_type (event .type ):
92
- info = Info .from_event (event )
93
- return jsonify (info .to_dict ())
94
-
95
- flask_api_doc (app , config_path = str (CONF_PATH ), url_prefix = "/api" , title = "API doc" )
96
70
app .run (args .host , args .port )
97
71
98
72
0 commit comments