From 68eb21b5560844457d0f84729189e2337fe5d8b7 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Fri, 18 Oct 2024 14:17:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=85=8D=E7=BD=AETTS=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=94=AF=E6=8C=81=E8=AF=95=E5=90=AC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serializers/application_serializers.py | 10 ++ apps/application/urls.py | 2 + apps/application/views/application_views.py | 16 ++ .../model/tts.py | 3 + .../impl/openai_model_provider/model/tts.py | 3 + .../model/tts.py | 3 + .../impl/xf_model_provider/model/tts.py | 3 + .../xinference_model_provider/model/tts.py | 3 + ui/src/api/application.ts | 16 +- .../views/application/ApplicationSetting.vue | 5 +- .../component/TTSModeParamSettingDialog.vue | 156 ++++++++++++++++++ ui/src/workflow/nodes/base-node/index.vue | 4 +- 12 files changed, 218 insertions(+), 6 deletions(-) create mode 100644 ui/src/views/application/component/TTSModeParamSettingDialog.vue diff --git a/apps/application/serializers/application_serializers.py b/apps/application/serializers/application_serializers.py index cca5ce170..8338d6a20 100644 --- a/apps/application/serializers/application_serializers.py +++ b/apps/application/serializers/application_serializers.py @@ -973,6 +973,16 @@ class ApplicationSerializer(serializers.Serializer): **application.tts_model_params_setting) return model.text_to_speech(text) + def play_demo_text(self, form_data, with_valid=True): + text = '你好,这里是语音播放测试' + if with_valid: + self.is_valid(raise_exception=True) + application_id = self.data.get('application_id') + application = QuerySet(Application).filter(id=application_id).first() + if application.tts_model_enable: + model = get_model_instance_by_model_user_id(application.tts_model_id, application.user_id, **form_data) + return model.text_to_speech(text) + class ApplicationKeySerializerModel(serializers.ModelSerializer): class Meta: model = ApplicationApiKey diff --git a/apps/application/urls.py b/apps/application/urls.py index 2555b7dcd..558cbb152 100644 --- a/apps/application/urls.py +++ b/apps/application/urls.py @@ -70,5 +70,7 @@ urlpatterns = [ name='application/audio'), path('application//text_to_speech', views.Application.TextToSpeech.as_view(), name='application/audio'), + path('application//play_demo_text', views.Application.PlayDemoText.as_view(), + name='application/audio'), ] diff --git a/apps/application/views/application_views.py b/apps/application/views/application_views.py index d9c9043e9..8a7f4931c 100644 --- a/apps/application/views/application_views.py +++ b/apps/application/views/application_views.py @@ -566,3 +566,19 @@ class Application(APIView): request.data.get('text')) return HttpResponse(byte_data, status=200, headers={'Content-Type': 'audio/mp3', 'Content-Disposition': 'attachment; filename="abc.mp3"'}) + + class PlayDemoText(APIView): + authentication_classes = [TokenAuth] + + @action(methods=['POST'], detail=False) + @has_permissions(ViewPermission([RoleConstants.ADMIN, RoleConstants.USER, RoleConstants.APPLICATION_ACCESS_TOKEN], + [lambda r, keywords: Permission(group=Group.APPLICATION, + operate=Operate.USE, + dynamic_tag=keywords.get( + 'application_id'))], + compare=CompareConstants.AND)) + def post(self, request: Request, application_id: str): + byte_data = ApplicationSerializer.Operate( + data={'application_id': application_id, 'user_id': request.user.id}).play_demo_text(request.data) + return HttpResponse(byte_data, status=200, headers={'Content-Type': 'audio/mp3', + 'Content-Disposition': 'attachment; filename="abc.mp3"'}) diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py index 35a46d189..356f9f907 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py @@ -44,3 +44,6 @@ class AliyunBaiLianTextToSpeech(MaxKBBaseModel, BaseTextToSpeech): print(audio) raise Exception(audio) return audio + + def is_cache_model(self): + return False diff --git a/apps/setting/models_provider/impl/openai_model_provider/model/tts.py b/apps/setting/models_provider/impl/openai_model_provider/model/tts.py index 839a59dc1..1247eabb5 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/openai_model_provider/model/tts.py @@ -56,3 +56,6 @@ class OpenAITextToSpeech(MaxKBBaseModel, BaseTextToSpeech): input=text, ) as response: return response.read() + + def is_cache_model(self): + return False \ No newline at end of file diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py index 5b438dc78..6b6ace2cf 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py @@ -102,6 +102,9 @@ class VolcanicEngineTextToSpeech(MaxKBBaseModel, BaseTextToSpeech): return asyncio.run(self.submit(request_json, text)) + def is_cache_model(self): + return False + def token_auth(self): return {'Authorization': 'Bearer; {}'.format(self.volcanic_token)} diff --git a/apps/setting/models_provider/impl/xf_model_provider/model/tts.py b/apps/setting/models_provider/impl/xf_model_provider/model/tts.py index 9bea1207a..b0bc0b017 100644 --- a/apps/setting/models_provider/impl/xf_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/xf_model_provider/model/tts.py @@ -113,6 +113,9 @@ class XFSparkTextToSpeech(MaxKBBaseModel, BaseTextToSpeech): return asyncio.run(handle()) + def is_cache_model(self): + return False + @staticmethod async def handle_message(ws): audio_bytes: bytes = b'' diff --git a/apps/setting/models_provider/impl/xinference_model_provider/model/tts.py b/apps/setting/models_provider/impl/xinference_model_provider/model/tts.py index bf3f91910..22accfc6f 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/model/tts.py @@ -58,3 +58,6 @@ class XInferenceTextToSpeech(MaxKBBaseModel, BaseTextToSpeech): input=text, ) as response: return response.read() + + def is_cache_model(self): + return False \ No newline at end of file diff --git a/ui/src/api/application.ts b/ui/src/api/application.ts index 290e5d359..4672d6f95 100644 --- a/ui/src/api/application.ts +++ b/ui/src/api/application.ts @@ -344,7 +344,7 @@ const postSpeechToText: ( } /** - * 语音转文本 + * 文本转语音 */ const postTextToSpeech: ( application_id: String, @@ -353,6 +353,17 @@ const postTextToSpeech: ( ) => Promise> = (application_id, data, loading) => { return download(`${prefix}/${application_id}/text_to_speech`, 'post', data, undefined, loading) } + +/** + * 播放测试文本 + */ +const playDemoText: ( + application_id: String, + data: any, + loading?: Ref +) => Promise> = (application_id, data, loading) => { + return download(`${prefix}/${application_id}/play_demo_text`, 'post', data, undefined, loading) +} /** * 获取平台状态 */ @@ -430,5 +441,6 @@ export default { getPlatformConfig, updatePlatformConfig, updatePlatformStatus, - validatePassword + validatePassword, + playDemoText } diff --git a/ui/src/views/application/ApplicationSetting.vue b/ui/src/views/application/ApplicationSetting.vue index fd7fd6530..30c1c9998 100644 --- a/ui/src/views/application/ApplicationSetting.vue +++ b/ui/src/views/application/ApplicationSetting.vue @@ -533,7 +533,7 @@ - + >() -const TTSModeParamSettingDialogRef = ref>() +const TTSModeParamSettingDialogRef = ref>() const ParamSettingDialogRef = ref>() const createModelRef = ref>() const selectProviderRef = ref>() diff --git a/ui/src/views/application/component/TTSModeParamSettingDialog.vue b/ui/src/views/application/component/TTSModeParamSettingDialog.vue new file mode 100644 index 000000000..352a24f4b --- /dev/null +++ b/ui/src/views/application/component/TTSModeParamSettingDialog.vue @@ -0,0 +1,156 @@ + + + + + diff --git a/ui/src/workflow/nodes/base-node/index.vue b/ui/src/workflow/nodes/base-node/index.vue index d423e8dff..196be0f6c 100644 --- a/ui/src/workflow/nodes/base-node/index.vue +++ b/ui/src/workflow/nodes/base-node/index.vue @@ -258,7 +258,7 @@ - +