JC321 commited on
Commit
02f9722
·
verified ·
1 Parent(s): 39c4db5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -37,7 +37,11 @@ session = create_session_with_retry()
37
  # 初始化 Hugging Face Inference Client
38
  # 使用环境变量或者免费的公开模型
39
  HF_TOKEN = os.getenv("HF_TOKEN", None) # 可选:如果需要访问私有模型
40
- client = InferenceClient(token=HF_TOKEN)
 
 
 
 
41
 
42
  # 定义可用的 MCP 工具
43
  MCP_TOOLS = [
@@ -279,14 +283,9 @@ def query_financial_data(company_name, query_type):
279
  # 检查是否有错误
280
  if "error" in search_result:
281
  return f"❌ Server Error: {search_result.get('error')}\n\nResponse: {search_result.get('detail', 'N/A')}\n\nURL: {search_result.get('url', MCP_URL)}"
282
- return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
283
 
284
- try:
285
- result = search_resp.json()
286
- # 使用统一的 MCP 响应解析函数
287
- company = parse_mcp_response(result)
288
- except (ValueError, KeyError, json.JSONDecodeError) as e:
289
- return f"❌ JSON Parse Error: {str(e)}\n\nResponse: {search_resp.text[:500]}"
290
 
291
  if isinstance(company, dict) and company.get("error"):
292
  return f"❌ Error: {company['error']}"
@@ -665,6 +664,10 @@ Always be helpful, accurate, and cite the data sources when providing financial
665
 
666
  # 使用支持工具调用的模型(如 Qwen, Llama 等)
667
  try:
 
 
 
 
668
  response = client.chat_completion(
669
  messages=messages,
670
  model="Qwen/Qwen2.5-72B-Instruct", # 支持工具调用的模型
 
37
  # 初始化 Hugging Face Inference Client
38
  # 使用环境变量或者免费的公开模型
39
  HF_TOKEN = os.getenv("HF_TOKEN", None) # 可选:如果需要访问私有模型
40
+ try:
41
+ client = InferenceClient(token=HF_TOKEN)
42
+ except Exception as e:
43
+ print(f"Warning: Failed to initialize Hugging Face client: {e}")
44
+ client = None
45
 
46
  # 定义可用的 MCP 工具
47
  MCP_TOOLS = [
 
283
  # 检查是否有错误
284
  if "error" in search_result:
285
  return f"❌ Server Error: {search_result.get('error')}\n\nResponse: {search_result.get('detail', 'N/A')}\n\nURL: {search_result.get('url', MCP_URL)}"
 
286
 
287
+ # 解析搜索结果
288
+ company = parse_mcp_response(search_result)
 
 
 
 
289
 
290
  if isinstance(company, dict) and company.get("error"):
291
  return f"❌ Error: {company['error']}"
 
664
 
665
  # 使用支持工具调用的模型(如 Qwen, Llama 等)
666
  try:
667
+ # 检查 client 是否可用
668
+ if client is None:
669
+ return fallback_chatbot_response(message)
670
+
671
  response = client.chat_completion(
672
  messages=messages,
673
  model="Qwen/Qwen2.5-72B-Instruct", # 支持工具调用的模型