JC321 commited on
Commit
b3daa10
·
verified ·
1 Parent(s): ec3719f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -54,14 +54,17 @@ def execute_tool(tool_name, **kwargs):
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超链接"""
59
- if not source_form or source_form == 'N/A' or not cik:
60
  return source_form
61
 
62
- # 构建SEC EDGAR链接(这是一个示例,实际链接需要更多参数)
63
- edgar_url = f"https://www.sec.gov/edgar/browse/?CIK={cik}"
64
- return f"[{source_form}]({edgar_url})"
 
 
 
65
 
66
  def query_financial_data(company_name, query_type):
67
  """查询财务数据的主函数"""
@@ -139,7 +142,10 @@ def query_financial_data(company_name, query_type):
139
  result += f"- **Earnings Per Share**: {format_value(eps, False)}\n"
140
  result += f"- **Operating Expenses**: {format_value(opex)}\n"
141
  result += f"- **Operating Cash Flow**: {format_value(ocf)}\n"
142
- result += f"- **Source Form**: {create_source_link(data.get('source_form', 'N/A'), cik)}\n"
 
 
 
143
 
144
  elif internal_query_type == "3年趋势":
145
  metrics_resp = requests.post(
@@ -165,6 +171,9 @@ def query_financial_data(company_name, query_type):
165
  # 显示所有数据(包括年度和季度)
166
  all_data = metrics.get('metrics', [])
167
 
 
 
 
168
  result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
169
  result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
170
 
@@ -176,11 +185,11 @@ def query_financial_data(company_name, query_type):
176
  opex = (m.get('operating_expenses') or 0) / 1e9
177
  ocf = (m.get('operating_cash_flow') or 0) / 1e9
178
  source_form = m.get('source_form', 'N/A')
179
- cik_val = m.get('cik') or cik
180
 
181
  # 区分年度和季度
182
  period_prefix = "FY" if 'Q' not in period else ""
183
- source_link = create_source_link(source_form, cik_val)
184
 
185
  result += f"| {period_prefix}{period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, False)} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
186
 
@@ -206,6 +215,9 @@ def query_financial_data(company_name, query_type):
206
  # 显示所有数据(包括年度和季度)
207
  all_data = metrics.get('metrics', [])
208
 
 
 
 
209
  result += f"## 5-Year Financial Trends ({metrics.get('count', 0)} periods)\n\n"
210
  result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
211
  result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
@@ -218,11 +230,11 @@ def query_financial_data(company_name, query_type):
218
  opex = (m.get('operating_expenses') or 0) / 1e9
219
  ocf = (m.get('operating_cash_flow') or 0) / 1e9
220
  source_form = m.get('source_form', 'N/A')
221
- cik_val = m.get('cik') or cik
222
 
223
  # 区分年度和季度
224
  period_prefix = "FY" if 'Q' not in period else ""
225
- source_link = create_source_link(source_form, cik_val)
226
 
227
  result += f"| {period_prefix}{period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, False)} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
228
 
@@ -257,14 +269,12 @@ def query_financial_data(company_name, query_type):
257
  filing_date = filing.get('filing_date', 'N/A')
258
  accession_num = filing.get('accession_number', 'N/A')
259
  primary_doc = filing.get('primary_document', 'N/A')
 
260
 
261
- # 创建 SEC EDGAR 原始文档链接
262
- if accession_num != 'N/A' and primary_doc != 'N/A':
263
- # 移除 accession_number 中的连字符以匹配 SEC URL 格式
264
- acc_no_clean = accession_num.replace('-', '')
265
- doc_url = f"https://www.sec.gov/Archives/edgar/data/{cik}/{acc_no_clean}/{primary_doc}"
266
- form_link = f"[{form_type}]({doc_url})"
267
- primary_doc_link = f"[{primary_doc}]({doc_url})"
268
  else:
269
  form_link = form_type
270
  primary_doc_link = primary_doc
 
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, source_url=None):
58
+ """为Source Form创建超链接,使用MCP后端返回的URL"""
59
+ if not source_form or source_form == 'N/A':
60
  return source_form
61
 
62
+ # 如果后端提供了URL,使用后端的URL
63
+ if source_url and source_url != 'N/A':
64
+ return f"[{source_form}]({source_url})"
65
+
66
+ # 如果没有URL,只显示文本
67
+ return source_form
68
 
69
  def query_financial_data(company_name, query_type):
70
  """查询财务数据的主函数"""
 
142
  result += f"- **Earnings Per Share**: {format_value(eps, False)}\n"
143
  result += f"- **Operating Expenses**: {format_value(opex)}\n"
144
  result += f"- **Operating Cash Flow**: {format_value(ocf)}\n"
145
+ # 使用后端返回的 source_url
146
+ source_form = data.get('source_form', 'N/A')
147
+ source_url = data.get('source_url', None) # 从后端获取URL
148
+ result += f"- **Source Form**: {create_source_link(source_form, source_url)}\n"
149
 
150
  elif internal_query_type == "3年趋势":
151
  metrics_resp = requests.post(
 
171
  # 显示所有数据(包括年度和季度)
172
  all_data = metrics.get('metrics', [])
173
 
174
+ # 按期间降序排序,确保显示最近的3年数据
175
+ all_data = sorted(all_data, key=lambda x: x.get('period', '0000'), reverse=True)
176
+
177
  result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
178
  result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
179
 
 
185
  opex = (m.get('operating_expenses') or 0) / 1e9
186
  ocf = (m.get('operating_cash_flow') or 0) / 1e9
187
  source_form = m.get('source_form', 'N/A')
188
+ source_url = m.get('source_url', None) # 从后端获取URL
189
 
190
  # 区分年度和季度
191
  period_prefix = "FY" if 'Q' not in period else ""
192
+ source_link = create_source_link(source_form, source_url)
193
 
194
  result += f"| {period_prefix}{period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, False)} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
195
 
 
215
  # 显示所有数据(包括年度和季度)
216
  all_data = metrics.get('metrics', [])
217
 
218
+ # 按期间降序排序,确保显示最近的5年数据
219
+ all_data = sorted(all_data, key=lambda x: x.get('period', '0000'), reverse=True)
220
+
221
  result += f"## 5-Year Financial Trends ({metrics.get('count', 0)} periods)\n\n"
222
  result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
223
  result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
 
230
  opex = (m.get('operating_expenses') or 0) / 1e9
231
  ocf = (m.get('operating_cash_flow') or 0) / 1e9
232
  source_form = m.get('source_form', 'N/A')
233
+ source_url = m.get('source_url', None) # 从后端获取URL
234
 
235
  # 区分年度和季度
236
  period_prefix = "FY" if 'Q' not in period else ""
237
+ source_link = create_source_link(source_form, source_url)
238
 
239
  result += f"| {period_prefix}{period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, False)} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
240
 
 
269
  filing_date = filing.get('filing_date', 'N/A')
270
  accession_num = filing.get('accession_number', 'N/A')
271
  primary_doc = filing.get('primary_document', 'N/A')
272
+ filing_url = filing.get('filing_url', None) # 从后端获取URL
273
 
274
+ # 使用后端返回的URL创建链接
275
+ if filing_url and filing_url != 'N/A':
276
+ form_link = f"[{form_type}]({filing_url})"
277
+ primary_doc_link = f"[{primary_doc}]({filing_url})"
 
 
 
278
  else:
279
  form_link = form_type
280
  primary_doc_link = primary_doc