2023-12-27 06:02:17 +00:00
|
|
|
from fastapi import Depends, FastAPI, HTTPException, status
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from typing import List, Union, Optional
|
|
|
|
|
|
|
|
from fastapi import APIRouter
|
|
|
|
from pydantic import BaseModel
|
|
|
|
import json
|
|
|
|
from apps.web.models.modelfiles import (
|
|
|
|
Modelfiles,
|
|
|
|
ModelfileForm,
|
2023-12-27 06:51:52 +00:00
|
|
|
ModelfileTagNameForm,
|
|
|
|
ModelfileUpdateForm,
|
2023-12-27 06:02:17 +00:00
|
|
|
ModelfileResponse,
|
|
|
|
)
|
|
|
|
|
2024-02-09 00:05:01 +00:00
|
|
|
from utils.utils import get_current_user, get_admin_user
|
2023-12-27 06:02:17 +00:00
|
|
|
from constants import ERROR_MESSAGES
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
############################
|
|
|
|
# GetModelfiles
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/", response_model=List[ModelfileResponse])
|
2024-03-31 08:13:39 +00:00
|
|
|
async def get_modelfiles(
|
|
|
|
skip: int = 0, limit: int = 50, user=Depends(get_current_user)
|
|
|
|
):
|
2023-12-30 10:53:33 +00:00
|
|
|
return Modelfiles.get_modelfiles(skip, limit)
|
2023-12-27 06:02:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
# CreateNewModelfile
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/create", response_model=Optional[ModelfileResponse])
|
2024-03-31 08:13:39 +00:00
|
|
|
async def create_new_modelfile(form_data: ModelfileForm, user=Depends(get_admin_user)):
|
2023-12-30 10:53:33 +00:00
|
|
|
modelfile = Modelfiles.insert_new_modelfile(user.id, form_data)
|
|
|
|
|
|
|
|
if modelfile:
|
|
|
|
return ModelfileResponse(
|
|
|
|
**{
|
|
|
|
**modelfile.model_dump(),
|
2024-03-31 08:13:39 +00:00
|
|
|
"modelfile": json.loads(modelfile.modelfile),
|
|
|
|
}
|
|
|
|
)
|
2023-12-27 06:02:17 +00:00
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
2023-12-30 10:53:33 +00:00
|
|
|
detail=ERROR_MESSAGES.DEFAULT(),
|
2023-12-27 06:02:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
# GetModelfileByTagName
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
2023-12-27 06:51:52 +00:00
|
|
|
@router.post("/", response_model=Optional[ModelfileResponse])
|
2024-03-31 08:13:39 +00:00
|
|
|
async def get_modelfile_by_tag_name(
|
|
|
|
form_data: ModelfileTagNameForm, user=Depends(get_current_user)
|
|
|
|
):
|
2023-12-30 10:53:33 +00:00
|
|
|
modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)
|
|
|
|
|
|
|
|
if modelfile:
|
|
|
|
return ModelfileResponse(
|
|
|
|
**{
|
|
|
|
**modelfile.model_dump(),
|
2024-03-31 08:13:39 +00:00
|
|
|
"modelfile": json.loads(modelfile.modelfile),
|
|
|
|
}
|
|
|
|
)
|
2023-12-27 06:02:17 +00:00
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
2023-12-30 10:53:33 +00:00
|
|
|
detail=ERROR_MESSAGES.NOT_FOUND,
|
2023-12-27 06:02:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
# UpdateModelfileByTagName
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
2023-12-27 06:51:52 +00:00
|
|
|
@router.post("/update", response_model=Optional[ModelfileResponse])
|
2024-03-31 08:13:39 +00:00
|
|
|
async def update_modelfile_by_tag_name(
|
|
|
|
form_data: ModelfileUpdateForm, user=Depends(get_admin_user)
|
|
|
|
):
|
2023-12-30 10:53:33 +00:00
|
|
|
modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)
|
|
|
|
if modelfile:
|
|
|
|
updated_modelfile = {
|
|
|
|
**json.loads(modelfile.modelfile),
|
|
|
|
**form_data.modelfile,
|
|
|
|
}
|
|
|
|
|
|
|
|
modelfile = Modelfiles.update_modelfile_by_tag_name(
|
2024-03-31 08:13:39 +00:00
|
|
|
form_data.tag_name, updated_modelfile
|
|
|
|
)
|
2023-12-30 10:53:33 +00:00
|
|
|
|
|
|
|
return ModelfileResponse(
|
|
|
|
**{
|
|
|
|
**modelfile.model_dump(),
|
2024-03-31 08:13:39 +00:00
|
|
|
"modelfile": json.loads(modelfile.modelfile),
|
|
|
|
}
|
|
|
|
)
|
2023-12-27 06:02:17 +00:00
|
|
|
else:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
2023-12-30 10:53:33 +00:00
|
|
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
2023-12-27 06:02:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
############################
|
|
|
|
# DeleteModelfileByTagName
|
|
|
|
############################
|
|
|
|
|
|
|
|
|
2023-12-27 06:51:52 +00:00
|
|
|
@router.delete("/delete", response_model=bool)
|
2024-03-31 08:13:39 +00:00
|
|
|
async def delete_modelfile_by_tag_name(
|
|
|
|
form_data: ModelfileTagNameForm, user=Depends(get_admin_user)
|
|
|
|
):
|
2023-12-30 10:53:33 +00:00
|
|
|
result = Modelfiles.delete_modelfile_by_tag_name(form_data.tag_name)
|
|
|
|
return result
|