JC321 commited on
Commit
d0f8d86
·
verified ·
1 Parent(s): 369f3a5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -16
app.py CHANGED
@@ -2,6 +2,9 @@ import gradio as gr
2
  import requests
3
  import json
4
  import os
 
 
 
5
 
6
  MCP_SPACE = "JC321/EasyReportsMCPServer"
7
  MCP_URL = "https://jc321-easyreportsmcpserver.hf.space"
@@ -12,6 +15,23 @@ HEADERS = {
12
  "User-Agent": "SEC-Query-Assistant/1.0 (jtyxabc@gmail.com)"
13
  }
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # 格式化数值显示
16
  def format_value(value, value_type="money"):
17
  """
@@ -141,18 +161,21 @@ def query_financial_data(company_name, query_type):
141
  try:
142
  # 使用 MCP 协议调用工具
143
  # 先搜索公司(使用 advanced_search_company)
144
- search_resp = requests.post(
145
- f"{MCP_URL}/message",
146
- json={
147
- "method": "tools/call",
148
- "params": {
149
- "name": "advanced_search_company",
150
- "arguments": {"company_input": company_name}
151
- }
152
- },
153
- headers=HEADERS,
154
- timeout=60 # 增加到60秒
155
- )
 
 
 
156
 
157
  if search_resp.status_code != 200:
158
  return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
@@ -184,7 +207,7 @@ def query_financial_data(company_name, query_type):
184
 
185
  # 根据查询类型获取数据
186
  if internal_query_type == "最新财务数据":
187
- data_resp = requests.post(
188
  f"{MCP_URL}/message",
189
  json={
190
  "method": "tools/call",
@@ -230,7 +253,7 @@ def query_financial_data(company_name, query_type):
230
  result += f"- **Source Form**: {create_source_link(source_form, source_url)}\n"
231
 
232
  elif internal_query_type == "3年趋势":
233
- metrics_resp = requests.post(
234
  f"{MCP_URL}/message",
235
  json={
236
  "method": "tools/call",
@@ -312,7 +335,7 @@ def query_financial_data(company_name, query_type):
312
  result += f"| {display_period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, 'eps')} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
313
 
314
  elif internal_query_type == "5年趋势":
315
- metrics_resp = requests.post(
316
  f"{MCP_URL}/message",
317
  json={
318
  "method": "tools/call",
@@ -394,7 +417,7 @@ def query_financial_data(company_name, query_type):
394
 
395
  elif internal_query_type == "公司报表列表":
396
  # 查询公司所有报表
397
- filings_resp = requests.post(
398
  f"{MCP_URL}/message",
399
  json={
400
  "method": "tools/call",
@@ -518,6 +541,12 @@ with gr.Blocks(title="SEC Financial Data Query Assistant") as demo:
518
  gr.Markdown("# 🤖 SEC Financial Data Query Assistant")
519
  gr.Markdown("Query SEC financial data for US listed companies through MCP Server")
520
 
 
 
 
 
 
 
521
  with gr.Tab("AI Assistant"):
522
  # 使用 Gradio ChatInterface(兼容 4.44.1)
523
  chat = gr.ChatInterface(
 
2
  import requests
3
  import json
4
  import os
5
+ import time
6
+ from requests.adapters import HTTPAdapter
7
+ from urllib3.util.retry import Retry
8
 
9
  MCP_SPACE = "JC321/EasyReportsMCPServer"
10
  MCP_URL = "https://jc321-easyreportsmcpserver.hf.space"
 
15
  "User-Agent": "SEC-Query-Assistant/1.0 (jtyxabc@gmail.com)"
16
  }
17
 
18
+ # 创建带重试的 requests session
19
+ def create_session_with_retry():
20
+ """创建带重试机制的 requests session"""
21
+ session = requests.Session()
22
+ retry = Retry(
23
+ total=3, # 最多重试3次
24
+ backoff_factor=1, # 重试间隔:1秒, 2秒, 4秒
25
+ status_forcelist=[500, 502, 503, 504], # 这些状态码会触发重试
26
+ )
27
+ adapter = HTTPAdapter(max_retries=retry)
28
+ session.mount('http://', adapter)
29
+ session.mount('https://', adapter)
30
+ return session
31
+
32
+ # 创建全局 session
33
+ session = create_session_with_retry()
34
+
35
  # 格式化数值显示
36
  def format_value(value, value_type="money"):
37
  """
 
161
  try:
162
  # 使用 MCP 协议调用工具
163
  # 先搜索公司(使用 advanced_search_company)
164
+ try:
165
+ search_resp = session.post(
166
+ f"{MCP_URL}/message",
167
+ json={
168
+ "method": "tools/call",
169
+ "params": {
170
+ "name": "advanced_search_company",
171
+ "arguments": {"company_input": company_name}
172
+ }
173
+ },
174
+ headers=HEADERS,
175
+ timeout=60 # 增加到60秒
176
+ )
177
+ except requests.exceptions.Timeout:
178
+ return f"❌ MCP Server Timeout: The server took too long to respond (>60s).\n\n**Possible reasons**:\n1. MCP Server is cold starting (first request after idle)\n2. Server is overloaded\n3. Network issues\n\n**Suggestion**: Please try again in a few moments. If the problem persists, the MCP Server at {MCP_URL} may be down."
179
 
180
  if search_resp.status_code != 200:
181
  return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
 
207
 
208
  # 根据查询类型获取数据
209
  if internal_query_type == "最新财务数据":
210
+ data_resp = session.post(
211
  f"{MCP_URL}/message",
212
  json={
213
  "method": "tools/call",
 
253
  result += f"- **Source Form**: {create_source_link(source_form, source_url)}\n"
254
 
255
  elif internal_query_type == "3年趋势":
256
+ metrics_resp = session.post(
257
  f"{MCP_URL}/message",
258
  json={
259
  "method": "tools/call",
 
335
  result += f"| {display_period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, 'eps')} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
336
 
337
  elif internal_query_type == "5年趋势":
338
+ metrics_resp = session.post(
339
  f"{MCP_URL}/message",
340
  json={
341
  "method": "tools/call",
 
417
 
418
  elif internal_query_type == "公司报表列表":
419
  # 查询公司所有报表
420
+ filings_resp = session.post(
421
  f"{MCP_URL}/message",
422
  json={
423
  "method": "tools/call",
 
541
  gr.Markdown("# 🤖 SEC Financial Data Query Assistant")
542
  gr.Markdown("Query SEC financial data for US listed companies through MCP Server")
543
 
544
+ # 添加 MCP Server 状态提示
545
+ with gr.Row():
546
+ gr.Markdown(f"🔗 **MCP Server**: [{MCP_URL}]({MCP_URL})")
547
+ with gr.Row():
548
+ gr.Markdown("⚠️ **Note**: First query after idle may take 1-2 minutes (server cold start). Please be patient.")
549
+
550
  with gr.Tab("AI Assistant"):
551
  # 使用 Gradio ChatInterface(兼容 4.44.1)
552
  chat = gr.ChatInterface(