This repository has been archived on 2025-09-28. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
transcript_api/transcriptapi/server.py

45 lines
1.2 KiB
Python
Raw Permalink Normal View History

2024-01-31 16:11:59 +01:00
# Copyright (c) 2024 Julian Müller (ChaoticByte)
from os import getpid as _getpid
from sanic import Sanic as _Sanic
from sanic import empty as _empty
from sanic import Request as _Request
from . import env as _env
from .msg import ComponentLogger as _ComponentLogger
from .stt import STT as _STT
def get_app() -> _Sanic:
app = _Sanic("TranscriptAPI")
@app.get("/ping")
async def ping(_):
return _empty(status=200)
@app.post('/')
async def transcribe(request: _Request):
audio = request.files.get("audio").body
if len(audio) < 1:
return _empty(400)
resp = await request.respond(content_type="text/plain")
for s in app.ctx.stt.transcribe(audio):
await resp.send(s)
await resp.eof()
@app.before_server_start
async def setup_stt(app):
app.ctx.stt = _STT(_env.API_STT_MODEL, n_threads=_env.API_STT_THREADS, logger=_ComponentLogger(f"{_getpid()}/STT"))
2024-01-31 16:11:59 +01:00
@app.after_server_start
async def init_stt(app):
app.ctx.stt.init()
@app.on_response
async def middleware(_, response):
response.headers["Access-Control-Allow-Origin"] = _env.ACCESS_CONTROL_ALLOW_ORIGIN
return app