modelfiles.py 5.1 KB

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