How to get the textbox input in a function in the chatbot streaming app

I am just starting with gradio and copied a code from gradio

import gradio as gr
import random
import time
from googletrans import Translator

translator = Translator()

def my _translate_func(response, translate_to):
return translator.translate(response,dest=translate_to).text


with gr.Blocks() as demo:
drop_lang = gr.Dropdown([“nl”, “de”, “en”,“fr”], label=“Languages”)
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button(“Clear”)

def user(user_message, history):
    return "", history + [[user_message, None]]

def bot(message,history):
    bot_message = random.choice(["How are you?", "I love you", "I'm very hungry"])
    history[-1][1] = ""
    for character in bot_message:
        history[-1][1] += character
        yield history

msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
    bot, chatbot, chatbot
) None, None, chatbot, queue=False)


My question is how can i replace the example of random.choice(…)

with my my my _translate_func function. that it accepts the language chosen and the input from the textbox…

Thanks in advance.


I solve it by replacing the random with the translate function and as input the history[len(history)-1][0]

Hi @hboen!

Glad you figured it out. Yes the history is a list of lists. The first element of the list is the user message and the bot message is the second element. So you have the right idea!

Hi @freddyaboulton, I am sorry to post here, but could you check my thread?

I am having a similar question to @hboen, but haven’t been able to figure how to make it work.

Any comment is greatly appreciated.