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 gr
import os
import time
from AppChatBot import main
from custom_actions import recog

history =

title = ’ :robot: 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) #function calls gTTs speach_recognizer
history.append((textaud, None)) #appending the text to the history due not to change the bot function
return textaud

def bot(history):
history[-1][1] = “” #by gradio example
mens = history[len(history)-1][0] #workaround
response = main(mens) #main function that call openAI chatbot with functions calling

for character in response:  #streaming text caracter
    history[-1][1] += character
    yield history

with gr.Blocks(theme=gr.themes.Soft()) as demo:



with gr.Group():

    chatbot = gr.Chatbot(
        avatar_images=(None, (os.path.join(os.path.dirname(__file__), "robot.gif"))),

with gr.Row():
    txt = gr.Textbox(
        placeholder="Enter text and press enter, or record an audio",
    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)

if name == “main”: