Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -243,46 +243,24 @@ def query_financial_data(company_name, query_type):
|
|
| 243 |
timeout=60
|
| 244 |
)
|
| 245 |
|
| 246 |
-
# 调试:显示 HTTP 响应状态
|
| 247 |
-
result += f"\n**Debug Info (3-Year)**:\n- HTTP Status: {metrics_resp.status_code}\n"
|
| 248 |
-
|
| 249 |
if metrics_resp.status_code != 200:
|
| 250 |
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 251 |
|
| 252 |
try:
|
| 253 |
metrics_result = metrics_resp.json()
|
| 254 |
-
# 调试:显示原始 JSON 响应
|
| 255 |
-
result += f"- Raw Response Length: {len(metrics_resp.text)} chars\n"
|
| 256 |
-
result += f"- Response Preview: {metrics_resp.text[:200]}...\n\n"
|
| 257 |
-
|
| 258 |
# 使用统一的 MCP 响应解析函数
|
| 259 |
metrics = parse_mcp_response(metrics_result)
|
| 260 |
-
|
| 261 |
-
# 调试:显示解析后的数据类型和内容
|
| 262 |
-
result += f"- Parsed Type: {type(metrics).__name__}\n"
|
| 263 |
-
if isinstance(metrics, dict):
|
| 264 |
-
result += f"- Parsed Keys: {list(metrics.keys())}\n"
|
| 265 |
-
result += f"- Periods: {metrics.get('periods', 'N/A')}\n"
|
| 266 |
-
result += f"- Data Length: {len(metrics.get('data', []))}\n\n"
|
| 267 |
except (ValueError, KeyError, json.JSONDecodeError) as e:
|
| 268 |
return result + f"❌ JSON Parse Error: {str(e)}\n\nResponse: {metrics_resp.text[:500]}"
|
| 269 |
|
| 270 |
if isinstance(metrics, dict) and metrics.get("error"):
|
| 271 |
return result + f"❌ {metrics['error']}"
|
| 272 |
|
| 273 |
-
# 调试:显示原始响应
|
| 274 |
-
if not isinstance(metrics, dict):
|
| 275 |
-
return result + f"❌ Invalid response format\n\nDebug: {str(metrics)[:500]}"
|
| 276 |
-
|
| 277 |
result += f"## 3-Year Financial Trends ({metrics.get('periods', 0)} periods)\n\n"
|
| 278 |
|
| 279 |
# 显示所有数据(包括年度和季度)
|
| 280 |
all_data = metrics.get('data', []) # MCP Server 返回的字段是 'data'
|
| 281 |
|
| 282 |
-
# 调试:检查是否有数据
|
| 283 |
-
if not all_data:
|
| 284 |
-
return result + f"❌ No data returned from MCP Server\n\nDebug: metrics keys = {list(metrics.keys())}\n\nFull response: {json.dumps(metrics, indent=2, ensure_ascii=False)[:1000]}"
|
| 285 |
-
|
| 286 |
# 去重:根据period和source_form去重
|
| 287 |
seen = set()
|
| 288 |
unique_data = []
|
|
@@ -347,44 +325,22 @@ def query_financial_data(company_name, query_type):
|
|
| 347 |
timeout=60
|
| 348 |
)
|
| 349 |
|
| 350 |
-
# 调试:显示 HTTP 响应状态
|
| 351 |
-
result += f"\n**Debug Info (5-Year)**:\n- HTTP Status: {metrics_resp.status_code}\n"
|
| 352 |
-
|
| 353 |
if metrics_resp.status_code != 200:
|
| 354 |
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 355 |
|
| 356 |
try:
|
| 357 |
metrics_result = metrics_resp.json()
|
| 358 |
-
# 调试:显示原始 JSON 响应
|
| 359 |
-
result += f"- Raw Response Length: {len(metrics_resp.text)} chars\n"
|
| 360 |
-
result += f"- Response Preview: {metrics_resp.text[:200]}...\n\n"
|
| 361 |
-
|
| 362 |
# 使用统一的 MCP 响应解析函数
|
| 363 |
metrics = parse_mcp_response(metrics_result)
|
| 364 |
-
|
| 365 |
-
# 调试:显示解析后的数据类型和内容
|
| 366 |
-
result += f"- Parsed Type: {type(metrics).__name__}\n"
|
| 367 |
-
if isinstance(metrics, dict):
|
| 368 |
-
result += f"- Parsed Keys: {list(metrics.keys())}\n"
|
| 369 |
-
result += f"- Periods: {metrics.get('periods', 'N/A')}\n"
|
| 370 |
-
result += f"- Data Length: {len(metrics.get('data', []))}\n\n"
|
| 371 |
except (ValueError, KeyError, json.JSONDecodeError) as e:
|
| 372 |
return result + f"❌ JSON Parse Error: {str(e)}\n\nResponse: {metrics_resp.text[:500]}"
|
| 373 |
|
| 374 |
if isinstance(metrics, dict) and metrics.get("error"):
|
| 375 |
return result + f"❌ {metrics['error']}"
|
| 376 |
|
| 377 |
-
# 调试:显示原始响应
|
| 378 |
-
if not isinstance(metrics, dict):
|
| 379 |
-
return result + f"❌ Invalid response format\n\nDebug: {str(metrics)[:500]}"
|
| 380 |
-
|
| 381 |
# 显示所有数据(包括年度和季度)
|
| 382 |
all_data = metrics.get('data', []) # MCP Server 返回的字段是 'data'
|
| 383 |
|
| 384 |
-
# 调试:检查是否有数据
|
| 385 |
-
if not all_data:
|
| 386 |
-
return result + f"❌ No data returned from MCP Server\n\nDebug: metrics keys = {list(metrics.keys())}\n\nFull response: {json.dumps(metrics, indent=2, ensure_ascii=False)[:1000]}"
|
| 387 |
-
|
| 388 |
# 去重:根据period和source_form去重
|
| 389 |
seen = set()
|
| 390 |
unique_data = []
|
|
@@ -625,4 +581,10 @@ with gr.Blocks(title="SEC Financial Data Query Assistant") as demo:
|
|
| 625 |
gr.Markdown("---")
|
| 626 |
gr.Markdown(f"**Data Source**: SEC EDGAR | **MCP Server**: `{MCP_URL}`")
|
| 627 |
|
| 628 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
timeout=60
|
| 244 |
)
|
| 245 |
|
|
|
|
|
|
|
|
|
|
| 246 |
if metrics_resp.status_code != 200:
|
| 247 |
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 248 |
|
| 249 |
try:
|
| 250 |
metrics_result = metrics_resp.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
# 使用统一的 MCP 响应解析函数
|
| 252 |
metrics = parse_mcp_response(metrics_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
except (ValueError, KeyError, json.JSONDecodeError) as e:
|
| 254 |
return result + f"❌ JSON Parse Error: {str(e)}\n\nResponse: {metrics_resp.text[:500]}"
|
| 255 |
|
| 256 |
if isinstance(metrics, dict) and metrics.get("error"):
|
| 257 |
return result + f"❌ {metrics['error']}"
|
| 258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
result += f"## 3-Year Financial Trends ({metrics.get('periods', 0)} periods)\n\n"
|
| 260 |
|
| 261 |
# 显示所有数据(包括年度和季度)
|
| 262 |
all_data = metrics.get('data', []) # MCP Server 返回的字段是 'data'
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
# 去重:根据period和source_form去重
|
| 265 |
seen = set()
|
| 266 |
unique_data = []
|
|
|
|
| 325 |
timeout=60
|
| 326 |
)
|
| 327 |
|
|
|
|
|
|
|
|
|
|
| 328 |
if metrics_resp.status_code != 200:
|
| 329 |
return result + f"❌ Server Error: HTTP {metrics_resp.status_code}\n\n{metrics_resp.text[:500]}"
|
| 330 |
|
| 331 |
try:
|
| 332 |
metrics_result = metrics_resp.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
# 使用统一的 MCP 响应解析函数
|
| 334 |
metrics = parse_mcp_response(metrics_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
except (ValueError, KeyError, json.JSONDecodeError) as e:
|
| 336 |
return result + f"❌ JSON Parse Error: {str(e)}\n\nResponse: {metrics_resp.text[:500]}"
|
| 337 |
|
| 338 |
if isinstance(metrics, dict) and metrics.get("error"):
|
| 339 |
return result + f"❌ {metrics['error']}"
|
| 340 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
# 显示所有数据(包括年度和季度)
|
| 342 |
all_data = metrics.get('data', []) # MCP Server 返回的字段是 'data'
|
| 343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
# 去重:根据period和source_form去重
|
| 345 |
seen = set()
|
| 346 |
unique_data = []
|
|
|
|
| 581 |
gr.Markdown("---")
|
| 582 |
gr.Markdown(f"**Data Source**: SEC EDGAR | **MCP Server**: `{MCP_URL}`")
|
| 583 |
|
| 584 |
+
# Launch the app for Hugging Face Space
|
| 585 |
+
if __name__ == "__main__":
|
| 586 |
+
demo.launch(
|
| 587 |
+
server_name="0.0.0.0",
|
| 588 |
+
server_port=7860,
|
| 589 |
+
show_error=True
|
| 590 |
+
)
|