main.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from fastapi import FastAPI, HTTPException
  2. from fastapi.responses import HTMLResponse
  3. import sys
  4. import subprocess
  5. import yaml
  6. import os
  7. app = FastAPI()
  8. # 读取配置文件
  9. BASE_DIR = os.path.dirname(os.path.abspath(__file__))
  10. with open(os.path.join(BASE_DIR,"config/config.yaml"), "r", encoding="utf-8") as f:
  11. config = yaml.safe_load(f)
  12. PROJECT_DEFAULTS = {
  13. "JMJD01": {
  14. "lat": 22.555907,
  15. "lon": 113.135232,
  16. "capacity": 4050,
  17. },
  18. "YUWEI": {
  19. "lat": 22.615142,
  20. "lon": 110.305066,
  21. "capacity": 3000,
  22. },
  23. "ZHONGXIN": {
  24. "lat": 23.542156,
  25. "lon": 112.996233,
  26. "capacity": 2250,
  27. },
  28. }
  29. API_HOME_PATH = os.path.join(BASE_DIR, "templates", "api_home.html")
  30. @app.get("/", response_class=HTMLResponse)
  31. def root():
  32. with open(API_HOME_PATH, "r", encoding="utf-8") as f:
  33. return f.read()
  34. @app.get("/api/v1/power/short")
  35. def create_prediction(
  36. projs:str = 'JMJD01',
  37. init_time: str = "2026-11-25 08:00:00"
  38. ):
  39. if projs not in PROJECT_DEFAULTS:
  40. raise HTTPException(
  41. status_code=400,
  42. detail=f"不支持的场站代码: {projs}",
  43. )
  44. cmd = [
  45. sys.executable,
  46. config['model']['short_algorithm_file'],
  47. "--projs", projs ,
  48. "--model_name", 'GRU',
  49. "--release", 'v24',
  50. "--lat", str(PROJECT_DEFAULTS[f'{projs}']['lat']),
  51. "--lon", str(PROJECT_DEFAULTS[f'{projs}']['lon']),
  52. "--short_term_seq", "288",
  53. "--medium_term_seq", "960",
  54. "--capacity", str(PROJECT_DEFAULTS[f'{projs}']['capacity']),
  55. "--saved_model_root", config['model']['saved_model_root'],
  56. "--saved_result_root", "-",
  57. "--init_time", init_time,
  58. ]
  59. result = subprocess.run(
  60. cmd,
  61. cwd=BASE_DIR,
  62. capture_output=True,
  63. text=True,
  64. )
  65. if result.returncode != 0:
  66. raise HTTPException(
  67. status_code=500,
  68. detail={
  69. "message": "预测失败",
  70. "return_code": result.returncode,
  71. "stdout": result.stdout,
  72. "stderr": result.stderr,
  73. },
  74. )
  75. return {
  76. "success": True,
  77. "message": "预测完成",
  78. "return_code": result.returncode,
  79. "stdout": result.stdout,
  80. }
  81. @app.get("/api/v1/power/medium")
  82. def create_prediction(
  83. projs:str = 'JMJD01',
  84. init_time: str = "2026-11-25 08:00:00"
  85. ):
  86. if projs not in PROJECT_DEFAULTS:
  87. raise HTTPException(
  88. status_code=400,
  89. detail=f"不支持的场站代码: {projs}",
  90. )
  91. cmd = [
  92. sys.executable,
  93. config['model']['medium_algorithm_file'],
  94. "--projs", projs ,
  95. "--model_name", 'GRU',
  96. "--release", 'v24',
  97. "--lat", str(PROJECT_DEFAULTS[f'{projs}']['lat']),
  98. "--lon", str(PROJECT_DEFAULTS[f'{projs}']['lon']),
  99. "--short_term_seq", "288",
  100. "--medium_term_seq", "960",
  101. "--capacity", str(PROJECT_DEFAULTS[f'{projs}']['capacity']),
  102. "--saved_model_root", config['model']['saved_model_root'],
  103. "--saved_result_root", "-",
  104. "--init_time", init_time,
  105. ]
  106. result = subprocess.run(
  107. cmd,
  108. cwd=BASE_DIR,
  109. capture_output=True,
  110. text=True,
  111. )
  112. if result.returncode != 0:
  113. raise HTTPException(
  114. status_code=500,
  115. detail={
  116. "message": "预测失败",
  117. "return_code": result.returncode,
  118. "stdout": result.stdout,
  119. "stderr": result.stderr,
  120. },
  121. )
  122. return {
  123. "success": True,
  124. "message": "预测完成",
  125. "return_code": result.returncode,
  126. "stdout": result.stdout,
  127. }
  128. if __name__ == "__main__":
  129. import uvicorn
  130. uvicorn.run(
  131. app,
  132. host=config['FastAPI']['host'],
  133. port=config['FastAPI']['port']
  134. )