Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
import whisper
|
| 3 |
+
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
import wave
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Load Whisper model
|
| 10 |
+
model = whisper.load_model("base") # Change to the model you want to use
|
| 11 |
+
|
| 12 |
+
@app.post("/transcribe/")
|
| 13 |
+
async def transcribe(file: UploadFile = File(...)):
|
| 14 |
+
audio_data = await file.read()
|
| 15 |
+
|
| 16 |
+
# Convert the uploaded file to numpy array
|
| 17 |
+
with wave.open(io.BytesIO(audio_data), "rb") as wav_reader:
|
| 18 |
+
samples = wav_reader.getnframes()
|
| 19 |
+
audio = wav_reader.readframes(samples)
|
| 20 |
+
audio_as_np_int16 = np.frombuffer(audio, dtype=np.int16)
|
| 21 |
+
audio_as_np_float32 = audio_as_np_int16.astype(np.float32) / np.iinfo(np.int16).max
|
| 22 |
+
|
| 23 |
+
# Transcribe the audio using the Whisper model
|
| 24 |
+
result = model.transcribe(audio_as_np_float32)
|
| 25 |
+
text = result['text'].strip()
|
| 26 |
+
|
| 27 |
+
return {"transcription": text}
|