JC321 commited on
Commit
4eb508b
·
verified ·
1 Parent(s): 2e8d15f

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +39 -39
  2. app.py +100 -84
  3. requirements.txt +1 -1
README.md CHANGED
@@ -1,39 +1,39 @@
1
- ---
2
- title: SEC Financial Data Query Assistant
3
- emoji: 📊
4
- colorFrom: blue
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 6.0.1
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- ---
12
-
13
- # SEC Financial Data Query Assistant
14
-
15
- A Gradio-based web application for querying SEC financial data through MCP Server.
16
-
17
- ## Features
18
-
19
- - 🔍 Search companies by name or ticker symbol
20
- - 📈 View latest financial data
21
- - 📊 Analyze 3-year and 5-year financial trends
22
- - 💰 Display revenue, net income, EPS, operating expenses, and cash flow metrics
23
-
24
- ## Usage
25
-
26
- Simply enter a company name or ticker symbol (e.g., NVIDIA, AAPL, Microsoft) and select the query type:
27
- - **Latest Financial Data**: Shows the most recent fiscal year data
28
- - **3-Year Trend**: Displays financial trends over 3 years
29
- - **5-Year Trend**: Displays financial trends over 5 years
30
-
31
- ## Data Source
32
-
33
- SEC EDGAR data via MCP Server: https://jc321-easyreportdatemcp.hf.space
34
-
35
- ## Technology Stack
36
-
37
- - **Frontend**: Gradio 4.0+
38
- - **Backend**: Python with requests
39
- - **Data Source**: SEC EDGAR via MCP Server
 
1
+ ---
2
+ title: SEC Financial Data Query Assistant
3
+ emoji: 📊
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 4.0.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ # SEC Financial Data Query Assistant
14
+
15
+ A Gradio-based web application for querying SEC financial data through MCP Server.
16
+
17
+ ## Features
18
+
19
+ - 🔍 Search companies by name or ticker symbol
20
+ - 📈 View latest financial data
21
+ - 📊 Analyze 3-year and 5-year financial trends
22
+ - 💰 Display revenue, net income, EPS, operating expenses, and cash flow metrics
23
+
24
+ ## Usage
25
+
26
+ Simply enter a company name or ticker symbol (e.g., NVIDIA, AAPL, Microsoft) and select the query type:
27
+ - **Latest Financial Data**: Shows the most recent fiscal year data
28
+ - **3-Year Trend**: Displays financial trends over 3 years
29
+ - **5-Year Trend**: Displays financial trends over 5 years
30
+
31
+ ## Data Source
32
+
33
+ SEC EDGAR data via MCP Server: https://jc321-easyreportdatemcp.hf.space
34
+
35
+ ## Technology Stack
36
+
37
+ - **Frontend**: Gradio 4.0+
38
+ - **Backend**: Python with requests
39
+ - **Data Source**: SEC EDGAR via MCP Server
app.py CHANGED
@@ -22,6 +22,37 @@ def format_value(value, is_money=True):
22
  else:
23
  return f"{value:.2f}"
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  # 创建超链接
26
  def create_source_link(source_form, cik=None):
27
  """为Source Form创建SEC EDGAR超链接"""
@@ -207,20 +238,28 @@ def chatbot_response(message, history):
207
  """聊天机器人响应函数,集成MCP工具"""
208
  try:
209
  # 检查是否是财务查询相关问题
210
- if any(keyword in message.lower() for keyword in ['financial', 'revenue', 'income', 'earnings', 'cash flow', 'expenses', '财务', '收入', '利润']):
 
 
211
  # 提取公司名称和查询类型
212
- company_keywords = ['apple', 'microsoft', 'nvidia', 'tesla', 'alibaba', 'google', 'amazon']
213
  detected_company = None
214
 
215
  for company in company_keywords:
216
  if company in message.lower():
217
- detected_company = company.capitalize()
 
 
 
 
 
 
218
  break
219
 
220
  if detected_company:
221
  # 根据问题内容选择查询类型
222
- if 'trend' in message.lower() or '趋势' in message:
223
- if '5' in message or 'five' in message:
224
  query_type = '5-Year Trends'
225
  else:
226
  query_type = '3-Year Trends'
@@ -229,98 +268,75 @@ def chatbot_response(message, history):
229
 
230
  # 调用财务查询函数
231
  result = query_financial_data(detected_company, query_type)
232
- return f"I found financial information for {detected_company}:\n\n{result}"
 
 
233
 
234
  # 如果不是财务查询,返回通用回复
235
- return "Hello! I'm a financial data assistant. I can help you query SEC financial data for US listed companies. Try asking about companies like Apple, Microsoft, NVIDIA, Tesla, or Alibaba. For example: 'Show me Apple's latest financial data' or 'What's NVIDIA's 3-year trend?'"
236
 
237
  except Exception as e:
