tts.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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
  7. from models_provider.base_model_provider import BaseModelCredential, ValidCode
  8. class XInferenceTTSModelGeneralParams(BaseForm):
  9. # ['中文女', '中文男', '日语男', '粤语女', '英文女', '英文男', '韩语女']
  10. voice = forms.SingleSelect(
  11. TooltipLabel(_('timbre'), ''),
  12. required=True, default_value='中文女',
  13. text_field='value',
  14. value_field='value',
  15. option_list=[
  16. {'text': _('Chinese female'), 'value': '中文女'},
  17. {'text': _('Chinese male'), 'value': '中文男'},
  18. {'text': _('Japanese male'), 'value': '日语男'},
  19. {'text': _('Cantonese female'), 'value': '粤语女'},
  20. {'text': _('English female'), 'value': '英文女'},
  21. {'text': _('English male'), 'value': '英文男'},
  22. {'text': _('Korean female'), 'value': '韩语女'},
  23. ])
  24. class XInferenceTTSModelCredential(BaseForm, BaseModelCredential):
  25. api_base = forms.TextInputField('API URL', required=True)
  26. api_key = forms.PasswordInputField('API Key', required=True)
  27. def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
  28. raise_exception=False):
  29. model_type_list = provider.get_model_type_list()
  30. if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
  31. raise AppApiException(ValidCode.valid_error.value,
  32. gettext('{model_type} Model type is not supported').format(model_type=model_type))
  33. for key in ['api_base', 'api_key']:
  34. if key not in model_credential:
  35. if raise_exception:
  36. raise AppApiException(ValidCode.valid_error.value, gettext('{key} is required').format(key=key))
  37. else:
  38. return False
  39. try:
  40. model = provider.get_model(model_type, model_name, model_credential, **model_params)
  41. model.check_auth()
  42. except Exception as e:
  43. if isinstance(e, AppApiException):
  44. raise e
  45. if raise_exception:
  46. raise AppApiException(ValidCode.valid_error.value,
  47. gettext(
  48. 'Verification failed, please check whether the parameters are correct: {error}').format(
  49. error=str(e)))
  50. else:
  51. return False
  52. return True
  53. def encryption_dict(self, model: Dict[str, object]):
  54. return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
  55. def get_model_params_setting_form(self, model_name):
  56. return XInferenceTTSModelGeneralParams()