From a6169fb6f990ec1648e6e47d73e9e165ac58a898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=96ner?= Date: Sun, 6 Oct 2024 18:23:50 +0200 Subject: [PATCH] Improve application start method --- Dockerfile | 2 +- Dockerfile.gpu | 2 +- app/webservice.py | 33 ++++++++++++++++++++++++++++++--- docs/build.md | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index e9d5c4c..bdae06f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -63,4 +63,4 @@ RUN poetry install EXPOSE 9000 -ENTRYPOINT ["gunicorn", "--bind", "0.0.0.0:9000", "--workers", "1", "--timeout", "0", "app.webservice:app", "-k", "uvicorn.workers.UvicornWorker"] +ENTRYPOINT ["whisper-asr-webservice"] diff --git a/Dockerfile.gpu b/Dockerfile.gpu index 766f17f..b605707 100644 --- a/Dockerfile.gpu +++ b/Dockerfile.gpu @@ -81,4 +81,4 @@ RUN $POETRY_VENV/bin/pip install torch==1.13.1+cu117 -f https://download.pytorch EXPOSE 9000 -CMD gunicorn --bind 0.0.0.0:9000 --workers 1 --timeout 0 app.webservice:app -k uvicorn.workers.UvicornWorker +CMD whisper-asr-webservice diff --git a/app/webservice.py b/app/webservice.py index 6042f11..5391afe 100644 --- a/app/webservice.py +++ b/app/webservice.py @@ -1,11 +1,13 @@ import importlib.metadata import os from os import path -from typing import Annotated, BinaryIO, Union +from typing import Annotated, BinaryIO, Optional, Union from urllib.parse import quote +import click import ffmpeg import numpy as np +import uvicorn from fastapi import FastAPI, File, Query, UploadFile, applications from fastapi.openapi.docs import get_swagger_ui_html from fastapi.responses import RedirectResponse, StreamingResponse @@ -14,9 +16,9 @@ ASR_ENGINE = os.getenv("ASR_ENGINE", "openai_whisper") if ASR_ENGINE == "faster_whisper": - from .faster_whisper.core import language_detection, transcribe + from app.faster_whisper.core import language_detection, transcribe else: - from .openai_whisper.core import language_detection, transcribe + from app.openai_whisper.core import language_detection, transcribe SAMPLE_RATE = 16000 LANGUAGE_CODES = sorted(tokenizer.LANGUAGES.keys()) @@ -122,3 +124,28 @@ def load_audio(file: BinaryIO, encode=True, sr: int = SAMPLE_RATE): out = file.read() return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0 + +@click.command() +@click.option( + "-h", + "--host", + metavar="HOST", + default="0.0.0.0", + help="Host for the webservice (default: 0.0.0.0)", +) +@click.option( + "-p", + "--port", + metavar="PORT", + default=9000, + help="Port for the webservice (default: 9000)", +) +@click.version_option(version=projectMetadata["Version"]) +def start( + host: str, + port: Optional[int] = None +): + uvicorn.run(app, host=host, port=port) + +if __name__ == "__main__": + start() \ No newline at end of file diff --git a/docs/build.md b/docs/build.md index a7352eb..96fa96c 100644 --- a/docs/build.md +++ b/docs/build.md @@ -24,7 +24,7 @@ poetry install Starting the Webservice: ```sh -poetry run gunicorn --bind 0.0.0.0:9000 --workers 1 --timeout 0 app.webservice:app -k uvicorn.workers.UvicornWorker +poetry run whisper-asr-webservice --host 0.0.0.0 --port 9000 ``` ### Build