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

**URL:** <https://discuss.huggingface.co/t/how-to-get-the-textbox-input-in-a-function-in-the-chatbot-streaming-app/41361>\
**Category:** 🔒 Gradio\
**Created:** [May 29, 2023, 8:28pm UTC](https://discuss.huggingface.co/t/how-to-get-the-textbox-input-in-a-function-in-the-chatbot-streaming-app/41361 "2023-05-29T20:28:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hboen](https://avatars.discourse-cdn.com/v4/letter/h/9f8e36/32.png) [@hboen](https://discuss.huggingface.co/u/hboen)\
**Post date:** [May 29, 2023, 8:28pm UTC](https://discuss.huggingface.co/t/how-to-get-the-textbox-input-in-a-function-in-the-chatbot-streaming-app/41361/1 "2023-05-29T20:28:20Z")

</div>

Hi,  
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

#gradio chat1.py demo.app

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
        time.sleep(0.05)
        yield history

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

```

demo.queue()  
demo.launch()

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.

Henry

---

<div class="post-metadata">

**Author:** ![hboen](https://avatars.discourse-cdn.com/v4/letter/h/9f8e36/32.png) [@hboen](https://discuss.huggingface.co/u/hboen)\
**Post date:** [May 31, 2023, 9:31pm UTC](https://discuss.huggingface.co/t/how-to-get-the-textbox-input-in-a-function-in-the-chatbot-streaming-app/41361/2 "2023-05-31T21:31:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [June 1, 2023, 3:23am UTC](https://discuss.huggingface.co/t/how-to-get-the-textbox-input-in-a-function-in-the-chatbot-streaming-app/41361/3 "2023-06-01T03:23:48Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![kamiyan89](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@kamiyan89](https://discuss.huggingface.co/u/kamiyan89)\
**Post date:** [June 16, 2023, 2:13am UTC](https://discuss.huggingface.co/t/how-to-get-the-textbox-input-in-a-function-in-the-chatbot-streaming-app/41361/4 "2023-06-16T02:13:03Z")

</div>

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

> [@How to implement chatbot streaming with a function](https://discuss.huggingface.co/t/how-to-implement-chatbot-streaming-with-a-function/43395):
>
> HI everyone. I am trying to build a basicchatbot with streaming. from the documentation we have the following code. import gradio as gr import random import time with gr.Blocks() as demo: chatbot = gr.Chatbot() msg = gr.Textbox() clear = gr.Button("Clear") def respond(message, chat\_history): bot\_message = random.choice(["How are you?", "I love you", "I'm very hungry"]) chat\_history.append((message, bot\_message)) time.sleep(2) return "", chat\_h…

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.
