83 lines
3.7 KiB
Python
83 lines
3.7 KiB
Python
"""
|
|
Proxy router — transparently forwards existing Whisper server endpoints.
|
|
NEVER modify these routes or their behavior.
|
|
"""
|
|
import os
|
|
import logging
|
|
from fastapi import APIRouter, Request, UploadFile, File, Form
|
|
from fastapi.responses import Response
|
|
import httpx
|
|
|
|
logger = logging.getLogger(__name__)
|
|
WHISPER_ORIGIN = os.getenv("WHISPER_ORIGIN", "http://10.100.16.13:5003")
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
async def _proxy_get(path: str) -> Response:
|
|
"""Forward a GET request to the original Whisper server."""
|
|
async with httpx.AsyncClient(timeout=10.0) as client:
|
|
resp = await client.get(f"{WHISPER_ORIGIN}{path}")
|
|
return Response(
|
|
content=resp.content,
|
|
status_code=resp.status_code,
|
|
media_type=resp.headers.get("content-type", "application/json"),
|
|
)
|
|
|
|
|
|
async def _proxy_upload(path: str, file_bytes: bytes, filename: str, extra_fields: dict) -> Response:
|
|
"""Forward a multipart file upload to the original Whisper server."""
|
|
async with httpx.AsyncClient(timeout=600.0) as client:
|
|
files = {"file": (filename, file_bytes, "application/octet-stream")}
|
|
data = extra_fields
|
|
resp = await client.post(f"{WHISPER_ORIGIN}{path}", files=files, data=data)
|
|
return Response(
|
|
content=resp.content,
|
|
status_code=resp.status_code,
|
|
media_type=resp.headers.get("content-type", "application/json"),
|
|
)
|
|
|
|
|
|
# ── Proxy: GET /health ───────────────────────────────────────────────────────
|
|
@router.get("/health", summary="Health — proxied from original Whisper server")
|
|
async def health():
|
|
return await _proxy_get("/health")
|
|
|
|
|
|
# ── Proxy: POST /transcribe ──────────────────────────────────────────────────
|
|
@router.post("/transcribe", summary="Transcribe — proxied from original Whisper server")
|
|
async def transcribe(
|
|
file: UploadFile = File(...),
|
|
language: str = Form("pt"),
|
|
):
|
|
file_bytes = await file.read()
|
|
return await _proxy_upload("/transcribe", file_bytes, file.filename or "audio.wav", {"language": language})
|
|
|
|
|
|
# ── Proxy: POST /transcribe-segments ────────────────────────────────────────
|
|
@router.post("/transcribe-segments", summary="Transcribe Segments — proxied from original Whisper server")
|
|
async def transcribe_segments(
|
|
file: UploadFile = File(...),
|
|
language: str = Form("pt"),
|
|
):
|
|
file_bytes = await file.read()
|
|
return await _proxy_upload("/transcribe-segments", file_bytes, file.filename or "audio.wav", {"language": language})
|
|
|
|
|
|
# ── Proxy: POST /transcribe-text ────────────────────────────────────────────
|
|
@router.post("/transcribe-text", summary="Transcribe Text — proxied from original Whisper server")
|
|
async def transcribe_text(
|
|
file: UploadFile = File(...),
|
|
):
|
|
file_bytes = await file.read()
|
|
return await _proxy_upload("/transcribe-text", file_bytes, file.filename or "audio.wav", {})
|
|
|
|
|
|
# ── Proxy: POST /whisper ─────────────────────────────────────────────────────
|
|
@router.post("/whisper", summary="Whisper Compat — proxied from original Whisper server")
|
|
async def whisper_compat(
|
|
file: UploadFile = File(...),
|
|
):
|
|
file_bytes = await file.read()
|
|
return await _proxy_upload("/whisper", file_bytes, file.filename or "audio.wav", {})
|