Forráskód Böngészése

Merge feature/jhm into master

小明同学 2 órája
szülő
commit
be094f003f

BIN
.DS_Store


BIN
forecast-algorithm/.DS_Store


+ 16 - 43
forecast-algorithm/README.md

@@ -147,59 +147,32 @@ python -m pip install fastapi uvicorn psycopg2-binary
 python -m pip install -r medium_short_term/requirements.txt
 ```
 
-## 5. 数据库配置
+## 5. Config配置
 
 ```sh
-目前是测试数据库,手动导入数据库环境变量
-export PGHOST="112.124.19.19"
-export PGPORT="5432"
-export PGDATABASE="electricity"
-export PGUSER="postgres"
-export PGPOSSWORD="ow9NV!lkf"
-```
-
-## 6. 启动 FastAPI 服务
-
-修改配置文件**config/config.yaml**
-
-```yaml
-FastAPI: # 关于fast api的配置
-  host: "0.0.0.0" 
-  port: 8000
+DataBase:  数据库配置
+  host: "112.124.19.19"
+  port: 5432
+  user: "postgres"
+  password: "ow9NV!lkf"
+  database: "electricity"
 
-model: # 关于超短期,短期,中期模型权重和模型脚本路径配置
-	# 项目路径('/Users/hm/Desktop/forecast/forecast-algorithm')+算法路径('medium_short_term/scripts/prediction_short.py')
-  short_algorithm_file: '/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_short.py'
-  medium_algorithm_file: '/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_medium.py'
-  # 项目路径('/Users/hm/Desktop/forecast/forecast-algorithm')+权重路径('medium_short_term/saved_model')
-  saved_model_root: "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/saved_model"
+FastAPI: FastApi配置
+  host: "0.0.0.0"
+  port: 8090
 
+Project: 项目根目录
+  project_root: "/Users/hm/Desktop/forecast"
 ```
 
-在**forecast-algorithm**目录执行:
+# 6. 接口程序启动
 
-```sh
-python main.py
-```
+运行代码
 
-默认监听:
-
-```python
-http://0.0.0.0:8000
+```shell
+bash run.sh ./config
 ```
 
-本机访问:
-
-```text
-http://127.0.0.1:8000
-```
-
-> 访问http://127.0.0.1:8000展示API主页信息
-
-<img src="assets/API_home_page.png" alt="image-20261026105801289" style="zoom:25%;"/>
-
-
-
 
 ## 7. 调用短期预测接口
 

+ 10 - 5
forecast-algorithm/config/config.yaml

@@ -1,8 +1,13 @@
+DataBase:
+  host: "112.124.19.19"
+  port: 5432
+  user: "postgres"
+  password: "ow9NV!lkf"
+  database: "electricity"
+
 FastAPI:
   host: "0.0.0.0"
-  port: 8000
+  port: 8090
 
-model:
-  short_algorithm_file: '/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_short.py'
-  medium_algorithm_file: '/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_medium.py'
-  saved_model_root: "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/saved_model"
+Project:
+  project_root: "/Users/hm/Desktop/forecast"

+ 4 - 4
forecast-algorithm/main.py

