stt.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # coding=utf-8
  2. from typing import Dict
  3. from django.utils.translation import gettext as _
  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 VolcanicEngineSTTModelParams(BaseForm):
  10. uid = forms.TextInputField(
  11. TooltipLabel(_('User ID'),_('If not passed, the default value is streaming_asr_demo')),
  12. required=True,
  13. default_value='streaming_asr_demo'
  14. )
  15. class VolcanicEngineSTTModelCredential(BaseForm, BaseModelCredential):
  16. volcanic_api_url = forms.TextInputField('API URL', required=True,
  17. default_value='wss://openspeech.bytedance.com/api/v2/asr')
  18. volcanic_app_id = forms.TextInputField('App ID', required=True)
  19. volcanic_token = forms.PasswordInputField('Access Token', required=True)
  20. volcanic_cluster = forms.TextInputField('Cluster ID', required=True)
  21. def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider,
  22. raise_exception=False):
  23. model_type_list = provider.get_model_type_list()
  24. if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
  25. raise AppApiException(ValidCode.valid_error.value,
  26. _('{model_type} Model type is not supported').format(model_type=model_type))
  27. for key in ['volcanic_api_url', 'volcanic_app_id', 'volcanic_token', 'volcanic_cluster']:
  28. if key not in model_credential:
  29. if raise_exception:
  30. raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key))
  31. else:
  32. return False
  33. try:
  34. model = provider.get_model(model_type, model_name, model_credential, **model_params)
  35. model.check_auth()
  36. except Exception as e:
  37. maxkb_logger.error(f'Exception: {e}', exc_info=True)
  38. if isinstance(e, AppApiException):
  39. raise e
  40. if raise_exception:
  41. raise AppApiException(ValidCode.valid_error.value,
  42. _('Verification failed, please check whether the parameters are correct: {error}').format(
  43. error=str(e)))
  44. else:
  45. return False
  46. return True
  47. def encryption_dict(self, model: Dict[str, object]):
  48. return {**model, 'volcanic_token': super().encryption(model.get('volcanic_token', ''))}
  49. def get_model_params_setting_form(self, model_name):
  50. return VolcanicEngineSTTModelParams()