| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  | import logging | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from flask import request | 
					
						
							| 
									
										
										
										
											2024-02-15 22:41:18 +08:00
										 |  |  | from flask_restful import Resource, reqparse | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from werkzeug.exceptions import InternalServerError | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  | import services | 
					
						
							|  |  |  | from controllers.console import api | 
					
						
							|  |  |  | from controllers.console.app import _get_app | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from controllers.console.app.error import ( | 
					
						
							|  |  |  |     AppUnavailableError, | 
					
						
							|  |  |  |     AudioTooLargeError, | 
					
						
							|  |  |  |     CompletionRequestError, | 
					
						
							|  |  |  |     NoAudioUploadedError, | 
					
						
							|  |  |  |     ProviderModelCurrentlyNotSupportError, | 
					
						
							|  |  |  |     ProviderNotInitializeError, | 
					
						
							|  |  |  |     ProviderNotSupportSpeechToTextError, | 
					
						
							|  |  |  |     ProviderQuotaExceededError, | 
					
						
							|  |  |  |     UnsupportedAudioTypeError, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  | from controllers.console.setup import setup_required | 
					
						
							|  |  |  | from controllers.console.wraps import account_initialization_required | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from core.errors.error import ModelCurrentlyNotSupportError, ProviderTokenNotInitError, QuotaExceededError | 
					
						
							|  |  |  | from core.model_runtime.errors.invoke import InvokeError | 
					
						
							|  |  |  | from libs.login import login_required | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  | from services.audio_service import AudioService | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from services.errors.audio import ( | 
					
						
							|  |  |  |     AudioTooLargeServiceError, | 
					
						
							|  |  |  |     NoAudioUploadedServiceError, | 
					
						
							|  |  |  |     ProviderNotSupportSpeechToTextServiceError, | 
					
						
							|  |  |  |     UnsupportedAudioTypeServiceError, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ChatMessageAudioApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def post(self, app_id): | 
					
						
							|  |  |  |         app_id = str(app_id) | 
					
						
							|  |  |  |         app_model = _get_app(app_id, 'chat') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         file = request.files['file'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |             response = AudioService.transcript_asr( | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  |                 tenant_id=app_model.tenant_id, | 
					
						
							| 
									
										
										
										
											2024-02-15 22:41:18 +08:00
										 |  |  |                 file=file, | 
					
						
							|  |  |  |                 end_user=None, | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             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() | 
					
						
							| 
									
										
										
										
											2023-07-17 00:14:19 +08:00
										 |  |  |         except ProviderTokenNotInitError as ex: | 
					
						
							|  |  |  |             raise ProviderNotInitializeError(ex.description) | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  |         except QuotaExceededError: | 
					
						
							|  |  |  |             raise ProviderQuotaExceededError() | 
					
						
							|  |  |  |         except ModelCurrentlyNotSupportError: | 
					
						
							|  |  |  |             raise ProviderModelCurrentlyNotSupportError() | 
					
						
							| 
									
										
										
										
											2024-01-02 23:42:00 +08:00
										 |  |  |         except InvokeError as e: | 
					
						
							| 
									
										
										
										
											2024-01-04 17:49:55 +08:00
										 |  |  |             raise CompletionRequestError(e.description) | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  |         except ValueError as e: | 
					
						
							|  |  |  |             raise e | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							| 
									
										
										
										
											2024-02-15 22:41:18 +08:00
										 |  |  |             logging.exception(f"internal server error, {str(e)}.") | 
					
						
							| 
									
										
										
										
											2023-07-07 17:50:42 +08:00
										 |  |  |             raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ChatMessageTextApi(Resource): | 
					
						
							|  |  |  |     @setup_required | 
					
						
							|  |  |  |     @login_required | 
					
						
							|  |  |  |     @account_initialization_required | 
					
						
							|  |  |  |     def post(self, app_id): | 
					
						
							|  |  |  |         app_id = str(app_id) | 
					
						
							|  |  |  |         app_model = _get_app(app_id, None) | 
					
						
							| 
									
										
										
										
											2024-02-15 22:41:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |         try: | 
					
						
							|  |  |  |             response = AudioService.transcript_tts( | 
					
						
							|  |  |  |                 tenant_id=app_model.tenant_id, | 
					
						
							|  |  |  |                 text=request.form['text'], | 
					
						
							| 
									
										
										
										
											2024-02-15 22:41:18 +08:00
										 |  |  |                 voice=app_model.app_model_config.text_to_speech_dict.get('voice'), | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |                 streaming=False | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return {'data': response.data.decode('latin1')} | 
					
						
							|  |  |  |         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: | 
					
						
							| 
									
										
										
										
											2024-02-15 22:41:18 +08:00
										 |  |  |             logging.exception(f"internal server error, {str(e)}.") | 
					
						
							|  |  |  |             raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TextModesApi(Resource): | 
					
						
							|  |  |  |     def get(self, app_id: str): | 
					
						
							|  |  |  |         app_model = _get_app(str(app_id)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             parser = reqparse.RequestParser() | 
					
						
							|  |  |  |             parser.add_argument('language', type=str, required=True, location='args') | 
					
						
							|  |  |  |             args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             response = AudioService.transcript_tts_voices( | 
					
						
							|  |  |  |                 tenant_id=app_model.tenant_id, | 
					
						
							|  |  |  |                 language=args['language'], | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return response | 
					
						
							|  |  |  |         except services.errors.audio.ProviderNotSupportTextToSpeechLanageServiceError: | 
					
						
							|  |  |  |             raise AppUnavailableError("Text to audio voices language parameter loss.") | 
					
						
							|  |  |  |         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(f"internal server error, {str(e)}.") | 
					
						
							| 
									
										
										
										
											2024-01-24 01:05:37 +08:00
										 |  |  |             raise InternalServerError() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | api.add_resource(ChatMessageAudioApi, '/apps/<uuid:app_id>/audio-to-text') | 
					
						
							|  |  |  | api.add_resource(ChatMessageTextApi, '/apps/<uuid:app_id>/text-to-audio') | 
					
						
							| 
									
										
										
										
											2024-02-15 22:41:18 +08:00
										 |  |  | api.add_resource(TextModesApi, '/apps/<uuid:app_id>/text-to-audio/voices') |