当前位置: 首页 > news >正文

python语音机器人(青云客免费api)

强调:不用登录注册,直接使用就好



青云客智能聊天机器人API

python代码,直接可以运行:
1、安装库:

pip install requests pyttsx3 SpeechRecognition sounddevice numpy scipy

2、完整代码:

import requests
import pyttsx3
import speech_recognition as sr
import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wavfile
import tempfileclass ChatBot:def __init__(self, api_url, key="free", appid="0"):self.api_url = api_urlself.key = keyself.appid = appidself.tts_engine = pyttsx3.init()self.tts_engine.setProperty('rate', 150)  # 设置语速self.tts_engine.setProperty('volume', 1.0)  # 设置音量 (0.0到1.0)def send_message(self, message):params = {"key": self.key,"appid": self.appid,"msg": message}try:response = requests.get(self.api_url, params=params)response.raise_for_status()  # 检查请求是否成功data = response.json()if data.get("result") == 0:return data.get("content")else:return "对不起,我无法处理您的请求。"except requests.RequestException as e:return f"请求失败:{e}"def speak(self, text):self.tts_engine.say(text)self.tts_engine.runAndWait()def recognize_speech():# 设置录音参数sample_rate = 16000  # 采样率duration = 5  # 录音时长,秒print("请开始说话...")try:# 录制音频audio_data = sd.rec(int(sample_rate * duration), samplerate=sample_rate, channels=1, dtype='int16')sd.wait()  # 等待录音完成# 保存临时音频文件with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio:wavfile.write(temp_audio.name, sample_rate, audio_data)temp_audio_path = temp_audio.name# 使用 speech_recognition 进行语音识别recognizer = sr.Recognizer()with sr.AudioFile(temp_audio_path) as source:audio = recognizer.record(source)text = recognizer.recognize_google(audio, language="zh-CN")print(f"你: {text}")return textexcept sr.UnknownValueError:print("无法识别语音,请再试一次。")return Noneexcept sr.RequestError as e:print(f"语音识别服务出错: {e}")return Noneexcept Exception as e:print(f"录音出错: {e}")return Nonedef main():bot = ChatBot(api_url="http://api.qingyunke.com/api.php")print("欢迎使用语音聊天机器人!说“退出”结束对话。")bot.speak("欢迎使用语音聊天机器人,说退出结束对话。")while True:user_input = recognize_speech()if user_input is None:continueif "退出" in user_input:print("机器人: 再见!")bot.speak("再见!")breakresponse = bot.send_message(user_input)print(f"机器人: {response}")bot.speak(response)if __name__ == "__main__":main()

成功的同学,麻烦关注支持一下哦~~

http://www.lryc.cn/news/511147.html

相关文章:

  • 使用ArcGIS/ArcGIS pro绘制六边形/三角形/菱形渔网图
  • 5.系统学习-PyTorch与多层感知机
  • AIGC与虚拟身份及元宇宙的未来:虚拟人物创作与智能交互
  • 基于大模型LLM 应用方案
  • 实用技巧:关于 AD修改原理图库如何同步更新到有原理图 的解决方法
  • 区块链平台安全属性解释
  • 1228作业
  • Machine-learning the skill of mutual fund managers
  • Windows下Python+PyCharm的安装步骤及PyCharm的使用
  • Anaconda+PyTorch(CPU版)安装
  • 第 28 章 - ES 源码篇 - Elasticsearch 启动与插件加载机制解析
  • 机床数据采集网关在某机械制造企业的应用
  • 美团Android开发200道面试题及参考答案(上)
  • MQTT协议的应用场景及特点和常见的概念03
  • 电脑缺失sxs.dll文件要怎么解决?
  • 数据处的存储与处理——添加数组
  • 24-12-28-pytorch深度学习CUDA的GPU加速环境配置步骤
  • YOLO系列正传(五)YOLOv4论文精解(上):从CSPNet、SPP、PANet到CSPDarknet-53
  • 【AIGC-ChatGPT副业提示词指令 - 动图】魔法咖啡馆:一个融合创意与治愈的互动体验设计
  • AIGC在电影与影视制作中的应用:提高创作效率与创意的无限可能
  • 第三百四十六节 JavaFX教程 - JavaFX绑定
  • IDEA+Docker一键部署项目SpringBoot项目
  • vue Promise使用
  • Tomcat调优相关理解
  • uni-app开发-识图小程序-主要功能以及首页实现
  • vue3 ref reactive响应式数据,赋值的问题、解构失去响应式问题
  • 算法常用库函数——C++篇
  • 怎么把多个PDF合并到一起-免费实用PDF编辑处理工具分享
  • RFC协议简要介绍——有关TCP拥塞控制的RFC
  • Speckly:基于Speckle文档的RAG智能问答机器人