models.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import json
  2. import logging
  3. from typing import Optional
  4. import peewee as pw
  5. from peewee import *
  6. from playhouse.shortcuts import model_to_dict
  7. from pydantic import BaseModel, ConfigDict
  8. from apps.web.internal.db import DB, JSONField
  9. from typing import List, Union, Optional
  10. from config import SRC_LOG_LEVELS
  11. import time
  12. log = logging.getLogger(__name__)
  13. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  14. ####################
  15. # Models DB Schema
  16. ####################
  17. # ModelParams is a model for the data stored in the params field of the Model table
  18. class ModelParams(BaseModel):
  19. model_config = ConfigDict(extra="allow")
  20. pass
  21. # ModelMeta is a model for the data stored in the meta field of the Model table
  22. class ModelMeta(BaseModel):
  23. profile_image_url: Optional[str] = "/favicon.png"
  24. description: Optional[str] = None
  25. """
  26. User-facing description of the model.
  27. """
  28. capabilities: Optional[dict] = None
  29. model_config = ConfigDict(extra="allow")
  30. pass
  31. class Model(pw.Model):
  32. id = pw.TextField(unique=True)
  33. """
  34. The model's id as used in the API. If set to an existing model, it will override the model.
  35. """
  36. user_id = pw.TextField()
  37. base_model_id = pw.TextField(null=True)
  38. """
  39. An optional pointer to the actual model that should be used when proxying requests.
  40. """
  41. name = pw.TextField()
  42. """
  43. The human-readable display name of the model.
  44. """
  45. params = JSONField()
  46. """
  47. Holds a JSON encoded blob of parameters, see `ModelParams`.
  48. """
  49. meta = JSONField()
  50. """
  51. Holds a JSON encoded blob of metadata, see `ModelMeta`.
  52. """
  53. updated_at = BigIntegerField()
  54. created_at = BigIntegerField()
  55. class Meta:
  56. database = DB
  57. class ModelModel(BaseModel):
  58. id: str
  59. user_id: str
  60. base_model_id: Optional[str] = None
  61. name: str
  62. params: ModelParams
  63. meta: ModelMeta
  64. updated_at: int # timestamp in epoch
  65. created_at: int # timestamp in epoch
  66. ####################
  67. # Forms
  68. ####################
  69. class ModelResponse(BaseModel):
  70. id: str
  71. name: str
  72. meta: ModelMeta
  73. updated_at: int # timestamp in epoch
  74. created_at: int # timestamp in epoch
  75. class ModelForm(BaseModel):
  76. id: str
  77. base_model_id: Optional[str] = None
  78. name: str
  79. meta: ModelMeta
  80. params: ModelParams
  81. class ModelsTable:
  82. def __init__(
  83. self,
  84. db: pw.SqliteDatabase | pw.PostgresqlDatabase,
  85. ):
  86. self.db = db
  87. self.db.create_tables([Model])
  88. def insert_new_model(
  89. self, form_data: ModelForm, user_id: str
  90. ) -> Optional[ModelModel]:
  91. model = ModelModel(
  92. **{
  93. **form_data.model_dump(),
  94. "user_id": user_id,
  95. "created_at": int(time.time()),
  96. "updated_at": int(time.time()),
  97. }
  98. )
  99. try:
  100. result = Model.create(**model.model_dump())
  101. if result:
  102. return model
  103. else:
  104. return None
  105. except Exception as e:
  106. print(e)
  107. return None
  108. def get_all_models(self) -> List[ModelModel]:
  109. return [ModelModel(**model_to_dict(model)) for model in Model.select()]
  110. def get_model_by_id(self, id: str) -> Optional[ModelModel]:
  111. try:
  112. model = Model.get(Model.id == id)
  113. return ModelModel(**model_to_dict(model))
  114. except:
  115. return None
  116. def update_model_by_id(self, id: str, model: ModelForm) -> Optional[ModelModel]:
  117. try:
  118. # update only the fields that are present in the model
  119. query = Model.update(**model.model_dump()).where(Model.id == id)
  120. query.execute()
  121. model = Model.get(Model.id == id)
  122. return ModelModel(**model_to_dict(model))
  123. except Exception as e:
  124. print(e)
  125. return None
  126. def delete_model_by_id(self, id: str) -> bool:
  127. try:
  128. query = Model.delete().where(Model.id == id)
  129. query.execute()
  130. return True
  131. except:
  132. return False
  133. Models = ModelsTable(DB)