File size: 867 Bytes
d222613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile
import whisper
import numpy as np
import io
import wave

app = FastAPI()

# Load Whisper model
model = whisper.load_model("base")  # Change to the model you want to use

@app.post("/transcribe/")
async def transcribe(file: UploadFile = File(...)):
    audio_data = await file.read()
    
    # Convert the uploaded file to numpy array
    with wave.open(io.BytesIO(audio_data), "rb") as wav_reader:
        samples = wav_reader.getnframes()
        audio = wav_reader.readframes(samples)
        audio_as_np_int16 = np.frombuffer(audio, dtype=np.int16)
        audio_as_np_float32 = audio_as_np_int16.astype(np.float32) / np.iinfo(np.int16).max
    
    # Transcribe the audio using the Whisper model
    result = model.transcribe(audio_as_np_float32)
    text = result['text'].strip()

    return {"transcription": text}