How to get the microphone streaming input file when using blocks?

I’m trying to convert a simple example from interface to block, and I have missing part

The app displays every 2 seconds the temporary recording wav file name:

import gradio as gr
import time

def transcribe(audio, state=""):
    time.sleep(2)
    return audio


inf = gr.Interface(
    fn=transcribe,
    inputs=[
        gr.Audio(source="microphone", type="filepath", streaming=True),
    ],
    outputs=[
        "textbox",
    ],
    live=True)

if __name__ == "__main__":
    inf.launch()

I’m trying to write this simple example with blocks, but I don’t know how to connect the transcribe function to the new file event ?

import gradio as gr
import time

def transcribe(audio, state=""):
    time.sleep(2)
    return audio

with gr.Blocks() as demo:
    input_mic = gr.Audio(source="microphone", type="filepath", streaming=True)
    out_text  = gr.Textbox()

# how to connect transcribe function to the input_mic event ?

if __name__ == "__main__":
    demo.launch()

this responds to your question


import gradio as gr
from transformers import pipeline
import time

pipe = pipeline("automatic-speech-recognition")

def transcribe(audio, state=""):
    print(audio)
    time.sleep(2)
    text = pipe(audio)["text"]
    state += text + " "
    return state, state


with gr.Blocks() as demo:
  state = gr.State(value="")
  with gr.Row():
      with gr.Column():
        audio = gr.Audio(source="microphone", type="filepath") 
      with gr.Column():
        textbox = gr.Textbox()
  audio.stream(fn=transcribe, inputs=[audio, state], outputs=[textbox, state])

demo.launch(debug=True)
1 Like