File size: 1,015 Bytes
b3bf7c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from fastapi import APIRouter, File, UploadFile, Form
from huggingface_hub import InferenceClient

router = APIRouter()

@router.post("/v1/audio/transcriptions", tags=["Automatic Speech Recognition"])
# Add model and audio parameters to the function signature
async def automatic_speech_recognition(
    model: str = Form(..., description="The model to use for ASR. Can be a model ID hosted on the Hugging Face Hub or a URL to a deployed Inference Endpoint. If not provided, the default recommended model for ASR will be used."),
    audio: UploadFile = File(..., description="The content to transcribe. It can be raw audio bytes, local audio file, or a URL to an audio file.")
):
    # Use the 'model' parameter from the form data
    client = InferenceClient(model=model)

    # Read the uploaded file content
    audio_bytes = await audio.read()

    # Pass the audio bytes to the client method
    res = client.automatic_speech_recognition(
        audio=audio_bytes
    )
    # Return the result
    return res