使用科大讯飞api以及文心一言模型实现对话

因为比赛要用到人机进行对话所以写以下python代码。以下为3.8

代码中相关api以及密匙都更改过,请自己去获得密匙

科大讯飞网址讯飞开放平台-以语音交互为核心的人工智能开放平台 (xfyun.cn)

百度千帆文心一言百度智能云千帆大模型 (baidu.com)

首先通过麦克风获取人的声音,并且调用科大讯飞api实现语音转文字,接着根据人说的话在不在用户固定的对答库中进行判断,如果在则根据问答库固定内容进行回答,如果不在则发给文心一言api进行回答。

import pyttsx3

import websocket

import hashlib

import base64

import hmac

import json

from urllib.parse import urlencode

import time

import ssl

from pydub import AudioSegment

from wsgiref.handlers import format_date_time

from datetime import datetime

from time import mktime

import _thread as thread

import pyaudio

import sys

import os

import requests

from yy import text_to_speech  #文字转语音函数调用



 

STATUS_FIRST_FRAME = 0  # 第一帧的标识

STATUS_CONTINUE_FRAME = 1  # 中间帧标识

STATUS_LAST_FRAME = 2  # 最后一帧的标识

silence_counter = 0

SILENCE_THRESHOLD = 5  # 静音阈值,单位为帧数

# 定义保存文件路径

save_folder = r"C:\Users\=xxxxxxxxxxt"  # 替换为你想保存文件的路径

# 修改成文心一言sapi key和secret key

API_KEY = "ylY7jwOX6OMa0PxxxxxxxGq"

SECRET_KEY = "6eCR15qZxO2sYnHbH5wxxxxxxxxxb3"

question = ['你是谁', '我是不失,你的小笨蛋',

             ‘ 我是不是世界上最帅的’, ’是的‘,

            ]

class Ws_Param(object):

    # 初始化

    def __init__(self, APPID, APIKey, APISecret):

        self.APPID = APPID

        self.APIKey = APIKey

        self.APISecret = APISecret


 

        # 公共参数(common)

        self.CommonArgs = {"app_id": self.APPID}

        # 业务参数(business),更多个性化参数可在官网查看

        self.BusinessArgs = {"domain": "iat", "language": "zh_cn", "accent": "mandarin", "vinfo":1,"vad_eos":10000}

    # 生成url

    def create_url(self):

        url = 'wss://ws-api.xfyun.cn/v2/iat'

        # 生成RFC1123格式的时间戳

        now = datetime.now()

        date = format_date_time(mktime(now.timetuple()))

        # 拼接字符串

        signature_origin = "host: " + "ws-api.xfyun.cn" + "\n"

        signature_origin += "date: " + date + "\n"

        signature_origin += "GET " + "/v2/iat " + "HTTP/1.1"

        # 进行hmac-sha256进行加密

        signature_sha = hmac.new(self.APISecret.encode('utf-8'), signature_origin.encode('utf-8'),

                                 digestmod=hashlib.sha256).digest()

        signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')

        authorization_origin = "api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"" % (

            self.APIKey, "hmac-sha256", "host date request-line", signature_sha)

        authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')

        # 将请求的鉴权参数组合为字典

        v = {

            "authorization": authorization,

            "date": date,

            "host": "ws-api.xfyun.cn"

        }

        # 拼接鉴权参数,生成url

        url = url + '?' + urlencode(v)

        # print("date: ",date)

        # print("v: ",v)

        # 此处打印出建立连接时候的url,参考本demo的时候可取消上方打印的注释,比对相同参数时生成的url与自己代码生成的url是否一致

        # print('websocket url :', url)

        return url


 

# 收到websocket消息的处理

