yokoha commited on
Commit
e998c1d
Β·
verified Β·
1 Parent(s): 3c21712

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -27
app.py CHANGED
@@ -1219,6 +1219,9 @@ if len(item_df) < 2:
1219
  else:
1220
  st.success(f"μ„ νƒν•œ ν’ˆλͺ© '{selected_item}'에 λŒ€ν•΄ {len(item_df)}개의 데이터가 μžˆμŠ΅λ‹ˆλ‹€.")
1221
 
 
 
 
1222
  # -------------------------------------------------
1223
  # MACRO FORECAST 1996‑2030 ------------------------
1224
  # -------------------------------------------------
@@ -1332,20 +1335,51 @@ else:
1332
  # 차트 ν‘œμ‹œ
1333
  st.plotly_chart(fig, use_container_width=True)
1334
 
1335
- # 2030λ…„ μ˜ˆμΈ‘κ°€ ν‘œμ‹œ
1336
  try:
1337
  latest_price = macro_df.iloc[-1]["price"]
1338
- # 2030λ…„ λ§ˆμ§€λ§‰ μ›” μ°ΎκΈ°
1339
- target_date = pd.Timestamp("2030-12-31")
1340
- close_dates = fc_macro.loc[(fc_macro["ds"] - target_date).abs().argsort()[:1], "ds"].values[0]
1341
- macro_pred = fc_macro.loc[fc_macro["ds"] == close_dates, "yhat"].iloc[0]
1342
- macro_pct = (macro_pred - latest_price) / latest_price * 100
1343
 
1344
- col1, col2 = st.columns(2)
1345
- with col1:
1346
- st.metric("ν˜„μž¬ 가격", format_currency(latest_price))
1347
- with col2:
1348
- st.metric("2030λ…„ μ˜ˆμΈ‘κ°€", format_currency(macro_pred), f"{macro_pct:+.1f}%")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1349
  except Exception as e:
1350
  st.error(f"μ˜ˆμΈ‘κ°€ 계산 였λ₯˜: {str(e)}")
1351
  else:
@@ -1363,6 +1397,9 @@ else:
1363
  fig.update_layout(title=f"{selected_item} κ³Όκ±° 가격")
1364
  st.plotly_chart(fig, use_container_width=True)
1365
 
 
 
 
1366
  # -------------------------------------------------
1367
  # MICRO FORECAST 2024‑2026 ------------------------
1368
  # -------------------------------------------------
@@ -1506,27 +1543,90 @@ else:
1506
  hide_index=True
1507
  )
1508
 
1509
- # 2026λ…„ μ˜ˆμΈ‘κ°€ ν‘œμ‹œ
1510
- try:
1511
- latest_price = monthly_historical.iloc[-1]["price"] if not monthly_historical.empty else micro_df.iloc[-1]["price"]
1512
-
1513
- # 2026λ…„ λ§ˆμ§€λ§‰ μ›” μ°ΎκΈ°
1514
- target_date = pd.Timestamp("2026-12-31")
1515
- close_dates = monthly_forecast.loc[(monthly_forecast["ds"] - target_date).abs().argsort()[:1], "ds"].values[0]
1516
- micro_pred = monthly_forecast.loc[monthly_forecast["ds"] == close_dates, "yhat"].iloc[0]
1517
- micro_pct = (micro_pred - latest_price) / latest_price * 100
1518
 
