Feat: 모델 삭제 API 구현

This commit is contained in:
김진현 2024-09-18 02:20:27 +09:00
parent 085aa71791
commit 0503da4e41
2 changed files with 17 additions and 3 deletions

View File

@ -2,7 +2,8 @@ from fastapi import APIRouter, HTTPException
from schemas.model_create_request import ModelCreateRequest from schemas.model_create_request import ModelCreateRequest
from services.init_model import create_pretrained_model, create_default_model from services.init_model import create_pretrained_model, create_default_model
from services.load_model import load_model from services.load_model import load_model
from utils.file_utils import get_model_paths from utils.file_utils import get_model_paths, delete_file
import re
router = APIRouter() router = APIRouter()
@ -42,8 +43,16 @@ def model_create(request: ModelCreateRequest):
return {"model_path": model_path} return {"model_path": model_path}
@router.delete("/delete", status_code=204) @router.delete("/delete", status_code=204)
def model_delete(): def model_delete(model_path:str):
pass pattern = r'^resources[/\\]projects[/\\](\d+)[/\\]models[/\\]([a-f0-9\-]+)\.pt$'
if not re.match(pattern, model_path):
raise HTTPException(status_code=400,
detail= "Invalid path format")
try:
delete_file(model_path)
except FileNotFoundError:
raise HTTPException(status_code=404,
detail= "모델을 찾을 수 없습니다.")
@router.post("/upload") @router.post("/upload")
def model_upload(): def model_upload():

View File

@ -152,5 +152,10 @@ def join_path(path, *paths):
def get_model_paths(project_id:int): def get_model_paths(project_id:int):
path = os.path.join("resources","projects",str(project_id), "models") path = os.path.join("resources","projects",str(project_id), "models")
if not os.path.exists(path):
raise FileNotFoundError()
files = os.listdir(path) files = os.listdir(path)
return [os.path.join(path, file) for file in files if file.endswith(".pt")] return [os.path.join(path, file) for file in files if file.endswith(".pt")]
def delete_file(path):
os.remove(path)