@@ -52,7 +52,7 @@ def create_prediction(
 
     cmd = [
         sys.executable,
-        config['model']['short_algorithm_file'],
+        os.path.join(BASE_DIR, "medium_short_term", "scripts", "prediction_short.py"),
         "--projs", projs ,
         "--model_name", 'GRU',
         "--release", 'v24',
@@ -61,7 +61,7 @@ def create_prediction(
         "--short_term_seq", "288",
         "--medium_term_seq", "960",
         "--capacity", str(PROJECT_DEFAULTS[f'{projs}']['capacity']),
-        "--saved_model_root", config['model']['saved_model_root'],
+        "--saved_model_root", os.path.join(BASE_DIR,"medium_short_term","saved_model"),
         "--saved_result_root", "-",
         "--init_time", init_time,
     ]
@@ -105,7 +105,7 @@ def create_prediction(
 
     cmd = [
         sys.executable,
-        config['model']['medium_algorithm_file'],
+        os.path.join(BASE_DIR, "medium_short_term", "scripts", "prediction_medium.py"),
         "--projs", projs ,
         "--model_name", 'GRU',
         "--release", 'v24',
@@ -114,7 +114,7 @@ def create_prediction(
         "--short_term_seq", "288",
         "--medium_term_seq", "960",
         "--capacity", str(PROJECT_DEFAULTS[f'{projs}']['capacity']),
-        "--saved_model_root", config['model']['saved_model_root'],
+        "--saved_model_root", os.path.join(BASE_DIR,"medium_short_term","saved_model"),
         "--saved_result_root", "-",
         "--init_time", init_time,
     ]

BIN
forecast-algorithm/medium_short_term/scripts/__pycache__/ModelLoading.cpython-314.pyc


+ 2 - 2
forecast-algorithm/medium_short_term/scripts/prediction_medium.py

@@ -84,7 +84,7 @@ def _to_epoch_seconds(value: Union[int, float, str, datetime]) -> int:
 
     return int(ts.timestamp())
 
-def get_short_term_weather_forecast(
+def get_medium_term_weather_forecast(
     station_id: int,
     start_time: Union[int, float, str, datetime],
     data_model_name: str = "EC",
@@ -465,7 +465,7 @@ if __name__ == '__main__':
         # 中期:输出 =(1,960,9),输出 =(1,960)
         # 注意⚠️:特征顺序不要改变,保持上述描述顺序。
     init_time = args.init_time  #YYYY-MM-DD HH:MM:SS
-    df = get_short_term_weather_forecast(
+    df = get_medium_term_weather_forecast(
             station_id = ststation_id[f'{args.projs}'],
             start_time = init_time,
             data_model_name = "EC",

+ 11 - 5
forecast-algorithm/medium_short_term/scripts/prediction_short.py

@@ -10,6 +10,7 @@ import os
 from typing import Union
 import psycopg2
 from psycopg2.extras import execute_values
+import yaml
 
 weather_utils_path = os.path.abspath(
     os.path.join(os.path.dirname(__file__), "..")
@@ -25,12 +26,17 @@ _LOCAL_COLS = ("time_shanghai", "time_local", "time_bj", "时间戳")
 # ============================================================
 # 数据库配置
 # ============================================================
+# 读取配置文件
+BASE_DIR = os.getenv("ConfigDir", "--")
+with open(os.path.join(BASE_DIR,"config.yaml"), "r", encoding="utf-8") as f:
+    config = yaml.safe_load(f)
+
 DB_CONFIG = {
-    "host": os.getenv("PGHOST", "112.124.19.19"),
-    "port": int(os.getenv("PGPORT", "5432")),
-    "dbname": os.getenv("PGDATABASE", "electricity"),
-    "user": os.getenv("PGUSER", "postgres"),
-    "password": os.getenv("PGPOSSWORD", "ow9NV!lkf"),
+    "host":config['DataBase']['host'],
+    "port": int(config['DataBase']['port']),
+    "dbname": config['DataBase']['database'],
+    "user": config['DataBase']['user'],
+    "password": config['DataBase']['password'],
     "connect_timeout": 10,
 }
 

+ 21 - 0
forecast-algorithm/run.sh

@@ -0,0 +1,21 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+if [ "$#" -lt 1 ]; then
+  echo "Usage: bash run.sh /path/to/config"
+  exit 1
+fi
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+CONFIG_DIR="$1"
+
+# 相对路径转换为绝对路径
+if [[ "$CONFIG_DIR" != /* ]]; then
+  CONFIG_DIR="$(cd "$CONFIG_DIR" && pwd)"
+fi
+
+export ConfigDir="$CONFIG_DIR"
+
+cd "$SCRIPT_DIR"
+exec python main.py