ysharma HF staff commited on
Commit
61a2221
·
verified ·
1 Parent(s): 15ffc1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -28
app.py CHANGED
@@ -4,47 +4,75 @@ import time
4
  def bot_response(message, history):
5
  print("\n=== Bot Response Function ===")
6
  print(f"Received message: {message}")
7
- print(f"Initial history: {history}")
8
 
9
  history = history or []
10
- messages = []
11
 
12
- # First message
13
- print("\nAdding first message pair...")
14
- messages.append({"role": "assistant", "content": "Thinking..."})
15
- history.extend(messages)
16
- print(f"History after first message: {history}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  yield history
18
  time.sleep(1)
19
 
20
- # Second message
21
- print("\nAdding second message...")
22
- messages = [{"role": "assistant", "content": "This is the part 1 of bot response"}]
23
- history.extend(messages)
24
- print(f"History after second message: {history}")
 
25
  yield history
26
  time.sleep(1)
27
 
28
- # Third message
29
- print("\nAdding third message...")
30
- messages = [{"role": "assistant", "content": "And this is part 2"}]
31
- history.extend(messages)
32
- print(f"History after third message: {history}")
 
 
 
 
 
 
33
  yield history
34
  time.sleep(1)
35
 
36
- # Fourth message
37
- print("\nAdding fourth message...")
38
- messages = [{"role": "assistant", "content": "LAstly, some code:\n```python\nprint('hello world')\n```"}]
39
- history.extend(messages)
40
- print(f"History after fourth message: {history}")
 
 
 
 
 
 
41
  yield history
42
 
43
  with gr.Blocks() as demo:
44
  chatbot = gr.Chatbot(
45
  height=600,
46
  group_consecutive_messages=False,
47
- type="messages"
 
48
  )
49
  msg = gr.Textbox()
50
  clear = gr.Button("Clear")
@@ -52,13 +80,10 @@ with gr.Blocks() as demo:
52
  def user(user_message, history):
53
  print("\n=== User Message Function ===")
54
  print(f"User message: {user_message}")
55
- print(f"Current history: {history}")
56
 
57
- # Only add user message if it's not empty
58
  if user_message.strip():
59
- new_history = history + [{"role": "user", "content": user_message}]
60
- print(f"New history after adding user message: {new_history}")
61
- return "", new_history
62
  return "", history
63
 
64
  msg.submit(
 
4
  def bot_response(message, history):
5
  print("\n=== Bot Response Function ===")
6
  print(f"Received message: {message}")
 
7
 
8
  history = history or []
 
9
 
10
+ # Regular message without metadata
11
+ msg = gr.ChatMessage(
12
+ role="assistant",
13
+ content="Let me help you analyze that..."
14
+ )
15
+ history.append(msg)
16
+ yield history
17
+ time.sleep(1)
18
+
19
+ # Tool execution with metadata
20
+ parent_id = "tool_call_1"
21
+ tool_msg = gr.ChatMessage(
22
+ role="assistant",
23
+ content="```python\nprint('Running analysis...')\n```",
24
+ metadata={
25
+ "title": "🛠️ Using Python Interpreter",
26
+ "id": parent_id,
27
+ "status": "pending"
28
+ }
29
+ )
30
+ history.append(tool_msg)
31
  yield history
32
  time.sleep(1)
33
 
34
+ # Regular message showing progress
35
+ progress_msg = gr.ChatMessage(
36
+ role="assistant",
37
+ content="Processing your request..."
38
+ )
39
+ history.append(progress_msg)
40
  yield history
41
  time.sleep(1)
42
 
43
+ # Execution result with metadata
44
+ execution_msg = gr.ChatMessage(
45
+ role="assistant",
46
+ content="Output: Analysis complete",
47
+ metadata={
48
+ "title": "📝 Execution Result",
49
+ "parent_id": parent_id,
50
+ "status": "pending"
51
+ }
52
+ )
53
+ history.append(execution_msg)
54
  yield history
55
  time.sleep(1)
56
 
57
+ # Update tool messages to done
58
+ tool_msg.metadata["status"] = "done"
59
+ execution_msg.metadata["status"] = "done"
60
+ yield history
61
+
62
+ # Final message without metadata
63
+ final_msg = gr.ChatMessage(
64
+ role="assistant",
65
+ content="Based on the analysis, I can confirm the process completed successfully."
66
+ )
67
+ history.append(final_msg)
68
  yield history
69
 
70
  with gr.Blocks() as demo:
71
  chatbot = gr.Chatbot(
72
  height=600,
73
  group_consecutive_messages=False,
74
+ type="messages",
75
+ bubble_full_width=False
76
  )
77
  msg = gr.Textbox()
78
  clear = gr.Button("Clear")
 
80
  def user(user_message, history):
81
  print("\n=== User Message Function ===")
82
  print(f"User message: {user_message}")
 
83
 
 
84
  if user_message.strip():
85
+ history.append(gr.ChatMessage(role="user", content=user_message))
86
+ return "", history
 
87
  return "", history
88
 
89
  msg.submit(