2024-05-19 10:46:24 +00:00
|
|
|
import json
|
2024-05-19 14:11:28 +00:00
|
|
|
import logging
|
2024-05-19 10:46:24 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2024-05-24 06:47:01 +00:00
|
|
|
from pydantic import BaseModel, ConfigDict
|
2024-06-24 11:21:51 +00:00
|
|
|
from sqlalchemy import String, Column, BigInteger, Text
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
from apps.webui.internal.db import Base, JSONField, Session
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
from typing import List, Union, Optional
|
2024-05-19 14:11:28 +00:00
|
|
|
from config import SRC_LOG_LEVELS
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
import time
|
|
|
|
|
2024-05-19 14:11:28 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["MODELS"])
|
|
|
|
|
2024-05-19 10:46:24 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Models DB Schema
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
# ModelParams is a model for the data stored in the params field of the Model table
|
|
|
|
class ModelParams(BaseModel):
|
2024-05-24 06:47:01 +00:00
|
|
|
model_config = ConfigDict(extra="allow")
|
2024-05-21 21:05:16 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# ModelMeta is a model for the data stored in the meta field of the Model table
|
|
|
|
class ModelMeta(BaseModel):
|
2024-05-25 01:26:36 +00:00
|
|
|
profile_image_url: Optional[str] = "/favicon.png"
|
|
|
|
|
2024-05-24 06:47:01 +00:00
|
|
|
description: Optional[str] = None
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
2024-05-19 14:11:28 +00:00
|
|
|
User-facing description of the model.
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
|
2024-05-25 06:34:58 +00:00
|
|
|
capabilities: Optional[dict] = None
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-05-24 06:47:01 +00:00
|
|
|
model_config = ConfigDict(extra="allow")
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
class Model(Base):
|
|
|
|
__tablename__ = "model"
|
|
|
|
|
2024-06-24 11:21:51 +00:00
|
|
|
id = Column(Text, primary_key=True)
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
The model's id as used in the API. If set to an existing model, it will override the model.
|
|
|
|
"""
|
2024-06-24 11:21:51 +00:00
|
|
|
user_id = Column(Text)
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-06-24 11:21:51 +00:00
|
|
|
base_model_id = Column(Text, nullable=True)
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
2024-05-21 21:05:16 +00:00
|
|
|
An optional pointer to the actual model that should be used when proxying requests.
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
|
2024-06-24 11:21:51 +00:00
|
|
|
name = Column(Text)
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
2024-05-21 21:05:16 +00:00
|
|
|
The human-readable display name of the model.
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
params = Column(JSONField)
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
2024-05-21 21:05:16 +00:00
|
|
|
Holds a JSON encoded blob of parameters, see `ModelParams`.
|
2024-05-19 10:46:24 +00:00
|
|
|
"""
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
meta = Column(JSONField)
|
2024-05-24 05:58:26 +00:00
|
|
|
"""
|
|
|
|
Holds a JSON encoded blob of metadata, see `ModelMeta`.
|
|
|
|
"""
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
updated_at = Column(BigInteger)
|
|
|
|
created_at = Column(BigInteger)
|
2024-05-19 10:46:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ModelModel(BaseModel):
|
|
|
|
id: str
|
2024-05-25 01:26:36 +00:00
|
|
|
user_id: str
|
2024-05-21 21:05:16 +00:00
|
|
|
base_model_id: Optional[str] = None
|
2024-05-24 07:26:00 +00:00
|
|
|
|
2024-05-19 10:46:24 +00:00
|
|
|
name: str
|
2024-05-21 21:05:16 +00:00
|
|
|
params: ModelParams
|
2024-05-24 05:58:26 +00:00
|
|
|
meta: ModelMeta
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
2024-05-19 10:46:24 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Forms
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
class ModelResponse(BaseModel):
|
|
|
|
id: str
|
|
|
|
name: str
|
|
|
|
meta: ModelMeta
|
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
|
|
|
|
|
|
|
|
class ModelForm(BaseModel):
|
|
|
|
id: str
|
|
|
|
base_model_id: Optional[str] = None
|
|
|
|
name: str
|
|
|
|
meta: ModelMeta
|
|
|
|
params: ModelParams
|
|
|
|
|
|
|
|
|
2024-05-19 10:46:24 +00:00
|
|
|
class ModelsTable:
|
|
|
|
|
2024-05-25 01:26:36 +00:00
|
|
|
def insert_new_model(
|
2024-06-21 12:58:57 +00:00
|
|
|
self, form_data: ModelForm, user_id: str
|
2024-05-25 01:26:36 +00:00
|
|
|
) -> Optional[ModelModel]:
|
|
|
|
model = ModelModel(
|
|
|
|
**{
|
|
|
|
**form_data.model_dump(),
|
|
|
|
"user_id": user_id,
|
|
|
|
"created_at": int(time.time()),
|
|
|
|
"updated_at": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
2024-05-24 07:26:00 +00:00
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
result = Model(**model.model_dump())
|
|
|
|
Session.add(result)
|
|
|
|
Session.commit()
|
|
|
|
Session.refresh(result)
|
|
|
|
|
|
|
|
if result:
|
|
|
|
return ModelModel.model_validate(result)
|
|
|
|
else:
|
|
|
|
return None
|
2024-05-25 01:26:36 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2024-05-24 07:26:00 +00:00
|
|
|
return None
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def get_all_models(self) -> List[ModelModel]:
|
2024-06-24 11:55:18 +00:00
|
|
|
return [
|
|
|
|
ModelModel.model_validate(model) for model in Session.query(Model).all()
|
|
|
|
]
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def get_model_by_id(self, id: str) -> Optional[ModelModel]:
|
2024-05-19 10:46:24 +00:00
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
model = Session.get(Model, id)
|
|
|
|
return ModelModel.model_validate(model)
|
2024-05-24 07:26:00 +00:00
|
|
|
except:
|
|
|
|
return None
|
2024-05-19 10:46:24 +00:00
|
|
|
|
2024-06-24 07:57:08 +00:00
|
|
|
def update_model_by_id(self, id: str, model: ModelForm) -> Optional[ModelModel]:
|
2024-05-24 07:26:00 +00:00
|
|
|
try:
|
|
|
|
# update only the fields that are present in the model
|
2024-06-24 11:06:15 +00:00
|
|
|
model = Session.query(Model).get(id)
|
|
|
|
model.update(**model.model_dump())
|
|
|
|
Session.commit()
|
|
|
|
Session.refresh(model)
|
|
|
|
return ModelModel.model_validate(model)
|
2024-05-25 05:21:57 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
2024-05-24 07:26:00 +00:00
|
|
|
return None
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def delete_model_by_id(self, id: str) -> bool:
|
2024-05-24 07:26:00 +00:00
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
Session.query(Model).filter_by(id=id).delete()
|
2024-05-19 10:46:24 +00:00
|
|
|
return True
|
2024-05-24 07:26:00 +00:00
|
|
|
except:
|
2024-05-19 10:46:24 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
Models = ModelsTable()
|