|
| 1 | +"""Module containing server endpoints and startup functionality""" |
| 2 | + |
1 | 3 | import json |
2 | 4 | from concurrent.futures import ThreadPoolExecutor |
3 | 5 | from pathlib import Path |
|
8 | 10 | from sanic.log import logger |
9 | 11 |
|
10 | 12 | from deepspeech_server.engine import SpeechToTextEngine |
11 | | -from deepspeech_server.models import Response, Error |
| 13 | +from deepspeech_server.models import Response, ErrorResponse |
12 | 14 |
|
13 | 15 | # Load app configs and initialize DeepSpeech model |
14 | 16 | conf = ConfigFactory.parse_file("application.conf") |
|
24 | 26 |
|
25 | 27 | @app.route("/", methods=["GET"]) |
26 | 28 | async def healthcheck(_): |
| 29 | + """Route for simple healthcheck that simply returns greeting message""" |
27 | 30 | return response.text("Welcome to DeepSpeech Server!") |
28 | 31 |
|
29 | 32 |
|
30 | | -@app.websocket("/api/v1/stt") |
31 | | -async def stt(request, ws): |
| 33 | +@app.websocket(conf['server.stt_endpoint']) |
| 34 | +async def stt(request, websocket): |
| 35 | + """Route for requesting speech-to-text transcription""" |
32 | 36 | logger.debug(f"Received {request.method} request at {request.path}") |
33 | 37 | try: |
34 | | - audio = await ws.recv() |
| 38 | + audio = await websocket.recv() |
35 | 39 |
|
36 | 40 | inference_start = perf_counter() |
37 | 41 | text = await app.loop.run_in_executor(executor, lambda: engine.run(audio)) |
38 | 42 | inference_end = perf_counter() - inference_start |
39 | 43 |
|
40 | | - await ws.send(json.dumps(Response(text, inference_end).__dict__)) |
| 44 | + await websocket.send(json.dumps(Response(text, inference_end).__dict__)) |
41 | 45 | logger.debug(f"Completed {request.method} request at {request.path} in {inference_end} seconds") |
42 | | - except Exception as e: # pylint: disable=broad-except |
43 | | - logger.debug(f"Failed to process {request.method} request at {request.path}. The exception is: {str(e)}.") |
44 | | - await ws.send(json.dumps(Error("Something went wrong").__dict__)) |
| 46 | + except Exception as ex: # pylint: disable=broad-except |
| 47 | + logger.debug(f"Failed to process {request.method} request at {request.path}. The exception is: {str(ex)}.") |
| 48 | + await websocket.send(json.dumps(ErrorResponse("Something went wrong").__dict__)) |
45 | 49 |
|
46 | | - await ws.close() |
| 50 | + await websocket.close() |
47 | 51 |
|
48 | 52 |
|
49 | 53 | if __name__ == "__main__": |
|
0 commit comments