ttv.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # coding=utf-8
  2. from typing import Dict
  3. from django.utils.translation import gettext_lazy as _, gettext
  4. from common import forms
  5. from common.exception.app_exception import AppApiException
  6. from common.forms import BaseForm, TooltipLabel, SingleSelect, TextInputField
  7. from common.forms.switch_field import SwitchField
  8. from models_provider.base_model_provider import BaseModelCredential, ValidCode
  9. from common.utils.logger import maxkb_logger
  10. class VolcanicEngineTTVModelGeneralParams(BaseForm):
  11. resolution = SingleSelect(
  12. TooltipLabel(_('Resolution'), _('Resolution')),
  13. required=True,
  14. default_value='480P',
  15. option_list=[
  16. {'value': '480P', 'label': '480P'},
  17. {'value': '720P', 'label': '720P'},
  18. {'value': '1080P', 'label': '1080P'},
  19. ],
  20. text_field='label',
  21. value_field='value'
  22. )
  23. ratio = SingleSelect(
  24. TooltipLabel(_('Ratio'), _('Ratio')),
  25. required=True,
  26. default_value='16:9',
  27. option_list=[
  28. {'value': '16:9', 'label': '16:9'},
  29. {'value': '9:16', 'label': '9:16'},
  30. {'value': '1:1', 'label': '1:1'},
  31. {'value': '4:3', 'label': '4:3'},
  32. {'value': '3:4', 'label': '3:4'},
  33. {'value': '21:9', 'label': '21:9'},
  34. ],
  35. text_field='label',
  36. value_field='value'
  37. )
  38. duration = TextInputField(
  39. TooltipLabel(_('Duration'), _('Duration')),
  40. required=True,
  41. default_value=5,
  42. )
  43. watermark = SwitchField(
  44. TooltipLabel(_('Watermark'), _('Whether to add watermark')),
  45. attrs={"active-value": "true", "inactive-value": "false"},
  46. default_value=False,
  47. )
  48. class VolcanicEngineTTVModelCredential(BaseForm, BaseModelCredential):
  49. api_key = forms.PasswordInputField('Api key', required=True)
  50. def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
  51. raise_exception=False):
  52. model_type_list = provider.get_model_type_list()
  53. if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
  54. raise AppApiException(ValidCode.valid_error.value,
  55. gettext('{model_type} Model type is not supported').format(model_type=model_type))
  56. for key in ['api_key']:
  57. if key not in model_credential:
  58. if raise_exception:
  59. raise AppApiException(ValidCode.valid_error.value, gettext('{key} is required').format(key=key))
  60. else:
  61. return False
  62. try:
  63. model = provider.get_model(model_type, model_name, model_credential, **model_params)
  64. model.check_auth()
  65. except Exception as e:
  66. maxkb_logger.error(f'Exception: {e}', exc_info=True)
  67. if isinstance(e, AppApiException):
  68. raise e
  69. if raise_exception:
  70. raise AppApiException(ValidCode.valid_error.value, gettext(
  71. 'Verification failed, please check whether the parameters are correct: {error}').format(
  72. error=str(e)))
  73. else:
  74. return False
  75. return True
  76. def encryption_dict(self, model: Dict[str, object]):
  77. return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
  78. def get_model_params_setting_form(self, model_name):
  79. return VolcanicEngineTTVModelGeneralParams()