Update app.py
Browse files
app.py
CHANGED
|
@@ -6,26 +6,46 @@ import torchaudio
|
|
| 6 |
import numpy as np
|
| 7 |
import io
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
-
|
| 12 |
-
asr = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 13 |
|
| 14 |
@app.post("/predict")
|
| 15 |
async def predict(file: UploadFile = File(...)):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import io
|
| 8 |
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
import torch
|
| 11 |
+
import torchaudio
|
| 12 |
+
from transformers import pipeline
|
| 13 |
+
from fastapi import FastAPI, UploadFile, File
|
| 14 |
+
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
+
asr = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
|
|
|
| 18 |
|
| 19 |
@app.post("/predict")
|
| 20 |
async def predict(file: UploadFile = File(...)):
|
| 21 |
+
audio_path = "/tmp/input.wav"
|
| 22 |
+
with open(audio_path, "wb") as f:
|
| 23 |
+
f.write(await file.read())
|
| 24 |
+
|
| 25 |
+
# Load audio safely (no torchcodec)
|
| 26 |
+
waveform, sr = torchaudio.load(audio_path)
|
| 27 |
+
waveform = waveform.to(torch.float32) # force float
|
| 28 |
+
result = asr({"array": waveform[0].numpy(), "sampling_rate": sr})
|
| 29 |
+
return {"text": result["text"]}
|
| 30 |
+
|
| 31 |
+
# # Load model once at startup
|
| 32 |
+
# asr = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
|
| 33 |
+
|
| 34 |
+
# @app.post("/predict")
|
| 35 |
+
# async def predict(file: UploadFile = File(...)):
|
| 36 |
+
# try:
|
| 37 |
+
# # Read file bytes
|
| 38 |
+
# audio_bytes = await file.read()
|
| 39 |
+
# audio, sr = torchaudio.load(io.BytesIO(audio_bytes))
|
| 40 |
+
# # Convert to float32 if necessary
|
| 41 |
+
# if audio.dtype != torch.float32:
|
| 42 |
+
# audio = audio.to(torch.float32)
|
| 43 |
+
# # Convert to numpy array
|
| 44 |
+
# np_audio = audio.squeeze().numpy()
|
| 45 |
+
# # Run inference
|
| 46 |
+
# result = asr({"array": np_audio, "sampling_rate": sr})
|
| 47 |
+
# text = result["text"]
|
| 48 |
+
# return JSONResponse({"text": text})
|
| 49 |
+
# except Exception as e:
|
| 50 |
+
# print("🔥 Error during transcription:", e)
|
| 51 |
+
# return JSONResponse({"error": str(e)}, status_code=500)
|