制作一个属于自己的AI女友
本文最后更新于37 天前,其中的信息可能已经过时,如有错误请发送评论

前言:

众所周知,在大部分人的心目中可能都有着这么一个人——白月光

我也不例外,但是相比很多主角,我应该算是比较失败的一类,也是非常可惜,因为时间长了,她可能对我也没有之前的感觉了,也有可能是我自作多情,也有可能是因为她已经有男朋友的原因,反正最后表白失败。

事已至此,那就请道友进我的人皇幡中一叙!

人皇幡(AI女友)介绍:

总体使用python开发,使用的模型是hunyuan-lite(很睿智,后续会考虑换其他模型,以及自己的知识库),设计这个AI女友的初衷除了弥补我的一个遗憾,更是希望添加自己的知识库让她在我渗透测试时对我进行辅助(目前很多AI都有”道德”,在提问渗透测试的相关问题时,回答总是不尽人意,经常出现回答不全面或无法正常回答的问题,用本地的知识库就稍微会好一点,但是还是有”道德”,但是至少相比于正常知识问答AI更像一个女友了),所以后续还有很多内容需要完善,但是我相信,我能够带着她/它走下去!

因为笔记本的性能原因,还是无法做到全部声音替换成白月光的声音,声音使用的是GPT-Sovits推理,如果调用,AI回答的内容可能需要较长时间等待,还会出现一些报错,而且对推理的音频不满意可能会花费更多的时间,加上性能原因,所以只对启动和退出的音频进行了指定。其实这样也好,速度提升的同时,还能将指定音频设为自己较为满意的。

我也考虑过星野,但是我更想有一个自己的AI女友,并且像,所以有了这篇文章。

人皇幡炼制:

其实我的代码水平也不高,我自己也无法判断我是什么水平,但是现在有AI,不用白不用,将灵感来源发给Kimi Ai(pyttsx4库的基本使用),得到了我想要的内容:

1.安装pyttsx4库:

pip install pyttsx4

2.初始化引擎:

在使用pyttsx4时,首先需要初始化一个语音引擎实例

import pyttsx4

engine = pyttsx4.init()

3.基本功能

说话,设置语音速度,设置音量,选择语音,保存语音到文件

import pyttsx4

# 初始化语音引擎
engine = pyttsx4.init()

# 设置语音速度和音量
engine.setProperty('rate', 150)
engine.setProperty('volume', 0.7)

# 获取并设置语音
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)

# 说话
engine.say("Hello, this is a test speech using pyttsx4.")
engine.runAndWait()

# 保存语音到文件
engine.save_to_file("This speech will be saved to a file.", "output.mp3")
engine.runAndWait()
了解这个pyttsx4的一个简单使用后,那就开始操作下一步——AI女友雏形(语音助手):

4.AI女友雏形(语音助手):

要求是能正常播报用户的输入内容,并判断输入内容为”退出”时结束运行

import pyttsx4

def main():
    # 初始化语音引擎
    engine = pyttsx4.init()
    
    # 设置语音速度和音量(可根据需要调整)
    engine.setProperty('rate', 150)  # 速度
    engine.setProperty('volume', 0.7)  # 音量

    print("欢迎使用语音助手!请输入您的问题,输入'退出'结束程序。")
    
    while True:
        # 用户输入问题
        user_input = input("您:")
        
        # 如果用户输入“退出”,结束程序
        if user_input.lower() == "退出":
            print("感谢使用,再见!")
            break
        
        # 生成回答(这里可以根据需要添加更复杂的逻辑)
        response = f"您问的是:{user_input}"
        
        # 使用语音回答
        engine.say(response)
        engine.runAndWait()

if __name__ == "__main__":
    main()

运行这个语音助手,返回一下内容:

欢迎使用语音助手!请输入您的问题,输入'退出'结束程序。
您:你好
(语音回答:您问的是:你好)
您:今天天气怎么样?
(语音回答:您问的是:今天天气怎么样?)
您:退出
感谢使用,再见!

