基于百度API+Flask实现网页版和图灵机器聊天
开发前准备
调用百度和图灵机器人相关的 参考链接:www.cnblogs.com/changtao/p/10596385.html
下载一个网页录音的js插件
链接:https://pan.baidu.com/s/1-dvlmwLlMzO3crZea35kiQ
提取码:qth0
前端开发:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是玩具</title>
</head>
<body>
<P><audio id="player" controls autoplay></audio></P>
<button onclick="start_reco()">录音</button>
<button onclick="stop_reco()">发送语音</button>
<div id="content"> </div> </body>
<script type="text/javascript" src="/static/Recorder.js"></script>
<script type="text/javascript" src="/static/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
var serv = "服务器ip"; var reco = null;
var audio_context = new AudioContext();//音频内容对象
navigator.getUserMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia); // 做的一个兼容问题 navigator.getUserMedia({audio: true}, create_stream, function (err) {
console.log(err)
}); function create_stream(user_media) {
var stream_input = audio_context.createMediaStreamSource(user_media);
reco = new Recorder(stream_input);
} function start_reco() {
reco.record();
} function stop_reco() {
reco.stop();
reco.exportWAV(function (wav_file) {
console.log(wav_file);
var formdata = new FormData(); // form 表单 {key:value}
formdata.append("reco",wav_file); // form input type="file"
$.ajax({
url: serv + "/upload",
type: 'post',
processData: false,
contentType: false,
data: formdata,
dataType: 'json',
success: function (data) {
console.log(data);
if(data.code == 0){
document.getElementById("player").src="拼接一个提交的地址"+data.filename;
document.getElementById("content").innerText=data.content
} }
})
});
reco.clear(); // 每次执行完,清空reco容器
} </script>
</html>
提示:语音处理方法的地址:https://www.cnblogs.com/changtao/p/10596385.html
后端开发:
from flask import Flask,request,redirect,render_template,jsonify,send_file
from uuid import uuid4
import audio2text app = Flask(__name__) @app.route('/')
def index():
return render_template('web_soon.html') @app.route('/upload',methods=['post'])
def upload():
fi = request.files.get('reco')
fi_name = f"{uuid4()}.m4a"
fi.save(fi_name) ret_sb = audio2text.speech_sb(fi_name) //语音识别的函数
ret_npl = audio2text.my_npl(ret_sb.get('result'),'123') // 自然语言处理
ret_hc_filename = audio2text.speech_hc(uuid4(),ret_npl) // 将自然语言处理的结果合成语音 ret = {
"filename":ret_hc_filename,
"content":ret_npl,
"code":0
}
return jsonify(ret)
@app.route("/get_file/<filename>")
def get_file(filename):
return send_file(filename) if __name__ == '__main__':
app.run(('0.0.0.0'),8600)
基于百度API+Flask实现网页版和图灵机器聊天的更多相关文章
- 百度地图热力图--批量地址转换应用(基于百度api)
需求:把外卖订餐地址做个用户分布热力图 思路分析:第一步去百度地图api开放平台找例子 http://lbsyun.baidu.com/jsdemo.htm#c1_15 首先从百度API的demo例子 ...
- WebRTC实现网页版多人视频聊天室
因为产品中要加入网页中网络会议的功能,这几天都在倒腾 WebRTC,现在分享下工作成果. 话说 WebRTC Real Time Communication 简称 RTC,是谷歌若干年前收购的一项技术 ...
- vue仿微信网页版|vue+web端聊天室|仿微信客户端vue版
一.项目介绍 基于Vue2.5.6+Vuex+vue-cli+vue-router+vue-gemini-scrollbar+swiper+elementUI等技术混合架构开发的仿微信web端聊天室— ...
- 基于百度语音识别API的Python语音识别小程序
一.功能概述 实现语音为文字,可以扩展到多种场景进行工作,这里只实现其基本的语言接收及转换功能. 在语言录入时,根据语言内容的多少与停顿时间,自动截取音频进行转换. 工作示例: 二.软件环境 操作系统 ...
- 基于.Net平台C#的微信网页版API
git上有很多类似的项目,但大多都是python和js的,为了便于.Net windows平台的使用,我重构了一个.Net版本的,已整理开源 https://github.com/leestar54/ ...
- 基于百度翻译API开发属于自己的翻译工具
你是否每天使用着网页翻译工具?你是否遇到过这种情况,上网过程中遇到一个很长的单词但是又不能复制,要开两个浏览器,一个打开百度翻译,照着另一个网页输入单词?你安装了各种翻译软件后,又删除,只因忍受不了那 ...
- 基于html5 canvas和js实现的水果忍者网页版
今天爱编程小编给大家分享一款基于html5 canvas和js实现的水果忍者网页版. <水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版 ...
- 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能
权声明:本文为博主原创文章,未经博主允许不得转载. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内.以及两点间的测距功能. 绘制多边形(蓝色) ...
- 微信号网页版api
Django Wechat Api djangowechatapi是基于wxpy和django制作的web应用 安装 使用pip pip install djangowechatapi 源码安装 gi ...
随机推荐
- dispatch_barrier_async--屏障是一个同步点
Discussion Calls to this function always return immediately after the block has been submitted and n ...
- UVALive - 3523 - Knights of the Round Table
Problem UVALive - 3523 - Knights of the Round Table Time Limit: 4500 mSec Problem Description Input ...
- android开发学习 ------- 关于getSupportFragmentManager()不可用的问题
在Android开发中,少不了Fragment的运用. 目前在实际运用中,有v-4包下支持的Fragment以及app包下的Fragment,这两个包下的FragmentManager获取方式有点区别 ...
- [LeetCode] 15. 三数之和
题目链接:https://leetcode-cn.com/problems/3sum/ 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a ...
- 19 python初学(os 模块,sys 模块,hashlib 模块)
os 模块: # _author: lily # _date: 2019/1/13 import os print(os.getcwd()) # 得到当前的工作目录 # print(os.chdir( ...
- 使用BigQuery分析GitHub上的C#代码
一年多以前,Google 在GitHub中提供了BigQuery用于查询的GitHub上的开源代码(open source code on GitHub available for querying) ...
- Linux和Uboot下eMMC boot分区读写
关键词:eMMC boot.PARTITION_CONFIG.force_ro等. 1. eMMC的分区 大部分eMMC都有类似如下的分区,其中BOOT.RPMB和UDA一般是默认存在的,gpp分区需 ...
- iOS开发基础-九宫格坐标(2)之模型
在iOS开发基础-九宫格(1)中,属性变量 apps 是从plist文件中加载数据的,在 viewDidLoad 方法中的第20行.26行中,直接通过字典的键名来获取相应的信息,使得 ViewCont ...
- HTML60秒倒计时
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C语言之概述
//添加对函数的说明(规范) #include<stdio.h> /*A simple C progress*/ int main(void) { int num; /*Define an ...