modelfiles.py 3.8 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
11
12
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.users import Users
from apps.web.models.modelfiles import (
    Modelfiles,
    ModelfileForm,
13
14
    ModelfileTagNameForm,
    ModelfileUpdateForm,
Timothy J. Baek's avatar
Timothy J. Baek committed
15
16
17
    ModelfileResponse,
)

18
from utils.utils import bearer_scheme, get_current_user
Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
21
22
23
24
25
26
27
28
29
from constants import ERROR_MESSAGES

router = APIRouter()

############################
# GetModelfiles
############################


@router.get("/", response_model=List[ModelfileResponse])
async def get_modelfiles(skip: int = 0, limit: int = 50, cred=Depends(bearer_scheme)):
30
    return Modelfiles.get_modelfiles(skip, limit)
Timothy J. Baek's avatar
Timothy J. Baek committed
31
32
33
34
35
36
37
38


############################
# CreateNewModelfile
############################


@router.post("/create", response_model=Optional[ModelfileResponse])
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
async def create_new_modelfile(
    form_data: ModelfileForm, user=Depends(get_current_user)
):
    if user.role != "admin":
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )

    modelfile = Modelfiles.insert_new_modelfile(user.id, form_data)

    if modelfile:
        return ModelfileResponse(
            **{
                **modelfile.model_dump(),
                "modelfile": json.loads(modelfile.modelfile),
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
57
58
59
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
60
            detail=ERROR_MESSAGES.DEFAULT(),
Timothy J. Baek's avatar
Timothy J. Baek committed
61
62
63
64
65
66
67
68
        )


############################
# GetModelfileByTagName
############################


69
@router.post("/", response_model=Optional[ModelfileResponse])
70
71
72
73
74
75
76
77
78
79
async def get_modelfile_by_tag_name(form_data: ModelfileTagNameForm):
    modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)

    if modelfile:
        return ModelfileResponse(
            **{
                **modelfile.model_dump(),
                "modelfile": json.loads(modelfile.modelfile),
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
80
81
82
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
83
            detail=ERROR_MESSAGES.NOT_FOUND,
Timothy J. Baek's avatar
Timothy J. Baek committed
84
85
86
87
88
89
90
91
        )


############################
# UpdateModelfileByTagName
############################


92
@router.post("/update", response_model=Optional[ModelfileResponse])
Timothy J. Baek's avatar
Timothy J. Baek committed
93
async def update_modelfile_by_tag_name(
94
    form_data: ModelfileUpdateForm, user=Depends(get_current_user)
Timothy J. Baek's avatar
Timothy J. Baek committed
95
):
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    if user.role != "admin":
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )
    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(
            form_data.tag_name, updated_modelfile
        )

        return ModelfileResponse(
            **{
                **modelfile.model_dump(),
                "modelfile": json.loads(modelfile.modelfile),
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
118
119
120
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
121
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
Timothy J. Baek's avatar
Timothy J. Baek committed
122
123
124
125
126
127
128
129
        )


############################
# DeleteModelfileByTagName
############################


130
131
@router.delete("/delete", response_model=bool)
async def delete_modelfile_by_tag_name(
132
    form_data: ModelfileTagNameForm, user=Depends(get_current_user)
133
):
134
    if user.role != "admin":
Timothy J. Baek's avatar
Timothy J. Baek committed
135
136
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
137
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
Timothy J. Baek's avatar
Timothy J. Baek committed
138
        )
139
140
141

    result = Modelfiles.delete_modelfile_by_tag_name(form_data.tag_name)
    return result