models.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from typing import Optional
  2. from apps.webui.models.models import ModelForm, ModelModel, ModelResponse, Models
  3. from constants import ERROR_MESSAGES
  4. from fastapi import APIRouter, Depends, HTTPException, Request, status
  5. from utils.utils import get_admin_user, get_verified_user
  6. router = APIRouter()
  7. ###########################
  8. # getModels
  9. ###########################
  10. @router.get("/", response_model=list[ModelResponse])
  11. async def get_models(user=Depends(get_verified_user)):
  12. return Models.get_all_models()
  13. ############################
  14. # AddNewModel
  15. ############################
  16. @router.post("/add", response_model=Optional[ModelModel])
  17. async def add_new_model(
  18. request: Request,
  19. form_data: ModelForm,
  20. user=Depends(get_admin_user),
  21. ):
  22. if form_data.id in request.app.state.MODELS:
  23. raise HTTPException(
  24. status_code=status.HTTP_401_UNAUTHORIZED,
  25. detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
  26. )
  27. else:
  28. model = Models.insert_new_model(form_data, user.id)
  29. if model:
  30. return model
  31. else:
  32. raise HTTPException(
  33. status_code=status.HTTP_401_UNAUTHORIZED,
  34. detail=ERROR_MESSAGES.DEFAULT(),
  35. )
  36. ############################
  37. # GetModelById
  38. ############################
  39. @router.get("/", response_model=Optional[ModelModel])
  40. async def get_model_by_id(id: str, user=Depends(get_verified_user)):
  41. model = Models.get_model_by_id(id)
  42. if model:
  43. return model
  44. else:
  45. raise HTTPException(
  46. status_code=status.HTTP_401_UNAUTHORIZED,
  47. detail=ERROR_MESSAGES.NOT_FOUND,
  48. )
  49. ############################
  50. # UpdateModelById
  51. ############################
  52. @router.post("/update", response_model=Optional[ModelModel])
  53. async def update_model_by_id(
  54. request: Request,
  55. id: str,
  56. form_data: ModelForm,
  57. user=Depends(get_admin_user),
  58. ):
  59. model = Models.get_model_by_id(id)
  60. if model:
  61. model = Models.update_model_by_id(id, form_data)
  62. return model
  63. else:
  64. if form_data.id in request.app.state.MODELS:
  65. model = Models.insert_new_model(form_data, user.id)
  66. if model:
  67. return model
  68. else:
  69. raise HTTPException(
  70. status_code=status.HTTP_401_UNAUTHORIZED,
  71. detail=ERROR_MESSAGES.DEFAULT(),
  72. )
  73. else:
  74. raise HTTPException(
  75. status_code=status.HTTP_401_UNAUTHORIZED,
  76. detail=ERROR_MESSAGES.DEFAULT(),
  77. )
  78. ############################
  79. # DeleteModelById
  80. ############################
  81. @router.delete("/delete", response_model=bool)
  82. async def delete_model_by_id(id: str, user=Depends(get_admin_user)):
  83. result = Models.delete_model_by_id(id)
  84. return result