Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions app_audio.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import speech_recognition as sr

recognizer = sr.Recognizer()
def transcribe_audio_file(file_path):
"""
Transcribes the speech from an audio file using Google's Web Speech API.

''' recording the sound '''
Args:
file_path (str): The path to the audio file.

with sr.AudioFile("./sample_audio/speech.wav") as source:
recorded_audio = recognizer.listen(source)
print("Done recording")
Returns:
str: The transcribed text, or an error message if transcription fails.
"""
recognizer = sr.Recognizer()

''' Recorgnizing the Audio '''
try:
print("Recognizing the text")
text = recognizer.recognize_google(
recorded_audio,
try:
with sr.AudioFile(file_path) as source:
print("Processing audio...")
audio_data = recognizer.record(source)

print("Recognizing the text...")
text = recognizer.recognize_google(
audio_data,
language="en-US"
)
print("Decoded Text : {}".format(text))
return f"Decoded Text: {text}"

except sr.UnknownValueError:
return "Error: Google Speech Recognition could not understand audio."
except sr.RequestError as e:
return f"Error: Could not request results from Google Speech Recognition service; {e}"
except FileNotFoundError:
return f"Error: The file '{file_path}' was not found."
except Exception as e:
return f"An unexpected error occurred: {e}"

except Exception as ex:
print(ex)
if __name__ == "__main__":
audio_file_path = "./sample_audio/speech.wav"
transcription_result = transcribe_audio_file(audio_file_path)
print(transcription_result)