JC321 commited on
Commit
2d33a19
·
verified ·
1 Parent(s): b3daa10

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -10
app.py CHANGED
@@ -171,13 +171,34 @@ def query_financial_data(company_name, query_type):
171
  # 显示所有数据(包括年度和季度)
172
  all_data = metrics.get('metrics', [])
173
 
 
 
 
 
 
 
 
 
 
174
  # 按期间降序排序,确保显示最近的3年数据
175
- all_data = sorted(all_data, key=lambda x: x.get('period', '0000'), reverse=True)
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
  result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
178
  result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
179
 
180
- for m in all_data:
181
  period = m.get('period', 'N/A')
182
  rev = (m.get('total_revenue') or 0) / 1e9
183
  inc = (m.get('net_income') or 0) / 1e9
@@ -187,11 +208,17 @@ def query_financial_data(company_name, query_type):
187
  source_form = m.get('source_form', 'N/A')
188
  source_url = m.get('source_url', None) # 从后端获取URL
189
 
190
- # 区分年度和季度
191
- period_prefix = "FY" if 'Q' not in period else ""
 
 
 
 
 
 
192
  source_link = create_source_link(source_form, source_url)
193
 
194
- result += f"| {period_prefix}{period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, False)} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
195
 
196
  elif internal_query_type == "5年趋势":
197
  metrics_resp = requests.post(
@@ -215,14 +242,35 @@ def query_financial_data(company_name, query_type):
215
  # 显示所有数据(包括年度和季度)
216
  all_data = metrics.get('metrics', [])
217
 
 
 
 
 
 
 
 
 
 
218
  # 按期间降序排序,确保显示最近的5年数据
219
- all_data = sorted(all_data, key=lambda x: x.get('period', '0000'), reverse=True)
 
 
 
 
 
 
 
 
 
 
 
 
220
 
221
  result += f"## 5-Year Financial Trends ({metrics.get('count', 0)} periods)\n\n"
222
  result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
223
  result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
224
 
225
- for m in all_data:
226
  period = m.get('period', 'N/A')
227
  rev = (m.get('total_revenue') or 0) / 1e9
228
  inc = (m.get('net_income') or 0) / 1e9
@@ -232,11 +280,17 @@ def query_financial_data(company_name, query_type):
232
  source_form = m.get('source_form', 'N/A')
233
  source_url = m.get('source_url', None) # 从后端获取URL
234
 
235
- # 区分年度和季度
236
- period_prefix = "FY" if 'Q' not in period else ""
 
 
 
 
 
 
237
  source_link = create_source_link(source_form, source_url)
238
 
239
- result += f"| {period_prefix}{period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, False)} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
240
 
241
  elif internal_query_type == "公司报表列表":
242
  # 查询公司所有报表
 
171
  # 显示所有数据(包括年度和季度)
172
  all_data = metrics.get('metrics', [])
173
 
174
+ # 去重:根据period和source_form去重
175
+ seen = set()
176
+ unique_data = []
177
+ for m in all_data:
178
+ key = (m.get('period', 'N/A'), m.get('source_form', 'N/A'))
179
+ if key not in seen:
180
+ seen.add(key)
181
+ unique_data.append(m)
182
+
183
  # 按期间降序排序,确保显示最近的3年数据
184
+ # 使用更智能的排序:先按年份,再按是否是季度
185
+ def sort_key(x):
186
+ period = x.get('period', '0000')
187
+ # 提取年份(前4位)
188
+ year = period[:4] if len(period) >= 4 else '0000'
189
+ # 如果有Q,提取季度号,否则设为5(让FY排在Q后面)
190
+ if 'Q' in period:
191
+ quarter = period[period.index('Q')+1] if period.index('Q')+1 < len(period) else '0'
192
+ return (year, 0, 5 - int(quarter)) # Q4, Q3, Q2, Q1
193
+ else:
194
+ return (year, 1, 0) # FY 排在同年的所有Q之后
195
+
196
+ unique_data = sorted(unique_data, key=sort_key, reverse=True)
197
 
198
  result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
199
  result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
200
 
201
+ for m in unique_data:
202
  period = m.get('period', 'N/A')
203
  rev = (m.get('total_revenue') or 0) / 1e9
204
  inc = (m.get('net_income') or 0) / 1e9
 
208
  source_form = m.get('source_form', 'N/A')
209
  source_url = m.get('source_url', None) # 从后端获取URL
210
 
211
+ # 区分年度和季度,修复双重FY前缀问题
212
+ if 'Q' in period:
213
+ # 季度数据,不添加前缀
214
+ display_period = period
215
+ else:
216
+ # 年度数据,只在没有FY的情况下添加
217
+ display_period = period if period.startswith('FY') else f"FY{period}"
218
+
219
  source_link = create_source_link(source_form, source_url)
220
 
221
+ result += f"| {display_period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, False)} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
222
 
223
  elif internal_query_type == "5年趋势":
224
  metrics_resp = requests.post(
 
242
  # 显示所有数据(包括年度和季度)
243
  all_data = metrics.get('metrics', [])
244
 
245
+ # 去重:根据period和source_form去重
246
+ seen = set()
247
+ unique_data = []
248
+ for m in all_data:
249
+ key = (m.get('period', 'N/A'), m.get('source_form', 'N/A'))
250
+ if key not in seen:
251
+ seen.add(key)
252
+ unique_data.append(m)
253
+
254
  # 按期间降序排序,确保显示最近的5年数据
255
+ # 使用更智能的排序:先按年份,再按是否是季度
256
+ def sort_key(x):
257
+ period = x.get('period', '0000')
258
+ # 提取年份(前4位)
259
+ year = period[:4] if len(period) >= 4 else '0000'
260
+ # 如果有Q,提取季度号,否则设为5(让FY排在Q后面)
261
+ if 'Q' in period:
262
+ quarter = period[period.index('Q')+1] if period.index('Q')+1 < len(period) else '0'
263
+ return (year, 0, 5 - int(quarter)) # Q4, Q3, Q2, Q1
264
+ else:
265
+ return (year, 1, 0) # FY 排在同年的所有Q之后
266
+
267
+ unique_data = sorted(unique_data, key=sort_key, reverse=True)
268
 
269
  result += f"## 5-Year Financial Trends ({metrics.get('count', 0)} periods)\n\n"
270
  result += "| Period | Revenue (B) | Net Income (B) | EPS | Operating Expenses (B) | Operating Cash Flow (B) | Source Form |\n"
271
  result += "|--------|-------------|----------------|-----|------------------------|-------------------------|-------------|\n"
272
 
273
+ for m in unique_data:
274
  period = m.get('period', 'N/A')
275
  rev = (m.get('total_revenue') or 0) / 1e9
276
  inc = (m.get('net_income') or 0) / 1e9
 
280
  source_form = m.get('source_form', 'N/A')
281
  source_url = m.get('source_url', None) # 从后端获取URL
282
 
283
+ # 区分年度和季度,修复双重FY前缀问题
284
+ if 'Q' in period:
285
+ # 季度数据,不添加前缀
286
+ display_period = period
287
+ else:
288
+ # 年度数据,只在没有FY的情况下添加
289
+ display_period = period if period.startswith('FY') else f"FY{period}"
290
+
291
  source_link = create_source_link(source_form, source_url)
292
 
293
+ result += f"| {display_period} | {format_value(rev)} | {format_value(inc)} | {format_value(eps_val, False)} | {format_value(opex)} | {format_value(ocf)} | {source_link} |\n"
294
 
295
  elif internal_query_type == "公司报表列表":
296
  # 查询公司所有报表