238
- return f"Sorry, I encountered an error: {str(e)}. Please try asking about financial data for specific companies."
239
 
240
  # 创建 Gradio 界面
241
  with gr.Blocks(title="SEC Financial Data Query Assistant") as demo:
242
  gr.Markdown("# 🤖 SEC Financial Data Query Assistant")
243
  gr.Markdown("Query SEC financial data for US listed companies through MCP Server")
244
 
245
- with gr.Row():
246
- # 左侧:聊天机器人
247
- with gr.Column(scale=1):
248
- gr.Markdown("## 💬 AI Financial Assistant")
249
- gr.Markdown("*Ask me about financial data for any US listed company!*")
250
- chatbot = gr.Chatbot(
251
- height=500,
252
- label="Chat with AI Assistant"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
  )
254
- chat_input = gr.Textbox(
255
- placeholder="e.g., 'Show me Apple's latest financial data' or 'What's NVIDIA's 3-year trend?'",
256
- label="Your Question",
257
- lines=2
 
258
  )
259
- chat_submit = gr.Button("🗨️ Send", variant="primary")
260
 
261
- # 右侧:原有的查询界面
262
- with gr.Column(scale=1):
263
- gr.Markdown("## 🔍 Direct Query")
264
- with gr.Row():
265
- company_input = gr.Textbox(
266
- label="Company Name or Stock Symbol",
267
- placeholder="e.g., NVIDIA, Apple, Alibaba, AAPL",
268
- scale=2
269
- )
270
- query_type = gr.Radio(
271
- ["Latest Financial Data", "3-Year Trends", "5-Year Trends"],
272
- label="Query Type",
273
- value="Latest Financial Data",
274
- scale=1
275
- )
276
-
277
- submit_btn = gr.Button("🔍 Query", variant="primary", size="lg")
278
- output = gr.Markdown(label="Query Results")
279
-
280
- # Chatbot 事件处理
281
- def clear_input():
282
- return ""
283
-
284
- chat_submit.click(
285
- fn=chatbot_response,
286
- inputs=[chat_input, chatbot],
287
- outputs=chatbot
288
- )
289
- chat_submit.click(
290
- fn=clear_input,
291
- outputs=chat_input
292
- )
293
-
294
- chat_input.submit(
295
- fn=chatbot_response,
296
- inputs=[chat_input, chatbot],
297
- outputs=chatbot
298
- )
299
- chat_input.submit(
300
- fn=clear_input,
301
- outputs=chat_input
302
- )
303
-
304
- # 示例
305
- gr.Examples(
306
- examples=[
307
- ["NVIDIA", "Latest Financial Data"],
308
- ["Apple", "3-Year Trends"],
309
- ["Microsoft", "5-Year Trends"],
310
- ["Alibaba", "Latest Financial Data"],
311
- ["Tesla", "3-Year Trends"]
312
- ],
313
- inputs=[company_input, query_type],
314
- outputs=output,
315
- fn=query_financial_data,
316
- cache_examples=False
317
- )
318
-
319
- submit_btn.click(
320
- fn=query_financial_data,
321
- inputs=[company_input, query_type],
322
- outputs=output
323
- )
324
 
325
  gr.Markdown("---")
326
  gr.Markdown(f"**Data Source**: SEC EDGAR | **MCP Server**: `{MCP_URL}`")
 
22
  else:
23
  return f"{value:.2f}"
24
 
25
+ # MCP 工具定义
26
+ def create_mcp_tools():
27
+ """创建 MCP 工具列表"""
28
+ return [
29
+ {
30
+ "name": "query_financial_data",
31
+ "description": "Query SEC financial data for US listed companies",
32
+ "parameters": {
33
+ "type": "object",
34
+ "properties": {
35
+ "company_name": {
36
+ "type": "string",
37
+ "description": "Company name or stock symbol (e.g., Apple, NVIDIA, AAPL)"
38
+ },
39
+ "query_type": {
40
+ "type": "string",
41
+ "enum": ["Latest Financial Data", "3-Year Trends", "5-Year Trends"],
42
+ "description": "Type of financial query"
43
+ }
44
+ },
45
+ "required": ["company_name", "query_type"]
46
+ }
47
+ }
48
+ ]
49
+
50
+ # 工具执行函数
51
+ def execute_tool(tool_name, **kwargs):
52
+ """执行 MCP 工具"""
53
+ if tool_name == "query_financial_data":
54
+ return query_financial_data(kwargs.get("company_name"), kwargs.get("query_type"))
55
+ return f"Unknown tool: {tool_name}"
56
  # 创建超链接
57
  def create_source_link(source_form, cik=None):
58
  """为Source Form创建SEC EDGAR超链接"""
 
238
  """聊天机器人响应函数,集成MCP工具"""
239
  try:
240
  # 检查是否是财务查询相关问题
