Spaces:
Build error
Build error
| from fastapi import FastAPI, UploadFile, File, HTTPException | |
| from pydantic import BaseModel | |
| from pathlib import Path | |
| import tempfile | |
| import asyncio | |
| from .utils import download_video, extract_audio, trim_silence | |
| from .accent_classifier import get_classifier | |
| api = FastAPI() | |
| class URLBody(BaseModel): | |
| url: str | |
| async def analyze_url(body: URLBody): | |
| with tempfile.TemporaryDirectory() as td: | |
| tdir = Path(td) | |
| video = await download_video(body.url, tdir) | |
| wav = tdir / "aud.wav" | |
| await extract_audio(video, wav) | |
| wav = trim_silence(wav) | |
| return get_classifier().classify(str(wav)) | |
| async def analyze_upload(file: UploadFile = File(...)): | |
| if not file.content_type.startswith(("audio", "video")): | |
| raise HTTPException(400, "Unsupported file type") | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=file.filename) as tmp: | |
| tmp.write(await file.read()) | |
| tmp.flush() | |
| path = Path(tmp.name) | |
| if file.content_type.startswith("video"): | |
| wav = path.with_suffix(".wav") | |
| await extract_audio(path, wav) | |
| else: | |
| wav = path | |
| wav = trim_silence(wav) | |
| return get_classifier().classify(str(wav)) | |
| async def health(): | |
| return {"status": "ok"} |