modelfiles.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. from fastapi import Response
  2. from fastapi import Depends, FastAPI, HTTPException, status
  3. from datetime import datetime, timedelta
  4. from typing import List, Union, Optional
  5. from fastapi import APIRouter
  6. from pydantic import BaseModel
  7. import json
  8. from apps.web.models.users import Users
  9. from apps.web.models.modelfiles import (
  10. Modelfiles,
  11. ModelfileForm,
  12. ModelfileTagNameForm,
  13. ModelfileUpdateForm,
  14. ModelfileResponse,
  15. )
  16. from utils.utils import (
  17. bearer_scheme,
  18. )
  19. from constants import ERROR_MESSAGES
  20. router = APIRouter()
  21. ############################
  22. # GetModelfiles
  23. ############################
  24. @router.get("/", response_model=List[ModelfileResponse])
  25. async def get_modelfiles(skip: int = 0, limit: int = 50, cred=Depends(bearer_scheme)):
  26. token = cred.credentials
  27. user = Users.get_user_by_token(token)
  28. if user:
  29. return Modelfiles.get_modelfiles(skip, limit)
  30. else:
  31. raise HTTPException(
  32. status_code=status.HTTP_401_UNAUTHORIZED,
  33. detail=ERROR_MESSAGES.INVALID_TOKEN,
  34. )
  35. ############################
  36. # CreateNewModelfile
  37. ############################
  38. @router.post("/create", response_model=Optional[ModelfileResponse])
  39. async def create_new_modelfile(form_data: ModelfileForm, cred=Depends(bearer_scheme)):
  40. token = cred.credentials
  41. user = Users.get_user_by_token(token)
  42. if user:
  43. # Admin Only
  44. if user.role == "admin":
  45. modelfile = Modelfiles.insert_new_modelfile(user.id, form_data)
  46. if modelfile:
  47. return ModelfileResponse(
  48. **{
  49. **modelfile.model_dump(),
  50. "modelfile": json.loads(modelfile.modelfile),
  51. }
  52. )
  53. else:
  54. raise HTTPException(
  55. status_code=status.HTTP_401_UNAUTHORIZED,
  56. detail=ERROR_MESSAGES.DEFAULT(),
  57. )
  58. else:
  59. raise HTTPException(
  60. status_code=status.HTTP_401_UNAUTHORIZED,
  61. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  62. )
  63. else:
  64. raise HTTPException(
  65. status_code=status.HTTP_401_UNAUTHORIZED,
  66. detail=ERROR_MESSAGES.INVALID_TOKEN,
  67. )
  68. ############################
  69. # GetModelfileByTagName
  70. ############################
  71. @router.post("/", response_model=Optional[ModelfileResponse])
  72. async def get_modelfile_by_tag_name(
  73. form_data: ModelfileTagNameForm, cred=Depends(bearer_scheme)
  74. ):
  75. token = cred.credentials
  76. user = Users.get_user_by_token(token)
  77. if user:
  78. modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)
  79. if modelfile:
  80. return ModelfileResponse(
  81. **{
  82. **modelfile.model_dump(),
  83. "modelfile": json.loads(modelfile.modelfile),
  84. }
  85. )
  86. else:
  87. raise HTTPException(
  88. status_code=status.HTTP_401_UNAUTHORIZED,
  89. detail=ERROR_MESSAGES.NOT_FOUND,
  90. )
  91. else:
  92. raise HTTPException(
  93. status_code=status.HTTP_401_UNAUTHORIZED,
  94. detail=ERROR_MESSAGES.INVALID_TOKEN,
  95. )
  96. ############################
  97. # UpdateModelfileByTagName
  98. ############################
  99. @router.post("/update", response_model=Optional[ModelfileResponse])
  100. async def update_modelfile_by_tag_name(
  101. form_data: ModelfileUpdateForm, cred=Depends(bearer_scheme)
  102. ):
  103. token = cred.credentials
  104. user = Users.get_user_by_token(token)
  105. if user:
  106. if user.role == "admin":
  107. modelfile = Modelfiles.get_modelfile_by_tag_name(form_data.tag_name)
  108. if modelfile:
  109. updated_modelfile = {
  110. **json.loads(modelfile.modelfile),
  111. **form_data.modelfile,
  112. }
  113. modelfile = Modelfiles.update_modelfile_by_tag_name(
  114. form_data.tag_name, updated_modelfile
  115. )
  116. return ModelfileResponse(
  117. **{
  118. **modelfile.model_dump(),
  119. "modelfile": json.loads(modelfile.modelfile),
  120. }
  121. )
  122. else:
  123. raise HTTPException(
  124. status_code=status.HTTP_401_UNAUTHORIZED,
  125. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  126. )
  127. else:
  128. raise HTTPException(
  129. status_code=status.HTTP_401_UNAUTHORIZED,
  130. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  131. )
  132. else:
  133. raise HTTPException(
  134. status_code=status.HTTP_401_UNAUTHORIZED,
  135. detail=ERROR_MESSAGES.INVALID_TOKEN,
  136. )
  137. ############################
  138. # DeleteModelfileByTagName
  139. ############################
  140. @router.delete("/delete", response_model=bool)
  141. async def delete_modelfile_by_tag_name(
  142. form_data: ModelfileTagNameForm, cred=Depends(bearer_scheme)
  143. ):
  144. token = cred.credentials
  145. user = Users.get_user_by_token(token)
  146. if user:
  147. if user.role == "admin":
  148. result = Modelfiles.delete_modelfile_by_tag_name(form_data.tag_name)
  149. return result
  150. else:
  151. raise HTTPException(
  152. status_code=status.HTTP_401_UNAUTHORIZED,
  153. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  154. )
  155. else:
  156. raise HTTPException(
  157. status_code=status.HTTP_401_UNAUTHORIZED,
  158. detail=ERROR_MESSAGES.INVALID_TOKEN,
  159. )