315| 0
|
[K10项目分享] 基于行空板K10的大语言对话模型 |
行空板语音对话机器人(Kimi版) 实现效果 通过外接麦克风语音提问 -> 行空板实时转文字 -> 调用Kimi API获取答案 -> 文字+语音合成输出 -> 触摸屏交互界面 一、硬件连接 组件 说明 行空板 ×1 主控设备 USB麦克风 ×1 语音输入(或使用兼容的I2S数字麦克风) 扬声器 ×1 音频输出(可用3.5mm耳机孔或USB声卡) 联网环境 Wi-Fi/USB共享网络 二、软件准备 1. API服务注册 注册 Kimi开放平台 获取API Key 2. 行空板终端安装依赖 bash 复制 pip install requests unihiker SpeechRecognition pydub pygame 三、完整代码 python 复制 from unihiker import GUI import requests import speech_recognition as sr import pygame from pygame import mixer import io import time # 初始化组件 gui = GUI() mixer.init() recognizer = sr.Recognizer() # ===== 配置区 ===== KIMI_API_KEY = "your_kimi_api_key" # 替换实际API Key VOICE_OUTPUT = True # 是否启用语音播报 # Kimi API调用函数 def ask_kimi(question): headers = { "Authorization": f"Bearer {KIMI_API_KEY}", "Content-Type": "application/json" } payload = { "model": "moonshot-v1-8k", "messages": [{"role": "user", "content": question}], "temperature": 0.3 } try: response = requests.post( "https://api.moonshot.cn/v1/chat/completions", headers=headers, json=payload ) return response.json()["choices"][0]["message"]["content"] except Exception as e: return f"API请求失败:{str(e)}" # 语音识别函数 def speech_to_text(): with sr.Microphone() as source: gui.draw_text(x=20, y=280, text="请说话...", color="red") audio = recognizer.listen(source, timeout=5) try: text = recognizer.recognize_google(audio, language='zh-CN') return text except sr.UnknownValueError: return "无法识别语音" except sr.RequestError: return "语音服务不可用" # 语音合成播放 def text_to_speech(text): if not VOICE_OUTPUT: return # 使用edge-tts或本地引擎(此处简化为系统语音) with open("output.wav", "wb") as f: f.write(requests.get(f"http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&text={text}").content) mixer.music.load("output.wav") mixer.music.play() # ===== 界面设计 ===== # 背景框 gui.draw_rect(x=10, y=10, w=220, h=240, color="#f0f0f0") # 对话历史 history_box = gui.draw_text(x=20, y=20, w=200, h=200, text="", color="#333") # 语音按钮 record_btn = gui.draw_button(x=80, y=260, w=80, h=40, text="按住说话") record_btn.config(touch_events={'press': True}) # 交互处理 def on_record(event): if event == "press": question = speech_to_text() history_box.config(text=history_box.cget("text") + f"\nYou: {question}") answer = ask_kimi(question) history_box.config(text=history_box.cget("text") + f"\nKimi: {answer}") text_to_speech(answer) record_btn.bind(event_type="press", handler=on_record) gui.keep_running() 四、使用说明 硬件连接 USB麦克风插入USB接口 扬声器接入3.5mm音频口 配置修改 替换代码中的KIMI_API_KEY 根据网络情况调整语音识别服务(可改用百度/讯飞API) 操作流程 点击屏幕"按住说话"按钮 对着麦克风提问(如:"如何给行空板编程?") 等待语音转文字 -> API响应 -> 答案显示与播报 五、优化方向 多模态交互 python 复制 # 添加表情反馈 emotion_icon = gui.draw_image(x=180, y=20, w=50, h=50, image='neutral.png') 上下文记忆 python 复制 # 在payload中添加历史对话 messages = [ {"role": "system", "content": "你是一个幽默的助手"}, {"role": "user", "content": "之前问题上下文..."}, {"role": "assistant", "content": "历史回答..."} ] 离线唤醒词 python 复制 # 使用snowboy热词检测 from voice_engine import HotwordDetector detector = HotwordDetector(model="kimi.pmdl") 注意事项 网络延迟可能影响响应速度 嘈杂环境需使用定向麦克风 API调用频率需符合平台限制 长时间录音注意内存管理 本方案实现了从 语音采集→AI推理→多模态反馈 的完整链路,可作为智能语音助手的开发原型。建议通过3D打印外壳制作成便携设备,拓展更多物联网控制功能。视频观看网址:https://www.bilibili.com/video/BV1DePueLEup/?vd_source=e2a5eeaba761832e554213a69e3ef3a2 |
© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed