main.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from fastapi import FastAPI, HTTPException
  2. import sys
  3. import subprocess
  4. app = FastAPI()
  5. @app.get("/")
  6. def root():
  7. return {
  8. "message": "服务运行中",
  9. "predict_api": "/api/v1/predictions",
  10. "docs": "/docs",
  11. }
  12. @app.get("/api/v1/power/short")
  13. def create_prediction(
  14. projs:str = 'JMJD01',
  15. init_time: str = "2026-11-25 08:00:00"
  16. ):
  17. PROJECT_DEFAULTS = {
  18. "JMJD01": {
  19. "lat": 22.555907,
  20. "lon": 113.135232,
  21. "capacity": 4050,
  22. },
  23. "YUWEI": {
  24. "lat": 22.615142,
  25. "lon": 110.305066,
  26. "capacity": 3000,
  27. },
  28. "ZHONGXIN": {
  29. "lat": 23.542156,
  30. "lon": 112.996233,
  31. "capacity": 2250,
  32. },
  33. }
  34. if projs not in PROJECT_DEFAULTS:
  35. raise HTTPException(
  36. status_code=400,
  37. detail=f"不支持的场站代码: {projs}",
  38. )
  39. cmd = [
  40. sys.executable,
  41. "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_short.py",
  42. "--projs", projs ,
  43. "--model_name", 'GRU',
  44. "--release", 'v24',
  45. "--lat", str(PROJECT_DEFAULTS[f'{projs}']['lat']),
  46. "--lon", str(PROJECT_DEFAULTS[f'{projs}']['lon']),
  47. "--short_term_seq", "288",
  48. "--medium_term_seq", "960",
  49. "--capacity", str(PROJECT_DEFAULTS[f'{projs}']['capacity']),
  50. "--saved_model_root", "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/saved_model",
  51. "--saved_result_root", "-",
  52. "--init_time", init_time,
  53. ]
  54. result = subprocess.run(
  55. cmd,
  56. cwd="/Users/hm/Desktop/forecast/forecast-algorithm",
  57. capture_output=True,
  58. text=True,
  59. )
  60. if result.returncode != 0:
  61. raise HTTPException(
  62. status_code=500,
  63. detail={
  64. "message": "预测失败",
  65. "return_code": result.returncode,
  66. "stdout": result.stdout,
  67. "stderr": result.stderr,
  68. },
  69. )
  70. return {
  71. "success": True,
  72. "message": "预测完成",
  73. "return_code": result.returncode,
  74. "stdout": result.stdout,
  75. }
  76. @app.get("/api/v1/power/medium")
  77. def create_prediction(
  78. projs:str = 'JMJD01',
  79. init_time: str = "2026-11-25 08:00:00"
  80. ):
  81. PROJECT_DEFAULTS = {
  82. "JMJD01": {
  83. "lat": 22.555907,
  84. "lon": 113.135232,
  85. "capacity": 4050,
  86. },
  87. "YUWEI": {
  88. "lat": 22.615142,
  89. "lon": 110.305066,
  90. "capacity": 3000,
  91. },
  92. "ZHONGXIN": {
  93. "lat": 23.542156,
  94. "lon": 112.996233,
  95. "capacity": 2250,
  96. },
  97. }
  98. if projs not in PROJECT_DEFAULTS:
  99. raise HTTPException(
  100. status_code=400,
  101. detail=f"不支持的场站代码: {projs}",
  102. )
  103. cmd = [
  104. sys.executable,
  105. "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_medium.py",
  106. "--projs", projs ,
  107. "--model_name", 'GRU',
  108. "--release", 'v24',
  109. "--lat", str(PROJECT_DEFAULTS[f'{projs}']['lat']),
  110. "--lon", str(PROJECT_DEFAULTS[f'{projs}']['lon']),
  111. "--short_term_seq", "288",
  112. "--medium_term_seq", "960",
  113. "--capacity", str(PROJECT_DEFAULTS[f'{projs}']['capacity']),
  114. "--saved_model_root", "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/saved_model",
  115. "--saved_result_root", "-",
  116. "--init_time", init_time,
  117. ]
  118. result = subprocess.run(
  119. cmd,
  120. cwd="/Users/hm/Desktop/forecast/forecast-algorithm",
  121. capture_output=True,
  122. text=True,
  123. )
  124. if result.returncode != 0:
  125. raise HTTPException(
  126. status_code=500,
  127. detail={
  128. "message": "预测失败",
  129. "return_code": result.returncode,
  130. "stdout": result.stdout,
  131. "stderr": result.stderr,
  132. },
  133. )
  134. return {
  135. "success": True,
  136. "message": "预测完成",
  137. "return_code": result.returncode,
  138. "stdout": result.stdout,
  139. }
  140. if __name__ == "__main__":
  141. import uvicorn
  142. uvicorn.run(app, host="0.0.0.0", port=8000)