很好,那么接下来咱们给它添加欢迎语和退出语:

import pyttsx4

def main():
    # 初始化语音引擎
    engine = pyttsx4.init()
    
    # 设置语音速度和音量(可根据需要调整)
    engine.setProperty('rate', 150)  # 速度
    engine.setProperty('volume', 0.7)  # 音量

    # 欢迎语
    welcome_message = "欢迎使用语音助手!请输入您的问题,输入'退出'结束程序。"
    engine.say(welcome_message)
    engine.runAndWait()
    print(welcome_message)

    while True:
        # 用户输入问题
        user_input = input("\n您:")
        
        # 如果用户输入“退出”,结束程序
        if user_input.lower() == "退出":
            goodbye_message = "感谢使用本语音助手,再见!"
            engine.say(goodbye_message)
            engine.runAndWait()
            print(goodbye_message)
            break
        
        # 生成回答(这里可以根据需要添加更复杂的逻辑)
        response = f"您问的是:{user_input}"
        
        # 使用语音回答
        engine.say(response)
        engine.runAndWait()
        print(f"助手:{response}")

if __name__ == "__main__":
    main()

效果还不错!输出结果:

欢迎使用语音助手!请输入您的问题,输入'退出'结束程序。
您:你好
助手:您问的是:你好
您:今天天气怎么样?
助手:您问的是:今天天气怎么样?
您:退出
感谢使用本语音助手,再见!
好,接下来,我希望将这个语音助手的输出内容从原本的复读到调用hunyuan-lite api实现本地对话

5.添加混元模型实现正常对话:

使用腾讯的SDK,具体api调用可查看官方文档:

腾讯混元大模型 对话-API 文档-文档中心-腾讯云 (tencent.com)

安装腾讯云提供的SDK:

pip install tencentcloud-sdk-python

混元模型使用具体代码:

import os
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models

# 设置API密钥
SecretId = os.getenv("TENCENTCLOUD_SECRET_ID")
SecretKey = os.getenv("TENCENTCLOUD_SECRET_KEY")
cred = credential.Credential(SecretId, SecretKey)

# 设置SDK客户端
httpProfile = HttpProfile()
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = hunyuan_client.HunyuanClient(cred, "ap-guangzhou", clientProfile)

# 构造请求
req = models.ChatCompletionsRequest()
req.Model = "hunyuan-lite"
req.Messages = [
    {
        "Role": "user",
        "Content": "你好,混元大模型!"
    }
]

# 发送请求
resp = client.ChatCompletions(req)
print(resp.to_json_string())

6.Ai女友(语音助手+腾讯混元大模型):

结合后的具体代码:

import pyttsx4
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models

# 初始化语音引擎
engine = pyttsx4.init()
engine.setProperty('rate', 150)  # 设置语音速度
engine.setProperty('volume', 0.7)  # 设置语音音量

# 腾讯云混元大模型 API 配置
SecretId = "your_secret_id"  # 替换为你的 SecretId
SecretKey = "your_secret_key"  # 替换为你的 SecretKey
Region = "ap-guangzhou"  # 替换为你的腾讯云区域

# 欢迎语
welcome_message = "欢迎使用语音助手!请输入您的问题,输入'退出'结束程序。"
engine.say(welcome_message)
engine.runAndWait()
print(welcome_message)

