File size: 1,139 Bytes
fba6023
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from __future__ import annotations

from fastapi import APIRouter, Request

from app.api.media import get_processor
from app.core.response import SuccessResponse

router = APIRouter(prefix="/v1/whisper", tags=["whisper"])


async def _transcribe(request: Request, default_format: str | None = None) -> SuccessResponse:
    request.state.operation = "whisper.transcribe"
    processor = get_processor(request)
    resolved = await processor.resolver.resolve(request)
    if default_format and "output_format" not in resolved.params:
        resolved.params["output_format"] = default_format
    return await processor.run_whisper(resolved)


@router.post("/transcribe", response_model=SuccessResponse)
async def transcribe(request: Request) -> SuccessResponse:
    return await _transcribe(request)


@router.post("/subtitles", response_model=SuccessResponse)
async def subtitles(request: Request) -> SuccessResponse:
    return await _transcribe(request, "srt")


@router.post("/detect-language", response_model=SuccessResponse)
async def detect_language(request: Request) -> SuccessResponse:
    return await _transcribe(request, "json")