Ali2206 commited on
Commit
9b25d93
·
verified ·
1 Parent(s): cbf5c2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -9
app.py CHANGED
@@ -5,6 +5,7 @@ import gradio as gr
5
  from multiprocessing import freeze_support
6
  import importlib
7
  import inspect
 
8
 
9
  # === Fix path to include src/txagent
10
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
@@ -55,7 +56,6 @@ def create_ui(agent):
55
 
56
  # === Core handler (streaming generator)
57
  def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
58
- # Must yield a list of {"role": ..., "content": ...} dicts
59
  generator = agent.run_gradio_chat(
60
  message=message,
61
  history=history,
@@ -66,15 +66,30 @@ def create_ui(agent):
66
  conversation=conversation,
67
  max_round=max_round
68
  )
 
69
  for update in generator:
70
- # Convert to list of dicts if not already
71
- formatted = [
72
- {"role": m["role"], "content": m["content"]}
73
- if isinstance(m, dict)
74
- else {"role": m.role, "content": m.content}
75
- for m in update
76
- ]
77
- yield formatted
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  # === Trigger handlers
80
  send_button.click(
 
5
  from multiprocessing import freeze_support
6
  import importlib
7
  import inspect
8
+ import json
9
 
10
  # === Fix path to include src/txagent
11
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
 
56
 
57
  # === Core handler (streaming generator)
58
  def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
 
59
  generator = agent.run_gradio_chat(
60
  message=message,
61
  history=history,
 
66
  conversation=conversation,
67
  max_round=max_round
68
  )
69
+
70
  for update in generator:
71
+ formatted_messages = []
72
+
73
+ for m in update:
74
+ role = m.get("role")
75
+ content = m.get("content", "")
76
+
77
+ # 🧠 Pretty-print tool calls
78
+ if "[TOOL_CALLS]" in content:
79
+ try:
80
+ thought, tools = content.split("[TOOL_CALLS]", 1)
81
+ tools_json = json.loads(tools.strip())
82
+ pretty_tools = json.dumps(tools_json, indent=2)
83
+
84
+ # Style it like a ChatGPT output
85
+ content = f"**🧠 Assistant reasoning:**\n\n{thought.strip()}\n\n" \
86
+ f"**🔧 Tool Calls:**\n\n```json\n{pretty_tools}\n```"
87
+ except Exception as e:
88
+ content = f"{content}\n\n⚠️ Failed to format tool calls: {e}"
89
+
90
+ formatted_messages.append({"role": role, "content": content})
91
+
92
+ yield formatted_messages
93
 
94
  # === Trigger handlers
95
  send_button.click(