1519
- col1, col2 = st.columns(2)
1520
- with col1:
1521
- st.metric("ν˜„μž¬ 가격", format_currency(latest_price))
1522
- with col2:
1523
- st.metric("2026λ…„ 12μ›” μ˜ˆμΈ‘κ°€", format_currency(micro_pred), f"{micro_pct:+.1f}%")
1524
- except Exception as e:
1525
- st.error(f"μ˜ˆμΈ‘κ°€ 계산 였λ₯˜: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1526
  else:
1527
  st.warning("단기 예츑 λͺ¨λΈμ„ 생성할 수 μ—†μŠ΅λ‹ˆλ‹€.")
1528
  except Exception as e:
1529
  st.error(f"단기 예츑 였λ₯˜: {str(e)}")
 
1530
 
1531
  # -------------------------------------------------
1532
  # SEASONALITY & PATTERN ---------------------------
 
1219
  else:
1220
  st.success(f"μ„ νƒν•œ ν’ˆλͺ© '{selected_item}'에 λŒ€ν•΄ {len(item_df)}개의 데이터가 μžˆμŠ΅λ‹ˆλ‹€.")
1221
 
1222
+ # -------------------------------------------------
1223
+ # MACRO FORECAST 1996‑2030 ------------------------
1224
+ # -------------------------------------------------
1225
  # -------------------------------------------------
1226
  # MACRO FORECAST 1996‑2030 ------------------------
1227
  # -------------------------------------------------
 
1335
  # 차트 ν‘œμ‹œ
1336
  st.plotly_chart(fig, use_container_width=True)
1337
 
1338
+ # 연도별 μ˜ˆμΈ‘κ°€ ν‘œμ‹œ
1339
  try:
1340
  latest_price = macro_df.iloc[-1]["price"]
 
 
 
 
 
1341
 
1342
+ # 연도별 μ˜ˆμΈ‘κ°€ 계산을 μœ„ν•œ ν•¨μˆ˜
1343
+ def get_yearly_prediction(year_end):
1344
+ target_date = pd.Timestamp(f"{year_end}-12-31")
1345
+ # λ‚ μ§œ 기반으둜 κ°€μž₯ κ°€κΉŒμš΄ λ‚ μ§œμ˜ μ˜ˆμΈ‘κ°’ μ°ΎκΈ°
1346
+ date_diffs = abs(fc_macro["ds"] - target_date)
1347
+ closest_idx = date_diffs.idxmin()
1348
+ pred_value = fc_macro.loc[closest_idx, "yhat"]
1349
+ pct_change = (pred_value - latest_price) / latest_price * 100
1350
+ return pred_value, pct_change
1351
+
1352
+ # 연도별 μ˜ˆμΈ‘κ°€ ν‘œμ‹œ
1353
+ col1, col2, col3 = st.columns(3)
1354
+
1355
+ # 2025λ…„ μ˜ˆμΈ‘κ°€
1356
+ pred_2025, pct_2025 = get_yearly_prediction(2025)
1357
+ col1.metric("2025λ…„ μ˜ˆμΈ‘κ°€", format_currency(pred_2025), f"{pct_2025:+.1f}%")
1358
+
1359
+ # 2027λ…„ μ˜ˆμΈ‘κ°€
1360
+ pred_2027, pct_2027 = get_yearly_prediction(2027)
1361
+ col2.metric("2027λ…„ μ˜ˆμΈ‘κ°€", format_currency(pred_2027), f"{pct_2027:+.1f}%")
1362
+
1363
+ # 2030λ…„ μ˜ˆμΈ‘κ°€
1364
+ pred_2030, pct_2030 = get_yearly_prediction(2030)
1365
+ col3.metric("2030λ…„ μ˜ˆμΈ‘κ°€", format_currency(pred_2030), f"{pct_2030:+.1f}%")
1366
+
1367
+ # μΆ”κ°€ 연도 μ˜ˆμΈ‘κ°€ (ν™•μž₯ κ°€λŠ₯)
1368
+ with st.expander("더 λ§Žμ€ 연도별 μ˜ˆμΈ‘κ°€ 보기"):
1369
+ col4, col5, col6 = st.columns(3)
1370
+
1371
+ # 2026λ…„ μ˜ˆμΈ‘κ°€
1372
+ pred_2026, pct_2026 = get_yearly_prediction(2026)
1373
+ col4.metric("2026λ…„ μ˜ˆμΈ‘κ°€", format_currency(pred_2026), f"{pct_2026:+.1f}%")
1374
+
1375
+ # 2028λ…„ μ˜ˆμΈ‘κ°€
1376
+ pred_2028, pct_2028 = get_yearly_prediction(2028)
1377
+ col5.metric("2028λ…„ μ˜ˆμΈ‘κ°€", format_currency(pred_2028), f"{pct_2028:+.1f}%")
1378
+
1379
+ # 2029λ…„ μ˜ˆμΈ‘κ°€
1380
+ pred_2029, pct_2029 = get_yearly_prediction(2029)
1381
+ col6.metric("2029λ…„ μ˜ˆμΈ‘κ°€", format_currency(pred_2029), f"{pct_2029:+.1f}%")
1382
+
1383
  except Exception as e:
1384
  st.error(f"μ˜ˆμΈ‘κ°€ 계산 였λ₯˜: {str(e)}")
1385
  else:
 
1397
  fig.update_layout(title=f"{selected_item} κ³Όκ±° 가격")
1398
  st.plotly_chart(fig, use_container_width=True)
1399
 
1400
+ # -------------------------------------------------
1401
+ # MICRO FORECAST 2024‑2026 ------------------------
1402
+ # -------------------------------------------------
1403
  # -------------------------------------------------
1404
  # MICRO FORECAST 2024‑2026 ------------------------
1405
  # -------------------------------------------------
 
1543
  hide_index=True
1544
  )
1545
 
1546
+ # 월별/���도별 μ˜ˆμΈ‘κ°€ ν‘œμ‹œ ν•¨μˆ˜
1547
+ def get_monthly_prediction(year, month):
1548
+ target_date = pd.Timestamp(f"{year}-{month:02d}-01")
1549
+ # κ°€μž₯ κ°€κΉŒμš΄ λ‚ μ§œμ˜ μ˜ˆμΈ‘κ°’ μ°ΎκΈ°
1550
+ date_diffs = abs(monthly_forecast["ds"] - target_date)
1551
+ closest_idx = date_diffs.idxmin()
 
 
 
1552
 
