Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1,72 +1,60 @@ | |
| 1 | 
             
            import os
         | 
| 2 | 
             
            import shutil
         | 
| 3 | 
             
            from pathlib import Path
         | 
| 4 | 
            -
            from flask import Flask,  | 
| 5 | 
            -
            from update_predictions import Config, main_task, get_business_info
         | 
| 6 |  | 
| 7 | 
             
            app = Flask(__name__)
         | 
| 8 |  | 
| 9 | 
            -
            #  | 
| 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 | 
            -
            #  | 
| 19 | 
             
            def initialize_html_template():
         | 
| 20 | 
            -
                #  | 
| 21 | 
            -
                 | 
| 22 | 
            -
                 | 
| 23 | 
            -
                dest_html = Path(Config["HTML_PATH"])
         | 
| 24 |  | 
| 25 | 
            -
                #  | 
|  | |
|  | |
|  | |
| 26 | 
             
                if not dest_html.exists():
         | 
| 27 | 
            -
                     | 
| 28 | 
            -
                     | 
| 29 | 
            -
                    
         | 
| 30 | 
            -
                     | 
| 31 | 
            -
                         | 
| 32 | 
            -
                         | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
                             | 
| 48 | 
            -
                        </ | 
| 49 | 
            -
                        < | 
| 50 | 
            -
                            < | 
| 51 | 
            -
                            <p | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 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 | 
            -
                #  | 
| 91 | 
            -
                 | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
                 | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 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 | 
            +
                
         |