mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 19:59:50 +08:00
13be84e4d4
Some checks are pending
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
131 lines
5.3 KiB
Python
131 lines
5.3 KiB
Python
import logging
|
|
|
|
from flask import request
|
|
from werkzeug.exceptions import InternalServerError
|
|
|
|
import services
|
|
from controllers.console import api
|
|
from controllers.console.app.error import (
|
|
AppUnavailableError,
|
|
AudioTooLargeError,
|
|
CompletionRequestError,
|
|
NoAudioUploadedError,
|
|
ProviderModelCurrentlyNotSupportError,
|
|
ProviderNotInitializeError,
|
|
ProviderNotSupportSpeechToTextError,
|
|
ProviderQuotaExceededError,
|
|
UnsupportedAudioTypeError,
|
|
)
|
|
from controllers.console.explore.wraps import InstalledAppResource
|
|
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
|
|
from core.model_runtime.errors.invoke import InvokeError
|
|
from models.model import AppMode
|
|
from services.audio_service import AudioService
|
|
from services.errors.audio import (
|
|
AudioTooLargeServiceError,
|
|
NoAudioUploadedServiceError,
|
|
ProviderNotSupportSpeechToTextServiceError,
|
|
UnsupportedAudioTypeServiceError,
|
|
)
|
|
|
|
|
|
class ChatAudioApi(InstalledAppResource):
|
|
def post(self, installed_app):
|
|
app_model = installed_app.app
|
|
|
|
file = request.files["file"]
|
|
|
|
try:
|
|
response = AudioService.transcript_asr(app_model=app_model, file=file, end_user=None)
|
|
|
|
return response
|
|
except services.errors.app_model_config.AppModelConfigBrokenError:
|
|
logging.exception("App model config broken.")
|
|
raise AppUnavailableError()
|
|
except NoAudioUploadedServiceError:
|
|
raise NoAudioUploadedError()
|
|
except AudioTooLargeServiceError as e:
|
|
raise AudioTooLargeError(str(e))
|
|
except UnsupportedAudioTypeServiceError:
|
|
raise UnsupportedAudioTypeError()
|
|
except ProviderNotSupportSpeechToTextServiceError:
|
|
raise ProviderNotSupportSpeechToTextError()
|
|
except ProviderTokenNotInitError as ex:
|
|
raise ProviderNotInitializeError(ex.description)
|
|
except QuotaExceededError:
|
|
raise ProviderQuotaExceededError()
|
|
except ModelCurrentlyNotSupportError:
|
|
raise ProviderModelCurrentlyNotSupportError()
|
|
except InvokeError as e:
|
|
raise CompletionRequestError(e.description)
|
|
except ValueError as e:
|
|
raise e
|
|
except Exception as e:
|
|
logging.exception("internal server error.")
|
|
raise InternalServerError()
|
|
|
|
|
|
class ChatTextApi(InstalledAppResource):
|
|
def post(self, installed_app):
|
|
from flask_restful import reqparse
|
|
|
|
app_model = installed_app.app
|
|
try:
|
|
parser = reqparse.RequestParser()
|
|
parser.add_argument("message_id", type=str, required=False, location="json")
|
|
parser.add_argument("voice", type=str, location="json")
|
|
parser.add_argument("text", type=str, location="json")
|
|
parser.add_argument("streaming", type=bool, location="json")
|
|
args = parser.parse_args()
|
|
|
|
message_id = args.get("message_id", None)
|
|
text = args.get("text", None)
|
|
if (
|
|
app_model.mode in [AppMode.ADVANCED_CHAT.value, AppMode.WORKFLOW.value]
|
|
and app_model.workflow
|
|
and app_model.workflow.features_dict
|
|
):
|
|
text_to_speech = app_model.workflow.features_dict.get("text_to_speech")
|
|
voice = args.get("voice") if args.get("voice") else text_to_speech.get("voice")
|
|
else:
|
|
try:
|
|
voice = (
|
|
args.get("voice")
|
|
if args.get("voice")
|
|
else app_model.app_model_config.text_to_speech_dict.get("voice")
|
|
)
|
|
except Exception:
|
|
voice = None
|
|
response = AudioService.transcript_tts(app_model=app_model, message_id=message_id, voice=voice, text=text)
|
|
return response
|
|
except services.errors.app_model_config.AppModelConfigBrokenError:
|
|
logging.exception("App model config broken.")
|
|
raise AppUnavailableError()
|
|
except NoAudioUploadedServiceError:
|
|
raise NoAudioUploadedError()
|
|
except AudioTooLargeServiceError as e:
|
|
raise AudioTooLargeError(str(e))
|
|
except UnsupportedAudioTypeServiceError:
|
|
raise UnsupportedAudioTypeError()
|
|
except ProviderNotSupportSpeechToTextServiceError:
|
|
raise ProviderNotSupportSpeechToTextError()
|
|
except ProviderTokenNotInitError as ex:
|
|
raise ProviderNotInitializeError(ex.description)
|
|
except QuotaExceededError:
|
|
raise ProviderQuotaExceededError()
|
|
except ModelCurrentlyNotSupportError:
|
|
raise ProviderModelCurrentlyNotSupportError()
|
|
except InvokeError as e:
|
|
raise CompletionRequestError(e.description)
|
|
except ValueError as e:
|
|
raise e
|
|
except Exception as e:
|
|
logging.exception("internal server error.")
|
|
raise InternalServerError()
|
|
|
|
|
|
api.add_resource(ChatAudioApi, "/installed-apps/<uuid:installed_app_id>/audio-to-text", endpoint="installed_app_audio")
|
|
api.add_resource(ChatTextApi, "/installed-apps/<uuid:installed_app_id>/text-to-audio", endpoint="installed_app_text")
|
|
# api.add_resource(ChatTextApiWithMessageId, '/installed-apps/<uuid:installed_app_id>/text-to-audio/message-id',
|
|
# endpoint='installed_app_text_with_message_id')
|