|
|
|
|
@ -4,6 +4,8 @@ import datetime
|
|
|
|
|
import logging
|
|
|
|
|
import shutil
|
|
|
|
|
import uvicorn
|
|
|
|
|
import schedule
|
|
|
|
|
import threading
|
|
|
|
|
import yaml
|
|
|
|
|
import numpy as np
|
|
|
|
|
from fastapi import FastAPI, Request, File, UploadFile
|
|
|
|
|
@ -20,6 +22,12 @@ app = FastAPI()
|
|
|
|
|
# 控制是否打印的宏定义
|
|
|
|
|
PRINT_LOG = True
|
|
|
|
|
|
|
|
|
|
# 日志保留天数
|
|
|
|
|
DAYS = 1
|
|
|
|
|
|
|
|
|
|
# 清理文件夹
|
|
|
|
|
DIRS = ["train_api", "inference_api", "evaluate_api"]
|
|
|
|
|
|
|
|
|
|
# 初始化配置文件
|
|
|
|
|
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "config/config.yaml"))
|
|
|
|
|
|
|
|
|
|
@ -69,6 +77,32 @@ def update_log_handler(log_path):
|
|
|
|
|
file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S'))
|
|
|
|
|
logger.addHandler(file_handler)
|
|
|
|
|
|
|
|
|
|
# 定义清理旧文件的函数
|
|
|
|
|
def clean_old_files(directory, days=DAYS):
|
|
|
|
|
now = time.time()
|
|
|
|
|
cutoff = now - (days * 86400) # 1天前的时间戳
|
|
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(directory):
|
|
|
|
|
for file in files:
|
|
|
|
|
file_path = os.path.join(root, file)
|
|
|
|
|
if os.path.getmtime(file_path) < cutoff:
|
|
|
|
|
os.remove(file_path)
|
|
|
|
|
log_print(f"Removed old file: {file_path}")
|
|
|
|
|
|
|
|
|
|
# 定期清理任务
|
|
|
|
|
def schedule_cleanup():
|
|
|
|
|
directories = DIRS
|
|
|
|
|
for directory in directories:
|
|
|
|
|
abs_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), directory))
|
|
|
|
|
clean_old_files(abs_directory)
|
|
|
|
|
|
|
|
|
|
# 启动定时任务
|
|
|
|
|
def start_scheduler():
|
|
|
|
|
schedule.every(1).hours.do(schedule_cleanup)
|
|
|
|
|
while True:
|
|
|
|
|
schedule.run_pending()
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
# 定义训练接口
|
|
|
|
|
@app.post("/train/")
|
|
|
|
|
async def train_model(request: Request, features_list: List[Features]):
|
|
|
|
|
@ -344,4 +378,10 @@ if __name__ == "__main__":
|
|
|
|
|
app.mount("/evaluate_api", StaticFiles(directory=static_dir_evaluate), name="static_dir_evaluate")
|
|
|
|
|
app.mount("/inference_api", StaticFiles(directory=static_dir_inference), name="static_dir_inference")
|
|
|
|
|
app.mount("/models", StaticFiles(directory=static_dir_models), name="static_dir_models")
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8088, reload=False)
|
|
|
|
|
|
|
|
|
|
# 启动定时任务
|
|
|
|
|
scheduler_thread = threading.Thread(target=start_scheduler)
|
|
|
|
|
scheduler_thread.daemon = True
|
|
|
|
|
scheduler_thread.start()
|
|
|
|
|
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=8089, reload=False)
|