narendraa commited on
Commit
2affccb
·
verified ·
1 Parent(s): dba71f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -18
app.py CHANGED
@@ -6,26 +6,46 @@ import torchaudio
6
  import numpy as np
7
  import io
8
 
 
 
 
 
 
 
9
  app = FastAPI()
10
 
11
- # Load model once at startup
12
- asr = pipeline("automatic-speech-recognition", model="openai/whisper-tiny")
13
 
14
  @app.post("/predict")
15
  async def predict(file: UploadFile = File(...)):
16
- try:
17
- # Read file bytes
18
- audio_bytes = await file.read()
19
- audio, sr = torchaudio.load(io.BytesIO(audio_bytes))
20
- # Convert to float32 if necessary
21
- if audio.dtype != torch.float32:
22
- audio = audio.to(torch.float32)
23
- # Convert to numpy array
24
- np_audio = audio.squeeze().numpy()
25
- # Run inference
26
- result = asr({"array": np_audio, "sampling_rate": sr})
27
- text = result["text"]
28
- return JSONResponse({"text": text})
29
- except Exception as e:
30
- print("🔥 Error during transcription:", e)
31
- return JSONResponse({"error": str(e)}, status_code=500)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)