JC321 commited on
Commit
05bacf6
·
verified ·
1 Parent(s): 3d83d1b

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +36 -14
app.py CHANGED
@@ -3,6 +3,12 @@ import requests
3
 
4
  MCP_URL = "https://jc321-easyreportdatemcp.hf.space"
5
 
 
 
 
 
 
 
6
  def query_financial_data(company_name, query_type):
7
  """查询财务数据的主函数"""
8
 
@@ -10,10 +16,12 @@ def query_financial_data(company_name, query_type):
10
  return "请输入公司名称或股票代码"
11
 
12
  try:
13
- # 搜索公司
 
14
  search_resp = requests.post(
15
- f"{MCP_URL}/api/advanced_search",
16
- json={"company_input": company_name},
 
17
  timeout=30
18
  )
19
 
@@ -22,7 +30,12 @@ def query_financial_data(company_name, query_type):
22
  return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
23
 
24
  try:
25
- company = search_resp.json()
 
 
 
 
 
26
  except ValueError as e:
27
  return f"❌ JSON Parse Error: {str(e)}\n\nResponse Text: {search_resp.text[:500]}"
28
 
@@ -38,8 +51,9 @@ def query_financial_data(company_name, query_type):
38
  # 根据查询类型获取数据
39
  if query_type == "最新财务数据":
40
  data_resp = requests.post(
41
- f"{MCP_URL}/api/get_latest_financial_data",
42
- json={"cik": cik},
 
43
  timeout=30
44
  )
45
 
@@ -47,7 +61,8 @@ def query_financial_data(company_name, query_type):
47
  return result + f"❌ Server Error: HTTP {data_resp.status_code}\n\n{data_resp.text[:500]}"
48
 
49
  try:
50
- data = data_resp.json()
 
51
  except ValueError as e:
52
  return result + f"❌ JSON Parse Error: {str(e)}\n\n{data_resp.text[:500]}"
53
 
@@ -62,8 +77,9 @@ def query_financial_data(company_name, query_type):
62
 
63
  elif query_type == "3年趋势":
64
  metrics_resp = requests.post(
65
- f"{MCP_URL}/api/extract_financial_metrics",
66
- json={"cik": cik, "years": 3},
 
67
  timeout=60
68
  )
69
 
@@ -71,7 +87,8 @@ def query_financial_data(company_name, query_type):
71
  return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
72
 
73
  try:
74
- metrics = metrics_resp.json()
 
75
  except ValueError as e:
76
  return result + f"❌ JSON Parse Error: {str(e)}\n\n{metrics_resp.text[:500]}"
77
 
@@ -95,8 +112,9 @@ def query_financial_data(company_name, query_type):
95
 
96
  elif query_type == "5年趋势":
97
  metrics_resp = requests.post(
98
- f"{MCP_URL}/api/extract_financial_metrics",
99
- json={"cik": cik, "years": 5},
 
100
  timeout=60
101
  )
102
 
@@ -104,7 +122,8 @@ def query_financial_data(company_name, query_type):
104
  return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
105
 
106
  try:
107
- metrics = metrics_resp.json()
 
108
  except ValueError as e:
109
  return result + f"❌ JSON Parse Error: {str(e)}\n\n{metrics_resp.text[:500]}"
110
 
@@ -125,8 +144,11 @@ def query_financial_data(company_name, query_type):
125
 
126
  return result
127
 
 
 
128
  except Exception as e:
129
- return f"❌ 发生错误: {str(e)}"
 
130
 
131
  # 创建 Gradio 界面
132
  with gr.Blocks(title="SEC Financial Data Query Assistant") as demo:
 
3
 
4
  MCP_URL = "https://jc321-easyreportdatemcp.hf.space"
5
 
6
+ # 设置请求头
7
+ HEADERS = {
8
+ "Content-Type": "application/json",
9
+ "User-Agent": "SEC-Query-Assistant/1.0 (jtyxabc@gmail.com)"
10
+ }
11
+
12
  def query_financial_data(company_name, query_type):
13
  """查询财务数据的主函数"""
14
 
 
16
  return "请输入公司名称或股票代码"
17
 
18
  try:
19
+ # Gradio Space API 调用方式
20
+ # 先搜索公司
21
  search_resp = requests.post(
22
+ f"{MCP_URL}/call/advanced_search",
23
+ json={"data": [company_name]},
24
+ headers=HEADERS,
25
  timeout=30
26
  )
27
 
 
30
  return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
31
 
32
  try:
33
+ resp_data = search_resp.json()
34
+ # Gradio API 返回格式: {"data": [result]}
35
+ if isinstance(resp_data, dict) and "data" in resp_data:
36
+ company = resp_data["data"][0] if resp_data["data"] else {}
37
+ else:
38
+ company = resp_data
39
  except ValueError as e:
40
  return f"❌ JSON Parse Error: {str(e)}\n\nResponse Text: {search_resp.text[:500]}"
41
 
 
51
  # 根据查询类型获取数据
52
  if query_type == "最新财务数据":
53
  data_resp = requests.post(
54
+ f"{MCP_URL}/call/get_latest_financial_data",
55
+ json={"data": [cik]},
56
+ headers=HEADERS,
57
  timeout=30
58
  )
59
 
 
61
  return result + f"❌ Server Error: HTTP {data_resp.status_code}\n\n{data_resp.text[:500]}"
62
 
63
  try:
64
+ resp_data = data_resp.json()
65
+ data = resp_data["data"][0] if isinstance(resp_data, dict) and "data" in resp_data else resp_data
66
  except ValueError as e:
67
  return result + f"❌ JSON Parse Error: {str(e)}\n\n{data_resp.text[:500]}"
68
 
 
77
 
78
  elif query_type == "3年趋势":
79
  metrics_resp = requests.post(
80
+ f"{MCP_URL}/call/extract_financial_metrics",
81
+ json={"data": [cik, 3]},
82
+ headers=HEADERS,
83
  timeout=60
84
  )
85
 
 
87
  return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
88
 
89
  try:
90
+ resp_data = metrics_resp.json()
91
+ metrics = resp_data["data"][0] if isinstance(resp_data, dict) and "data" in resp_data else resp_data
92
  except ValueError as e:
93
  return result + f"❌ JSON Parse Error: {str(e)}\n\n{metrics_resp.text[:500]}"
94
 
 
112
 
113
  elif query_type == "5年趋势":
114
  metrics_resp = requests.post(
115
+ f"{MCP_URL}/call/extract_financial_metrics",
116
+ json={"data": [cik, 5]},
117
+ headers=HEADERS,
118
  timeout=60
119
  )
120
 
 
122
  return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
123
 
124
  try:
125
+ resp_data = metrics_resp.json()
126
+ metrics = resp_data["data"][0] if isinstance(resp_data, dict) and "data" in resp_data else resp_data
127
  except ValueError as e:
128
  return result + f"❌ JSON Parse Error: {str(e)}\n\n{metrics_resp.text[:500]}"
129
 
 
144
 
145
  return result
146
 
147
+ except requests.exceptions.RequestException as e:
148
+ return f"❌ Network Error: {str(e)}\n\nMCP Server: {MCP_URL}"
149
  except Exception as e:
150
+ import traceback
151
+ return f"❌ Unexpected Error: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
152
 
153
  # 创建 Gradio 界面
154
  with gr.Blocks(title="SEC Financial Data Query Assistant") as demo: