fiewolf1000 commited on
Commit
4a918c0
·
verified ·
1 Parent(s): c2b2703

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -71
app.py CHANGED
@@ -1,72 +1,60 @@
1
  import os
2
  import shutil
3
  from pathlib import Path
4
- from flask import Flask, render_template_string, send_file
5
- from update_predictions import Config, main_task, get_business_info
6
 
7
  app = Flask(__name__)
8
 
9
- # 统一配置(与update_predictions.py保持一致)
10
- Config = {
11
- "REPO_PATH": Path(__file__).parent.resolve(),
12
- "MODEL_PATH": os.path.join("/tmp", "Kronos_model"),
13
- # 关键修改:HTML和图表都存放在/tmp目录(可写)
14
- "HTML_PATH": os.path.join("/tmp", "index.html"),
15
- "CHART_PATH": os.path.join("/tmp", "prediction_chart.png")
16
- }
17
 
18
- # 确保/tmp目录下有模板文件
19
  def initialize_html_template():
20
- # 源模板路径(项目中的原始index.html)
21
- src_html = Config["REPO_PATH"] / "templates" / "index.html"
22
- # 目标路径(/tmp下的可写副本)
23
- dest_html = Path(Config["HTML_PATH"])
24
 
25
- # 如果目标不存在,从源模板复制
 
 
 
26
  if not dest_html.exists():
27
- # 确保/tmp目录存在
28
- dest_html.parent.mkdir(parents=True, exist_ok=True)
29
-
30
- if src_html.exists():
31
- shutil.copy2(src_html, dest_html)
32
- print(f"已复制模板文件到可写目录:{dest_html}")
33
- else:
34
- # 如果源模板也不存在,创建基础HTML
35
- base_html = """
36
- <!DOCTYPE html>
37
- <html>
38
- <head>
39
- <title>清华大学Kronos股票K型大模型上证指数预测</title>
40
- <style>
41
- body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }
42
- .metric { margin: 20px 0; padding: 10px; background: #f5f5f5; border-radius: 5px; }
43
- .metric-label { font-weight: bold; color: #333; }
44
- .metric-value { font-size: 1.5em; color: #2c3e50; }
45
- #chart-container { margin-top: 30px; }
46
- img { max-width: 100%; height: auto; }
47
- </style>
48
- </head>
49
- <body>
50
- <h1>清华大学Kronos股票K型大模型上证指数概率预测</h1>
51
- <p>最后更新时间:<strong id="update-time">未更新</strong>(UTC)</p>
52
- <p>同 步 网 站:<strong><a href="http://15115656.top" target="_blank">火狼工具站</a></strong></p>
53
- <div class="metric">
54
- <p class="metric-label">24个交易日上涨概率:</p>
55
- <p class="metric-value" id="upside-prob">--%</p>
56
- </div>
57
- <div class="metric">
58
- <p class="metric-label">波动率放大概率:</p>
59
- <p class="metric-value" id="vol-amp-prob">--%</p>
60
- </div>
61
- <div id="chart-container">
62
- <img src="/prediction_chart.png" alt="上证指数预测图表">
63
- </div>
64
- </body>
65
- </html>
66
- """
67
- with open(dest_html, 'w', encoding='utf-8') as f:
68
- f.write(base_html)
69
- print(f"已创建基础模板文件:{dest_html}")
70
 
71
  # 初始化模型(全局一次)
72
  from update_predictions import load_local_model as load_model
@@ -76,27 +64,25 @@ predictor = load_model()
76
  initialize_html_template()
77
 
78
  @app.route("/")
79
-
80
  def index():
81
  # 获取当前业务日
82
  current_business_date, _ = get_business_info()
83
 
84
  # 容错处理:检查配置键是否存在,不存在则执行主任务
85
- # 同时处理键存在但值为None的情况(首次运行)
86
  if ("LAST_INFERENCED_BUSINESS_DATE" not in Config
87
  or Config["LAST_INFERENCED_BUSINESS_DATE"] != current_business_date):
88
  main_task(predictor)
89
 
90
- # 读取并返回 HTML
91
- try:
92
- with open(Config["HTML_PATH"], 'r', encoding='utf-8') as f:
93
- html_content = f.read()
94
- return render_template_string(html_content)
95
- except FileNotFoundError:
96
- # 处理HTML文件不存在的情况
97
- return "预测页面生成中,请稍后刷新", 202
98
- except Exception as e:
99
- return f"页面加载错误:{str(e)}", 500
100
 
101
 
102
  @app.route("/prediction_chart.png")
@@ -110,3 +96,4 @@ def get_chart():
110
 
111
  if __name__ == "__main__":
112
  app.run(host="0.0.0.0", port=7860)
 
 
1
  import os
2
  import shutil
3
  from pathlib import Path
4
+ from flask import Flask, render_template, send_file
5
+ from update_predictions import Config as up_Config, main_task, get_business_info
6
 
7
  app = Flask(__name__)
8
 
9
+ # 确保与update_predictions.py共享配置
10
+ Config = up_Config
 
 
 
 
 
 
11
 
12
+ # 确保模板目录存在
13
  def initialize_html_template():
14
+ # 创建templates目录(如果不存在)
15
+ template_dir = Path(__file__).parent / "templates"
16
+ template_dir.mkdir(parents=True, exist_ok=True)
 
17
 
18
+ # 目标模板路径
19
+ dest_html = template_dir / "index.html"
20
+
21
+ # 如果模板不存在,创建基础模板
22
  if not dest_html.exists():
23
+ base_html = """
24
+ <!DOCTYPE html>
25
+ <html>
26
+ <head>
27
+ <title>清华大学Kronos股票K型大模型上证指数预测</title>
28
+ <style>
29
+ body { font-family: Arial, sans-serif; max-width: 1200px; margin: 0 auto; padding: 20px; }
30
+ .metric { margin: 20px 0; padding: 10px; background: #f5f5f5; border-radius: 5px; }
31
+ .metric-label { font-weight: bold; color: #333; }
32
+ .metric-value { font-size: 1.5em; color: #2c3e50; }
33
+ #chart-container { margin-top: 30px; }
34
+ img { max-width: 100%; height: auto; }
35
+ </style>
36
+ </head>
37
+ <body>
38
+ <h1>清华大学Kronos股票K型大模型上证指数概率预测</h1>
39
+ <p>最后更新时间:<strong>{{ update_time }}</strong>(UTC)</p>
40
+ <p>同 步 网 站:<strong><a href="http://15115656.top" target="_blank">火狼工具站</a></strong></p>
41
+ <div class="metric">
42
+ <p class="metric-label">24个交易日上涨概率:</p>
43
+ <p class="metric-value">{{ upside_prob }}%</p>
44
+ </div>
45
+ <div class="metric">
46
+ <p class="metric-label">波动率放大概率:</p>
47
+ <p class="metric-value">{{ vol_amp_prob }}%</p>
48
+ </div>
49
+ <div id="chart-container">
50
+ <img src="/prediction_chart.png" alt="上证指数预测图表">
51
+ </div>
52
+ </body>
53
+ </html>
54
+ """
55
+ with open(dest_html, 'w', encoding='utf-8') as f:
56
+ f.write(base_html)
57
+ print(f"已创建基础模板文件:{dest_html}")
 
 
 
 
 
 
 
 
58
 
59
  # 初始化模型(全局一次)
60
  from update_predictions import load_local_model as load_model
 
64
  initialize_html_template()
65
 
66
  @app.route("/")
 
67
  def index():
68
  # 获取当前业务日
69
  current_business_date, _ = get_business_info()
70
 
71
  # 容错处理:检查配置键是否存在,不存在则执行主任务
 
72
  if ("LAST_INFERENCED_BUSINESS_DATE" not in Config
73
  or Config["LAST_INFERENCED_BUSINESS_DATE"] != current_business_date):
74
  main_task(predictor)
75
 
76
+ # 从配置中获取需要显示的数据
77
+ update_time = Config.get("update_time", "未更新")
78
+ upside_prob = Config.get("upside_prob", "--")
79
+ vol_amp_prob = Config.get("vol_amp_prob", "--")
80
+
81
+ # 使用Jinja2模板渲染数据
82
+ return render_template("index.html",
83
+ update_time=update_time,
84
+ upside_prob=upside_prob,
85
+ vol_amp_prob=vol_amp_prob)
86
 
87
 
88
  @app.route("/prediction_chart.png")
 
96
 
97
  if __name__ == "__main__":
98
  app.run(host="0.0.0.0", port=7860)
99
+