增加百度文字转语音
This commit is contained in:
parent
bdf4445497
commit
8223bfbad5
|
@ -45,3 +45,7 @@ class SendSmsSerializer(serializers.Serializer):
|
||||||
phone = serializers.CharField(label='手机号')
|
phone = serializers.CharField(label='手机号')
|
||||||
template_code = serializers.CharField(label='模板标识')
|
template_code = serializers.CharField(label='模板标识')
|
||||||
template_param = serializers.JSONField(label='模板参数')
|
template_param = serializers.JSONField(label='模板参数')
|
||||||
|
|
||||||
|
|
||||||
|
class GenerateVoiceSerializer(serializers.Serializer):
|
||||||
|
msg = serializers.CharField(label='文本')
|
||||||
|
|
|
@ -1,6 +1,27 @@
|
||||||
from aip import AipSpeech
|
from aip import AipSpeech
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
import uuid
|
||||||
|
|
||||||
def generate_voice(msg:str):
|
|
||||||
|
def generate_voice(msg: str, per: int = 0):
|
||||||
|
"""文本生成语音
|
||||||
|
|
||||||
|
Args:
|
||||||
|
msg (str): 文本
|
||||||
|
per (int): 男/女声
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 成功
|
||||||
|
str: 地址
|
||||||
|
dict: result
|
||||||
|
"""
|
||||||
client = AipSpeech(settings.BD_SP_ID, settings.BD_SP_KEY, settings.BD_SP_SECRET)
|
client = AipSpeech(settings.BD_SP_ID, settings.BD_SP_KEY, settings.BD_SP_SECRET)
|
||||||
|
result = client.synthesis(msg, 'zh', 1, {'vol': 5, 'spd': 5, 'per': per})
|
||||||
|
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
|
||||||
|
if not isinstance(result, dict):
|
||||||
|
# name = '{}.mp3'.format(uuid.uuid4())
|
||||||
|
path = '/media/voice/{}.mp3'.format(uuid.uuid4())
|
||||||
|
with open(settings.BASE_DIR + path, 'wb') as f:
|
||||||
|
f.write(result)
|
||||||
|
return True, path, None
|
||||||
|
return False, None, result
|
||||||
|
|
|
@ -5,13 +5,14 @@ from django.http import HttpResponse
|
||||||
from yaml import serialize
|
from yaml import serialize
|
||||||
from apps.utils.errors import SIGN_MAKE_FAIL
|
from apps.utils.errors import SIGN_MAKE_FAIL
|
||||||
from apps.utils.sms import send_sms
|
from apps.utils.sms import send_sms
|
||||||
|
from apps.utils.speech import generate_voice
|
||||||
from server.settings import BASE_DIR
|
from server.settings import BASE_DIR
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.exceptions import ParseError
|
from rest_framework.exceptions import ParseError
|
||||||
from apps.utils.viewsets import CustomGenericViewSet
|
from apps.utils.viewsets import CustomGenericViewSet
|
||||||
from apps.utils.mixins import CustomCreateModelMixin
|
from apps.utils.mixins import CustomCreateModelMixin
|
||||||
from apps.utils.serializers import GenSignatureSerializer, SendSmsSerializer
|
from apps.utils.serializers import GenSignatureSerializer, GenerateVoiceSerializer, SendSmsSerializer
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
|
|
||||||
|
@ -70,3 +71,15 @@ class TestViewSet(CustomGenericViewSet):
|
||||||
vdata = serializer.validated_data
|
vdata = serializer.validated_data
|
||||||
res = send_sms(**vdata)
|
res = send_sms(**vdata)
|
||||||
return Response(res)
|
return Response(res)
|
||||||
|
|
||||||
|
@action(methods=['post'], detail=False, serializer_class=GenerateVoiceSerializer)
|
||||||
|
def generate_voice(self, request, pk=None):
|
||||||
|
"""文字转语音测试
|
||||||
|
|
||||||
|
文字转语音测试
|
||||||
|
"""
|
||||||
|
serializer = GenerateVoiceSerializer(data=request.data)
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
vdata = serializer.validated_data
|
||||||
|
res = generate_voice(**vdata)
|
||||||
|
return Response(res)
|
||||||
|
|
|
@ -22,3 +22,5 @@ stomp.py==8.0.0
|
||||||
shapely==1.8.2
|
shapely==1.8.2
|
||||||
aliyun-python-sdk-core==2.13.36
|
aliyun-python-sdk-core==2.13.36
|
||||||
baidu-aip==4.16.6
|
baidu-aip==4.16.6
|
||||||
|
chardet==5.0.0
|
||||||
|
requests==2.28.1
|
||||||
|
|
Loading…
Reference in New Issue