Skip to content

Commit

Permalink
fix: add type hints for App model and improve error handling in audio…
Browse files Browse the repository at this point in the history
… services (#12677)

Signed-off-by: -LAN- <[email protected]>
  • Loading branch information
laipz8200 authored Jan 13, 2025
1 parent c700364 commit cb34991
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions api/controllers/console/app/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError
from core.model_runtime.errors.invoke import InvokeError
from libs.login import login_required
from models.model import AppMode
from models import App, AppMode
from services.audio_service import AudioService
from services.errors.audio import (
AudioTooLargeServiceError,
Expand Down Expand Up @@ -79,7 +79,7 @@ class ChatMessageTextApi(Resource):
@login_required
@account_initialization_required
@get_app_model
def post(self, app_model):
def post(self, app_model: App):
from werkzeug.exceptions import InternalServerError

try:
Expand All @@ -98,9 +98,13 @@ def post(self, app_model):
and app_model.workflow.features_dict
):
text_to_speech = app_model.workflow.features_dict.get("text_to_speech")
if text_to_speech is None:
raise ValueError("TTS is not enabled")
voice = args.get("voice") or text_to_speech.get("voice")
else:
try:
if app_model.app_model_config is None:
raise ValueError("AppModelConfig not found")
voice = args.get("voice") or app_model.app_model_config.text_to_speech_dict.get("voice")
except Exception:
voice = None
Expand Down
4 changes: 3 additions & 1 deletion api/services/audio_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def transcript_tts(
from app import app
from extensions.ext_database import db

def invoke_tts(text_content: str, app_model, voice: Optional[str] = None):
def invoke_tts(text_content: str, app_model: App, voice: Optional[str] = None):
with app.app_context():
if app_model.mode in {AppMode.ADVANCED_CHAT.value, AppMode.WORKFLOW.value}:
workflow = app_model.workflow
Expand All @@ -95,6 +95,8 @@ def invoke_tts(text_content: str, app_model, voice: Optional[str] = None):

voice = features_dict["text_to_speech"].get("voice") if voice is None else voice
else:
if app_model.app_model_config is None:
raise ValueError("AppModelConfig not found")
text_to_speech_dict = app_model.app_model_config.text_to_speech_dict

if not text_to_speech_dict.get("enabled"):
Expand Down

0 comments on commit cb34991

Please sign in to comment.