JC321 commited on
Commit
53cf0bf
·
verified ·
1 Parent(s): ecbeffc

Upload 2 files

Browse files
Files changed (2) hide show
  1. README.md +20 -3
  2. app.py +53 -41
README.md CHANGED
@@ -16,7 +16,7 @@ A Gradio-based AI-powered web application for querying SEC financial data throug
16
 
17
  ## ✨ Features
18
 
19
- - 🤖 **Intelligent AI Assistant**: Chat naturally with Qwen/Qwen2.5-72B-Instruct model
20
  - 🛠️ **Automatic Tool Calling**: AI automatically selects and calls MCP tools based on your questions
21
  - 🔍 Search companies by name or ticker symbol
22
  - 📈 View latest financial data
@@ -26,7 +26,24 @@ A Gradio-based AI-powered web application for querying SEC financial data throug
26
 
27
  ## 🚀 Quick Start
28
 
29
- No configuration needed! Just start asking questions:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  ### AI Assistant Tab
32
 
@@ -79,7 +96,7 @@ SEC EDGAR data via MCP Server: https://huggingface.co/spaces/JC321/EasyReportDat
79
 
80
  - **Frontend**: Gradio 6.0.1
81
  - **Backend**: Python with requests
82
- - **AI Model**: Qwen/Qwen2.5-72B-Instruct (via Hugging Face Inference API)
83
  - **MCP Protocol**: FastMCP with HTTP transport (stateless)
84
  - **Data Source**: SEC EDGAR via MCP Server
85
 
 
16
 
17
  ## ✨ Features
18
 
19
+ - 🤖 **Intelligent AI Assistant**: Chat naturally with **Qwen/Qwen2.5-VL-72B-Instruct:nebius** model
20
  - 🛠️ **Automatic Tool Calling**: AI automatically selects and calls MCP tools based on your questions
21
  - 🔍 Search companies by name or ticker symbol
22
  - 📈 View latest financial data
 
26
 
27
  ## 🚀 Quick Start
28
 