def on_message(ws, message):


 

    result = ''

    try:

        code = json.loads(message)["code"]

        sid = json.loads(message)["sid"]

        if code != 0:

            errMsg = json.loads(message)["message"]

            print("sid:%s call error:%s code is:%s" % (sid, errMsg, code))

        else:

            data = json.loads(message)["data"]["result"]["ws"]

            for i in data:

                for w in i["cw"]:

                    result += w["w"]

                if result == '。' or result=='?' or result==',':

                    result = ''

                    pass

            if result == "再见" or result == "退出":

                print("用户:"+result)

                sys.exit()

            elif result in question:

                print ( "用户: %s" % (result) )

                # reply是机器人回答的话

                reply = question[question.index ( result ) + 1]

            else:

                print("people: %s" % (result))

                # 根据语音识别文字给出智能回答

                #此处是百度的智能机器人api

                url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=" + get_access_token()

                payload = json.dumps({

                    "messages": [

                        {

                            "role": "user",

                            "content": result

                        }

                    ]

                })

                headers = {'Content-Type': 'application/json'}

                response = requests.request("POST", url, headers=headers, data=payload).json()

                reply = response['result']

    except Exception as e:

        print("receive msg,but parse exception:", e)

    ws.close()

    # 打开文件准备写入

    file_path = os.path.join(save_folder, "result.txt")

    with open(file_path, "a", encoding="utf-8") as file:

        # 在回调函数中写入结果

        file.write("people: %s\n" % result)  # 将用户的消息写入文件

        file.write("robot: %s\n" % reply)   # 将机器人的回复写入文件

    # 输出回答            

    print("AI: %s" % reply)  # 打印AI的回答

    ws.close()

      # 语音合成

    text_to_speech(reply, save_folder)  # 使用讯飞的语音合成 API

    progress_bar(3)  # 运行5秒的进度条

    text_to_speech("请说话", save_folder)

   

    # dialogue_flag = True


 

def progress_bar(duration):

    for i in range(101): # 0 to 100

        sys.stdout.write("\r[%-100s] %d%%" % ('='*i, i)) # 打印进度条

        sys.stdout.flush() # 刷新输出缓冲区

        time.sleep(duration/100) # 暂停

    sys.stdout.write("\n") # 新的一行

def get_access_token():

    """

    使用 AK,SK 生成鉴权签名(Access Token)

    :return: access_token,或是None(如果错误)

    """

    url = "https://aip.baidubce.com/oauth/2.0/token"

    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}

    return str(requests.post(url, params=params).json().get("access_token"))

# 收到websocket错误的处理

def on_error(ws, error):

    print("### error:", error)


 

# 收到websocket关闭的处理

def on_close(ws):

    pass

    print("### closed  record ###")


 

def on_open(ws):

    def run(*args):  # 将wsParam作为参数传递

        status = STATUS_FIRST_FRAME  

        CHUNK = 520                

        FORMAT = pyaudio.paInt16  

        CHANNELS = 1  

        RATE = 16000  

        p = pyaudio.PyAudio()

       

        stream = p.open(format=FORMAT,  

                        channels=CHANNELS,  

                        rate=RATE,  

                        input=True,

                        frames_per_buffer=CHUNK)

        print("- - - - - - - Start Recording ...- - - - - - - ")

        for i in range(0,int(RATE/CHUNK*60)):

            buf = stream.read(CHUNK)

           

            if not buf:

                print("over")

                status = STATUS_LAST_FRAME

                silence_counter += 1  

            else:

                silence_counter = 0  

            if silence_counter >= SILENCE_THRESHOLD:

                print("Long time no sound detected. Entering sleep mode.")

                break

            if status == STATUS_FIRST_FRAME:

                d = {"common": wsParam.CommonArgs,

                     "business": wsParam.BusinessArgs,

                     "data": {"status": 0, "format": "audio/L16;rate=16000",

                              "audio": str(base64.b64encode(buf), 'utf-8'),

                              "encoding": "raw"}}

                d = json.dumps(d)

                ws.send(d)

                status = STATUS_CONTINUE_FRAME

            elif status == STATUS_CONTINUE_FRAME:

                d = {"data": {"status": 1, "format": "audio/L16;rate=16000",

                              "audio": str(base64.b64encode(buf), 'utf-8'),

                              "encoding": "raw"}}

                ws.send(json.dumps(d))

            elif status == STATUS_LAST_FRAME:  

                d = {"data": {"status": 2, "format": "audio/L16;rate=16000",

                              "audio": str(base64.b64encode(buf), 'utf-8'),

                              "encoding": "raw"}}

                ws.send(json.dumps(d))

                time.sleep(1)

                break

        stream.stop_stream()

        stream.close()

        p.terminate()

        ws.close()

    thread.start_new_thread(run, ())  # 不再传递 wsParam


 

# def run():

if __name__ == "__main__":

    # global wsParam

    #此处是讯飞语音的api接口

    wsParam = Ws_Param(APPID='a54xxxxd', APIKey='a0da0eb117e75cxxxxxxx',

                       APISecret='YjU5MjYwZmNmODM0Yxxxxxxxxxxxxxxx')

    websocket.enableTrace(False)

    wsUrl = wsParam.create_url()

    while True:  # 添加一个无限循环

        ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close)

        ws.on_open = lambda ws: on_open(ws)

        ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}, ping_timeout=5)

 以上代码实现了语音转文字并且和文心一言大模型对话,对话记录可以打印在终端上并且保存在txt文档里面。text_to_speech文字转语音函数移步我的另一个博客。

  • 38
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值