Spaces:
Sleeping
Sleeping
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 | |
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} |