1553
+ if closest_idx in monthly_forecast.index:
1554
+ pred_value = monthly_forecast.loc[closest_idx, "yhat"]
1555
+
1556
+ # ν˜„μž¬ 가격 κΈ°μ€€ λ³€ν™”μœ¨ 계산
1557
+ latest_price = monthly_historical.iloc[-1]["price"] if not monthly_historical.empty else micro_df.iloc[-1]["price"]
1558
+ pct_change = (pred_value - latest_price) / latest_price * 100
1559
+
1560
+ return pred_value, pct_change
1561
+ else:
1562
+ return None, None
1563
+
1564
+ # 2025λ…„κ³Ό 2026λ…„μ˜ μ£Όμš” 월별 μ˜ˆμΈ‘κ°€
1565
+ st.subheader("μ£Όμš” 월별 μ˜ˆμΈ‘κ°€")
1566
+
1567
+ col1, col2, col3 = st.columns(3)
1568
+
1569
+ # 2025λ…„ 6μ›” μ˜ˆμΈ‘κ°€
1570
+ pred_2025_06, pct_2025_06 = get_monthly_prediction(2025, 6)
1571
+ if pred_2025_06 is not None:
1572
+ col1.metric("2025λ…„ 6μ›”", format_currency(pred_2025_06), f"{pct_2025_06:+.1f}%")
1573
+ else:
1574
+ col1.metric("2025λ…„ 6μ›”", "데이터 μ—†μŒ", "0%")
1575
+
1576
+ # 2025λ…„ 12μ›” μ˜ˆμΈ‘κ°€
1577
+ pred_2025_12, pct_2025_12 = get_monthly_prediction(2025, 12)
1578
+ if pred_2025_12 is not None:
1579
+ col2.metric("2025λ…„ 12μ›”", format_currency(pred_2025_12), f"{pct_2025_12:+.1f}%")
1580
+ else:
1581
+ col2.metric("2025λ…„ 12μ›”", "데이터 μ—†μŒ", "0%")
1582
+
1583
+ # 2026λ…„ 12μ›” μ˜ˆμΈ‘κ°€
1584
+ pred_2026_12, pct_2026_12 = get_monthly_prediction(2026, 12)
1585
+ if pred_2026_12 is not None:
1586
+ col3.metric("2026λ…„ 12μ›”", format_currency(pred_2026_12), f"{pct_2026_12:+.1f}%")
1587
+ else:
1588
+ col3.metric("2026λ…„ 12μ›”", "데이터 μ—†μŒ", "0%")
1589
+
1590
+ # 농산물 κ³„μ ˆμ„±μ— λ§žλŠ” μΆ”κ°€ 월별 데이터 ν‘œμ‹œ
1591
+ with st.expander("더 λ§Žμ€ 월별 μ˜ˆμΈ‘κ°€ 보기"):
1592
+ # λΆ„κΈ°λ³„λ‘œ λ‚˜λˆ μ„œ ν‘œμ‹œ
1593
+ for year in [2025, 2026]:
1594
+ st.write(f"### {year}λ…„ 뢄기별 μ˜ˆμΈ‘κ°€")
1595
+ q1, q2, q3, q4 = st.columns(4)
1596
+
1597
+ # 1λΆ„κΈ° (3μ›”)
1598
+ pred_q1, pct_q1 = get_monthly_prediction(year, 3)
1599
+ if pred_q1 is not None:
1600
+ q1.metric(f"{year}λ…„ 3μ›”", format_currency(pred_q1), f"{pct_q1:+.1f}%")
1601
+ else:
1602
+ q1.metric(f"{year}λ…„ 3μ›”", "데이터 μ—†μŒ", "0%")
1603
+
1604
+ # 2λΆ„κΈ° (6μ›”)
1605
+ pred_q2, pct_q2 = get_monthly_prediction(year, 6)
1606
+ if pred_q2 is not None:
1607
+ q2.metric(f"{year}λ…„ 6μ›”", format_currency(pred_q2), f"{pct_q2:+.1f}%")
1608
+ else:
1609
+ q2.metric(f"{year}λ…„ 6μ›”", "데이터 μ—†μŒ", "0%")
1610
+
1611
+ # 3λΆ„κΈ° (9μ›”)
1612
+ pred_q3, pct_q3 = get_monthly_prediction(year, 9)
1613
+ if pred_q3 is not None:
1614
+ q3.metric(f"{year}λ…„ 9μ›”", format_currency(pred_q3), f"{pct_q3:+.1f}%")
1615
+ else:
1616
+ q3.metric(f"{year}λ…„ 9μ›”", "데이터 μ—†μŒ", "0%")
1617
+
1618
+ # 4λΆ„κΈ° (12μ›”)
1619
+ pred_q4, pct_q4 = get_monthly_prediction(year, 12)
1620
+ if pred_q4 is not None:
1621
+ q4.metric(f"{year}λ…„ 12μ›”", format_currency(pred_q4), f"{pct_q4:+.1f}%")
1622
+ else:
1623
+ q4.metric(f"{year}λ…„ 12μ›”", "데이터 μ—†μŒ", "0%")
1624
+
1625
  else:
1626
  st.warning("단기 예츑 λͺ¨λΈμ„ 생성할 수 μ—†μŠ΅λ‹ˆλ‹€.")
1627
  except Exception as e:
1628
  st.error(f"단기 예츑 였λ₯˜: {str(e)}")
1629
+ st.code(traceback.format_exc())
1630
 
1631
  # -------------------------------------------------
1632
  # SEASONALITY & PATTERN ---------------------------