from fastapi import FastAPI, File, UploadFile
from fastapi.responses import RedirectResponse

from . import __version__
from .audio import load_audio
from .inference import transcribe

# TODO use pydantic config
SAMPLE_RATE = 16000


app = FastAPI(
    title="Local Whisper",
    description="Making OpenAPI's Open Whisper available via ReST API locally.",
    version=__version__,
    swagger_ui_parameters={"defaultModelsExpandDepth": -1},
    license_info={
        "name": "MIT License",
    },
)


@app.get("/", response_class=RedirectResponse, include_in_schema=False)
async def index():
    return "/docs"


@app.post("/audio/transcriptions")
async def asr(file: UploadFile = File(...)):
    if file.content_type.startswith("audio/"):
        transcription = transcribe(load_audio(file.file))
        return {"text": transcription["text"]}