29
+ ### Prerequisites
30
+
31
+ - Python 3.9+
32
+ - Hugging Face API token (get one at https://huggingface.co/settings/tokens)
33
+
34
+ ### Installation
35
+
36
+ 1. Clone the repository
37
+ 2. Install dependencies: `pip install -r requirements.txt`
38
+ 3. Create a `.env` file with your HF token:
39
+ ```
40
+ HF_TOKEN=your_token_here
41
+ ```
42
+ 4. Run: `python app.py`
43
+
44
+ ### Usage
45
+
46
+ No additional configuration needed! Just start asking questions:
47
 
48
  ### AI Assistant Tab
49
 
 
96
 
97
  - **Frontend**: Gradio 6.0.1
98
  - **Backend**: Python with requests
99
+ - **AI Model**: Qwen/Qwen2.5-VL-72B-Instruct:nebius (via Hugging Face Inference API)
100
  - **MCP Protocol**: FastMCP with HTTP transport (stateless)
101
  - **Data Source**: SEC EDGAR via MCP Server
102
 
app.py CHANGED
@@ -35,10 +35,18 @@ def create_session_with_retry():
35
  session = create_session_with_retry()
36
 
37
  # 初始化 Hugging Face Inference Client
38
- # 直接使用免费的公开 API,无需 token
39
  try:
40
- client = InferenceClient()
41
- print("✅ Hugging Face client initialized (free tier)")
 
 
 
 
 
 
 
 
42
  except Exception as e:
43
  print(f"❌ Warning: Failed to initialize Hugging Face client: {e}")
44
  client = None
@@ -617,36 +625,21 @@ def chatbot_response(message, history):
617
  # 构建对话历史
618
  messages = []
619
 
620
- # 系统提示词 - 财报分析专家
621
- system_prompt = """You are a financial reporting analysis expert, specializing in analyzing company financial data and providing professional insights.
622
 
623
- Your expertise:
624
- - Deep understanding of financial statements (10-K, 10-Q filings)
625
- - Expert at identifying trends, patterns, and anomalies in financial data
626
- - Skilled at comparing metrics across different periods and competitors
627
- - Proficient in calculating and interpreting key financial ratios
628
 
629
- Your available tools:
630
- 1. **advanced_search_company(company_input)** - Find US companies by name or ticker symbol
631
- 2. **get_latest_financial_data(cik)** - Retrieve the most recent financial report
632
- 3. **extract_financial_metrics(cik, years)** - Get multi-year financial trends (3 or 5 years)
 
633
 
634
- How to analyze:
635
- - When users ask about a company, automatically fetch relevant data
636
- - Provide professional analysis with specific numbers and percentages
637
- - Calculate YoY growth rates, profit margins, and other key metrics
638
- - Identify trends and explain their business implications
639
- - Compare current performance with historical data
640
- - Highlight both strengths and concerns in the financial data
641
-
642
- Analysis framework:
643
- - Revenue analysis: Growth rate, trend direction, stability
644
- - Profitability: Net income, profit margins, efficiency
645
- - Cash flow: Operating cash flow adequacy, cash generation ability
646
- - Per-share metrics: EPS trends, shareholder value creation
647
- - Overall assessment: Financial health and future outlook
648
-
649
- Remember: Provide data-driven insights, not just raw numbers. Help users understand what the numbers really mean for the business."""
650
 
651
  messages.append({"role": "system", "content": system_prompt})
652
 
@@ -685,10 +678,11 @@ Remember: Provide data-driven insights, not just raw numbers. Help users underst
685
 
686
  response = client.chat_completion(
687
  messages=messages,
688
- model="Qwen/Qwen2.5-72B-Instruct", # 支持工具调用的模型
689
  tools=MCP_TOOLS,
690
- max_tokens=2500, # 增加token数,支持更详细的分析
691
- temperature=0.8 # 较高的temperature使回答更自然、更有创意
 
692
  )
693
 
694
  print(f"✅ LLM response received (iteration {iteration})")
@@ -735,23 +729,38 @@ Remember: Provide data-driven insights, not just raw numbers. Help users underst
735
  break
736
 
737
  except ValueError as ve:
738
- # API Key 相关错误(虽然我们不使用 token,但仍然可能出现)
739
  error_msg = str(ve)
 
740
  if "api_key" in error_msg.lower() or "token" in error_msg.lower():
741
- print(f"⚠️ LLM API rate limit or authentication issue: {ve}")
742
  print("ℹ️ Falling back to simple response logic")
743
  return fallback_chatbot_response(message)
744
  else:
745
  raise
746
  except Exception as e:
747
- # 其他 LLM API 错误(如速率限制)
748
- print(f"⚠️ LLM API error (possibly rate limited): {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
749
  print("ℹ️ Falling back to simple response logic")
750
  return fallback_chatbot_response(message)
751
 
752
  # 构建最终响应
753
  final_response = ""
754
 
 
 
 
755
  # 如果有工具调用,显示调用日志
756
  if tool_calls_log:
757
  final_response += "**🛠️ MCP Tools Used:**\n\n"
@@ -769,6 +778,9 @@ Remember: Provide data-driven insights, not just raw numbers. Help users underst
769
 
770
  def fallback_chatbot_response(message):
771
  """退回策略:当 LLM API 不可用时使用的简单逻辑"""
 
 
 
772
  # 检查是否是财务查询相关问题
773
  financial_keywords = ['financial', 'revenue', 'income', 'earnings', 'cash flow', 'expenses', '财务', '收入', '利润', 'data', 'trend', 'performance']
774
 
@@ -800,12 +812,12 @@ def fallback_chatbot_response(message):
800
 
801
  # 调用财务查询函数
802
  result = query_financial_data(detected_company, query_type)
803
- return f"Here's the financial information for {detected_company}:\n\n{result}"
804
  else:
805
- return "I can help you query financial data! Please specify a company name. For example: 'Show me Apple's latest financial data' or 'What's NVIDIA's 3-year trend?' \n\nSupported companies include: Apple, Microsoft, NVIDIA, Tesla, Alibaba, Google, Amazon, and more."
806
 
807
  # 如果不是财务查询,返回通用回复
808
- return "Hello! I'm a financial data assistant powered by SEC EDGAR data. I can help you query financial information for US listed companies.\n\n**What I can do:**\n- Get latest financial data (revenue, income, EPS, etc.)\n- Show 3-year or 5-year financial trends\n- Provide detailed financial metrics\n\n**Try asking:**\n- 'Show me Apple's latest financial data'\n- 'What's NVIDIA's 3-year financial trend?'\n- 'How is Microsoft performing financially?'"
809
 
810
  # 包装函数,显示加载状态
811
  def query_with_status(company, query_type):
@@ -831,7 +843,7 @@ with gr.Blocks(title="SEC Financial Data Query Assistant") as demo:
831
  # 显示 AI 功能说明
832
  gr.Markdown("""
833
  <div style='padding: 15px; background: #d4edda; border-left: 4px solid #28a745; margin: 10px 0; border-radius: 4px;'>
834
- <strong>✅ AI Assistant Enabled:</strong> Powered by Qwen/Qwen2.5-72B-Instruct model with automatic MCP tool calling.
835
  <br>
836
  <strong>💬 Ask me anything:</strong> I can understand natural language and automatically fetch financial data when needed!
837
  </div>
 
35
  session = create_session_with_retry()
36
 
37
  # 初始化 Hugging Face Inference Client
38
+ # 使用 Qwen/Qwen2.5-VL-72B-Instruct:nebius 模型(更强大的视觉语言模型)
39
  try:
40
+ # Hugging Face Space 环境变量读取 token
41
+ # HF Space 设置中配置 HF_TOKEN secret
42
+ hf_token = os.environ.get("HF_TOKEN")
43
+ if hf_token:
44
+ client = InferenceClient(api_key=hf_token)
45
+ print(f"✅ Hugging Face client initialized with Qwen/Qwen2.5-VL-72B-Instruct:nebius model")
46
+ else:
47
+ print("⚠️ HF_TOKEN not found in environment variables")
48
+ print("ℹ️ Please add HF_TOKEN in your Hugging Face Space Settings > Repository secrets")
49
+ client = None
50
  except Exception as e:
51
  print(f"❌ Warning: Failed to initialize Hugging Face client: {e}")
52
  client = None
 
625
  # 构建对话历史
626
  messages = []
627
 
628
+ # 系统提示词 - 自由智能的财报分析助手
629
+ system_prompt = """You are an intelligent financial analysis assistant with access to real-time SEC EDGAR data.
630
 
631
+ You have 3 powerful tools to fetch financial data:
632
+ 1. advanced_search_company(company_input) - Search for any US company
633
+ 2. get_latest_financial_data(cik) - Get latest financial report
634
+ 3. extract_financial_metrics(cik, years) - Get multi-year trends (3 or 5 years)
 
635
 
636
+ When users ask about a company:
637
+ - Automatically use tools to fetch the data they need
638
+ - Analyze the numbers and provide insights
639
+ - Explain trends, growth rates, and what they mean
640
+ - Be conversational and helpful
641
 
642
+ You can handle any financial question - from simple data queries to complex multi-company comparisons. Be creative and thorough in your analysis."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
643
 
644
  messages.append({"role": "system", "content": system_prompt})
645
 
 
678
 
679
  response = client.chat_completion(
680
  messages=messages,
681
+ model="Qwen/Qwen2.5-VL-72B-Instruct:nebius", # 使用更强大的视觉语言模型
682
  tools=MCP_TOOLS,
683
+ max_tokens=3000,
684
+ temperature=0.7,
685
+ tool_choice="auto" # 让模型自动决定是否使用工具
686
  )
687
 
688
  print(f"✅ LLM response received (iteration {iteration})")
 
729
  break
730
 
731
  except ValueError as ve:
 
732
  error_msg = str(ve)
733
+ print(f"⚠️ LLM API error: {ve}")
734
  if "api_key" in error_msg.lower() or "token" in error_msg.lower():
 
735
  print("ℹ️ Falling back to simple response logic")
736
  return fallback_chatbot_response(message)
737
  else:
738
  raise
739
  except Exception as e:
740
+ print(f"⚠️ LLM API error: {type(e).__name__} - {e}")
741
+ # 如果是工具调用相关错误,尝试不使用工具重新请求
742
+ if "tool" in str(e).lower() or "function" in str(e).lower():
743
+ print("🔄 Tool calling failed, retrying without tools...")
744
+ try:
745
+ response = client.chat_completion(
746
+ messages=messages,
747
+ model="Qwen/Qwen2.5-VL-72B-Instruct:nebius",
748
+ max_tokens=3000,
749
+ temperature=0.7
750
+ )
751
+ response_text = response.choices[0].message.content
752
+ return response_text
753
+ except:
754
+ pass
755
  print("ℹ️ Falling back to simple response logic")
756
  return fallback_chatbot_response(message)
757
 
758
  # 构建最终响应
759
  final_response = ""
760
 
761
+ # 显示调试信息:使用的模型
762
+ final_response += f"<div style='padding: 8px; background: #e3f2fd; border-left: 3px solid #2196f3; margin-bottom: 10px; font-size: 0.9em;'>🤖 <strong>Model:</strong> Qwen/Qwen2.5-VL-72B-Instruct:nebius | <strong>Iterations:</strong> {iteration}</div>\n\n"
763
+
764
  # 如果有工具调用,显示调用日志
765
  if tool_calls_log:
766
  final_response += "**🛠️ MCP Tools Used:**\n\n"
 
778
 
779
  def fallback_chatbot_response(message):
780
  """退回策略:当 LLM API 不可用时使用的简单逻辑"""
781
+ # 显示警告信息
782
+ fallback_warning = "<div style='padding: 10px; background: #fff3cd; border-left: 4px solid #ffc107; margin-bottom: 15px;'>⚠️ <strong>Notice:</strong> LLM API unavailable, using fallback logic.</div>\n\n"
783
+
784
  # 检查是否是财务查询相关问题
785
  financial_keywords = ['financial', 'revenue', 'income', 'earnings', 'cash flow', 'expenses', '财务', '收入', '利润', 'data', 'trend', 'performance']
786
 
 
812
 
813
  # 调用财务查询函数
814
  result = query_financial_data(detected_company, query_type)
815
+ return fallback_warning + f"Here's the financial information for {detected_company}:\n\n{result}"
816
  else:
817
+ return fallback_warning + "I can help you query financial data! Please specify a company name. For example: 'Show me Apple's latest financial data' or 'What's NVIDIA's 3-year trend?' \n\nSupported companies include: Apple, Microsoft, NVIDIA, Tesla, Alibaba, Google, Amazon, and more."
818
 
819
  # 如果不是财务查询,返回通用回复
820
+ return fallback_warning + "Hello! I'm a financial data assistant powered by SEC EDGAR data. I can help you query financial information for US listed companies.\n\n**What I can do:**\n- Get latest financial data (revenue, income, EPS, etc.)\n- Show 3-year or 5-year financial trends\n- Provide detailed financial metrics\n\n**Try asking:**\n- 'Show me Apple's latest financial data'\n- 'What's NVIDIA's 3-year financial trend?'\n- 'How is Microsoft performing financially?'"
821
 
822
  # 包装函数,显示加载状态
823
  def query_with_status(company, query_type):
 
843
  # 显示 AI 功能说明
844
  gr.Markdown("""
845
  <div style='padding: 15px; background: #d4edda; border-left: 4px solid #28a745; margin: 10px 0; border-radius: 4px;'>
846
+ <strong>✅ AI Assistant Enabled:</strong> Powered by Qwen/Qwen2.5-VL-72B-Instruct:nebius model with automatic MCP tool calling.
847
  <br>
848
  <strong>💬 Ask me anything:</strong> I can understand natural language and automatically fetch financial data when needed!
849
  </div>