voice-to-text / app.py
arshadrana's picture
Update app.py
0fe9a40 verified
raw
history blame
1.11 kB
import gradio as gr
import speech_recognition as sr
def transcribe_audio(audio_input):
recognizer = sr.Recognizer()
# Extract the file path from the Gradio audio input
if isinstance(audio_input, tuple):
audio_file_path = audio_input[0]
else:
raise ValueError("Expected audio_input to be a tuple, got {}".format(type(audio_input)))
# Load the audio file
with sr.AudioFile(audio_file_path) as source:
audio_data = recognizer.record(source)
try:
# Transcribe the audio data
text = recognizer.recognize_google(audio_data)
return text
except sr.UnknownValueError:
return "Google Speech Recognition could not understand audio"
except sr.RequestError as e:
return f"Could not request results from Google Speech Recognition service; {e}"
# Create the Gradio interface
iface = gr.Interface(
fn=transcribe_audio,
inputs="audio",
outputs="text",
title="Voice to Text Converter",
description="Upload an audio file and get the transcribed text."
)
# Launch the interface
iface.launch()