abdullahalioo commited on
Commit
507c064
·
verified ·
1 Parent(s): a64822b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -12
main.py CHANGED
@@ -1,5 +1,4 @@
1
- from fastapi import FastAPI, File, UploadFile, HTTPException
2
- from fastapi.responses import JSONResponse
3
  import speech_recognition as sr
4
  import os
5
  import uuid
@@ -11,25 +10,26 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
11
 
12
  @app.post("/voice-to-text")
13
  async def voice_to_text(file: UploadFile = File(...)):
14
- # Check file type
15
  if not file.filename.endswith(".wav"):
16
  raise HTTPException(status_code=400, detail="Only .wav files are supported")
17
 
18
  # Save uploaded file
19
- temp_filename = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}.wav")
20
- with open(temp_filename, "wb") as f:
21
  f.write(await file.read())
22
 
23
- # Recognize speech
24
  recognizer = sr.Recognizer()
25
  try:
26
- with sr.AudioFile(temp_filename) as source:
27
  audio_data = recognizer.record(source)
28
- text = recognizer.recognize_google(audio_data)
29
- return JSONResponse(content={"text": text})
 
30
  except sr.UnknownValueError:
31
- raise HTTPException(status_code=400, detail="Speech not recognized")
32
  except sr.RequestError as e:
33
- raise HTTPException(status_code=500, detail=f"API error: {e}")
34
  finally:
35
- os.remove(temp_filename)
 
1
+ from fastapi import FastAPI, UploadFile, File, HTTPException
 
2
  import speech_recognition as sr
3
  import os
4
  import uuid
 
10
 
11
  @app.post("/voice-to-text")
12
  async def voice_to_text(file: UploadFile = File(...)):
13
+ # Check for .wav file
14
  if not file.filename.endswith(".wav"):
15
  raise HTTPException(status_code=400, detail="Only .wav files are supported")
16
 
17
  # Save uploaded file
18
+ file_path = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}.wav")
19
+ with open(file_path, "wb") as f:
20
  f.write(await file.read())
21
 
22
+ # Speech recognition
23
  recognizer = sr.Recognizer()
24
  try:
25
+ with sr.AudioFile(file_path) as source:
26
  audio_data = recognizer.record(source)
27
+ recognized_text = recognizer.recognize_google(audio_data)
28
+ return {"recognized_text": recognized_text}
29
+
30
  except sr.UnknownValueError:
31
+ raise HTTPException(status_code=400, detail="Could not understand audio")
32
  except sr.RequestError as e:
33
+ raise HTTPException(status_code=500, detail=f"Speech API error: {e}")
34
  finally:
35
+ os.remove(file_path)