MalikIbrar commited on
Commit
9bdf0c3
1 Parent(s): d604b45
Files changed (1) hide show
  1. main.py +4 -22
main.py CHANGED
@@ -2,10 +2,7 @@ from fastapi import FastAPI, File, UploadFile, HTTPException
2
  from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3
  import torch
4
  import uvicorn
5
- import soundfile as sf
6
  from fastapi.middleware.cors import CORSMiddleware
7
- import os
8
- import tempfile
9
 
10
  # Initialize FastAPI
11
  app = FastAPI()
@@ -46,31 +43,16 @@ pipe = pipeline(
46
  @app.post("/transcribe")
47
  async def transcribe_audio(file: UploadFile = File(...)):
48
  try:
49
- # Create a temporary file to save the uploaded content
50
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
51
- temp_file.write(await file.read())
52
- temp_path = temp_file.name
53
 
54
- # Load the audio file using soundfile
55
- audio, sr = sf.read(temp_path)
56
-
57
- # Ensure the sample rate is 16000 Hz
58
- if sr != 16000:
59
- raise HTTPException(status_code=400, detail="Sample rate must be 16000 Hz.")
60
-
61
- # Pass the processed audio to the pipeline
62
- result = pipe(temp_path)
63
-
64
- # Remove the temp file after processing
65
- os.remove(temp_path)
66
 
67
  # Return the transcribed text
68
  return {"text": result["text"]}
69
 
70
  except Exception as e:
71
- # Clean up temp file in case of error
72
- if 'temp_path' in locals() and os.path.exists(temp_path):
73
- os.remove(temp_path)
74
  raise HTTPException(status_code=500, detail=f"Error occurred: {str(e)}")
75
 
76
  @app.get("/")
 
2
  from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
3
  import torch
4
  import uvicorn
 
5
  from fastapi.middleware.cors import CORSMiddleware
 
 
6
 
7
  # Initialize FastAPI
8
  app = FastAPI()
 
43
  @app.post("/transcribe")
44
  async def transcribe_audio(file: UploadFile = File(...)):
45
  try:
46
+ # Read the audio file bytes directly from the uploaded file
47
+ audio_bytes = await file.read()
 
 
48
 
49
+ # Pass the raw audio bytes to the pipeline
50
+ result = pipe(audio_bytes)
 
 
 
 
 
 
 
 
 
 
51
 
52
  # Return the transcribed text
53
  return {"text": result["text"]}
54
 
55
  except Exception as e:
 
 
 
56
  raise HTTPException(status_code=500, detail=f"Error occurred: {str(e)}")
57
 
58
  @app.get("/")