File size: 1,188 Bytes
309b067
 
 
 
 
 
c65b28c
 
 
 
85b27a4
 
 
 
309b067
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio as gr
import speech_recognition as sr

def transcribe_audio(audio_file):
    recognizer = sr.Recognizer()
    
    # Check if audio_file is a tuple and extract the file path
    if isinstance(audio_file, tuple):
        audio_file = audio_file[0]
    
    # Ensure the audio_file is a file path
    if not isinstance(audio_file, str):
        raise ValueError("Expected audio_file to be a file path, got {}".format(type(audio_file)))
    
    # Load the audio file
    with sr.AudioFile(audio_file) 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()