Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -18,14 +18,11 @@ def query_financial_data(company_name, query_type):
|
|
| 18 |
return "请输入公司名称或股票代码"
|
| 19 |
|
| 20 |
try:
|
| 21 |
-
#
|
| 22 |
# 先搜索公司
|
| 23 |
search_resp = requests.post(
|
| 24 |
-
f"{MCP_URL}/api/
|
| 25 |
-
json={
|
| 26 |
-
"data": [company_name],
|
| 27 |
-
"fn_index": 0 # advanced_search 函数索引
|
| 28 |
-
},
|
| 29 |
headers=HEADERS,
|
| 30 |
timeout=30
|
| 31 |
)
|
|
@@ -34,12 +31,7 @@ def query_financial_data(company_name, query_type):
|
|
| 34 |
return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
|
| 38 |
-
# Gradio API 返回格式: {"data": [result]}
|
| 39 |
-
if "data" in result_data and result_data["data"]:
|
| 40 |
-
company = result_data["data"][0]
|
| 41 |
-
else:
|
| 42 |
-
return f"❌ 无法解析响应: {result_data}"
|
| 43 |
except (ValueError, KeyError) as e:
|
| 44 |
return f"❌ JSON Parse Error: {str(e)}\n\nResponse: {search_resp.text[:500]}"
|
| 45 |
|
|
@@ -55,11 +47,8 @@ def query_financial_data(company_name, query_type):
|
|
| 55 |
# 根据查询类型获取数据
|
| 56 |
if query_type == "最新财务数据":
|
| 57 |
data_resp = requests.post(
|
| 58 |
-
f"{MCP_URL}/api/
|
| 59 |
-
json={
|
| 60 |
-
"data": [cik],
|
| 61 |
-
"fn_index": 1 # get_latest_financial_data 函数索引
|
| 62 |
-
},
|
| 63 |
headers=HEADERS,
|
| 64 |
timeout=30
|
| 65 |
)
|
|
@@ -68,8 +57,7 @@ def query_financial_data(company_name, query_type):
|
|
| 68 |
return result + f"❌ Server Error: HTTP {data_resp.status_code}\n\n{data_resp.text[:500]}"
|
| 69 |
|
| 70 |
try:
|
| 71 |
-
|
| 72 |
-
data = data_result["data"][0] if "data" in data_result else data_result
|
| 73 |
except (ValueError, KeyError) as e:
|
| 74 |
return result + f"❌ JSON Parse Error: {str(e)}\n\n{data_resp.text[:500]}"
|
| 75 |
|
|
@@ -84,11 +72,8 @@ def query_financial_data(company_name, query_type):
|
|
| 84 |
|
| 85 |
elif query_type == "3年趋势":
|
| 86 |
metrics_resp = requests.post(
|
| 87 |
-
f"{MCP_URL}/api/
|
| 88 |
-
json={
|
| 89 |
-
"data": [cik, 3],
|
| 90 |
-
"fn_index": 2 # extract_financial_metrics 函数索引
|
| 91 |
-
},
|
| 92 |
headers=HEADERS,
|
| 93 |
timeout=60
|
| 94 |
)
|
|
@@ -97,8 +82,7 @@ def query_financial_data(company_name, query_type):
|
|
| 97 |
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 98 |
|
| 99 |
try:
|
| 100 |
-
|
| 101 |
-
metrics = metrics_result["data"][0] if "data" in metrics_result else metrics_result
|
| 102 |
except (ValueError, KeyError) as e:
|
| 103 |
return result + f"❌ JSON Parse Error: {str(e)}\n\n{metrics_resp.text[:500]}"
|
| 104 |
|
|
@@ -122,11 +106,8 @@ def query_financial_data(company_name, query_type):
|
|
| 122 |
|
| 123 |
elif query_type == "5年趋势":
|
| 124 |
metrics_resp = requests.post(
|
| 125 |
-
f"{MCP_URL}/api/
|
| 126 |
-
json={
|
| 127 |
-
"data": [cik, 5],
|
| 128 |
-
"fn_index": 2 # extract_financial_metrics 函数索引
|
| 129 |
-
},
|
| 130 |
headers=HEADERS,
|
| 131 |
timeout=60
|
| 132 |
)
|
|
@@ -135,8 +116,7 @@ def query_financial_data(company_name, query_type):
|
|
| 135 |
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 136 |
|
| 137 |
try:
|
| 138 |
-
|
| 139 |
-
metrics = metrics_result["data"][0] if "data" in metrics_result else metrics_result
|
| 140 |
except (ValueError, KeyError) as e:
|
| 141 |
return result + f"❌ JSON Parse Error: {str(e)}\n\n{metrics_resp.text[:500]}"
|
| 142 |
|
|
|
|
| 18 |
return "请输入公司名称或股票代码"
|
| 19 |
|
| 20 |
try:
|
| 21 |
+
# 直接调用 FastAPI 的 REST API 端点
|
| 22 |
# 先搜索公司
|
| 23 |
search_resp = requests.post(
|
| 24 |
+
f"{MCP_URL}/api/advanced_search",
|
| 25 |
+
json={"company_input": company_name},
|
|
|
|
|
|
|
|
|
|
| 26 |
headers=HEADERS,
|
| 27 |
timeout=30
|
| 28 |
)
|
|
|
|
| 31 |
return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
company = search_resp.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except (ValueError, KeyError) as e:
|
| 36 |
return f"❌ JSON Parse Error: {str(e)}\n\nResponse: {search_resp.text[:500]}"
|
| 37 |
|
|
|
|
| 47 |
# 根据查询类型获取数据
|
| 48 |
if query_type == "最新财务数据":
|
| 49 |
data_resp = requests.post(
|
| 50 |
+
f"{MCP_URL}/api/get_latest_financial_data",
|
| 51 |
+
json={"cik": cik},
|
|
|
|
|
|
|
|
|
|
| 52 |
headers=HEADERS,
|
| 53 |
timeout=30
|
| 54 |
)
|
|
|
|
| 57 |
return result + f"❌ Server Error: HTTP {data_resp.status_code}\n\n{data_resp.text[:500]}"
|
| 58 |
|
| 59 |
try:
|
| 60 |
+
data = data_resp.json()
|
|
|
|
| 61 |
except (ValueError, KeyError) as e:
|
| 62 |
return result + f"❌ JSON Parse Error: {str(e)}\n\n{data_resp.text[:500]}"
|
| 63 |
|
|
|
|
| 72 |
|
| 73 |
elif query_type == "3年趋势":
|
| 74 |
metrics_resp = requests.post(
|
| 75 |
+
f"{MCP_URL}/api/extract_financial_metrics",
|
| 76 |
+
json={"cik": cik, "years": 3},
|
|
|
|
|
|
|
|
|
|
| 77 |
headers=HEADERS,
|
| 78 |
timeout=60
|
| 79 |
)
|
|
|
|
| 82 |
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 83 |
|
| 84 |
try:
|
| 85 |
+
metrics = metrics_resp.json()
|
|
|
|
| 86 |
except (ValueError, KeyError) as e:
|
| 87 |
return result + f"❌ JSON Parse Error: {str(e)}\n\n{metrics_resp.text[:500]}"
|
| 88 |
|
|
|
|
| 106 |
|
| 107 |
elif query_type == "5年趋势":
|
| 108 |
metrics_resp = requests.post(
|
| 109 |
+
f"{MCP_URL}/api/extract_financial_metrics",
|
| 110 |
+
json={"cik": cik, "years": 5},
|
|
|
|
|
|
|
|
|
|
| 111 |
headers=HEADERS,
|
| 112 |
timeout=60
|
| 113 |
)
|
|
|
|
| 116 |
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 117 |
|
| 118 |
try:
|
| 119 |
+
metrics = metrics_resp.json()
|
|
|
|
| 120 |
except (ValueError, KeyError) as e:
|
| 121 |
return result + f"❌ JSON Parse Error: {str(e)}\n\n{metrics_resp.text[:500]}"
|
| 122 |
|