wsj1995 commited on
Commit
d9bed9b
·
1 Parent(s): 425b993

feat: 修改文件存储规则

Browse files
Files changed (1) hide show
  1. api.py +12 -12
api.py CHANGED
@@ -58,15 +58,15 @@ def download_file(url: str):
58
  def read_root():
59
  return {"Hello": "World!"}
60
 
61
- @router.post("/upload/{userId}/{modelId}/{filename}")
62
- async def upload_file(userId: str, modelId: str, filename: str, file: UploadFile = File(...)):
63
- file_location_folder = os.path.join(UPLOAD_DIRECTORY,userId)
64
  file_location = os.path.join(file_location_folder, filename)
65
  os.makedirs(file_location_folder, exist_ok=True)
66
  try:
67
  with open(file_location, "wb") as buffer:
68
  buffer.write(await file.read())
69
- callback(modelId,'UPLOADING')
70
  pathInRepo = f"{userId}/{filename}"
71
  huggingfaceApi = HfApi(token=HUGGINGFACE_API_TOKEN)
72
  huggingfaceApi.upload_file(
@@ -75,17 +75,17 @@ async def upload_file(userId: str, modelId: str, filename: str, file: UploadFile
75
  repo_id=REPO_ID,
76
  repo_type="model"
77
  )
78
- callback(modelId,'UPLOADED')
79
  except Exception as e:
80
  print(e)
81
- callback(modelId,'FAIL')
82
  os.remove(file_location)
83
- return {'success': True, 'id': modelId}
84
 
85
 
86
- @router.get("/download/{userId}/{filename}")
87
- def download(userId: str, filename: str):
88
- pathInRepo = f"{userId}/{filename}"
89
  response = download_file(pathInRepo)
90
  # 创建流式响应
91
  return StreamingResponse(response.iter_content(chunk_size=1024),
@@ -95,7 +95,7 @@ def download(userId: str, filename: str):
95
 
96
 
97
 
98
- def callback(modelId,status):
99
  timestamp = int(time.time()) + 60
100
  rand = 0
101
  uid = 0
@@ -104,6 +104,6 @@ def callback(modelId,status):
104
  md5 = md5hash.hexdigest()
105
  res = requests.post(f"{os.environ.get('CALLBACK_DOMAIN')}{url}?auth_key={timestamp}-{rand}-{uid}-{md5}",json={
106
  'status': status,
107
- 'model_id': modelId
108
  })
109
  print(f"回调结果 {res.status_code}")
 
58
  def read_root():
59
  return {"Hello": "World!"}
60
 
61
+ @router.post("/upload/{userId}/{modelId}/{modelVersionId}/{filename}")
62
+ async def upload_file(userId: str, modelId: str, modelVersionId:str, filename: str, file: UploadFile = File(...)):
63
+ file_location_folder = os.path.join(UPLOAD_DIRECTORY,userId,modelId,modelVersionId)
64
  file_location = os.path.join(file_location_folder, filename)
65
  os.makedirs(file_location_folder, exist_ok=True)
66
  try:
67
  with open(file_location, "wb") as buffer:
68
  buffer.write(await file.read())
69
+ callback(modelVersionId,'UPLOADING')
70
  pathInRepo = f"{userId}/{filename}"
71
  huggingfaceApi = HfApi(token=HUGGINGFACE_API_TOKEN)
72
  huggingfaceApi.upload_file(
 
75
  repo_id=REPO_ID,
76
  repo_type="model"
77
  )
78
+ callback(modelVersionId,'UPLOADED')
79
  except Exception as e:
80
  print(e)
81
+ callback(modelVersionId,'FAIL')
82
  os.remove(file_location)
83
+ return {'success': True, 'id': modelVersionId}
84
 
85
 
86
+ @router.get("/download/{userId}/{modelId}/{modelVersionId}/{filename}")
87
+ def download(userId: str,modelId:str, modelVersionId:str, filename: str):
88
+ pathInRepo = f"{userId}/{modelId}/{modelVersionId}/{filename}"
89
  response = download_file(pathInRepo)
90
  # 创建流式响应
91
  return StreamingResponse(response.iter_content(chunk_size=1024),
 
95
 
96
 
97
 
98
+ def callback(modelVersionId,status):
99
  timestamp = int(time.time()) + 60
100
  rand = 0
101
  uid = 0
 
104
  md5 = md5hash.hexdigest()
105
  res = requests.post(f"{os.environ.get('CALLBACK_DOMAIN')}{url}?auth_key={timestamp}-{rand}-{uid}-{md5}",json={
106
  'status': status,
107
+ 'model_version_id': modelVersionId
108
  })
109
  print(f"回调结果 {res.status_code}")