| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- from fastapi import FastAPI, HTTPException
- import sys
- import subprocess
- app = FastAPI()
- @app.get("/")
- def root():
- return {
- "message": "服务运行中",
- "predict_api": "/api/v1/predictions",
- "docs": "/docs",
- }
- @app.get("/api/v1/power/short")
- def create_prediction(
- projs:str = 'JMJD01',
- init_time: str = "2026-11-25 08:00:00"
- ):
- PROJECT_DEFAULTS = {
- "JMJD01": {
- "lat": 22.555907,
- "lon": 113.135232,
- "capacity": 4050,
- },
- "YUWEI": {
- "lat": 22.615142,
- "lon": 110.305066,
- "capacity": 3000,
- },
- "ZHONGXIN": {
- "lat": 23.542156,
- "lon": 112.996233,
- "capacity": 2250,
- },
- }
- if projs not in PROJECT_DEFAULTS:
- raise HTTPException(
- status_code=400,
- detail=f"不支持的场站代码: {projs}",
- )
- cmd = [
- sys.executable,
- "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_short.py",
- "--projs", projs ,
- "--model_name", 'GRU',
- "--release", 'v24',
- "--lat", str(PROJECT_DEFAULTS[f'{projs}']['lat']),
- "--lon", str(PROJECT_DEFAULTS[f'{projs}']['lon']),
- "--short_term_seq", "288",
- "--medium_term_seq", "960",
- "--capacity", str(PROJECT_DEFAULTS[f'{projs}']['capacity']),
- "--saved_model_root", "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/saved_model",
- "--saved_result_root", "-",
- "--init_time", init_time,
- ]
- result = subprocess.run(
- cmd,
- cwd="/Users/hm/Desktop/forecast/forecast-algorithm",
- capture_output=True,
- text=True,
- )
- if result.returncode != 0:
- raise HTTPException(
- status_code=500,
- detail={
- "message": "预测失败",
- "return_code": result.returncode,
- "stdout": result.stdout,
- "stderr": result.stderr,
- },
- )
- return {
- "success": True,
- "message": "预测完成",
- "return_code": result.returncode,
- "stdout": result.stdout,
- }
- @app.get("/api/v1/power/medium")
- def create_prediction(
- projs:str = 'JMJD01',
- init_time: str = "2026-11-25 08:00:00"
- ):
- PROJECT_DEFAULTS = {
- "JMJD01": {
- "lat": 22.555907,
- "lon": 113.135232,
- "capacity": 4050,
- },
- "YUWEI": {
- "lat": 22.615142,
- "lon": 110.305066,
- "capacity": 3000,
- },
- "ZHONGXIN": {
- "lat": 23.542156,
- "lon": 112.996233,
- "capacity": 2250,
- },
- }
- if projs not in PROJECT_DEFAULTS:
- raise HTTPException(
- status_code=400,
- detail=f"不支持的场站代码: {projs}",
- )
- cmd = [
- sys.executable,
- "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/scripts/prediction_medium.py",
- "--projs", projs ,
- "--model_name", 'GRU',
- "--release", 'v24',
- "--lat", str(PROJECT_DEFAULTS[f'{projs}']['lat']),
- "--lon", str(PROJECT_DEFAULTS[f'{projs}']['lon']),
- "--short_term_seq", "288",
- "--medium_term_seq", "960",
- "--capacity", str(PROJECT_DEFAULTS[f'{projs}']['capacity']),
- "--saved_model_root", "/Users/hm/Desktop/forecast/forecast-algorithm/medium_short_term/saved_model",
- "--saved_result_root", "-",
- "--init_time", init_time,
- ]
- result = subprocess.run(
- cmd,
- cwd="/Users/hm/Desktop/forecast/forecast-algorithm",
- capture_output=True,
- text=True,
- )
- if result.returncode != 0:
- raise HTTPException(
- status_code=500,
- detail={
- "message": "预测失败",
- "return_code": result.returncode,
- "stdout": result.stdout,
- "stderr": result.stderr,
- },
- )
- return {
- "success": True,
- "message": "预测完成",
- "return_code": result.returncode,
- "stdout": result.stdout,
- }
- if __name__ == "__main__":
- import uvicorn
- uvicorn.run(app, host="0.0.0.0", port=8000)
|