File size: 1,275 Bytes
ba52610
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
284ee47
 
 
ba52610
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
import time

# Inspired by: https://www.gradio.app/guides/agents-and-tool-usage


def interact_with_agent(prompt, messages=[]):
    """Simulate generation of two assistant messages after delays"""
    time.sleep(2)
    messages.append(
        gr.ChatMessage(
            role="assistant", content="Progress timer stops here (or becomes invisible)"
        )
    )
    yield messages
    time.sleep(2)
    messages.append(
        gr.ChatMessage(role="assistant", content="But I want it to stop here")
    )
    yield messages


def str_to_message(content, role="user"):
    return [gr.ChatMessage(role=role, content=content)]


with gr.Blocks() as demo:
    chatbot = gr.Chatbot(
        type="messages",
    )
    input = gr.Textbox(
        "Start the simulation with two assistant messages after 2-second delays",
        autofocus=True,
    )
    input.submit(
        # Update chatbot UI with user message immediately
        str_to_message,
        input,
        chatbot,
    ).then(
        # Update chatbot UI with assistant messages
        interact_with_agent,
        [input, chatbot],
        chatbot,
    )
    gr.Markdown(
        "Reported as [Gradio issue #11637](https://github.com/gradio-app/gradio/issues/11637)"
    )

demo.launch()