241
+ financial_keywords = ['financial', 'revenue', 'income', 'earnings', 'cash flow', 'expenses', '财务', '收入', '利润', 'data', 'trend', 'performance']
242
+
243
+ if any(keyword in message.lower() for keyword in financial_keywords):
244
  # 提取公司名称和查询类型
245
+ company_keywords = ['apple', 'microsoft', 'nvidia', 'tesla', 'alibaba', 'google', 'amazon', 'meta', 'tsla', 'aapl', 'msft', 'nvda', 'googl', 'amzn']
246
  detected_company = None
247
 
248
  for company in company_keywords:
249
  if company in message.lower():
250
+ if company in ['aapl']: detected_company = 'Apple'
251
+ elif company in ['msft']: detected_company = 'Microsoft'
252
+ elif company in ['nvda']: detected_company = 'NVIDIA'
253
+ elif company in ['tsla']: detected_company = 'Tesla'
254
+ elif company in ['googl']: detected_company = 'Google'
255
+ elif company in ['amzn']: detected_company = 'Amazon'
256
+ else: detected_company = company.capitalize()
257
  break
258
 
259
  if detected_company:
260
  # 根据问题内容选择查询类型
261
+ if any(word in message.lower() for word in ['trend', '趋势', 'history', 'historical', 'over time']):
262
+ if any(word in message for word in ['5', 'five', '五年']):
263
  query_type = '5-Year Trends'
264
  else:
265
  query_type = '3-Year Trends'
 
268
 
269
  # 调用财务查询函数
270
  result = query_financial_data(detected_company, query_type)
271
+ return f"Here's the financial information for {detected_company}:\n\n{result}"
272
+ else:
273
+ 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."
274
 
275
  # 如果不是财务查询,返回通用回复
276
+ 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?'"
277
 
278
  except Exception as e:
279
+ return f"Sorry, I encountered an error: {str(e)}. Please try asking about financial data for specific companies like Apple, Microsoft, NVIDIA, Tesla, etc."
280
 
281
  # 创建 Gradio 界面
282
  with gr.Blocks(title="SEC Financial Data Query Assistant") as demo:
283
  gr.Markdown("# 🤖 SEC Financial Data Query Assistant")
284
  gr.Markdown("Query SEC financial data for US listed companies through MCP Server")
285
 
286
+ with gr.Tab("AI Assistant"):
287
+ # 使用 Gradio 6.0.1 的 ChatInterface 和 MCP 支持
288
+ chat = gr.ChatInterface(
289
+ fn=chatbot_response,
290
+ title="💬 Financial AI Assistant",
291
+ description="Ask me about financial data for any US listed company! I can help you query latest data or analyze trends.",
292
+ examples=[
293
+ "Show me Apple's latest financial data",
294
+ "What's NVIDIA's 3-year financial trend?",
295
+ "Get Microsoft's 5-year financial trends",
296
+ "How is Tesla performing financially?"
297
+ ],
298
+ cache_examples=False
299
+ )
300
+
301
+ with gr.Tab("Direct Query"):
302
+ gr.Markdown("## 🔍 Direct Financial Data Query")
303
+
304
+ with gr.Row():
305
+ company_input = gr.Textbox(
306
+ label="Company Name or Stock Symbol",
307
+ placeholder="e.g., NVIDIA, Apple, Alibaba, AAPL",
308
+ scale=2
309
  )
310
+ query_type = gr.Radio(
311
+ ["Latest Financial Data", "3-Year Trends", "5-Year Trends"],
312
+ label="Query Type",
313
+ value="Latest Financial Data",
314
+ scale=1
315
  )
 
316
 
317
+ submit_btn = gr.Button("🔍 Query", variant="primary", size="lg")
318
+ output = gr.Markdown(label="Query Results")
319
+
320
+ # 示例
321
+ gr.Examples(
322
+ examples=[
323
+ ["NVIDIA", "Latest Financial Data"],
324
+ ["Apple", "3-Year Trends"],
325
+ ["Microsoft", "5-Year Trends"],
326
+ ["Alibaba", "Latest Financial Data"],
327
+ ["Tesla", "3-Year Trends"]
328
+ ],
329
+ inputs=[company_input, query_type],
330
+ outputs=output,
331
+ fn=query_financial_data,
332
+ cache_examples=False
333
+ )
334
+
335
+ submit_btn.click(
336
+ fn=query_financial_data,
337
+ inputs=[company_input, query_type],
338
+ outputs=output
339
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
 
341
  gr.Markdown("---")
342
  gr.Markdown(f"**Data Source**: SEC EDGAR | **MCP Server**: `{MCP_URL}`")
requirements.txt CHANGED
@@ -1,2 +1,2 @@
1
- gradio>=4.44.0
2
  requests
 
1
+ gradio[mcp,oauth]==6.0.1
2
  requests