Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +75 -46
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
|
| 4 |
|
|
|
|
| 5 |
MCP_URL = "https://jc321-easyreportdatemcp.hf.space"
|
| 6 |
|
| 7 |
# 设置请求头
|
|
@@ -10,50 +11,38 @@ HEADERS = {
|
|
| 10 |
"User-Agent": "SEC-Query-Assistant/1.0 (jtyxabc@gmail.com)"
|
| 11 |
}
|
| 12 |
|
| 13 |
-
# 初始化 Gradio Client
|
| 14 |
-
client = None
|
| 15 |
-
client_error = None
|
| 16 |
-
|
| 17 |
-
def get_client():
|
| 18 |
-
"""获取或初始化 Gradio Client"""
|
| 19 |
-
global client, client_error
|
| 20 |
-
if client is not None:
|
| 21 |
-
return client
|
| 22 |
-
|
| 23 |
-
try:
|
| 24 |
-
client = Client(MCP_URL, verbose=False)
|
| 25 |
-
return client
|
| 26 |
-
except Exception as e:
|
| 27 |
-
client_error = str(e)
|
| 28 |
-
return None
|
| 29 |
-
|
| 30 |
def query_financial_data(company_name, query_type):
|
| 31 |
"""查询财务数据的主函数"""
|
| 32 |
|
| 33 |
if not company_name:
|
| 34 |
return "请输入公司名称或股票代码"
|
| 35 |
|
| 36 |
-
# 获取 client
|
| 37 |
-
current_client = get_client()
|
| 38 |
-
if current_client is None:
|
| 39 |
-
error_msg = f"❌ Gradio Client 初始化失败\n\n"
|
| 40 |
-
error_msg += f"**MCP Server**: {MCP_URL}\n"
|
| 41 |
-
if client_error:
|
| 42 |
-
error_msg += f"**错误信息**: {client_error}\n\n"
|
| 43 |
-
error_msg += "**可能原因**:\n"
|
| 44 |
-
error_msg += "- MCP 服务可能正在启动中,请稍后重试\n"
|
| 45 |
-
error_msg += "- MCP 服务可能不可用或已暂停\n"
|
| 46 |
-
error_msg += "- 网络连接问题\n"
|
| 47 |
-
return error_msg
|
| 48 |
-
|
| 49 |
try:
|
| 50 |
-
#
|
| 51 |
# 先搜索公司
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if isinstance(company, dict) and company.get("error"):
|
| 58 |
return f"❌ 错误: {company['error']}"
|
| 59 |
|
|
@@ -65,11 +54,25 @@ def query_financial_data(company_name, query_type):
|
|
| 65 |
|
| 66 |
# 根据查询类型获取数据
|
| 67 |
if query_type == "最新财务数据":
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
if isinstance(data, dict) and data.get("error"):
|
| 74 |
return result + f"❌ {data['error']}"
|
| 75 |
|
|
@@ -80,12 +83,25 @@ def query_financial_data(company_name, query_type):
|
|
| 80 |
result += f"- **来源**: {data.get('source_form', 'N/A')}\n"
|
| 81 |
|
| 82 |
elif query_type == "3年趋势":
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
if isinstance(metrics, dict) and metrics.get("error"):
|
| 90 |
return result + f"❌ {metrics['error']}"
|
| 91 |
|
|
@@ -105,12 +121,25 @@ def query_financial_data(company_name, query_type):
|
|
| 105 |
result += f"| FY{period} | ${rev:.2f}B | ${inc:.2f}B | ${eps:.2f} |\n"
|
| 106 |
|
| 107 |
elif query_type == "5年趋势":
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
)
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
if isinstance(metrics, dict) and metrics.get("error"):
|
| 115 |
return result + f"❌ {metrics['error']}"
|
| 116 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
+
MCP_SPACE = "JC321/EasyReportDateMCP"
|
| 6 |
MCP_URL = "https://jc321-easyreportdatemcp.hf.space"
|
| 7 |
|
| 8 |
# 设置请求头
|
|
|
|
| 11 |
"User-Agent": "SEC-Query-Assistant/1.0 (jtyxabc@gmail.com)"
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def query_financial_data(company_name, query_type):
|
| 15 |
"""查询财务数据的主函数"""
|
| 16 |
|
| 17 |
if not company_name:
|
| 18 |
return "请输入公司名称或股票代码"
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
+
# 直接使用 HTTP POST 调用 Gradio API
|
| 22 |
# 先搜索公司
|
| 23 |
+
search_resp = requests.post(
|
| 24 |
+
f"{MCP_URL}/api/predict",
|
| 25 |
+
json={
|
| 26 |
+
"data": [company_name],
|
| 27 |
+
"fn_index": 0 # advanced_search 函数索引
|
| 28 |
+
},
|
| 29 |
+
headers=HEADERS,
|
| 30 |
+
timeout=30
|
| 31 |
)
|
| 32 |
|
| 33 |
+
if search_resp.status_code != 200:
|
| 34 |
+
return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
result_data = search_resp.json()
|
| 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 |
+
|
| 46 |
if isinstance(company, dict) and company.get("error"):
|
| 47 |
return f"❌ 错误: {company['error']}"
|
| 48 |
|
|
|
|
| 54 |
|
| 55 |
# 根据查询类型获取数据
|
| 56 |
if query_type == "最新财务数据":
|
| 57 |
+
data_resp = requests.post(
|
| 58 |
+
f"{MCP_URL}/api/predict",
|
| 59 |
+
json={
|
| 60 |
+
"data": [cik],
|
| 61 |
+
"fn_index": 1 # get_latest_financial_data 函数索引
|
| 62 |
+
},
|
| 63 |
+
headers=HEADERS,
|
| 64 |
+
timeout=30
|
| 65 |
)
|
| 66 |
|
| 67 |
+
if data_resp.status_code != 200:
|
| 68 |
+
return result + f"❌ Server Error: HTTP {data_resp.status_code}\n\n{data_resp.text[:500]}"
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
data_result = data_resp.json()
|
| 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 |
+
|
| 76 |
if isinstance(data, dict) and data.get("error"):
|
| 77 |
return result + f"❌ {data['error']}"
|
| 78 |
|
|
|
|
| 83 |
result += f"- **来源**: {data.get('source_form', 'N/A')}\n"
|
| 84 |
|
| 85 |
elif query_type == "3年趋势":
|
| 86 |
+
metrics_resp = requests.post(
|
| 87 |
+
f"{MCP_URL}/api/predict",
|
| 88 |
+
json={
|
| 89 |
+
"data": [cik, 3],
|
| 90 |
+
"fn_index": 2 # extract_financial_metrics 函数索引
|
| 91 |
+
},
|
| 92 |
+
headers=HEADERS,
|
| 93 |
+
timeout=60
|
| 94 |
)
|
| 95 |
|
| 96 |
+
if metrics_resp.status_code != 200:
|
| 97 |
+
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
metrics_result = metrics_resp.json()
|
| 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 |
+
|
| 105 |
if isinstance(metrics, dict) and metrics.get("error"):
|
| 106 |
return result + f"❌ {metrics['error']}"
|
| 107 |
|
|
|
|
| 121 |
result += f"| FY{period} | ${rev:.2f}B | ${inc:.2f}B | ${eps:.2f} |\n"
|
| 122 |
|
| 123 |
elif query_type == "5年趋势":
|
| 124 |
+
metrics_resp = requests.post(
|
| 125 |
+
f"{MCP_URL}/api/predict",
|
| 126 |
+
json={
|
| 127 |
+
"data": [cik, 5],
|
| 128 |
+
"fn_index": 2 # extract_financial_metrics 函数索引
|
| 129 |
+
},
|
| 130 |
+
headers=HEADERS,
|
| 131 |
+
timeout=60
|
| 132 |
)
|
| 133 |
|
| 134 |
+
if metrics_resp.status_code != 200:
|
| 135 |
+
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 136 |
+
|
| 137 |
+
try:
|
| 138 |
+
metrics_result = metrics_resp.json()
|
| 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 |
+
|
| 143 |
if isinstance(metrics, dict) and metrics.get("error"):
|
| 144 |
return result + f"❌ {metrics['error']}"
|
| 145 |
|
requirements.txt
CHANGED
|
@@ -1,3 +1,2 @@
|
|
| 1 |
gradio>=4.0.0
|
| 2 |
-
requests
|
| 3 |
-
gradio_client
|
|
|
|
| 1 |
gradio>=4.0.0
|
| 2 |
+
requests
|
|
|