JC321 commited on
Commit
34df737
·
verified ·
1 Parent(s): 104d61b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -60
app.py CHANGED
@@ -201,64 +201,15 @@ def chatbot_response(message, history):
201
  else:
202
  break
203
 
204
- # 构建响应前缀
205
- response_prefix = "<div style='padding: 8px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 6px; margin-bottom: 10px;'>\n"
206
- response_prefix += "<span style='color: white; font-size: 0.9em;'>🤖 <strong>Qwen2.5-72B</strong></span>\n"
207
- response_prefix += "</div>\n\n"
208
 
209
- # 显示工具调用(可展开/折叠)
210
  if tool_calls_log:
211
- # 添加 JavaScript 脚本用于切换展开/折叠
212
- response_prefix += """<script>
213
- function toggleToolDetails(id) {
214
- var details = document.getElementById('tool-details-' + id);
215
- var arrow = document.getElementById('tool-arrow-' + id);
216
- if (details.style.display === 'none' || details.style.display === '') {
217
- details.style.display = 'block';
218
- arrow.innerHTML = '▼';
219
- } else {
220
- details.style.display = 'none';
221
- arrow.innerHTML = '▶';
222
- }
223
- }
224
- </script>\n"""
225
-
226
- response_prefix += "<div style='background: #f8f9fa; border-radius: 6px; padding: 10px; margin-bottom: 12px;'>\n"
227
- response_prefix += "<div style='font-weight: 600; color: #495057; margin-bottom: 8px;'>🛠️ Tools Used</div>\n"
228
-
229
- tool_icons = {"advanced_search_company": "🔍", "get_latest_financial_data": "📊", "extract_financial_metrics": "📈",
230
- "get_quote": "💹", "get_market_news": "📰", "get_company_news": "📢"}
231
-
232
- for idx, tool_call in enumerate(tool_calls_log):
233
- icon = tool_icons.get(tool_call['name'], "⚙️")
234
- tool_id = f"tool_{idx}"
235
-
236
- # 工具主体(可点击)
237
- response_prefix += f"<div style='background: white; padding: 8px 10px; margin: 6px 0; border-radius: 4px; border-left: 3px solid #28a745; cursor: pointer;' onclick='toggleToolDetails(\"{tool_id}\")'>\n"
238
- response_prefix += f"<span id='tool-arrow-{tool_id}' style='color: #666; margin-right: 6px; font-size: 0.8em;'>▶</span>"
239
- response_prefix += f"<span style='color: #28a745; font-weight: 600;'>{icon} {tool_call['name']}</span>\n"
240
- response_prefix += f"</div>\n"
241
-
242
- # 工具详情(默认隐藏)
243
- response_prefix += f"<div id='tool-details-{tool_id}' style='display: none; background: #fff; padding: 10px; margin: 0 0 6px 20px; border-radius: 4px; border: 1px solid #e0e0e0; font-size: 0.85em;'>\n"
244
-
245
- # 输入参数
246
- response_prefix += "<div style='margin-bottom: 8px;'>\n"
247
- response_prefix += "<strong style='color: #2196f3;'>📝 Input:</strong>\n"
248
- response_prefix += f"<pre style='background: #f5f5f5; padding: 8px; border-radius: 4px; margin: 4px 0; overflow-x: auto;'>{json.dumps(tool_call['arguments'], indent=2, ensure_ascii=False)}</pre>\n"
249
- response_prefix += "</div>\n"
250
-
251
- # 输出结果
252
- if 'result' in tool_call:
253
- response_prefix += "<div>\n"
254
- response_prefix += "<strong style='color: #4caf50;'>✅ Output:</strong>\n"
255
- result_preview = str(tool_call['result'])[:200] + '...' if len(str(tool_call['result'])) > 200 else str(tool_call['result'])
256
- response_prefix += f"<pre style='background: #f5f5f5; padding: 8px; border-radius: 4px; margin: 4px 0; overflow-x: auto;'>{result_preview}</pre>\n"
257
- response_prefix += "</div>\n"
258
-
259
- response_prefix += "</div>\n"
260
-
261
- response_prefix += "</div>\n\n"
262
 
263
  # 流式输出最终答案
264
  yield response_prefix
@@ -286,9 +237,9 @@ function toggleToolDetails(id) {
286
  else:
287
  yield f"❌ Error: {error_detail}\n\n{traceback.format_exc()[:500]}"
288
 
289
- # ========== Gradio 界面 ==========
290
- with gr.Blocks(title="Financial & Market AI Assistant") as demo:
291
- gr.Markdown("# 🤖 Financial & Market AI Assistant")
292
 
293
  chat = gr.ChatInterface(
294
  fn=chatbot_response,
@@ -299,7 +250,7 @@ with gr.Blocks(title="Financial & Market AI Assistant") as demo:
299
  "Get the latest market news about crypto",
300
  "Compare Microsoft's latest earnings with its current stock price",
301
  ],
302
- chatbot=gr.Chatbot(height=750, show_copy_button=True),
303
  )
304
 
305
  # 启动应用
 
201
  else:
202
  break
203
 
204
+ # 构建响应前缀(简化版)
205
+ response_prefix = ""
 
 
206
 
207
+ # 显示工具调用(简单文本)
208
  if tool_calls_log:
209
+ response_prefix += "**🛠️ Tools Used:**\n\n"
210
+ for tool_call in tool_calls_log:
211
+ response_prefix += f"- `{tool_call['name']}` {json.dumps(tool_call['arguments'], ensure_ascii=False)}\n"
212
+ response_prefix += "\n---\n\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
  # 流式输出最终答案
215
  yield response_prefix
 
237
  else:
238
  yield f"❌ Error: {error_detail}\n\n{traceback.format_exc()[:500]}"
239
 
240
+ # ========== Gradio 界面(极简版)==========
241
+ with gr.Blocks(title="Financial AI Assistant") as demo:
242
+ gr.Markdown("# 💬 Financial AI Assistant")
243
 
244
  chat = gr.ChatInterface(
245
  fn=chatbot_response,
 
250
  "Get the latest market news about crypto",
251
  "Compare Microsoft's latest earnings with its current stock price",
252
  ],
253
+ chatbot=gr.Chatbot(height=600),
254
  )
255
 
256
  # 启动应用