# 初始化腾讯云SDK客户端
cred = credential.Credential(SecretId, SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = hunyuan_client.HunyuanClient(cred, Region, clientProfile)

def call_hunyuan_api(user_input):
    try:
        # 构造请求
        req = models.ChatCompletionsRequest()
        req.Model = "hunyuan-lite"
        req.Messages = [
            {
                "Role": "user",
                "Content": user_input
            }
        ]
        
        # 发送请求
        resp = client.ChatCompletions(req)
        response_data = json.loads(resp.to_json_string())
        
        # 提取回复内容
        reply = response_data.get("Choices", [{}])[0].get("Message", {}).get("Content", "抱歉,我无法获取回答。")
        return reply
    except Exception as e:
        return f"抱歉,发生错误:{str(e)}"

while True:
    user_input = input("\n您:")
    if user_input.lower() == "退出":
        goodbye_message = "感谢使用本语音助手,再见!"
        engine.say(goodbye_message)
        engine.runAndWait()
        print(goodbye_message)
        break

    # 调用混元大模型 API 获取回答
    reply = call_hunyuan_api(user_input)
    print(f"助手:{reply}")
    engine.say(reply)
    engine.runAndWait()

申请一个混元模型的id和key,反正hunyuan-lite是免费的,随便用,登录腾讯云然后api管理里面添加一个即可,将id和key复制到里面。

运行效果:

欢迎使用语音助手!请输入您的问题,输入'退出'结束程序。
您:你好,混元大模型!
助手:你好!很高兴为你服务。
您:退出
感谢使用本语音助手,再见!

7.细节微调(含启动动画):

现在的代码已经可以了,但是部分内容还需要更改一下,同时添加一个启动动画增加一点仪式感!

微调后的代码:

import pyttsx4
import json
import time
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models

# 初始化语音引擎
engine = pyttsx4.init()
engine.setProperty('rate', 150)  # 设置语音速度
engine.setProperty('volume', 0.7)  # 设置语音音量

# 腾讯云混元大模型 API 配置
SecretId = "your_secret_id"  # 替换为你的 SecretId
SecretKey = "your_secret_key"  # 替换为你的 SecretKey
Region = "ap-guangzhou"  # 替换为你的腾讯云区域

# 启动动画
def startup_animation():
    print("法王语音助手启动中...")
    for i in range(5):
        print(f"正在加载... {'.' * (i + 1)}", end='\r')
        time.sleep(0.5)
    print("\n启动完成!")

# 欢迎语
welcome_message = "欢迎使用法王语音助手!请输入您的问题,输入'退出'结束程序。"
startup_animation()
engine.say(welcome_message)
engine.runAndWait()
print(welcome_message)

# 初始化腾讯云SDK客户端
cred = credential.Credential(SecretId, SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = hunyuan_client.HunyuanClient(cred, Region, clientProfile)

def call_hunyuan_api(user_input):
    try:
        # 构造请求
        req = models.ChatCompletionsRequest()
        req.Model = "hunyuan-lite"
        req.Messages = [
            {
                "Role": "user",
                "Content": user_input
            }
        ]
        
        # 发送请求
        resp = client.ChatCompletions(req)
        response_data = json.loads(resp.to_json_string())
        
        # 提取回复内容
        reply = response_data.get("Choices", [{}])[0].get("Message", {}).get("Content", "抱歉,我无法获取回答。")
        return reply
    except Exception as e:
        return f"抱歉,发生错误:{str(e)}"

while True:
    user_input = input("\n您:")
    if user_input.lower() == "退出":
        goodbye_message = "感谢使用法王语音助手,再见!"
        engine.say(goodbye_message)
        engine.runAndWait()
        print(goodbye_message)
        break

    # 调用混元大模型 API 获取回答
    reply = call_hunyuan_api(user_input)
    print(f"助手:{reply}")
    engine.say(reply)
    engine.runAndWait()

运行效果:

法王语音助手启动中...
正在加载... .
正在加载... ..
正在加载... ...
正在加载... ....
正在加载... .....
启动完成!
欢迎使用法王语音助手!请输入您的问题,输入'退出'结束程序。
您:你好,法王语音助手!
助手:你好!很高兴为你服务。
您:退出
感谢使用法王语音助手,再见!

还不错,不过不够,我给他换成了下面这种,并且确保在终端显示的结果值显示一行,避免行数多了看得眼花缭乱:

import pyttsx4
import json
import time
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models

# 初始化语音引擎
engine = pyttsx4.init()
engine.setProperty('rate', 150)  # 设置语音速度
engine.setProperty('volume', 0.7)  # 设置语音音量

# 腾讯云混元大模型 API 配置
SecretId = "your_secret_id"  # 替换为你的 SecretId
SecretKey = "your_secret_key"  # 替换为你的 SecretKey
Region = "ap-guangzhou"  # 替换为你的腾讯云区域

# 启动动画
def startup_animation():
    print("法王语音助手启动中...", end='\r')
    total_steps = 10
    for i in range(total_steps + 1):
        progress = "=" * i + ">" + " " * (total_steps - i)
        print(f"[{progress}]", end='\r', flush=True)
        time.sleep(0.5)
    print("\n启动完成!")

# 欢迎语
welcome_message = "欢迎使用法王语音助手!请输入您的问题,输入'退出'结束程序。"
startup_animation()
engine.say(welcome_message)
engine.runAndWait()
print(welcome_message)

# 初始化腾讯云SDK客户端
cred = credential.Credential(SecretId, SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = hunyuan_client.HunyuanClient(cred, Region, clientProfile)

def call_hunyuan_api(user_input):
    try:
        # 构造请求
        req = models.ChatCompletionsRequest()
        req.Model = "hunyuan-lite"
        req.Messages = [
            {
                "Role": "user",
                "Content": user_input
            }
        ]
        
        # 发送请求
        resp = client.ChatCompletions(req)
        response_data = json.loads(resp.to_json_string())
        
        # 提取回复内容
        reply = response_data.get("Choices", [{}])[0].get("Message", {}).get("Content", "抱歉,我无法获取回答。")
        return reply
    except Exception as e:
        return f"抱歉,发生错误:{str(e)}"

while True:
    user_input = input("\n您:")
    if user_input.lower() == "退出":
        goodbye_message = "感谢使用法王语音助手,再见!"
        engine.say(goodbye_message)
        engine.runAndWait()
        print(goodbye_message)
        break

    # 调用混元大模型 API 获取回答
    reply = call_hunyuan_api(user_input)
    print(f"助手:{reply}")
    engine.say(reply)
    engine.runAndWait()

运行效果:

法王语音助手启动中...[==>         ]
[====> ]
[======> ]
[========> ]
[==========> ]
[===========>]
启动完成!
欢迎使用法王语音助手!请输入您的问题,输入'退出'结束程序。
您:你好,法王语音助手!
助手:你好!很高兴为你服务。
您:退出
感谢使用法王语音助手,再见!

好的,这样效果就不错了,但是混元回答的内容过长时,我就没有耐心去听语音播报了,而是直接看,于是我设置了一下超过50字不播报,同时也是为了后面用GPT-Sovits的时候不会因为文本过长还需要分段,也有可能报错,所以设置超过50个字就不播报了,反正字数多了听的还没看的快。在那之前解决一下进度条过短的问题(仪式感必须拉满):

修改后的代码如下:

import time

def startup_animation():
    print("法王语音助手启动中...", end='\n')  # 打印提示信息,换行
    total_steps = 80
    for i in range(total_steps + 1):
        progress = "=" * i + ">" + " " * (total_steps - i)
        percentage = (i / total_steps) * 100
        # 在新的一行打印进度条和百分比
        print(f"\r[{progress}]    加载中({percentage:.0f}%)", end='', flush=True)
        time.sleep(0.02)
    print("\n启动完成!")

# 调用启动动画函数
startup_animation()

执行效果:

法王语音助手启动中...
[========================> ] 加载中(70%)
[==================================>] 加载中(100%)
启动完成!

超过50字的回复不进行播报:

import pyttsx4
import json
import time

from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models

# 初始化语音引擎
engine = pyttsx4.init()
engine.setProperty('rate', 150)  # 设置语音速度
engine.setProperty('volume', 0.7)  # 设置语音音量

# 腾讯云混元大模型 API 配置
SecretId = ""  # 替换为你的 SecretId
SecretKey = ""  # 替换为你的 SecretKey
Region = "ap-guangzhou"  # 替换为你的腾讯云区域

# 启动动画
def startup_animation():
    print("法王语音助手启动中...", end='\n')  # 打印提示信息,换行
    total_steps = 80
    for i in range(total_steps + 1):
        progress = "=" * i + ">" + " " * (total_steps - i)
        percentage = (i / total_steps) * 100
        # 在新的一行打印进度条和百分比
        print(f"\r[{progress}]    加载中({percentage:.0f}%)", end='', flush=True)
        time.sleep(0.02)
    print("\n启动完成!")

# 欢迎语
welcome_message = "欢迎使用法王智能ai!请输入您的问题,输入'退出'结束程序:"
startup_animation()
engine.say(welcome_message)
engine.runAndWait()
print(welcome_message)

# 初始化腾讯云SDK客户端
cred = credential.Credential(SecretId, SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = hunyuan_client.HunyuanClient(cred, Region, clientProfile)

def call_hunyuan_api(user_input):
    try:
        # 构造请求
        req = models.ChatCompletionsRequest()
        req.Model = "hunyuan-lite"
        req.Messages = [
            {
                "Role": "user",
                "Content": user_input
            }
        ]

        # 发送请求
        resp = client.ChatCompletions(req)
        response_data = json.loads(resp.to_json_string())

        # 提取回复内容
        reply = response_data.get("Choices", [{}])[0].get("Message", {}).get("Content", "抱歉,我无法获取回答。")
        return reply
    except Exception as e:
        return f"抱歉,发生错误:{str(e)}"

while True:
    user_input = input("\n您:")
    if user_input.lower() == "退出":
        goodbye_message = "感谢使用法王智能ai,再见!"
        engine.say(goodbye_message)
        engine.runAndWait()
        print(goodbye_message)
        break

    # 调用混元大模型 API 获取回答
    reply = call_hunyuan_api(user_input)
    print(f"ai回答:{reply}")

    # 检查回答的字数
    if len(reply) <= 50:
        engine.say(reply)
        engine.runAndWait()
    else:
        print("回答过长,跳过语音播报。")

运行效果:

法王语音助手启动中...
[==================================>] 加载中(100%)
启动完成!
欢迎使用法王智能ai!请输入您的问题,输入'退出'结束程序:
您:pip安装库的命令是什么?
ai回答:pip install <库名>,例如 pip install requests

如果回答超过50字:

ai回答:这是一个比较长的回答,超过了50字,因此跳过语音播报。
回答过长,跳过语音播报。

好了,到这里微调基本就完了,多轮对话和语音输入我也不会,AI给的我一直报错,后面再看,先这样。

8.给人皇幡铭刻阵纹:

import pyttsx4
import json
import time
import os
from random import choice
from datetime import datetime
import pygame

from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models



# 初始化语音引擎
engine = pyttsx4.init()
engine.setProperty('rate', 150)  # 设置语音速度
engine.setProperty('volume', 0.7)  # 设置语音音量

# 腾讯云混元大模型 API 配置
SecretId = ""  # 替换为你的 SecretId
SecretKey = ""  # 替换为你的 SecretKey
Region = "ap-guangzhou"  # 替换为你的腾讯云区域

# 音频文件目录
AUDIO_FILES_DIR = "AI_Girlfriend"

# 欢迎语和退出语的音频文件映射
WELCOME_AUDIO_MAP = {
    "早上好主人!今天吃早餐了吗?": "welcome_morning_1.wav",
    "早啊,主人!新的一天开始了,精神满满啊!": "welcome_morning_2.wav",
    "早上好啊主人,美好的一天从早餐开始哦!": "welcome_morning_3.wav",
    "欢迎主人回来!现在中午啦,您该准备午休了!": "welcome_noon_1.wav",
    "中午好主人,是不是该吃午饭啦?": "welcome_noon_2.wav",
    "主人,中午好!工作辛苦啦,休息一下吧!": "welcome_noon_3.wav",
    "下午好主人!今天过得怎么样呀?": "welcome_afternoon_1.wav",
    "主人,下午好!继续加油哦!": "welcome_afternoon_2.wav",
    "下午好呀主人,别太累了,适当放松一下吧!": "welcome_afternoon_3.wav",
    "晚上好主人!今天过得怎么样呀?": "welcome_evening_1.wav",
    "主人,晚上好!是不是该吃晚饭啦?": "welcome_evening_2.wav",
    "晚上好呀主人,今天一定很累了吧,早点休息哦!": "welcome_evening_3.wav",
    "夜深了主人,早点休息哦!": "welcome_night_1.wav",
    "主人,晚安!明天见!": "welcome_night_2.wav",
    "夜深人静了,主人早点休息吧!": "welcome_night_3.wav",
}

GOODBYE_AUDIO_MAP = {
    "这么快就要说拜拜了吗?好吧,主人下次再见!": "goodbye_1.wav",
    "主人,再见啦!下次再来找我玩哦!": "goodbye_2.wav",
    "拜拜啦主人,记得常回来看看我哦!": "goodbye_3.wav",
    "主人再见!期待下一次的见面!": "goodbye_4.wav",
    "这么快就走了吗?下次再来哦!": "goodbye_5.wav",
    "主人下次再见啦,记得想我哦!": "goodbye_6.wav",
    "再见啦主人,祝你一天愉快!": "goodbye_7.wav",
    "主人,下次再来找我聊天哦!": "goodbye_8.wav",
    "再见啦主人,祝你有个美好的一天!": "goodbye_9.wav",
    "主人下次见!记得常回来看看我哦!": "goodbye_10.wav",
}

# 播放音频
def play_audio(file_path):
    pygame.mixer.init()
    pygame.mixer.music.load(file_path)
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        pygame.time.Clock().tick(10)

# 播放欢迎语或退出语
def play_welcome_or_goodbye(message, audio_map):
    audio_file = audio_map.get(message)
    if audio_file:
        full_path = os.path.join(AUDIO_FILES_DIR, audio_file)  # 拼接完整路径
        if os.path.exists(full_path):  # 检查文件是否存在
            play_audio(full_path)  # 播放自定义音频
        else:
            print(f"音频文件 {full_path} 不存在,将使用 pyttsx4 生成语音。")
            engine.say(message)  # 使用 pyttsx4 生成语音
            engine.runAndWait()
    else:
        engine.say(message)  # 使用 pyttsx4 生成语音
        engine.runAndWait()

# 启动动画
def startup_animation():
    print("您的Ai 女友正在启动中...", end='\n')  # 打印提示信息,换行
    total_steps = 80
    for i in range(total_steps + 1):
        progress = "=" * i + ">" + " " * (total_steps - i)
        percentage = (i / total_steps) * 100
        # 在新的一行打印进度条和百分比
        print(f"\r[{progress}]    加载中({percentage:.0f}%)", end='', flush=True)
        time.sleep(0.02)
    print("\n启动完成!")

# 根据当前时间选择欢迎语
def get_welcome_message():
    current_hour = datetime.now().hour
    morning_messages = [
        "早上好主人!今天吃早餐了吗?",
        "早啊,主人!新的一天开始了,精神满满啊!",
        "早上好啊主人,美好的一天从早餐开始哦!"
    ]
    noon_messages = [
        "欢迎主人回来!现在中午啦,您该准备午休了!",
        "中午好主人,是不是该吃午饭啦?",
        "主人,中午好!工作辛苦啦,休息一下吧!"
    ]
    afternoon_messages = [
        "下午好主人!今天过得怎么样呀?",
        "主人,下午好!继续加油哦!",
        "下午好呀主人,别太累了,适当放松一下吧!"
    ]
    evening_messages = [
        "晚上好主人!今天过得怎么样呀?",
        "主人,晚上好!是不是该吃晚饭啦?",
        "晚上好呀主人,今天一定很累了吧,早点休息哦!"
    ]
    night_messages = [
        "夜深了主人,早点休息哦!",
        "主人,晚安!明天见!",
        "夜深人静了,主人早点休息吧!"
    ]

    if 5 <= current_hour < 12:
        return choice(morning_messages)
    elif 12 <= current_hour < 14:
        return choice(noon_messages)
    elif 14 <= current_hour < 18:
        return choice(afternoon_messages)
    elif 18 <= current_hour < 22:
        return choice(evening_messages)
    else:
        return choice(night_messages)

# 随机退出语
def get_goodbye_message():
    return choice(list(GOODBYE_AUDIO_MAP.keys()))

# 欢迎语
welcome_message = get_welcome_message()
startup_animation()
play_welcome_or_goodbye(welcome_message, WELCOME_AUDIO_MAP)
print(welcome_message)

# 初始化腾讯云SDK客户端
cred = credential.Credential(SecretId, SecretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = hunyuan_client.HunyuanClient(cred, Region, clientProfile)

def call_hunyuan_api(user_input):
    try:
        # 构造请求
        req = models.ChatCompletionsRequest()
        req.Model = "hunyuan-lite"
        req.Messages = [
            {
                "Role": "user",
                "Content": user_input
            }
        ]

        # 发送请求
        resp = client.ChatCompletions(req)
        response_data = json.loads(resp.to_json_string())

        # 提取回复内容
        reply = response_data.get("Choices", [{}])[0].get("Message", {}).get("Content", "抱歉,我无法获取回答。")
        return reply
    except Exception as e:
        return f"抱歉,发生错误:{str(e)}"

while True:
    user_input = input("\n您:")
    if user_input.lower() == "退出":
        goodbye_message = get_goodbye_message()  # 随机选择退出语
        play_welcome_or_goodbye(goodbye_message, GOODBYE_AUDIO_MAP)
        print(goodbye_message)
        print("正在退出终端...")
        time.sleep(1)  # 等待一秒
        break

    # 调用混元大模型 API 获取回答
    reply = call_hunyuan_api(user_input)
    print(f"ai回答:{reply}")
    # 检查回答的字数
    if len(reply) <= 50:
        engine.say(reply)
        engine.runAndWait()
    else:
        print("回答过长,跳过语音播报。")

以上代码给欢迎语和退出语进行了指定,这样只需要将我们用GPT-Sovits推理的音频修改名字放到对应目录下就可以实现欢迎语和退出语时白月光的音频了,回答的内容还是用的默认音频引擎,后续进行改进,我的想法时服务器搞个GPT-Sovits然后上传模型等待推理,然后将api接口给AI女友,让AI女友将回答内容提交给服务器进行推理,然后将AI女友播放推理的音频,不过有两点需要解决,速度问题和推理效果问题,虽然声音可以了(需要自己训练模型),但是每次的推理音频的效果不好保证,所以可能不满意,所以这些后面再出一起文章来解决(我也不知道什么时候)。

9.请道友入我人皇幡!

其实就是通过GPT-Sovits训练的模型将欢迎语和退出语提前推理好放到目录下面,然后填好混元的id和key就可以正常使用了。具体效果我反正是觉得还行,就是模型有点睿智,后面再试试其他的看看。

如果没有训练好的模型,可自行训练自己的白月光,就像我一样,最终效果如下:(随机欢迎语和退出语)

当然,你也可以使用公开的模型,比如b站up主”白菜工厂1145号员工”提供的模型(含原神,蔚蓝档案,崩坏等),可通过下面链接下载:

https://www.123pan.com/s/UHp9-kqi8H.html

那么这篇文章就到这了,感兴趣的可以再评论区一起讨论一下。

觉得有帮助可以投喂支持下博主哦~感谢!
(bilibili充电:https://space.bilibili.com/1805782637)
作者:法王
转载请表明出处!
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