79239713

Date: 2024-11-30 14:12:16
Score: 3.5
Natty:
Report link

I search and saw some solution, but they are too simple. My code is from: https://www.gradio.app/guides/creating-a-custom-chatbot-with-blocks , like below:

It is very common to get current session user in bot function, but bot function cannot get Request parameter, I don't know how to get session information in bot function. Their demo only has very simple case, does anyone know how to get request in this case? Thanks!

def add_message(history, message):
for x in message["files"]:
    history.append({"role": "user", "content": {"path": x}})
if message["text"] is not None:
    history.append({"role": "user", "content": message["text"]})
return history, gr.MultimodalTextbox(value=None, interactive=False)

def bot(history: list):
    response = "**That's cool!**"
    history.append({"role": "assistant", "content": ""})
    for character in response:
        history[-1]["content"] += character
    time.sleep(0.05)
    yield history


with gr.Blocks() as demo:
chatbot = gr.Chatbot(elem_id="chatbot", bubble_full_width=False, type="messages")

chat_input = gr.MultimodalTextbox(
    interactive=True,
    file_count="multiple",
    placeholder="Enter message or upload file...",
    show_label=False,
)

chat_msg = chat_input.submit(
    add_message, [chatbot, chat_input], [chatbot, chat_input]
)
bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])

chatbot.like(print_like_dislike, None, None, like_user_message=True)

demo.launch()

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • RegEx Blacklisted phrase (2): does anyone know
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: peter