add tts api (#2107)
Browse files### What problem does this PR solve?
add tts api
- [x] New Feature (non-breaking change which adds functionality)
---------
Co-authored-by: Zhedong Cen <cenzhedong2@126.com>
Co-authored-by: Kevin Hu <kevinhu.sh@gmail.com>
api/apps/conversation_app.py
CHANGED
|
@@ -15,8 +15,10 @@
|
|
| 15 |
#
|
| 16 |
from copy import deepcopy
|
| 17 |
from flask import request, Response
|
| 18 |
-
from flask_login import login_required
|
| 19 |
from api.db.services.dialog_service import DialogService, ConversationService, chat
|
|
|
|
|
|
|
| 20 |
from api.utils.api_utils import server_error_response, get_data_error_result, validate_request
|
| 21 |
from api.utils import get_uuid
|
| 22 |
from api.utils.api_utils import get_json_result
|
|
@@ -176,6 +178,38 @@ def completion():
|
|
| 176 |
return server_error_response(e)
|
| 177 |
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
@manager.route('/delete_msg', methods=['POST'])
|
| 180 |
@login_required
|
| 181 |
@validate_request("conversation_id", "message_id")
|
|
@@ -221,4 +255,4 @@ def thumbup():
|
|
| 221 |
break
|
| 222 |
|
| 223 |
ConversationService.update_by_id(conv["id"], conv)
|
| 224 |
-
return get_json_result(data=conv)
|
|
|
|
| 15 |
#
|
| 16 |
from copy import deepcopy
|
| 17 |
from flask import request, Response
|
| 18 |
+
from flask_login import login_required,current_user
|
| 19 |
from api.db.services.dialog_service import DialogService, ConversationService, chat
|
| 20 |
+
from api.db.services.llm_service import LLMBundle, TenantService
|
| 21 |
+
from api.db import LLMType
|
| 22 |
from api.utils.api_utils import server_error_response, get_data_error_result, validate_request
|
| 23 |
from api.utils import get_uuid
|
| 24 |
from api.utils.api_utils import get_json_result
|
|
|
|
| 178 |
return server_error_response(e)
|
| 179 |
|
| 180 |
|
| 181 |
+
@manager.route('/tts', methods=['POST'])
|
| 182 |
+
@login_required
|
| 183 |
+
def tts():
|
| 184 |
+
req = request.json
|
| 185 |
+
text = req["text"]
|
| 186 |
+
|
| 187 |
+
tenants = TenantService.get_by_user_id(current_user.id)
|
| 188 |
+
if not tenants:
|
| 189 |
+
return get_data_error_result(retmsg="Tenant not found!")
|
| 190 |
+
|
| 191 |
+
tts_id = tenants[0]["tts_id"]
|
| 192 |
+
if not tts_id:
|
| 193 |
+
return get_data_error_result(retmsg="No default TTS model is set")
|
| 194 |
+
|
| 195 |
+
tts_mdl = LLMBundle(tenants[0]["tenant_id"], LLMType.TTS, tts_id)
|
| 196 |
+
def stream_audio():
|
| 197 |
+
try:
|
| 198 |
+
for chunk in tts_mdl(text):
|
| 199 |
+
yield chunk
|
| 200 |
+
except Exception as e:
|
| 201 |
+
yield "data:" + json.dumps({"retcode": 500, "retmsg": str(e),
|
| 202 |
+
"data": {"answer": "**ERROR**: "+str(e)}},
|
| 203 |
+
ensure_ascii=False).encode('utf-8')
|
| 204 |
+
|
| 205 |
+
resp = Response(stream_audio(), mimetype="audio/mpeg")
|
| 206 |
+
resp.headers.add_header("Cache-Control", "no-cache")
|
| 207 |
+
resp.headers.add_header("Connection", "keep-alive")
|
| 208 |
+
resp.headers.add_header("X-Accel-Buffering", "no")
|
| 209 |
+
|
| 210 |
+
return resp
|
| 211 |
+
|
| 212 |
+
|
| 213 |
@manager.route('/delete_msg', methods=['POST'])
|
| 214 |
@login_required
|
| 215 |
@validate_request("conversation_id", "message_id")
|
|
|
|
| 255 |
break
|
| 256 |
|
| 257 |
ConversationService.update_by_id(conv["id"], conv)
|
| 258 |
+
return get_json_result(data=conv)
|
api/db/services/user_service.py
CHANGED
|
@@ -96,6 +96,7 @@ class TenantService(CommonService):
|
|
| 96 |
cls.model.rerank_id,
|
| 97 |
cls.model.asr_id,
|
| 98 |
cls.model.img2txt_id,
|
|
|
|
| 99 |
cls.model.parser_ids,
|
| 100 |
UserTenant.role]
|
| 101 |
return list(cls.model.select(*fields)
|
|
|
|
| 96 |
cls.model.rerank_id,
|
| 97 |
cls.model.asr_id,
|
| 98 |
cls.model.img2txt_id,
|
| 99 |
+
cls.model.tts_id,
|
| 100 |
cls.model.parser_ids,
|
| 101 |
UserTenant.role]
|
| 102 |
return list(cls.model.select(*fields)
|