From a43eaff923428141c6f2ad37f9ec777d603a63ad Mon Sep 17 00:00:00 2001 From: SurajMarda Date: Mon, 4 Aug 2025 23:15:20 -0700 Subject: [PATCH] Update app_audio.py --- app_audio.py | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/app_audio.py b/app_audio.py index 88819bd..49c007f 100644 --- a/app_audio.py +++ b/app_audio.py @@ -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) \ No newline at end of file +if __name__ == "__main__": + audio_file_path = "./sample_audio/speech.wav" + transcription_result = transcribe_audio_file(audio_file_path) + print(transcription_result)