modelfiles.py 3.85 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
################################################################################
#                              DEPRECATION NOTICE                              #
#                                                                              #
# This file has been deprecated since version 0.2.0.                           #
#                                                                              #
################################################################################


Timothy J. Baek's avatar
Timothy J. Baek committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from pydantic import BaseModel
from peewee import *
from playhouse.shortcuts import model_to_dict
from typing import List, Union, Optional
import time

from utils.utils import decode_token
from utils.misc import get_gravatar_url

from apps.web.internal.db import DB

import json

####################
Timothy J. Baek's avatar
Timothy J. Baek committed
23
# Modelfile DB Schema
Timothy J. Baek's avatar
Timothy J. Baek committed
24
25
26
27
28
29
30
####################


class Modelfile(Model):
    tag_name = CharField(unique=True)
    user_id = CharField()
    modelfile = TextField()
31
    timestamp = BigIntegerField()
Timothy J. Baek's avatar
Timothy J. Baek committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

    class Meta:
        database = DB


class ModelfileModel(BaseModel):
    tag_name: str
    user_id: str
    modelfile: str
    timestamp: int  # timestamp in epoch


####################
# Forms
####################


class ModelfileForm(BaseModel):
    modelfile: dict


53
54
55
56
57
58
59
60
class ModelfileTagNameForm(BaseModel):
    tag_name: str


class ModelfileUpdateForm(ModelfileForm, ModelfileTagNameForm):
    pass


Timothy J. Baek's avatar
Timothy J. Baek committed
61
62
63
64
65
66
67
68
class ModelfileResponse(BaseModel):
    tag_name: str
    user_id: str
    modelfile: dict
    timestamp: int  # timestamp in epoch


class ModelfilesTable:
69

Timothy J. Baek's avatar
Timothy J. Baek committed
70
71
72
73
74
    def __init__(self, db):
        self.db = db
        self.db.create_tables([Modelfile])

    def insert_new_modelfile(
Timothy J. Baek's avatar
Timothy J. Baek committed
75
76
        self, user_id: str, form_data: ModelfileForm
    ) -> Optional[ModelfileModel]:
77
        if "tagName" in form_data.modelfile:
Timothy J. Baek's avatar
Timothy J. Baek committed
78
79
80
            modelfile = ModelfileModel(
                **{
                    "user_id": user_id,
81
                    "tag_name": form_data.modelfile["tagName"],
Timothy J. Baek's avatar
Timothy J. Baek committed
82
83
                    "modelfile": json.dumps(form_data.modelfile),
                    "timestamp": int(time.time()),
Timothy J. Baek's avatar
Timothy J. Baek committed
84
85
                }
            )
86
87
88
89
90
91
92
93

            try:
                result = Modelfile.create(**modelfile.model_dump())
                if result:
                    return modelfile
                else:
                    return None
            except:
Timothy J. Baek's avatar
Timothy J. Baek committed
94
                return None
95

Timothy J. Baek's avatar
Timothy J. Baek committed
96
97
98
        else:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
99
    def get_modelfile_by_tag_name(self, tag_name: str) -> Optional[ModelfileModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
100
101
102
103
104
105
        try:
            modelfile = Modelfile.get(Modelfile.tag_name == tag_name)
            return ModelfileModel(**model_to_dict(modelfile))
        except:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
106
    def get_modelfiles(self, skip: int = 0, limit: int = 50) -> List[ModelfileResponse]:
Timothy J. Baek's avatar
Timothy J. Baek committed
107
108
109
110
        return [
            ModelfileResponse(
                **{
                    **model_to_dict(modelfile),
Timothy J. Baek's avatar
Timothy J. Baek committed
111
112
113
114
                    "modelfile": json.loads(modelfile.modelfile),
                }
            )
            for modelfile in Modelfile.select()
Timothy J. Baek's avatar
Timothy J. Baek committed
115
116
117
118
            # .limit(limit).offset(skip)
        ]

    def update_modelfile_by_tag_name(
Timothy J. Baek's avatar
Timothy J. Baek committed
119
120
        self, tag_name: str, modelfile: dict
    ) -> Optional[ModelfileModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
        try:
            query = Modelfile.update(
                modelfile=json.dumps(modelfile),
                timestamp=int(time.time()),
            ).where(Modelfile.tag_name == tag_name)

            query.execute()

            modelfile = Modelfile.get(Modelfile.tag_name == tag_name)
            return ModelfileModel(**model_to_dict(modelfile))
        except:
            return None

    def delete_modelfile_by_tag_name(self, tag_name: str) -> bool:
        try:
            query = Modelfile.delete().where((Modelfile.tag_name == tag_name))
            query.execute()  # Remove the rows, return number of rows removed.

            return True
        except:
            return False


Modelfiles = ModelfilesTable(DB)