AlanRex commited on
Commit
e10b453
·
verified ·
1 Parent(s): 944a027

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -84
app.py CHANGED
@@ -225,16 +225,16 @@ def simple_lstm_predict(ticker, n_days=5):
225
  # 建立 Dash 應用程式
226
  app = dash.Dash(__name__, suppress_callback_exceptions=True)
227
 
228
- # 新聞預測器初始化
229
- predictor = None
230
- try:
231
- if BertPredictor:
232
- print("正在初始化新聞情緒分析模型...")
233
- predictor = BertPredictor(max_news_per_keyword=5)
234
- print("新聞情緒分析模型初始化成功。")
235
- except Exception as e:
236
- print(f"錯誤:新聞情緒分析模型初始化失敗 - {e}")
237
- predictor = None
238
 
239
 
240
  # --- 頁面內容定義 ---
@@ -257,19 +257,20 @@ homepage_layout = html.Div([
257
  html.Div([dcc.Graph(id='taiex-prediction-chart')], style={'margin-top': '20px'})
258
  ], style={'background': 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)','padding': '25px','border-radius': '15px','box-shadow': '0 8px 25px rgba(0,0,0,0.15)','color': 'white','margin-bottom': '40px'}),
259
 
260
- html.Div([
261
- html.H3("📰 市場情緒與新聞分析", style={'color': '#E74C3C', 'margin-bottom': '20px'}),
262
- html.Div([
263
- html.Div([
264
- html.H4("市場情緒指標", style={'color': '#8E44AD'}),
265
- html.Div(id='sentiment-gauge')
266
- ], style={'width': '48%', 'display': 'inline-block'}),
267
- html.Div([
268
- html.H4("關鍵新聞摘要", style={'color': '#27AE60'}),
269
- html.Div(id='news-summary', style={'background': '#f8f9fa','padding': '15px','border-radius': '8px','max-height': '200px','overflow-y': 'auto'})
270
- ], style={'width': '48%', 'display': 'inline-block', 'margin-left': '4%'})
271
- ])
272
- ], style={'margin-top': '30px','padding': '20px','background': 'white','border-radius': '10px','box-shadow': '0 2px 10px rgba(0,0,0,0.1)'}),
 
273
 
274
  html.Div([
275
  html.H3("景氣燈號與 PMI 分析"),
@@ -420,67 +421,66 @@ def update_taiex_prediction(n_days):
420
 
421
  return result_text, fig
422
 
423
-
424
- @app.callback(
425
- dash.dependencies.Output('sentiment-gauge', 'children'),
426
- dash.dependencies.Output('news-summary', 'children')
427
- )
428
- def update_sentiment_analysis():
429
- """更新新聞情緒分析"""
430
- if not predictor:
431
- return html.Div("新聞情緒分析模型未初始化。"), html.Div("請檢查 'Bert_predict.py' 檔案是否存在。")
432
-
433
- try:
434
- sentiment_score, news_list = predictor.get_sentiment_score()
435
-
436
- except Exception as e:
437
- sentiment_score = None
438
- news_list = []
439
- print(f"情緒分析獲取失敗: {e}")
440
-
441
- # 1. 建立儀表板 (Gauge)
442
- if sentiment_score is not None:
443
- gauge_fig = go.Figure(go.Indicator(
444
- mode="gauge+number",
445
- value=sentiment_score,
446
- title={'text': "市場情緒分數 (0-100)"},
447
- domain={'x': [0, 1], 'y': [0, 1]},
448
- gauge={
449
- 'axis': {'range': [0, 100]},
450
- 'bar': {'color': "#667eea"},
451
- 'steps': [
452
- {'range': [0, 35], 'color': "rgba(217, 83, 79, 0.2)"},
453
- {'range': [35, 65], 'color': "rgba(240, 173, 78, 0.2)"},
454
- {'range': [65, 100], 'color': "rgba(92, 184, 92, 0.2)"}
455
- ],
456
- }
457
- ))
458
- gauge_fig.update_layout(height=200, margin=dict(l=30, r=30, t=50, b=20))
459
- gauge_content = dcc.Graph(figure=gauge_fig)
460
- else:
461
- # 處理無法計算分數的情況
462
- error_fig = go.Figure().add_annotation(text="今日尚無情緒分數", showarrow=False)
463
- error_fig.update_layout(height=200)
464
- gauge_content = dcc.Graph(figure=error_fig)
465
-
466
-
467
- # 2. predictor 獲取分數最高的3則新聞
468
- top_news_list = predictor.get_news()
469
-
470
- # 3. 建立新聞摘要元件
471
- if top_news_list: # 如果列表不為空
472
- news_content = html.Div([
473
- html.P(f"• {news}", style={
474
- 'margin': '8px 0',
475
- 'padding-left': '5px',
476
- 'font-size': '14px',
477
- 'border-left': '3px solid #E74C3C'
478
- }) for news in top_news_list
479
- ])
480
- else:
481
- news_content = html.Div("今日尚無重大新聞摘要。")
482
-
483
- return gauge_content, news_content
484
 
485
 
486
  @app.callback(
 
225
  # 建立 Dash 應用程式
226
  app = dash.Dash(__name__, suppress_callback_exceptions=True)
227
 
228
+ # 註解掉新聞預測器初始化
229
+ # predictor = None
230
+ # try:
231
+ # if BertPredictor:
232
+ # print("正在初始化新聞情緒分析模型...")
233
+ # predictor = BertPredictor(max_news_per_keyword=5)
234
+ # print("新聞情緒分析模型初始化成功。")
235
+ # except Exception as e:
236
+ # print(f"錯誤:新聞情緒分析模型初始化失敗 - {e}")
237
+ # predictor = None
238
 
239
 
240
  # --- 頁面內容定義 ---
 
257
  html.Div([dcc.Graph(id='taiex-prediction-chart')], style={'margin-top': '20px'})
258
  ], style={'background': 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)','padding': '25px','border-radius': '15px','box-shadow': '0 8px 25px rgba(0,0,0,0.15)','color': 'white','margin-bottom': '40px'}),
259
 
260
+ # 註解掉情緒分析區塊
261
+ # html.Div([
262
+ # html.H3("📰 市場情緒與新聞分析", style={'color': '#E74C3C', 'margin-bottom': '20px'}),
263
+ # html.Div([
264
+ # html.Div([
265
+ # html.H4("市場情緒指標", style={'color': '#8E44AD'}),
266
+ # html.Div(id='sentiment-gauge')
267
+ # ], style={'width': '48%', 'display': 'inline-block'}),
268
+ # html.Div([
269
+ # html.H4("關鍵新聞摘要", style={'color': '#27AE60'}),
270
+ # html.Div(id='news-summary', style={'background': '#f8f9fa','padding': '15px','border-radius': '8px','max-height': '200px','overflow-y': 'auto'})
271
+ # ], style={'width': '48%', 'display': 'inline-block', 'margin-left': '4%'})
272
+ # ])
273
+ # ], style={'margin-top': '30px','padding': '20px','background': 'white','border-radius': '10px','box-shadow': '0 2px 10px rgba(0,0,0,0.1)'}),
274
 
275
  html.Div([
276
  html.H3("景氣燈號與 PMI 分析"),
 
421
 
422
  return result_text, fig
423
 
424
+ # 註解掉情緒分析回調函數
425
+ # @app.callback(
426
+ # dash.dependencies.Output('sentiment-gauge', 'children'),
427
+ # dash.dependencies.Output('news-summary', 'children')
428
+ # )
429
+ # def update_sentiment_analysis():
430
+ # """更新新聞情緒分析"""
431
+ # if not predictor:
432
+ # return html.Div("新聞情緒分析模型未初始化。"), html.Div("請檢查 'Bert_predict.py' 檔案是否存在。")
433
+
434
+ # try:
435
+ # sentiment_score, news_list = predictor.get_sentiment_score()
436
+ # except Exception as e:
437
+ # sentiment_score = None
438
+ # news_list = []
439
+ # print(f"情緒分析獲取失敗: {e}")
440
+
441
+ # # 1. 建立儀表板 (Gauge)
442
+ # if sentiment_score is not None:
443
+ # gauge_fig = go.Figure(go.Indicator(
444
+ # mode="gauge+number",
445
+ # value=sentiment_score,
446
+ # title={'text': "市場情緒分數 (0-100)"},
447
+ # domain={'x': [0, 1], 'y': [0, 1]},
448
+ # gauge={
449
+ # 'axis': {'range': [0, 100]},
450
+ # 'bar': {'color': "#667eea"},
451
+ # 'steps': [
452
+ # {'range': [0, 35], 'color': "rgba(217, 83, 79, 0.2)"},
453
+ # {'range': [35, 65], 'color': "rgba(240, 173, 78, 0.2)"},
454
+ # {'range': [65, 100], 'color': "rgba(92, 184, 92, 0.2)"}
455
+ # ],
456
+ # }
457
+ # ))
458
+ # gauge_fig.update_layout(height=200, margin=dict(l=30, r=30, t=50, b=20))
459
+ # gauge_content = dcc.Graph(figure=gauge_fig)
460
+ # else:
461
+ # # 處理無法計算分數的情況
462
+ # error_fig = go.Figure().add_annotation(text="今日尚無情緒分數", showarrow=False)
463
+ # error_fig.update_layout(height=200)
464
+ # gauge_content = dcc.Graph(figure=error_fig)
465
+
466
+
467
+ # # 2. 從 predictor 獲取分數最高的3則新聞
468
+ # top_news_list = predictor.get_news()
469
+
470
+ # # 3. 建立新聞摘要元件
471
+ # if top_news_list: # 如果列表不為空
472
+ # news_content = html.Div([
473
+ # html.P(f"• {news}", style={
474
+ # 'margin': '8px 0',
475
+ # 'padding-left': '5px',
476
+ # 'font-size': '14px',
477
+ # 'border-left': '3px solid #E74C3C'
478
+ # }) for news in top_news_list
479
+ # ])
480
+ # else:
481
+ # news_content = html.Div("今日尚無重大新聞摘要。")
482
+
483
+ # return gauge_content, news_content
 
484
 
485
 
486
  @app.callback(