Improve use of .then - Example chat text and audio code
Hi. I have use example code of streaming chat and improve it with audio input. For as a beginner it was difficult to make it run… but after a few days searching and reading I got this working. I have commented the code, please help understand how to improve the use of the “.then” at the end of the code, is it correct?import gradio as grimport osimport timefrom AppChatBot import mainfrom custom_actions import recoghistory =title = ’AI ASSISTANT’description=“”“An AI Bot app uses with WebSearch, Weather Forecast, Chat with your PDF.”“”def add_text(history, text):history = history + [(text, None)]return history, gr.update(value=“”, interactive=False)def receivaudio(pathaudio):textaud = recog(pathaudio)#functioncalls gTTs speach_recognizerhistory.append((textaud, None))#appendingthe text to the history due not to change the bot functionreturn textauddef bot(history):history[-1][1] = “”#bygradio examplemens = history[len(history)-1][0]#workaroundresponse = main(mens)#mainfunction that call openAI chatbot with functions callingfor character in response:  #streaming text caracter
    history[-1][1] += character
    time.sleep(0.03)
    yield historywith gr.Blocks(theme=gr.themes.Soft()) as demo:gr.Markdown(f’{title}‘)gr.Markdown(f’{description}')with gr.Group():

    chatbot = gr.Chatbot(
        [],
        elem_id="chatbot",
        bubble_full_width=False,
        avatar_images=(None, (os.path.join(os.path.dirname(__file__), "robot.gif"))),
    )

with gr.Row():
    txt = gr.Textbox(
        scale=4,
        show_label=False,
        placeholder="Enter text and press enter, or record an audio",
        container=True,
        autofocus=True,
    )
    audio=gr.Audio(source="microphone", type="filepath",label='🎙️', interactive=True) #included audio
    
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
    bot, chatbot, chatbot
)
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False) # the gradio chat example ends here

# I have coding this part to add the audio. note the sequences of "then"
audio.stop_recording( receivaudio , inputs=audio, outputs=txt, queue=False).then(
    add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
        lambda: gr.update(interactive=True), None, [txt], queue=False).then(
            bot, chatbot, chatbot).then(lambda:None, None, audio, queue=False)ifname== “main”:demo.queue()demo.launch()