mirror of
https://github.com/OpenBMB/MiniCPM-V.git
synced 2026-02-05 18:29:18 +08:00
Update to MiniCPM-o 2.6
This commit is contained in:
61
web_demos/minicpm-o_2.6/web_server/src/hooks/useHttp.js
Normal file
61
web_demos/minicpm-o_2.6/web_server/src/hooks/useHttp.js
Normal file
@@ -0,0 +1,61 @@
|
||||
import axios from 'axios';
|
||||
import { setNewUserId, getNewUserId } from './useRandomId';
|
||||
|
||||
// 创建实例时配置默认值
|
||||
const service = axios.create({
|
||||
baseURL: '/',
|
||||
timeout: 30000,
|
||||
responseType: 'json'
|
||||
});
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(config => {
|
||||
if (config.url.includes('stream')) {
|
||||
config.timeout = 3000;
|
||||
}
|
||||
if (window.location.search) {
|
||||
config.url += window.location.search;
|
||||
}
|
||||
Object.assign(config.headers, ajaxHeader());
|
||||
return config;
|
||||
});
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
let res = response.data;
|
||||
if (response?.status === 200) {
|
||||
return Promise.resolve({
|
||||
code: 0,
|
||||
message: '',
|
||||
data: res
|
||||
});
|
||||
}
|
||||
return Promise.resolve({ code: -1, message: '网络异常,请稍后再试', data: null });
|
||||
},
|
||||
error => {
|
||||
const res = { code: -1, message: error?.response?.data?.detail || '网络异常,请稍后再试', data: null };
|
||||
return Promise.resolve(res);
|
||||
}
|
||||
);
|
||||
|
||||
export const ajaxHeader = () => {
|
||||
if (!localStorage.getItem('uid')) {
|
||||
setNewUserId();
|
||||
}
|
||||
return {
|
||||
'Content-Type': 'application/json;charset=UTF-8',
|
||||
Accept: 'application/json',
|
||||
service: 'minicpmo-server',
|
||||
uid: getNewUserId()
|
||||
};
|
||||
};
|
||||
|
||||
export default {
|
||||
get(url, params, config = {}) {
|
||||
return service.get(url, { params, ...config });
|
||||
},
|
||||
post(url, data, config = {}) {
|
||||
return service.post(url, data, { ...config });
|
||||
}
|
||||
};
|
||||
95
web_demos/minicpm-o_2.6/web_server/src/hooks/useQueue.js
Normal file
95
web_demos/minicpm-o_2.6/web_server/src/hooks/useQueue.js
Normal file
@@ -0,0 +1,95 @@
|
||||
export class TaskQueue {
|
||||
constructor() {
|
||||
this.tasks = [];
|
||||
this.isRunning = false;
|
||||
this.isPaused = false;
|
||||
this.currentTask = null;
|
||||
}
|
||||
|
||||
// 添加任务到队列
|
||||
addTask(task) {
|
||||
this.tasks.push(task);
|
||||
if (!this.isRunning) {
|
||||
this.start();
|
||||
}
|
||||
}
|
||||
|
||||
// 删除任务
|
||||
removeTask(taskToRemove) {
|
||||
this.tasks = this.tasks.filter(task => task !== taskToRemove);
|
||||
}
|
||||
|
||||
// 清空任务队列
|
||||
clearQueue() {
|
||||
this.tasks = [];
|
||||
}
|
||||
|
||||
// 暂停任务执行
|
||||
pause() {
|
||||
this.isPaused = true;
|
||||
}
|
||||
|
||||
// 恢复任务执行
|
||||
resume() {
|
||||
if (this.isPaused) {
|
||||
this.isPaused = false;
|
||||
if (!this.isRunning) {
|
||||
this.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 内部启动方法
|
||||
async start() {
|
||||
this.isRunning = true;
|
||||
while (this.tasks.length > 0 && !this.isPaused) {
|
||||
this.currentTask = this.tasks.shift();
|
||||
await this.currentTask();
|
||||
|
||||
// 检查是否暂停或任务队列已清空
|
||||
if (this.isPaused || this.tasks.length === 0) {
|
||||
this.isRunning = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.isRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 示例任务函数
|
||||
function exampleTask(id) {
|
||||
return () =>
|
||||
new Promise(resolve => {
|
||||
console.log(`Executing task ${id}`);
|
||||
setTimeout(() => {
|
||||
console.log(`Task ${id} completed`);
|
||||
resolve();
|
||||
}, 1000); // 每个任务耗时1秒
|
||||
});
|
||||
}
|
||||
|
||||
// 测试示例
|
||||
const queue = new TaskQueue();
|
||||
|
||||
// 添加任务到队列
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
queue.addTask(exampleTask(i));
|
||||
}
|
||||
|
||||
// 暂停队列,在2.5秒后执行
|
||||
setTimeout(() => {
|
||||
console.log('Pausing queue...');
|
||||
queue.pause();
|
||||
}, 2500);
|
||||
|
||||
// 恢复队列,在4.5秒后执行
|
||||
setTimeout(() => {
|
||||
console.log('Resuming queue...');
|
||||
queue.resume();
|
||||
}, 4500);
|
||||
|
||||
// 清空队列,在3秒后执行
|
||||
setTimeout(() => {
|
||||
console.log('Clearing queue...');
|
||||
queue.clearQueue();
|
||||
}, 3000);
|
||||
@@ -0,0 +1,9 @@
|
||||
const uid = 'uid';
|
||||
export const setNewUserId = () => {
|
||||
const randomId = Math.random().toString(36).slice(2).toUpperCase();
|
||||
localStorage.setItem(uid, randomId);
|
||||
return randomId;
|
||||
};
|
||||
export const getNewUserId = () => {
|
||||
return localStorage.getItem('uid');
|
||||
};
|
||||
38
web_demos/minicpm-o_2.6/web_server/src/hooks/useVoice.js
Normal file
38
web_demos/minicpm-o_2.6/web_server/src/hooks/useVoice.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const writeString = (view, offset, string) => {
|
||||
for (let i = 0; i < string.length; i++) {
|
||||
view.setUint8(offset + i, string.charCodeAt(i));
|
||||
}
|
||||
};
|
||||
const floatTo16BitPCM = (output, offset, input) => {
|
||||
for (let i = 0; i < input.length; i++, offset += 2) {
|
||||
const s = Math.max(-1, Math.min(1, input[i]));
|
||||
output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);
|
||||
}
|
||||
};
|
||||
// audio buffer to wav file, need add 44 length header
|
||||
export const encodeWAV = (samples, sampleRate) => {
|
||||
const buffer = new ArrayBuffer(44 + samples.length * 2);
|
||||
const view = new DataView(buffer);
|
||||
const numChannels = 1;
|
||||
const bitsPerSample = 16;
|
||||
|
||||
/* WAV 标头 */
|
||||
writeString(view, 0, 'RIFF');
|
||||
view.setUint32(4, 36 + samples.length * 2, true);
|
||||
writeString(view, 8, 'WAVE');
|
||||
writeString(view, 12, 'fmt ');
|
||||
view.setUint32(16, 16, true);
|
||||
view.setUint16(20, 1, true);
|
||||
view.setUint16(22, numChannels, true);
|
||||
view.setUint32(24, sampleRate, true);
|
||||
view.setUint32(28, (sampleRate * numChannels * bitsPerSample) / 8, true);
|
||||
view.setUint16(32, (numChannels * bitsPerSample) / 8, true);
|
||||
view.setUint16(34, bitsPerSample, true);
|
||||
writeString(view, 36, 'data');
|
||||
view.setUint32(40, samples.length * 2, true);
|
||||
|
||||
/* PCM 数据 */
|
||||
floatTo16BitPCM(view, 44, samples);
|
||||
|
||||
return new Blob([view], { type: 'audio/wav' });
|
||||
};
|
||||
Reference in New Issue
Block a user