tti.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # coding=utf-8
  2. from django.utils.translation import gettext_lazy as _, gettext
  3. from common import forms
  4. from common.exception.app_exception import AppApiException
  5. from common.forms import BaseForm, TooltipLabel
  6. from models_provider.base_model_provider import BaseModelCredential, ValidCode
  7. from common.utils.logger import maxkb_logger
  8. class TencentTTIModelParams(BaseForm):
  9. Style = forms.SingleSelect(
  10. TooltipLabel(_('painting style'), _('If not passed, the default value is 201 (Japanese anime style)')),
  11. required=True,
  12. default_value='201',
  13. option_list=[
  14. {'value': '000', 'label': _('Not limited to style')},
  15. {'value': '101', 'label': _('ink painting')},
  16. {'value': '102', 'label': _('concept art')},
  17. {'value': '103', 'label': _('Oil painting 1')},
  18. {'value': '118', 'label': _('Oil Painting 2 (Van Gogh)')},
  19. {'value': '104', 'label': _('watercolor painting')},
  20. {'value': '105', 'label': _('pixel art')},
  21. {'value': '106', 'label': _('impasto style')},
  22. {'value': '107', 'label': _('illustration')},
  23. {'value': '108', 'label': _('paper cut style')},
  24. {'value': '109', 'label': _('Impressionism 1 (Monet)')},
  25. {'value': '119', 'label': _('Impressionism 2')},
  26. {'value': '110', 'label': '2.5D'},
  27. {'value': '111', 'label': _('classical portraiture')},
  28. {'value': '112', 'label': _('black and white sketch')},
  29. {'value': '113', 'label': _('cyberpunk')},
  30. {'value': '114', 'label': _('science fiction style')},
  31. {'value': '115', 'label': _('dark style')},
  32. {'value': '116', 'label': '3D'},
  33. {'value': '117', 'label': _('vaporwave')},
  34. {'value': '201', 'label': _('Japanese animation')},
  35. {'value': '202', 'label': _('monster style')},
  36. {'value': '203', 'label': _('Beautiful ancient style')},
  37. {'value': '204', 'label': _('retro anime')},
  38. {'value': '301', 'label': _('Game cartoon hand drawing')},
  39. {'value': '401', 'label': _('Universal realistic style')},
  40. ],
  41. value_field='value',
  42. text_field='label'
  43. )
  44. Resolution = forms.SingleSelect(
  45. TooltipLabel(_('Generate image resolution'), _('If not transmitted, the default value is 768:768.')),
  46. required=True,
  47. default_value='768:768',
  48. option_list=[
  49. {'value': '768:768', 'label': '768:768(1:1)'},
  50. {'value': '768:1024', 'label': '768:1024(3:4)'},
  51. {'value': '1024:768', 'label': '1024:768(4:3)'},
  52. {'value': '1024:1024', 'label': '1024:1024(1:1)'},
  53. {'value': '720:1280', 'label': '720:1280(9:16)'},
  54. {'value': '1280:720', 'label': '1280:720(16:9)'},
  55. {'value': '768:1280', 'label': '768:1280(3:5)'},
  56. {'value': '1280:768', 'label': '1280:768(5:3)'},
  57. {'value': '1080:1920', 'label': '1080:1920(9:16)'},
  58. {'value': '1920:1080', 'label': '1920:1080(16:9)'},
  59. ],
  60. value_field='value',
  61. text_field='label'
  62. )
  63. class TencentTTIModelCredential(BaseForm, BaseModelCredential):
  64. REQUIRED_FIELDS = ['hunyuan_secret_id', 'hunyuan_secret_key']
  65. @classmethod
  66. def _validate_model_type(cls, model_type, provider, raise_exception=False):
  67. if not any(mt['value'] == model_type for mt in provider.get_model_type_list()):
  68. if raise_exception:
  69. raise AppApiException(ValidCode.valid_error.value,
  70. gettext('{model_type} Model type is not supported').format(model_type=model_type))
  71. return False
  72. return True
  73. @classmethod
  74. def _validate_credential_fields(cls, model_credential, raise_exception=False):
  75. missing_keys = [key for key in cls.REQUIRED_FIELDS if key not in model_credential]
  76. if missing_keys:
  77. if raise_exception:
  78. raise AppApiException(ValidCode.valid_error.value,
  79. gettext('{keys} is required').format(keys=", ".join(missing_keys)))
  80. return False
  81. return True
  82. def is_valid(self, model_type, model_name, model_credential, model_params, provider, raise_exception=False):
  83. if not (self._validate_model_type(model_type, provider, raise_exception) and
  84. self._validate_credential_fields(model_credential, raise_exception)):
  85. return False
  86. try:
  87. model = provider.get_model(model_type, model_name, model_credential, **model_params)
  88. model.check_auth()
  89. except Exception as e:
  90. maxkb_logger.error(f'Exception: {e}', exc_info=True)
  91. if raise_exception:
  92. raise AppApiException(ValidCode.valid_error.value,
  93. gettext(
  94. 'Verification failed, please check whether the parameters are correct: {error}').format(
  95. error=str(e)))
  96. return False
  97. return True
  98. def encryption_dict(self, model):
  99. return {**model, 'hunyuan_secret_key': super().encryption(model.get('hunyuan_secret_key', ''))}
  100. hunyuan_secret_id = forms.PasswordInputField('SecretId', required=True)
  101. hunyuan_secret_key = forms.PasswordInputField('SecretKey', required=True)
  102. def get_model_params_setting_form(self, model_name):
  103. return TencentTTIModelParams()