tbdavid2019 commited on
Commit
f101777
·
1 Parent(s): a79dcc6
Files changed (1) hide show
  1. app.py +34 -9
app.py CHANGED
@@ -2,6 +2,10 @@ import gradio as gr
2
  import yfinance as yf
3
  import pandas as pd
4
  import matplotlib.pyplot as plt
 
 
 
 
5
  from prophet import Prophet
6
  from datetime import datetime, timedelta
7
  import logging
@@ -10,6 +14,23 @@ import logging
10
  logging.basicConfig(level=logging.INFO,
11
  format='%(asctime)s - %(levelname)s - %(message)s')
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def predict_stock_price(stock_code, period, prediction_days):
14
  try:
15
  # 下載股票數據
@@ -67,12 +88,16 @@ def predict_stock_price(stock_code, period, prediction_days):
67
  logging.error(f"預測過程發生錯誤: {str(e)}")
68
  return f"預測過程發生錯誤: {str(e)}", None
69
 
 
 
 
70
  # Gradio 介面
71
  with gr.Blocks() as demo:
72
- gr.Markdown("# 台股預測系統")
73
 
74
- with gr.Row():
75
- with gr.Column():
 
76
  stock_input = gr.Textbox(
77
  label="股票代碼",
78
  placeholder="例如: 2330.TW",
@@ -92,12 +117,12 @@ with gr.Blocks() as demo:
92
  step=1,
93
  label="預測天數"
94
  )
95
-
96
- predict_button = gr.Button("開始預測", variant="primary")
97
-
98
- with gr.Column():
99
- output_plot = gr.Plot(label="股價預測圖")
100
- output_text = gr.Textbox(label="預測結果")
101
 
102
  predict_button.click(
103
  predict_stock_price,
 
2
  import yfinance as yf
3
  import pandas as pd
4
  import matplotlib.pyplot as plt
5
+ import matplotlib as mpl
6
+ import matplotlib.font_manager as fm
7
+ import tempfile
8
+ import requests
9
  from prophet import Prophet
10
  from datetime import datetime, timedelta
11
  import logging
 
14
  logging.basicConfig(level=logging.INFO,
15
  format='%(asctime)s - %(levelname)s - %(message)s')
16
 
17
+ # 字體設置
18
+ def setup_font():
19
+ try:
20
+ url_font = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_"
21
+ response_font = requests.get(url_font)
22
+
23
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.ttf') as tmp_file:
24
+ tmp_file.write(response_font.content)
25
+ tmp_file_path = tmp_file.name
26
+
27
+ fm.fontManager.addfont(tmp_file_path)
28
+ mpl.rc('font', family='Taipei Sans TC Beta')
29
+ except Exception as e:
30
+ logging.error(f"字體設置失敗: {str(e)}")
31
+ # 使用備用字體
32
+ mpl.rc('font', family='SimHei')
33
+
34
  def predict_stock_price(stock_code, period, prediction_days):
35
  try:
36
  # 下載股票數據
 
88
  logging.error(f"預測過程發生錯誤: {str(e)}")
89
  return f"預測過程發生錯誤: {str(e)}", None
90
 
91
+ # 初始化字體
92
+ setup_font()
93
+
94
  # Gradio 介面
95
  with gr.Blocks() as demo:
96
+ gr.Markdown("# 股票價格 Stock Price 預測系統 Prophet")
97
 
98
+ with gr.Column():
99
+ # 輸入區域
100
+ with gr.Row():
101
  stock_input = gr.Textbox(
102
  label="股票代碼",
103
  placeholder="例如: 2330.TW",
 
117
  step=1,
118
  label="預測天數"
119
  )
120
+
121
+ predict_button = gr.Button("開始預測", variant="primary")
122
+
123
+ # 輸出區域(垂直排列)
124
+ output_plot = gr.Plot(label="股價預測圖")
125
+ output_text = gr.Textbox(label="預測結果", lines=10)
126
 
127
  predict_button.click(
128
  predict_stock_price,