".github/vscode:/vscode.git/clone" did not exist on "b354e3c90d2a52dbdf9690d1c7197a0e48d15975"
modelfiles.py 3.46 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
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,
11
12
    ModelfileTagNameForm,
    ModelfileUpdateForm,
Timothy J. Baek's avatar
Timothy J. Baek committed
13
14
15
    ModelfileResponse,
)

16
from utils.utils import get_current_user, get_admin_user
Timothy J. Baek's avatar
Timothy J. Baek committed
17
18
19
20
21
22
23
24
25
26
from constants import ERROR_MESSAGES

router = APIRouter()

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


@router.get("/", response_model=List[ModelfileResponse])
27
28
29
async def get_modelfiles(skip: int = 0,
                         limit: int = 50,
                         user=Depends(get_current_user)):
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
async def create_new_modelfile(form_data: ModelfileForm,
40
                               user=Depends(get_admin_user)):
41
42
43
44
45
46
    modelfile = Modelfiles.insert_new_modelfile(user.id, form_data)

    if modelfile:
        return ModelfileResponse(
            **{
                **modelfile.model_dump(),
47
48
49
                "modelfile":
                json.loads(modelfile.modelfile),
            })
Timothy J. Baek's avatar
Timothy J. Baek committed
50
51
52
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
53
            detail=ERROR_MESSAGES.DEFAULT(),
Timothy J. Baek's avatar
Timothy J. Baek committed
54
55
56
57
58
59
60
61
        )


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


62
@router.post("/", response_model=Optional[ModelfileResponse])
63
64
async def get_modelfile_by_tag_name(form_data: ModelfileTagNameForm,
                                    user=Depends(get_current_user)):
65
66
67
68
69
70
    modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)

    if modelfile:
        return ModelfileResponse(
            **{
                **modelfile.model_dump(),
71
72
73
                "modelfile":
                json.loads(modelfile.modelfile),
            })
Timothy J. Baek's avatar
Timothy J. Baek committed
74
75
76
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
77
            detail=ERROR_MESSAGES.NOT_FOUND,
Timothy J. Baek's avatar
Timothy J. Baek committed
78
79
80
81
82
83
84
85
        )


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


86
@router.post("/update", response_model=Optional[ModelfileResponse])
87
async def update_modelfile_by_tag_name(form_data: ModelfileUpdateForm,
88
                                       user=Depends(get_admin_user)):
89
90
91
92
93
94
95
96
    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(
97
            form_data.tag_name, updated_modelfile)
98
99
100
101

        return ModelfileResponse(
            **{
                **modelfile.model_dump(),
102
103
104
                "modelfile":
                json.loads(modelfile.modelfile),
            })
Timothy J. Baek's avatar
Timothy J. Baek committed
105
106
107
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
108
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
Timothy J. Baek's avatar
Timothy J. Baek committed
109
110
111
112
113
114
115
116
        )


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


117
@router.delete("/delete", response_model=bool)
118
async def delete_modelfile_by_tag_name(form_data: ModelfileTagNameForm,
119
                                       user=Depends(get_admin_user)):
120
121
    result = Modelfiles.delete_modelfile_by_tag_name(form_data.tag_name)
    return result