-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ultrasev/concurrent-recording
fix(src.local_deploy): support concurrent recording
- Loading branch information
Showing
4 changed files
with
210 additions
and
28 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Build and Publish Docker image | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
build-and-push: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Log in to GitHub Container Registry | ||
uses: docker/login-action@v1 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.CR_PAT }} | ||
|
||
- name: Build and push Docker image | ||
uses: docker/build-push-action@v2 | ||
with: | ||
context: . | ||
file: ./Dockerfile | ||
push: true | ||
tags: ghcr.io/${{ github.repository_owner }}/whisper:latest |
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,11 @@ | ||
FROM python:3.8-slim | ||
WORKDIR /app/ | ||
COPY requirements.txt /app/ | ||
|
||
RUN apt update && apt install -y libpq-dev gcc portaudio19-dev | ||
RUN pip3 install -r requirements.txt | ||
RUN pip3 install uvicorn fastapi pydantic python-multipart loguru==0.7.0 | ||
|
||
COPY ./src/docker/whisper.py /app/ | ||
|
||
CMD ["uvicorn", "whisper:app", "--host", "0.0.0.0", "--port", "8000"] |
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,103 @@ | ||
#!/usr/bin/env python | ||
import asyncio | ||
import os | ||
import typing | ||
from concurrent.futures import ThreadPoolExecutor | ||
from io import BytesIO | ||
|
||
import av | ||
from fastapi import FastAPI, File, HTTPException, UploadFile | ||
from faster_whisper import WhisperModel | ||
from loguru import logger | ||
from starlette.middleware.base import BaseHTTPMiddleware | ||
from starlette.requests import Request | ||
from starlette.responses import JSONResponse | ||
|
||
model_size = os.getenv('MODEL', 'base') | ||
|
||
|
||
class ValidateFileTypeMiddleware(BaseHTTPMiddleware): | ||
async def dispatch(self, request: Request, call_next): | ||
if request.method.lower() == "post": | ||
try: | ||
response = await call_next(request) | ||
return response | ||
except av.error.InvalidDataError: | ||
return JSONResponse(status_code=400, | ||
content={"message": "Invalid file type"}) | ||
except Exception as e: | ||
return JSONResponse(status_code=500, | ||
content={"message": str(e)}) | ||
|
||
|
||
app = FastAPI() | ||
app.add_middleware(ValidateFileTypeMiddleware) | ||
|
||
|
||
async def asyncformer(sync_func: typing.Callable, *args, **kwargs): | ||
loop = asyncio.get_event_loop() | ||
with ThreadPoolExecutor() as pool: | ||
return await loop.run_in_executor(pool, sync_func, *args, **kwargs) | ||
|
||
|
||
class Transcriber: | ||
_instance = None | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if cls._instance is None: | ||
cls._instance = super(Transcriber, cls).__new__(cls) | ||
# Put any initialization here. | ||
return cls._instance | ||
|
||
def __init__( | ||
self, | ||
model_size: str, | ||
device: str = "auto", | ||
compute_type: str = "default", | ||
prompt: str = '实时/低延迟语音转写服务,林黛玉、倒拔、杨柳树、鲁迅、周树人、关键词、转写正确') -> None: | ||
""" FasterWhisper 语音转写 | ||
Args: | ||
model_size (str): 模型大小,可选项为 "tiny", "base", "small", "medium", "large" 。 | ||
更多信息参考:https://github.com/openai/whisper | ||
device (str, optional): 模型运行设备。 | ||
compute_type (str, optional): 计算类型。默认为"default"。 | ||
prompt (str, optional): 初始提示。如果需要转写简体中文,可以使用简体中文提示。 | ||
""" | ||
super().__init__() | ||
self.model_size = model_size | ||
self.device = device | ||
self.compute_type = compute_type | ||
self.prompt = prompt | ||
|
||
def __enter__(self) -> 'Transcriber': | ||
self._model = WhisperModel(self.model_size, | ||
device=self.device, | ||
compute_type=self.compute_type) | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback) -> None: | ||
pass | ||
|
||
async def __call__(self, audio: bytes) -> typing.AsyncGenerator[str, None]: | ||
def _process(): | ||
return self._model.transcribe(BytesIO(audio), | ||
initial_prompt=self.prompt, | ||
vad_filter=True) | ||
|
||
segments, info = await asyncformer(_process) | ||
for segment in segments: | ||
t = segment.text | ||
if self.prompt in t.strip(): | ||
continue | ||
if t.strip().replace('.', ''): | ||
logger.info(t) | ||
yield t | ||
|
||
|
||
@app.post("/v1/audio/transcriptions") | ||
async def _transcribe(file: UploadFile = File(...)): | ||
with Transcriber(model_size) as stt: | ||
audio = await file.read() | ||
text = ','.join([seg async for seg in stt(audio)]) | ||
return {"text": text} |
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