File size: 2,596 Bytes
b0205d9
 
 
 
15ffc1b
 
 
b0205d9
 
61a2221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0205d9
 
 
61a2221
 
 
 
 
 
b0205d9
 
 
61a2221
 
 
 
 
 
 
 
 
 
 
b0205d9
 
 
61a2221
 
 
 
 
 
 
 
 
 
 
b0205d9
 
 
 
15ffc1b
b0205d9
61a2221
 
b0205d9
 
 
 
 
15ffc1b
 
 
 
61a2221
 
15ffc1b
b0205d9
15ffc1b
 
 
 
 
 
 
 
b0205d9
15ffc1b
b0205d9
 
15ffc1b
b0205d9
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import gradio as gr
import time

def bot_response(message, history):
    print("\n=== Bot Response Function ===")
    print(f"Received message: {message}")
    
    history = history or []
    
    # Regular message without metadata
    msg = gr.ChatMessage(
        role="assistant",
        content="Let me help you analyze that..."
    )
    history.append(msg)
    yield history
    time.sleep(1)
    
    # Tool execution with metadata
    parent_id = "tool_call_1"
    tool_msg = gr.ChatMessage(
        role="assistant",
        content="```python\nprint('Running analysis...')\n```",
        metadata={
            "title": "πŸ› οΈ Using Python Interpreter",
            "id": parent_id,
            "status": "pending"
        }
    )
    history.append(tool_msg)
    yield history
    time.sleep(1)
    
    # Regular message showing progress
    progress_msg = gr.ChatMessage(
        role="assistant",
        content="Processing your request..."
    )
    history.append(progress_msg)
    yield history
    time.sleep(1)
    
    # Execution result with metadata
    execution_msg = gr.ChatMessage(
        role="assistant",
        content="Output: Analysis complete",
        metadata={
            "title": "πŸ“ Execution Result",
            "parent_id": parent_id,
            "status": "pending"
        }
    )
    history.append(execution_msg)
    yield history
    time.sleep(1)
    
    # Update tool messages to done
    tool_msg.metadata["status"] = "done"
    execution_msg.metadata["status"] = "done"
    yield history
    
    # Final message without metadata
    final_msg = gr.ChatMessage(
        role="assistant",
        content="Based on the analysis, I can confirm the process completed successfully."
    )
    history.append(final_msg)
    yield history

with gr.Blocks() as demo:
    chatbot = gr.Chatbot(
        height=600,
        group_consecutive_messages=False,
        type="messages",
        bubble_full_width=False
    )
    msg = gr.Textbox()
    clear = gr.Button("Clear")

    def user(user_message, history):
        print("\n=== User Message Function ===")
        print(f"User message: {user_message}")
        
        if user_message.strip():
            history.append(gr.ChatMessage(role="user", content=user_message))
            return "", history
        return "", history

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

#demo.queue()
demo.launch(debug=True)