tts.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. from common.utils.logger import maxkb_logger
  9. class VolcanicEngineTTSModelGeneralParams(BaseForm):
  10. voice_type = forms.SingleSelect(
  11. TooltipLabel(_('timbre'), _('Chinese sounds can support mixed scenes of Chinese and English')),
  12. required=True, default_value='zh_female_cancan_mars_bigtts',
  13. text_field='value',
  14. value_field='value',
  15. option_list=[
  16. {'text': '灿灿/Shiny', 'value': 'zh_female_cancan_mars_bigtts'},
  17. {'text': '清新女声', 'value': 'zh_female_qingxinnvsheng_mars_bigtts'},
  18. {'text': '爽快思思/Skye', 'value': 'zh_female_shuangkuaisisi_moon_bigtts'},
  19. {'text': '湾区大叔', 'value': 'zh_female_wanqudashu_moon_bigtts' },
  20. {'text': '呆萌川妹', 'value': 'zh_female_daimengchuanmei_moon_bigtts'},
  21. {'text': '广州德哥', 'value': 'zh_male_guozhoudege_moon_bigtts'},
  22. {'text': '北京小爷', 'value': 'zh_male_beijingxiaoye_moon_bigtts'},
  23. {'text': '少年梓辛/Brayan', 'value': 'zh_male_shaonianzixin_moon_bigtts'},
  24. {'text': '魅力女友', 'value': 'zh_female_meilinvyou_moon_bigtts'},
  25. ])
  26. speed_ratio = forms.SliderField(
  27. TooltipLabel(_('speaking speed'), _('[0.2,3], the default is 1, usually one decimal place is enough')),
  28. required=True, default_value=1,
  29. _min=0.2,
  30. _max=3,
  31. _step=0.1,
  32. precision=1)
  33. class VolcanicEngineTTSModelCredential(BaseForm, BaseModelCredential):
  34. volcanic_api_url = forms.TextInputField('API URL', required=True,
  35. default_value='wss://openspeech.bytedance.com/api/v1/tts/ws_binary')
  36. volcanic_app_id = forms.TextInputField('App ID', required=True)
  37. volcanic_token = forms.PasswordInputField('Access Token', required=True)
  38. volcanic_cluster = forms.TextInputField('Cluster ID', required=True)
  39. def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
  40. raise_exception=False):
  41. model_type_list = provider.get_model_type_list()
  42. if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
  43. raise AppApiException(ValidCode.valid_error.value,
  44. gettext('{model_type} Model type is not supported').format(model_type=model_type))
  45. for key in ['volcanic_api_url', 'volcanic_app_id', 'volcanic_token', 'volcanic_cluster']:
  46. if key not in model_credential:
  47. if raise_exception:
  48. raise AppApiException(ValidCode.valid_error.value, gettext('{key} is required').format(key=key))
  49. else:
  50. return False
  51. try:
  52. model = provider.get_model(model_type, model_name, model_credential, **model_params)
  53. model.check_auth()
  54. except Exception as e:
  55. maxkb_logger.error(f'Exception: {e}', exc_info=True)
  56. if isinstance(e, AppApiException):
  57. raise e
  58. if raise_exception:
  59. raise AppApiException(ValidCode.valid_error.value, gettext(
  60. 'Verification failed, please check whether the parameters are correct: {error}').format(
  61. error=str(e)))
  62. else:
  63. return False
  64. return True
  65. def encryption_dict(self, model: Dict[str, object]):
  66. return {**model, 'volcanic_token': super().encryption(model.get('volcanic_token', ''))}
  67. def get_model_params_setting_form(self, model_name):
  68. return VolcanicEngineTTSModelGeneralParams()