Bladeren bron

增加数据周期

wangdalin 1 jaar geleden
bovenliggende
commit
c2ca1f4f89
1 gewijzigde bestanden met toevoegingen van 19 en 2 verwijderingen
  1. 19 2
      name_classify_api.py

+ 19 - 2
name_classify_api.py

@@ -1,5 +1,5 @@
 import pandas as pd 
 import pandas as pd 
-import math, json, os
+import math, json, os, time, shutil
 import openai, re, ast, requests
 import openai, re, ast, requests
 from fastapi import FastAPI, UploadFile, File, Form
 from fastapi import FastAPI, UploadFile, File, Form
 from pydantic import BaseModel
 from pydantic import BaseModel
@@ -114,9 +114,26 @@ async def create_upload_file(file: UploadFile = File(...), client_id: str = Form
 @app.post("/classify/")
 @app.post("/classify/")
 async def classify_data(request: ClassificationRequest):
 async def classify_data(request: ClassificationRequest):
     try:
     try:
+        current_time = time.time()
+        TIME_THRESHOLD_FILEPATH = 30 * 24 * 60 * 60
+        TIME_THRESHOLD_FILE = 10 * 24 * 60 * 60
+        basic_path = './process'
+        for root, dirs, files in os.walk(basic_path, topdown=False):
+            # 删除文件
+            for file in files:
+                file_path = os.path.join(root, file)
+                if current_time - os.path.getmtime(file_path) > TIME_THRESHOLD_FILE:
+                    print(f"删除文件: {file_path}")
+                    os.remove(file_path)
+            # 删除文件夹
+            for dir in dirs:
+                dir_path = os.path.join(root, dir)
+                if current_time - os.path.getmtime(dir_path) > TIME_THRESHOLD_FILEPATH:
+                    print(f"删除文件夹: {dir_path}")
+                    shutil.rmtree(dir_path)
         prompt = """提供的数据:{chunk}
         prompt = """提供的数据:{chunk}
                 返回的数据:"""
                 返回的数据:"""
-        work_path = f'./process/{request.client_id}'
+        work_path = f'{basic_path}/{request.client_id}'
         if not os.path.exists(work_path):
         if not os.path.exists(work_path):
             os.makedirs(work_path, exist_ok=True)
             os.makedirs(work_path, exist_ok=True)
         timestamp_str = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
         timestamp_str = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")