File size: 916 Bytes
14335b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import speech_recognition as sr

# Speech-to-text conversion function
def transcribe_audio(audio):
    recognizer = sr.Recognizer()
    with sr.AudioFile(audio) as source:
        audio_data = recognizer.record(source)
    try:
        text = recognizer.recognize_google(audio_data)
        return text
    except sr.UnknownValueError:
        return "Could not understand the audio."
    except sr.RequestError as e:
        return f"Speech recognition error: {e}"

# Gradio UI
def main():
    iface = gr.Interface(
        fn=transcribe_audio,
        inputs=gr.Audio(sources=["microphone"], type="filepath", label="Speak something"),
        outputs=gr.Textbox(label="Transcribed Text"),
        title="Speech-to-Text Demo",
        description="Speak into the microphone and see the transcribed text below.",
        live=False
    )
    iface.launch()

if __name__ == "__main__":
    main()