fix: 基本逻辑
This commit is contained in:
parent
dc7b8bf343
commit
5be54dd239
|
|
@ -146,6 +146,10 @@
|
|||
<!-- 讯飞语音识别 -->
|
||||
<script>
|
||||
window.onload = function () {
|
||||
reset();
|
||||
};
|
||||
|
||||
function reset() {
|
||||
var videoElement = document.getElementById('myVideo');
|
||||
var startTime = 5; // 开始时间(以秒为单位)
|
||||
var endTime = 10; // 结束时间(以秒为单位)
|
||||
|
|
@ -154,7 +158,9 @@
|
|||
// 指定段落
|
||||
function playVideoSegment(startTime, endTime) {
|
||||
videoElement.currentTime = startTime;
|
||||
videoElement.play();
|
||||
setTimeout(() => {
|
||||
videoElement.play();
|
||||
}, 50);
|
||||
|
||||
// 添加timeupdate事件监听器
|
||||
timeUpdateListener = function() {
|
||||
|
|
@ -254,218 +260,224 @@
|
|||
/*
|
||||
* 实例化迅飞语音听写(流式版)WebAPI
|
||||
*/
|
||||
|
||||
let seconds = 10;
|
||||
var seconds = 10;
|
||||
var gaptime = null;
|
||||
const voice = new Voice({
|
||||
// 服务接口认证信息
|
||||
appId: '5f4ffdeb',
|
||||
apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5',
|
||||
apiKey: '0b17a761b6b7174b789f639119d7e29a',
|
||||
onWillStatusChange: function (oldStatus, newStatus) {},
|
||||
|
||||
onWillStatusChange: function (oldStatus, newStatus) {
|
||||
console.log(oldStatus, newStatus, '交互逻辑')
|
||||
},
|
||||
onTextChange: function (text) {
|
||||
// 监听识别结果的变化
|
||||
voiceTxt.innerText = text;
|
||||
marquee("marquee", "voice-txt");
|
||||
|
||||
let timer = setInterval(() => {
|
||||
seconds--;
|
||||
// 如果已有定时器在运行,则清除它
|
||||
if (gaptime) {
|
||||
clearInterval(gaptime);
|
||||
};
|
||||
// 重置倒计时
|
||||
seconds = 10;
|
||||
|
||||
if (seconds <= 0) {
|
||||
clearInterval(timer);
|
||||
console.log(text)
|
||||
|
||||
gaptime = setInterval(() => {
|
||||
console.log(seconds, '倒计时')
|
||||
seconds--;
|
||||
if (voiceTxt.innerText) {
|
||||
console.log('有 text 参数'); // 如果有 text 参数,打印 1
|
||||
clearInterval(gaptime);
|
||||
gaptime = null;
|
||||
|
||||
// 3秒钟内没有说话,就自动关闭
|
||||
if (text) {
|
||||
console.log(1); // 如果有 text 参数,打印 1
|
||||
} else {
|
||||
console.log("没有传入 text 参数"); // 如果没有 text 参数,执行其他逻辑
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
statusTxt.value = '正在听…';
|
||||
if (!isCallbackExecuted) {
|
||||
clearTimeout(times);
|
||||
times = setTimeout(() => {
|
||||
this.stop();
|
||||
|
||||
const params = { msg: text };
|
||||
|
||||
|
||||
// 3秒钟内没有说话,就自动关闭
|
||||
if (text) {
|
||||
statusTxt.value = '正在听…';
|
||||
if (!isCallbackExecuted) {
|
||||
clearTimeout(times);
|
||||
times = setTimeout(() => {
|
||||
this.stop();
|
||||
statusTxt.value = '正在说话';
|
||||
|
||||
const params = { msg: text };
|
||||
|
||||
// 判断关键字是否存在
|
||||
const result = parseHealthQuery(params.msg);
|
||||
isCallbackExecuted = true;
|
||||
|
||||
if (result) {
|
||||
DetailDay(result.dataKey, result.date).then(res => {
|
||||
let TargetData = res.data.data;
|
||||
|
||||
if (result.dataKey == 'ECGData') {
|
||||
TargetData.map(item => {
|
||||
delete item.data_msg.wavefrom;
|
||||
delete item.data_msg.list;
|
||||
})
|
||||
}
|
||||
if (result.dataKey == 'bloodLiquid') {
|
||||
TargetData.map(item => {
|
||||
item.data_msg.cholesterol = item.data_msg.cholesterol/100;
|
||||
item.data_msg.highDensity = item.data_msg.highDensity/100;
|
||||
item.data_msg.triacylglycerol = item.data_msg.triacylglycerol/100;
|
||||
item.data_msg.uricAcidVal = item.data_msg.uricAcidVal/10000;
|
||||
item.data_msg.lowDensity = item.data_msg.lowDensity/100;
|
||||
})
|
||||
}
|
||||
|
||||
Question = `请模仿全科医生的口吻与我: ${user}对话,我最近测量的${result.dataKey}数据为${JSON.stringify(TargetData)}, #提示:data_msg为值,hour_minute为检测时间。#提示:“[]”表示数据为空,请在小程序上传数据。#限制:回复不要带英文,要都转化成汉语。#限制:回复内容控制在150字。#限制:忽略“压力指数、疲劳指数、心肌炎风险、冠心病风险和动脉硬化”等数据。`
|
||||
|
||||
console.log(Question, '问题=========================问题');
|
||||
|
||||
|
||||
/*
|
||||
* 调用接口 传递关键信息 文字转语音
|
||||
*/
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
/*
|
||||
* 关键字转换
|
||||
*/
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": Question},
|
||||
{"role": "user", "content": `请问我${params.msg}正常吗`}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
// 判断关键字是否存在
|
||||
const result = parseHealthQuery(params.msg);
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
})
|
||||
} else {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
if (result) {
|
||||
DetailDay(result.dataKey, result.date).then(res => {
|
||||
let TargetData = res.data.data;
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
if (result.dataKey == 'ECGData') {
|
||||
TargetData.map(item => {
|
||||
delete item.data_msg.wavefrom;
|
||||
delete item.data_msg.list;
|
||||
})
|
||||
}
|
||||
if (result.dataKey == 'bloodLiquid') {
|
||||
TargetData.map(item => {
|
||||
item.data_msg.cholesterol = item.data_msg.cholesterol/100;
|
||||
item.data_msg.highDensity = item.data_msg.highDensity/100;
|
||||
item.data_msg.triacylglycerol = item.data_msg.triacylglycerol/100;
|
||||
item.data_msg.uricAcidVal = item.data_msg.uricAcidVal/10000;
|
||||
item.data_msg.lowDensity = item.data_msg.lowDensity/100;
|
||||
})
|
||||
}
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
Question = `请模仿全科医生的口吻与我: ${user}对话,我最近测量的${result.dataKey}数据为${JSON.stringify(TargetData)}, #提示:data_msg为值,hour_minute为检测时间。#提示:“[]”表示数据为空,请在小程序上传数据。#限制:回复不要带英文,要都转化成汉语。#限制:回复内容控制在150字。#限制:忽略“压力指数、疲劳指数、心肌炎风险、冠心病风险和动脉硬化”等数据。`
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
console.log(Question, '问题=========================问题');
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
|
||||
/*
|
||||
* 调用接口 传递关键信息 文字转语音
|
||||
*/
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
/*
|
||||
* 关键字转换
|
||||
*/
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": Question},
|
||||
{"role": "user", "content": `请问我${params.msg}正常吗`}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
})
|
||||
} else {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": `请模仿全科医生的口吻与我对话`},
|
||||
{"role": "user", "content": params.msg}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": `请模仿全科医生的口吻与我对话`},
|
||||
{"role": "user", "content": params.msg}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
}, 3000);
|
||||
}
|
||||
}, 3000);
|
||||
}
|
||||
} else if (seconds == 0){
|
||||
reset();
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -506,15 +518,24 @@
|
|||
|
||||
function startShibie() {
|
||||
/**开始识别**/
|
||||
|
||||
voiceTxt.innerText = '';
|
||||
stopRec.textContent = '暂停';
|
||||
voice.start();
|
||||
|
||||
isCallbackExecuted = false;
|
||||
|
||||
// 先静音即可处理解决(提前做交互)
|
||||
audioElement.muted = false;
|
||||
audioElement.pause();
|
||||
audioElement.currentTime = 0;
|
||||
|
||||
// 检查是否可以播放,避免中断
|
||||
if (!audioElement.paused) {
|
||||
audioElement.currentTime = 0;
|
||||
audioElement.pause();
|
||||
};
|
||||
|
||||
seconds = 10;
|
||||
|
||||
voice.start();
|
||||
|
||||
startRec.style.display = 'none';
|
||||
endRec.style.display = 'none';
|
||||
|
|
@ -523,20 +544,28 @@
|
|||
}
|
||||
|
||||
function closeShibie() {
|
||||
reset();
|
||||
|
||||
/**关闭识别**/
|
||||
voiceTxt.innerText = '';
|
||||
statusTxt.value = '';
|
||||
stopRec.textContent = '暂停';
|
||||
|
||||
voice.stop();
|
||||
|
||||
// 音频
|
||||
audioElement.pause();
|
||||
audioElement.currentTime = 0;
|
||||
// 检查是否可以播放,避免中断
|
||||
if (!audioElement.paused) {
|
||||
audioElement.currentTime = 0;
|
||||
audioElement.pause();
|
||||
};
|
||||
|
||||
// 视频
|
||||
replayVideoSegment(0, 60);
|
||||
|
||||
isCallbackExecuted = false;
|
||||
seconds = 10;
|
||||
|
||||
voice.stop();
|
||||
console.log('关闭识别');
|
||||
|
||||
startRec.style.display = 'block';
|
||||
endRec.style.display = 'none';
|
||||
|
|
@ -548,7 +577,7 @@
|
|||
function showModal() {
|
||||
modal.style.display = 'block';
|
||||
dialog.style.display = 'block';
|
||||
}
|
||||
}
|
||||
|
||||
// 隐藏弹窗和遮罩
|
||||
function hideModal() {
|
||||
|
|
@ -647,9 +676,11 @@
|
|||
stopRec.style.display = 'none';
|
||||
statusTxt.value = '';
|
||||
voiceTxt.innerText = '';
|
||||
voice.stop();
|
||||
replayVideoSegment(0, 60);
|
||||
hideModal()
|
||||
|
||||
// 播放结束重新开始监听
|
||||
startShibie();
|
||||
});
|
||||
|
||||
Subtitles = "";
|
||||
|
|
@ -748,7 +779,7 @@
|
|||
// 如果没有找到匹配的数据项,则返回null或错误信息(这里选择返回null)
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -146,6 +146,10 @@
|
|||
<!-- 讯飞语音识别 -->
|
||||
<script>
|
||||
window.onload = function () {
|
||||
reset();
|
||||
};
|
||||
|
||||
function reset() {
|
||||
var videoElement = document.getElementById('myVideo');
|
||||
var startTime = 5; // 开始时间(以秒为单位)
|
||||
var endTime = 10; // 结束时间(以秒为单位)
|
||||
|
|
@ -154,7 +158,9 @@
|
|||
// 指定段落
|
||||
function playVideoSegment(startTime, endTime) {
|
||||
videoElement.currentTime = startTime;
|
||||
videoElement.play();
|
||||
setTimeout(() => {
|
||||
videoElement.play();
|
||||
}, 50);
|
||||
|
||||
// 添加timeupdate事件监听器
|
||||
timeUpdateListener = function() {
|
||||
|
|
@ -254,201 +260,229 @@
|
|||
/*
|
||||
* 实例化迅飞语音听写(流式版)WebAPI
|
||||
*/
|
||||
var seconds = 10;
|
||||
var gaptime = null;
|
||||
const voice = new Voice({
|
||||
// 服务接口认证信息
|
||||
appId: '5f4ffdeb',
|
||||
apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5',
|
||||
apiKey: '0b17a761b6b7174b789f639119d7e29a',
|
||||
onWillStatusChange: function (oldStatus, newStatus) {},
|
||||
|
||||
onWillStatusChange: function (oldStatus, newStatus) {
|
||||
console.log(oldStatus, newStatus, '交互逻辑')
|
||||
},
|
||||
onTextChange: function (text) {
|
||||
// 监听识别结果的变化
|
||||
voiceTxt.innerText = text;
|
||||
marquee("marquee", "voice-txt");
|
||||
|
||||
// 3秒钟内没有说话,就自动关闭
|
||||
if (text) {
|
||||
statusTxt.value = '正在听…';
|
||||
if (!isCallbackExecuted) {
|
||||
clearTimeout(times);
|
||||
times = setTimeout(() => {
|
||||
this.stop();
|
||||
statusTxt.value = '正在说话';
|
||||
// 如果已有定时器在运行,则清除它
|
||||
if (gaptime) {
|
||||
clearInterval(gaptime);
|
||||
};
|
||||
// 重置倒计时
|
||||
seconds = 10;
|
||||
|
||||
const params = { msg: text };
|
||||
console.log(text)
|
||||
|
||||
// 判断关键字是否存在
|
||||
const result = parseHealthQuery(params.msg);
|
||||
isCallbackExecuted = true;
|
||||
gaptime = setInterval(() => {
|
||||
console.log(seconds, '倒计时')
|
||||
seconds--;
|
||||
if (voiceTxt.innerText) {
|
||||
console.log('有 text 参数'); // 如果有 text 参数,打印 1
|
||||
clearInterval(gaptime);
|
||||
gaptime = null;
|
||||
|
||||
if (result) {
|
||||
DetailDay(result.dataKey, result.date).then(res => {
|
||||
let TargetData = res.data.data;
|
||||
// 3秒钟内没有说话,就自动关闭
|
||||
if (text) {
|
||||
statusTxt.value = '正在听…';
|
||||
if (!isCallbackExecuted) {
|
||||
clearTimeout(times);
|
||||
times = setTimeout(() => {
|
||||
this.stop();
|
||||
statusTxt.value = '正在说话';
|
||||
|
||||
if (result.dataKey == 'ECGData') {
|
||||
TargetData.map(item => {
|
||||
delete item.data_msg.wavefrom;
|
||||
delete item.data_msg.list;
|
||||
})
|
||||
}
|
||||
if (result.dataKey == 'bloodLiquid') {
|
||||
TargetData.map(item => {
|
||||
item.data_msg.cholesterol = item.data_msg.cholesterol/100;
|
||||
item.data_msg.highDensity = item.data_msg.highDensity/100;
|
||||
item.data_msg.triacylglycerol = item.data_msg.triacylglycerol/100;
|
||||
item.data_msg.uricAcidVal = item.data_msg.uricAcidVal/10000;
|
||||
item.data_msg.lowDensity = item.data_msg.lowDensity/100;
|
||||
})
|
||||
}
|
||||
const params = { msg: text };
|
||||
|
||||
Question = `请模仿全科医生的口吻与我: ${user}对话,我最近测量的${result.dataKey}数据为${JSON.stringify(TargetData)}, #提示:data_msg为值,hour_minute为检测时间。#提示:“[]”表示数据为空,请在小程序上传数据。#限制:回复不要带英文,要都转化成汉语。#限制:回复内容控制在150字。#限制:忽略“压力指数、疲劳指数、心肌炎风险、冠心病风险和动脉硬化”等数据。`
|
||||
|
||||
console.log(Question, '问题=========================问题');
|
||||
|
||||
|
||||
/*
|
||||
* 调用接口 传递关键信息 文字转语音
|
||||
*/
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
/*
|
||||
* 关键字转换
|
||||
*/
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": Question},
|
||||
{"role": "user", "content": `请问我${params.msg}正常吗`}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
// 判断关键字是否存在
|
||||
const result = parseHealthQuery(params.msg);
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
})
|
||||
} else {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
if (result) {
|
||||
DetailDay(result.dataKey, result.date).then(res => {
|
||||
let TargetData = res.data.data;
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
if (result.dataKey == 'ECGData') {
|
||||
TargetData.map(item => {
|
||||
delete item.data_msg.wavefrom;
|
||||
delete item.data_msg.list;
|
||||
})
|
||||
}
|
||||
if (result.dataKey == 'bloodLiquid') {
|
||||
TargetData.map(item => {
|
||||
item.data_msg.cholesterol = item.data_msg.cholesterol/100;
|
||||
item.data_msg.highDensity = item.data_msg.highDensity/100;
|
||||
item.data_msg.triacylglycerol = item.data_msg.triacylglycerol/100;
|
||||
item.data_msg.uricAcidVal = item.data_msg.uricAcidVal/10000;
|
||||
item.data_msg.lowDensity = item.data_msg.lowDensity/100;
|
||||
})
|
||||
}
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
Question = `请模仿全科医生的口吻与我: ${user}对话,我最近测量的${result.dataKey}数据为${JSON.stringify(TargetData)}, #提示:data_msg为值,hour_minute为检测时间。#提示:“[]”表示数据为空,请在小程序上传数据。#限制:回复不要带英文,要都转化成汉语。#限制:回复内容控制在150字。#限制:忽略“压力指数、疲劳指数、心肌炎风险、冠心病风险和动脉硬化”等数据。`
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
console.log(Question, '问题=========================问题');
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
|
||||
/*
|
||||
* 调用接口 传递关键信息 文字转语音
|
||||
*/
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
/*
|
||||
* 关键字转换
|
||||
*/
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": Question},
|
||||
{"role": "user", "content": `请问我${params.msg}正常吗`}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
})
|
||||
} else {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": `请模仿全科医生的口吻与我对话`},
|
||||
{"role": "user", "content": params.msg}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": `请模仿全科医生的口吻与我对话`},
|
||||
{"role": "user", "content": params.msg}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
}, 3000);
|
||||
}
|
||||
}, 3000);
|
||||
}
|
||||
} else if (seconds == 0){
|
||||
clearInterval(gaptime);
|
||||
closeShibie();
|
||||
reset();
|
||||
gaptime = null;
|
||||
seconds = 10;
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -463,7 +497,7 @@
|
|||
closeShibie();
|
||||
});
|
||||
|
||||
// 关闭识别
|
||||
// 暂停识别
|
||||
stopRec.addEventListener("click", function() {
|
||||
stopPlay();
|
||||
});
|
||||
|
|
@ -475,25 +509,38 @@
|
|||
videoElement.play();
|
||||
isPlaying = true;
|
||||
stopRec.textContent = '暂停';
|
||||
statusTxt.value = '正在播放';
|
||||
endRec.style.display = 'block';
|
||||
} else {
|
||||
audioElement.pause();
|
||||
videoElement.pause();
|
||||
isPlaying = false;
|
||||
stopRec.textContent = '继续播放';
|
||||
statusTxt.value = '已暂停';
|
||||
endRec.style.display = 'none';
|
||||
}
|
||||
};
|
||||
|
||||
function startShibie() {
|
||||
/**开始识别**/
|
||||
|
||||
voiceTxt.innerText = '';
|
||||
stopRec.textContent = '暂停';
|
||||
voice.start();
|
||||
|
||||
isCallbackExecuted = false;
|
||||
|
||||
// 先静音即可处理解决(提前做交互)
|
||||
audioElement.muted = false;
|
||||
audioElement.pause();
|
||||
audioElement.currentTime = 0;
|
||||
|
||||
// 检查是否可以播放,避免中断
|
||||
if (!audioElement.paused) {
|
||||
audioElement.currentTime = 0;
|
||||
audioElement.pause();
|
||||
};
|
||||
|
||||
seconds = 10;
|
||||
|
||||
voice.start();
|
||||
|
||||
startRec.style.display = 'none';
|
||||
endRec.style.display = 'none';
|
||||
|
|
@ -507,15 +554,21 @@
|
|||
statusTxt.value = '';
|
||||
stopRec.textContent = '暂停';
|
||||
|
||||
voice.stop();
|
||||
|
||||
// 音频
|
||||
audioElement.pause();
|
||||
audioElement.currentTime = 0;
|
||||
// 检查是否可以播放,避免中断
|
||||
if (!audioElement.paused) {
|
||||
audioElement.currentTime = 0;
|
||||
audioElement.pause();
|
||||
};
|
||||
|
||||
// 视频
|
||||
replayVideoSegment(0, 60);
|
||||
|
||||
isCallbackExecuted = false;
|
||||
seconds = 10;
|
||||
|
||||
voice.stop();
|
||||
console.log('关闭识别');
|
||||
|
||||
startRec.style.display = 'block';
|
||||
endRec.style.display = 'none';
|
||||
|
|
@ -527,7 +580,7 @@
|
|||
function showModal() {
|
||||
modal.style.display = 'block';
|
||||
dialog.style.display = 'block';
|
||||
}
|
||||
}
|
||||
|
||||
// 隐藏弹窗和遮罩
|
||||
function hideModal() {
|
||||
|
|
@ -602,6 +655,8 @@
|
|||
const blob = new Blob([content_bytes], { type: 'audio/mp3' });
|
||||
const blobUrl = URL.createObjectURL(blob);
|
||||
|
||||
statusTxt.value = '正在说话';
|
||||
|
||||
// 字幕文字
|
||||
voiceTxt.innerText = str;
|
||||
marquee("marquee", "voice-txt");
|
||||
|
|
@ -621,11 +676,14 @@
|
|||
// 停止倒计时
|
||||
startRec.style.display = 'block';
|
||||
endRec.style.display = 'none';
|
||||
stopRec.style.display = 'none';
|
||||
statusTxt.value = '';
|
||||
voiceTxt.innerText = '';
|
||||
voice.stop();
|
||||
replayVideoSegment(0, 60);
|
||||
hideModal()
|
||||
|
||||
// 播放结束重新开始监听
|
||||
startShibie();
|
||||
});
|
||||
|
||||
Subtitles = "";
|
||||
|
|
@ -724,7 +782,7 @@
|
|||
// 如果没有找到匹配的数据项,则返回null或错误信息(这里选择返回null)
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -146,6 +146,10 @@
|
|||
<!-- 讯飞语音识别 -->
|
||||
<script>
|
||||
window.onload = function () {
|
||||
reset();
|
||||
};
|
||||
|
||||
function reset() {
|
||||
var videoElement = document.getElementById('myVideo');
|
||||
var startTime = 5; // 开始时间(以秒为单位)
|
||||
var endTime = 10; // 结束时间(以秒为单位)
|
||||
|
|
@ -154,7 +158,9 @@
|
|||
// 指定段落
|
||||
function playVideoSegment(startTime, endTime) {
|
||||
videoElement.currentTime = startTime;
|
||||
videoElement.play();
|
||||
setTimeout(() => {
|
||||
videoElement.play();
|
||||
}, 50);
|
||||
|
||||
// 添加timeupdate事件监听器
|
||||
timeUpdateListener = function() {
|
||||
|
|
@ -254,201 +260,229 @@
|
|||
/*
|
||||
* 实例化迅飞语音听写(流式版)WebAPI
|
||||
*/
|
||||
var seconds = 10;
|
||||
var gaptime = null;
|
||||
const voice = new Voice({
|
||||
// 服务接口认证信息
|
||||
appId: '5f4ffdeb',
|
||||
apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5',
|
||||
apiKey: '0b17a761b6b7174b789f639119d7e29a',
|
||||
onWillStatusChange: function (oldStatus, newStatus) {},
|
||||
|
||||
onWillStatusChange: function (oldStatus, newStatus) {
|
||||
console.log(oldStatus, newStatus, '交互逻辑')
|
||||
},
|
||||
onTextChange: function (text) {
|
||||
// 监听识别结果的变化
|
||||
voiceTxt.innerText = text;
|
||||
marquee("marquee", "voice-txt");
|
||||
|
||||
// 3秒钟内没有说话,就自动关闭
|
||||
if (text) {
|
||||
statusTxt.value = '正在听…';
|
||||
if (!isCallbackExecuted) {
|
||||
clearTimeout(times);
|
||||
times = setTimeout(() => {
|
||||
this.stop();
|
||||
statusTxt.value = '正在说话';
|
||||
// 如果已有定时器在运行,则清除它
|
||||
if (gaptime) {
|
||||
clearInterval(gaptime);
|
||||
};
|
||||
// 重置倒计时
|
||||
seconds = 10;
|
||||
|
||||
const params = { msg: text };
|
||||
console.log(text)
|
||||
|
||||
// 判断关键字是否存在
|
||||
const result = parseHealthQuery(params.msg);
|
||||
isCallbackExecuted = true;
|
||||
gaptime = setInterval(() => {
|
||||
console.log(seconds, '倒计时')
|
||||
seconds--;
|
||||
if (voiceTxt.innerText) {
|
||||
console.log('有 text 参数'); // 如果有 text 参数,打印 1
|
||||
clearInterval(gaptime);
|
||||
gaptime = null;
|
||||
|
||||
if (result) {
|
||||
DetailDay(result.dataKey, result.date).then(res => {
|
||||
let TargetData = res.data.data;
|
||||
// 3秒钟内没有说话,就自动关闭
|
||||
if (text) {
|
||||
statusTxt.value = '正在听…';
|
||||
if (!isCallbackExecuted) {
|
||||
clearTimeout(times);
|
||||
times = setTimeout(() => {
|
||||
this.stop();
|
||||
statusTxt.value = '正在说话';
|
||||
|
||||
if (result.dataKey == 'ECGData') {
|
||||
TargetData.map(item => {
|
||||
delete item.data_msg.wavefrom;
|
||||
delete item.data_msg.list;
|
||||
})
|
||||
}
|
||||
if (result.dataKey == 'bloodLiquid') {
|
||||
TargetData.map(item => {
|
||||
item.data_msg.cholesterol = item.data_msg.cholesterol/100;
|
||||
item.data_msg.highDensity = item.data_msg.highDensity/100;
|
||||
item.data_msg.triacylglycerol = item.data_msg.triacylglycerol/100;
|
||||
item.data_msg.uricAcidVal = item.data_msg.uricAcidVal/10000;
|
||||
item.data_msg.lowDensity = item.data_msg.lowDensity/100;
|
||||
})
|
||||
}
|
||||
const params = { msg: text };
|
||||
|
||||
Question = `请模仿全科医生的口吻与我: ${user}对话,我最近测量的${result.dataKey}数据为${JSON.stringify(TargetData)}, #提示:data_msg为值,hour_minute为检测时间。#提示:“[]”表示数据为空,请在小程序上传数据。#限制:回复不要带英文,要都转化成汉语。#限制:回复内容控制在150字。#限制:忽略“压力指数、疲劳指数、心肌炎风险、冠心病风险和动脉硬化”等数据。`
|
||||
|
||||
console.log(Question, '问题=========================问题');
|
||||
|
||||
|
||||
/*
|
||||
* 调用接口 传递关键信息 文字转语音
|
||||
*/
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
/*
|
||||
* 关键字转换
|
||||
*/
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": Question},
|
||||
{"role": "user", "content": `请问我${params.msg}正常吗`}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
// 判断关键字是否存在
|
||||
const result = parseHealthQuery(params.msg);
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
})
|
||||
} else {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
if (result) {
|
||||
DetailDay(result.dataKey, result.date).then(res => {
|
||||
let TargetData = res.data.data;
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
if (result.dataKey == 'ECGData') {
|
||||
TargetData.map(item => {
|
||||
delete item.data_msg.wavefrom;
|
||||
delete item.data_msg.list;
|
||||
})
|
||||
}
|
||||
if (result.dataKey == 'bloodLiquid') {
|
||||
TargetData.map(item => {
|
||||
item.data_msg.cholesterol = item.data_msg.cholesterol/100;
|
||||
item.data_msg.highDensity = item.data_msg.highDensity/100;
|
||||
item.data_msg.triacylglycerol = item.data_msg.triacylglycerol/100;
|
||||
item.data_msg.uricAcidVal = item.data_msg.uricAcidVal/10000;
|
||||
item.data_msg.lowDensity = item.data_msg.lowDensity/100;
|
||||
})
|
||||
}
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
Question = `请模仿全科医生的口吻与我: ${user}对话,我最近测量的${result.dataKey}数据为${JSON.stringify(TargetData)}, #提示:data_msg为值,hour_minute为检测时间。#提示:“[]”表示数据为空,请在小程序上传数据。#限制:回复不要带英文,要都转化成汉语。#限制:回复内容控制在150字。#限制:忽略“压力指数、疲劳指数、心肌炎风险、冠心病风险和动脉硬化”等数据。`
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
console.log(Question, '问题=========================问题');
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
|
||||
/*
|
||||
* 调用接口 传递关键信息 文字转语音
|
||||
*/
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
/*
|
||||
* 关键字转换
|
||||
*/
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": Question},
|
||||
{"role": "user", "content": `请问我${params.msg}正常吗`}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
})
|
||||
} else {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": `请模仿全科医生的口吻与我对话`},
|
||||
{"role": "user", "content": params.msg}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": `请模仿全科医生的口吻与我对话`},
|
||||
{"role": "user", "content": params.msg}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
}, 3000);
|
||||
}
|
||||
}, 3000);
|
||||
}
|
||||
} else if (seconds == 0){
|
||||
clearInterval(gaptime);
|
||||
closeShibie();
|
||||
reset();
|
||||
gaptime = null;
|
||||
seconds = 10;
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -463,7 +497,7 @@
|
|||
closeShibie();
|
||||
});
|
||||
|
||||
// 关闭识别
|
||||
// 暂停识别
|
||||
stopRec.addEventListener("click", function() {
|
||||
stopPlay();
|
||||
});
|
||||
|
|
@ -475,25 +509,38 @@
|
|||
videoElement.play();
|
||||
isPlaying = true;
|
||||
stopRec.textContent = '暂停';
|
||||
statusTxt.value = '正在播放';
|
||||
endRec.style.display = 'block';
|
||||
} else {
|
||||
audioElement.pause();
|
||||
videoElement.pause();
|
||||
isPlaying = false;
|
||||
stopRec.textContent = '继续播放';
|
||||
statusTxt.value = '已暂停';
|
||||
endRec.style.display = 'none';
|
||||
}
|
||||
};
|
||||
|
||||
function startShibie() {
|
||||
/**开始识别**/
|
||||
|
||||
voiceTxt.innerText = '';
|
||||
stopRec.textContent = '暂停';
|
||||
voice.start();
|
||||
|
||||
isCallbackExecuted = false;
|
||||
|
||||
// 先静音即可处理解决(提前做交互)
|
||||
audioElement.muted = false;
|
||||
audioElement.pause();
|
||||
audioElement.currentTime = 0;
|
||||
|
||||
// 检查是否可以播放,避免中断
|
||||
if (!audioElement.paused) {
|
||||
audioElement.currentTime = 0;
|
||||
audioElement.pause();
|
||||
};
|
||||
|
||||
seconds = 10;
|
||||
|
||||
voice.start();
|
||||
|
||||
startRec.style.display = 'none';
|
||||
endRec.style.display = 'none';
|
||||
|
|
@ -507,15 +554,21 @@
|
|||
statusTxt.value = '';
|
||||
stopRec.textContent = '暂停';
|
||||
|
||||
voice.stop();
|
||||
|
||||
// 音频
|
||||
audioElement.pause();
|
||||
audioElement.currentTime = 0;
|
||||
// 检查是否可以播放,避免中断
|
||||
if (!audioElement.paused) {
|
||||
audioElement.currentTime = 0;
|
||||
audioElement.pause();
|
||||
};
|
||||
|
||||
// 视频
|
||||
replayVideoSegment(0, 60);
|
||||
|
||||
isCallbackExecuted = false;
|
||||
seconds = 10;
|
||||
|
||||
voice.stop();
|
||||
console.log('关闭识别');
|
||||
|
||||
startRec.style.display = 'block';
|
||||
endRec.style.display = 'none';
|
||||
|
|
@ -527,7 +580,7 @@
|
|||
function showModal() {
|
||||
modal.style.display = 'block';
|
||||
dialog.style.display = 'block';
|
||||
}
|
||||
}
|
||||
|
||||
// 隐藏弹窗和遮罩
|
||||
function hideModal() {
|
||||
|
|
@ -602,6 +655,8 @@
|
|||
const blob = new Blob([content_bytes], { type: 'audio/mp3' });
|
||||
const blobUrl = URL.createObjectURL(blob);
|
||||
|
||||
statusTxt.value = '正在说话';
|
||||
|
||||
// 字幕文字
|
||||
voiceTxt.innerText = str;
|
||||
marquee("marquee", "voice-txt");
|
||||
|
|
@ -621,11 +676,14 @@
|
|||
// 停止倒计时
|
||||
startRec.style.display = 'block';
|
||||
endRec.style.display = 'none';
|
||||
stopRec.style.display = 'none';
|
||||
statusTxt.value = '';
|
||||
voiceTxt.innerText = '';
|
||||
voice.stop();
|
||||
replayVideoSegment(0, 60);
|
||||
hideModal()
|
||||
|
||||
// 播放结束重新开始监听
|
||||
startShibie();
|
||||
});
|
||||
|
||||
Subtitles = "";
|
||||
|
|
@ -724,7 +782,7 @@
|
|||
// 如果没有找到匹配的数据项,则返回null或错误信息(这里选择返回null)
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -146,6 +146,10 @@
|
|||
<!-- 讯飞语音识别 -->
|
||||
<script>
|
||||
window.onload = function () {
|
||||
reset();
|
||||
};
|
||||
|
||||
function reset() {
|
||||
var videoElement = document.getElementById('myVideo');
|
||||
var startTime = 5; // 开始时间(以秒为单位)
|
||||
var endTime = 10; // 结束时间(以秒为单位)
|
||||
|
|
@ -154,7 +158,9 @@
|
|||
// 指定段落
|
||||
function playVideoSegment(startTime, endTime) {
|
||||
videoElement.currentTime = startTime;
|
||||
videoElement.play();
|
||||
setTimeout(() => {
|
||||
videoElement.play();
|
||||
}, 50);
|
||||
|
||||
// 添加timeupdate事件监听器
|
||||
timeUpdateListener = function() {
|
||||
|
|
@ -254,218 +260,224 @@
|
|||
/*
|
||||
* 实例化迅飞语音听写(流式版)WebAPI
|
||||
*/
|
||||
|
||||
let seconds = 10;
|
||||
var seconds = 10;
|
||||
var gaptime = null;
|
||||
const voice = new Voice({
|
||||
// 服务接口认证信息
|
||||
appId: '5f4ffdeb',
|
||||
apiSecret: 'OGIwM2RlMjBkOTI5Mzk5YTJlMzUwODI5',
|
||||
apiKey: '0b17a761b6b7174b789f639119d7e29a',
|
||||
onWillStatusChange: function (oldStatus, newStatus) {},
|
||||
|
||||
onWillStatusChange: function (oldStatus, newStatus) {
|
||||
console.log(oldStatus, newStatus, '交互逻辑')
|
||||
},
|
||||
onTextChange: function (text) {
|
||||
// 监听识别结果的变化
|
||||
voiceTxt.innerText = text;
|
||||
marquee("marquee", "voice-txt");
|
||||
|
||||
let timer = setInterval(() => {
|
||||
seconds--;
|
||||
// 如果已有定时器在运行,则清除它
|
||||
if (gaptime) {
|
||||
clearInterval(gaptime);
|
||||
};
|
||||
// 重置倒计时
|
||||
seconds = 10;
|
||||
|
||||
if (seconds <= 0) {
|
||||
clearInterval(timer);
|
||||
console.log(text)
|
||||
|
||||
gaptime = setInterval(() => {
|
||||
console.log(seconds, '倒计时')
|
||||
seconds--;
|
||||
if (voiceTxt.innerText) {
|
||||
console.log('有 text 参数'); // 如果有 text 参数,打印 1
|
||||
clearInterval(gaptime);
|
||||
gaptime = null;
|
||||
|
||||
// 3秒钟内没有说话,就自动关闭
|
||||
if (text) {
|
||||
console.log(1); // 如果有 text 参数,打印 1
|
||||
} else {
|
||||
console.log("没有传入 text 参数"); // 如果没有 text 参数,执行其他逻辑
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
statusTxt.value = '正在听…';
|
||||
if (!isCallbackExecuted) {
|
||||
clearTimeout(times);
|
||||
times = setTimeout(() => {
|
||||
this.stop();
|
||||
|
||||
const params = { msg: text };
|
||||
|
||||
|
||||
// 3秒钟内没有说话,就自动关闭
|
||||
if (text) {
|
||||
statusTxt.value = '正在听…';
|
||||
if (!isCallbackExecuted) {
|
||||
clearTimeout(times);
|
||||
times = setTimeout(() => {
|
||||
this.stop();
|
||||
statusTxt.value = '正在说话';
|
||||
|
||||
const params = { msg: text };
|
||||
|
||||
// 判断关键字是否存在
|
||||
const result = parseHealthQuery(params.msg);
|
||||
isCallbackExecuted = true;
|
||||
|
||||
if (result) {
|
||||
DetailDay(result.dataKey, result.date).then(res => {
|
||||
let TargetData = res.data.data;
|
||||
|
||||
if (result.dataKey == 'ECGData') {
|
||||
TargetData.map(item => {
|
||||
delete item.data_msg.wavefrom;
|
||||
delete item.data_msg.list;
|
||||
})
|
||||
}
|
||||
if (result.dataKey == 'bloodLiquid') {
|
||||
TargetData.map(item => {
|
||||
item.data_msg.cholesterol = item.data_msg.cholesterol/100;
|
||||
item.data_msg.highDensity = item.data_msg.highDensity/100;
|
||||
item.data_msg.triacylglycerol = item.data_msg.triacylglycerol/100;
|
||||
item.data_msg.uricAcidVal = item.data_msg.uricAcidVal/10000;
|
||||
item.data_msg.lowDensity = item.data_msg.lowDensity/100;
|
||||
})
|
||||
}
|
||||
|
||||
Question = `请模仿全科医生的口吻与我: ${user}对话,我最近测量的${result.dataKey}数据为${JSON.stringify(TargetData)}, #提示:data_msg为值,hour_minute为检测时间。#提示:“[]”表示数据为空,请在小程序上传数据。#限制:回复不要带英文,要都转化成汉语。#限制:回复内容控制在150字。#限制:忽略“压力指数、疲劳指数、心肌炎风险、冠心病风险和动脉硬化”等数据。`
|
||||
|
||||
console.log(Question, '问题=========================问题');
|
||||
|
||||
|
||||
/*
|
||||
* 调用接口 传递关键信息 文字转语音
|
||||
*/
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
/*
|
||||
* 关键字转换
|
||||
*/
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": Question},
|
||||
{"role": "user", "content": `请问我${params.msg}正常吗`}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
// 判断关键字是否存在
|
||||
const result = parseHealthQuery(params.msg);
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
})
|
||||
} else {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
if (result) {
|
||||
DetailDay(result.dataKey, result.date).then(res => {
|
||||
let TargetData = res.data.data;
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
if (result.dataKey == 'ECGData') {
|
||||
TargetData.map(item => {
|
||||
delete item.data_msg.wavefrom;
|
||||
delete item.data_msg.list;
|
||||
})
|
||||
}
|
||||
if (result.dataKey == 'bloodLiquid') {
|
||||
TargetData.map(item => {
|
||||
item.data_msg.cholesterol = item.data_msg.cholesterol/100;
|
||||
item.data_msg.highDensity = item.data_msg.highDensity/100;
|
||||
item.data_msg.triacylglycerol = item.data_msg.triacylglycerol/100;
|
||||
item.data_msg.uricAcidVal = item.data_msg.uricAcidVal/10000;
|
||||
item.data_msg.lowDensity = item.data_msg.lowDensity/100;
|
||||
})
|
||||
}
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
Question = `请模仿全科医生的口吻与我: ${user}对话,我最近测量的${result.dataKey}数据为${JSON.stringify(TargetData)}, #提示:data_msg为值,hour_minute为检测时间。#提示:“[]”表示数据为空,请在小程序上传数据。#限制:回复不要带英文,要都转化成汉语。#限制:回复内容控制在150字。#限制:忽略“压力指数、疲劳指数、心肌炎风险、冠心病风险和动脉硬化”等数据。`
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
console.log(Question, '问题=========================问题');
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
|
||||
/*
|
||||
* 调用接口 传递关键信息 文字转语音
|
||||
*/
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
/*
|
||||
* 关键字转换
|
||||
*/
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": Question},
|
||||
{"role": "user", "content": `请问我${params.msg}正常吗`}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
})
|
||||
} else {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const url = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation';
|
||||
const apiKey = 'sk-cbb9b5ff44374fa2a8a258160ebb292d';
|
||||
|
||||
// 打开请求,设置为异步
|
||||
xhr.open('POST', url, true);
|
||||
|
||||
// 设置请求头
|
||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||
xhr.setRequestHeader('Authorization', 'Bearer ' + apiKey);
|
||||
xhr.setRequestHeader('X-DashScope-SSE', 'enable');
|
||||
|
||||
// 处理流式数据的接收(使用 progress 事件)
|
||||
xhr.onprogress = function () {
|
||||
const data = xhr.responseText;
|
||||
|
||||
// SSE 数据解析处理
|
||||
const lines = data.split('\n');
|
||||
lines.forEach(line => {
|
||||
if (line.startsWith('data:')) {
|
||||
const jsonStr = line.substring(5); // 去掉 'data:' 前缀
|
||||
const parsedData = JSON.parse(jsonStr);
|
||||
|
||||
// 解析 content 内容
|
||||
const content = parsedData.output.choices[0].message.content;
|
||||
if (content) {
|
||||
// 检查内容是否已经存在
|
||||
if (!Subtitles.includes(content)) {
|
||||
Subtitles += content;
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 检查请求完成
|
||||
xhr.onload = function () {
|
||||
if (xhr.status === 200) {
|
||||
statusTxt.value = '正在说话';
|
||||
RequestMicrosoft();
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": `请模仿全科医生的口吻与我对话`},
|
||||
{"role": "user", "content": params.msg}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// 错误处理
|
||||
xhr.onerror = function () {
|
||||
console.error('An error occurred during the transaction', xhr.statusText);
|
||||
};
|
||||
|
||||
// 发送请求
|
||||
const requestBody = {
|
||||
model: 'qwen-turbo',
|
||||
input: {
|
||||
messages: [
|
||||
{"role": "system", "content": `请模仿全科医生的口吻与我对话`},
|
||||
{"role": "user", "content": params.msg}
|
||||
]
|
||||
},
|
||||
parameters: {
|
||||
result_format: 'message',
|
||||
incremental_output: true
|
||||
}
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(requestBody));
|
||||
isCallbackExecuted = true;
|
||||
return;
|
||||
}, 3000);
|
||||
}
|
||||
}, 3000);
|
||||
}
|
||||
} else if (seconds == 0){
|
||||
reset();
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -506,15 +518,24 @@
|
|||
|
||||
function startShibie() {
|
||||
/**开始识别**/
|
||||
|
||||
voiceTxt.innerText = '';
|
||||
stopRec.textContent = '暂停';
|
||||
voice.start();
|
||||
|
||||
isCallbackExecuted = false;
|
||||
|
||||
// 先静音即可处理解决(提前做交互)
|
||||
audioElement.muted = false;
|
||||
audioElement.pause();
|
||||
audioElement.currentTime = 0;
|
||||
|
||||
// 检查是否可以播放,避免中断
|
||||
if (!audioElement.paused) {
|
||||
audioElement.currentTime = 0;
|
||||
audioElement.pause();
|
||||
};
|
||||
|
||||
seconds = 10;
|
||||
|
||||
voice.start();
|
||||
|
||||
startRec.style.display = 'none';
|
||||
endRec.style.display = 'none';
|
||||
|
|
@ -523,20 +544,28 @@
|
|||
}
|
||||
|
||||
function closeShibie() {
|
||||
reset();
|
||||
|
||||
/**关闭识别**/
|
||||
voiceTxt.innerText = '';
|
||||
statusTxt.value = '';
|
||||
stopRec.textContent = '暂停';
|
||||
|
||||
voice.stop();
|
||||
|
||||
// 音频
|
||||
audioElement.pause();
|
||||
audioElement.currentTime = 0;
|
||||
// 检查是否可以播放,避免中断
|
||||
if (!audioElement.paused) {
|
||||
audioElement.currentTime = 0;
|
||||
audioElement.pause();
|
||||
};
|
||||
|
||||
// 视频
|
||||
replayVideoSegment(0, 60);
|
||||
|
||||
isCallbackExecuted = false;
|
||||
seconds = 10;
|
||||
|
||||
voice.stop();
|
||||
console.log('关闭识别');
|
||||
|
||||
startRec.style.display = 'block';
|
||||
endRec.style.display = 'none';
|
||||
|
|
@ -548,7 +577,7 @@
|
|||
function showModal() {
|
||||
modal.style.display = 'block';
|
||||
dialog.style.display = 'block';
|
||||
}
|
||||
}
|
||||
|
||||
// 隐藏弹窗和遮罩
|
||||
function hideModal() {
|
||||
|
|
@ -647,9 +676,11 @@
|
|||
stopRec.style.display = 'none';
|
||||
statusTxt.value = '';
|
||||
voiceTxt.innerText = '';
|
||||
voice.stop();
|
||||
replayVideoSegment(0, 60);
|
||||
hideModal()
|
||||
|
||||
// 播放结束重新开始监听
|
||||
startShibie();
|
||||
});
|
||||
|
||||
Subtitles = "";
|
||||
|
|
@ -748,7 +779,7 @@
|
|||
// 如果没有找到匹配的数据项,则返回null或错误信息(这里选择返回null)
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in New Issue