File size: 2,254 Bytes
b0205d9
 
 
 
15ffc1b
 
 
 
b0205d9
 
 
 
15ffc1b
 
b0205d9
15ffc1b
b0205d9
 
 
 
15ffc1b
 
b0205d9
15ffc1b
b0205d9
 
 
 
15ffc1b
 
b0205d9
15ffc1b
b0205d9
 
 
 
15ffc1b
 
b0205d9
15ffc1b
b0205d9
 
 
 
15ffc1b
b0205d9
 
 
 
 
 
 
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
import gradio as gr
import time

def bot_response(message, history):
    print("\n=== Bot Response Function ===")
    print(f"Received message: {message}")
    print(f"Initial history: {history}")
    
    history = history or []
    messages = []
    
    # First message
    print("\nAdding first message pair...")
    messages.append({"role": "assistant", "content": "Thinking..."})
    history.extend(messages)
    print(f"History after first message: {history}")
    yield history
    time.sleep(1)
    
    # Second message
    print("\nAdding second message...")
    messages = [{"role": "assistant", "content": "This is the part 1 of bot response"}]
    history.extend(messages)
    print(f"History after second message: {history}")
    yield history
    time.sleep(1)
    
    # Third message
    print("\nAdding third message...")
    messages = [{"role": "assistant", "content": "And this is part 2"}]
    history.extend(messages)
    print(f"History after third message: {history}")
    yield history
    time.sleep(1)
    
    # Fourth message
    print("\nAdding fourth message...")
    messages = [{"role": "assistant", "content": "LAstly, some code:\n```python\nprint('hello world')\n```"}]
    history.extend(messages)
    print(f"History after fourth message: {history}")
    yield history

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

    def user(user_message, history):
        print("\n=== User Message Function ===")
        print(f"User message: {user_message}")
        print(f"Current history: {history}")
        
        # Only add user message if it's not empty
        if user_message.strip():
            new_history = history + [{"role": "user", "content": user_message}]
            print(f"New history after adding user message: {new_history}")
            return "", new_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)