File size: 1,213 Bytes
9aaf513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
from io import BytesIO
import numpy as np
import httpx
import faster_whisper
from pydantic import BaseModel
from fastapi import (
    HTTPException,
    UploadFile,
)
from typing import Annotated, Any, BinaryIO, Literal, Generator, Union, Optional, List, Tuple


class AudioInfo(BaseModel):
    duration: float


async def read_audio(

    file: Optional[UploadFile] = None,

    file_url: Optional[str] = None

):
    """Read audio from "UploadFile". This resamples sampling rates to 16000."""
    if (file and file_url) or (not file and not file_url):
        raise HTTPException(status_code=400, detail="Provide only one of file or file_url")

    if file:
        file_content = await file.read()
    elif file_url:
        async with httpx.AsyncClient() as client:
            file_response = await client.get(file_url)
        if file_response.status_code != 200:
            raise HTTPException(status_code=422, detail="Could not download the file")
        file_content = file_response.content
    file_bytes = BytesIO(file_content)
    audio = faster_whisper.audio.decode_audio(file_bytes)
    duration = len(audio) / 16000
    return audio, AudioInfo(duration=duration)