FunASR icon indicating copy to clipboard operation
FunASR copied to clipboard

where could I find some demo in nodejs

Open taoqf opened this issue 1 year ago • 7 comments
trafficstars

Notice: In order to resolve issues more efficiently, please raise issue following the template. (注意:为了更加高效率解决您遇到的问题,请按照模板提问,补充细节)

❓ Questions and Help

I can not find demo in funasr-samples, event in npm.

Before asking:

  1. search the issues.
  2. search the docs.

What is your question?

I wish there is an example written in nodejs

Code

What have you tried?

search funasr in npmjs.org

What's your environment?

  • OS (e.g., Linux): Linux
  • FunASR Version (e.g., 1.0.0):
  • ModelScope Version (e.g., 1.11.0):
  • PyTorch Version (e.g., 2.0.0):
  • How you installed funasr (pip, source):
  • Python version:
  • GPU (e.g., V100M32)
  • CUDA/cuDNN version (e.g., cuda11.7):
  • Docker version (e.g., funasr-runtime-sdk-cpu-0.4.1) funasr-runtime-sdk-cpu-0.4.4
  • Any other relevant information:

taoqf avatar Mar 14 '24 01:03 taoqf

Maybe you could develop it with the doc.

LauraGPT avatar Mar 14 '24 13:03 LauraGPT

I've tried send data, the most complicated part is splite data with nodejs.

taoqf avatar Mar 15 '24 00:03 taoqf

I don't finger out how many bytes should I send per time. Even I send them all, I could not get answer from server, too. this is my test code.

async function connect(): Promise<WebSocket> {
	const uri = `wss://0.0.0.0:10095`;
	return new Promise<WebSocket>((resolve, reject) => {
		const ws = new WebSocket(uri, [], {
			rejectUnauthorized: false,
			// secureOptions: SSL_OP_NO_TLSv1_2,
		});
		ws.on('open', () => {
			console.log('连接成功');
			resolve(ws);
			setInterval(() => {
				ws.send('ping');
			}, 1000);
		});
		ws.on('message', (raw) => {
			const data = JSON.parse(Buffer.from(raw as Buffer).toString('utf-8')) as {
				text: string;
				timestamp: string;
				wav_name: string;
				is_final: boolean;
				mode: '2pass-offline';
				stamp_sents: {
					end: number;
					punc: string;
					start: number;
					text_seg: string;
					ts_list: number[];
				}[];
			};
			console.log('收到消息', data.text);
		});
		ws.on('error', (err) => {
			console.log('连接失败', err);
			reject(err);
		});
	});
}
async function main(): Promise<void> {
	const filepath = '/home/taoqf/Downloads/testmp4.wav';
	const mode = '2pass';
	// const mode = 'offline';
	const useItn = true;

	// 读取hotword文件并构建fst_dict
	const fstDict = {
		'产教': 10,
		// '铲掉': 0
	};
	const hotwordMessage = JSON.stringify(fstDict);
	console.log(hotwordMessage);

	const ws = await connect();
	console.log('开始发送数据');
	// 模拟sample_rate和wav_format
	const sampleRate = 8000; // 假设的采样率
	const wavFormat = 'pcm';
	const chunk_size = [5, 10, 5];	// 表示流式模型latency配置,`[5,10,5]`,表示当前音频为600ms,并且回看300ms,又看300ms。
	const chunk_interval = 10;

	// 发送第一条消息
	// {"audio_fs":8000,"chunk_interval":10,"chunk_size":[5,10,5],"hotwords":"","is_speaking":true,"itn":true,"mode":"offline","wav_format":"pcm","wav_name":"demo"}, msg_data->msg={"access_num":0,"audio_fs":8000,"is_eof":false,"itn":true,"mode":"offline","wav_format":"pcm","wav_name":"demo"}
	const message = JSON.stringify({
		mode,
		chunk_size,
		chunk_interval,
		audio_fs: sampleRate,
		wav_name: 'demo', // 假设的wav名称
		wav_format: wavFormat,
		is_speaking: true,
		hotwords: hotwordMessage,
		itn: useItn
	});
	await ws.send(message);
	// 处理wav文件
	const buf = await readFile(filepath);
	let i = 0;	 // skip first 44 wav header
	// ws.send(buf);
	const int_chunk_size = 60 * chunk_size[1] / chunk_interval;
	const chunk = sampleRate / 1000 * int_chunk_size;
	console.log('开始发送文件内容', buf.length, chunk);
	while (i < buf.length) {
		const sub = buf.subarray(i, i += chunk * 2);
		ws.send(sub);
		console.log('发送', i);
		await sleep(100);
	}
	console.log('文件内容发送结束');
	// await sleep(100000);
	// end 
	const endMessage = JSON.stringify({ is_speaking: false });
	await ws.send(endMessage);

	console.log('发送结束');

	// await ws.close();
	console.log('end');
}

taoqf avatar Mar 15 '24 02:03 taoqf

@LauraGPT Could you please guid me? I really get into a dead end. I tried to call python client in nodejs, but I could only get results when the whole response is finished.

taoqf avatar Mar 16 '24 04:03 taoqf

Is there anyone can help me?

taoqf avatar Mar 18 '24 07:03 taoqf

@LauraGPT Could you please guid me? I really get into a dead end. I tried to call python client in nodejs, but I could only get results when the whole response is finished.

I do not have any skills about JS.

LauraGPT avatar Mar 19 '24 02:03 LauraGPT

@LauraGPT Thank you anyway. btw, the doc need to be updated, there is no chunk param in the doc. but in fact, I could not send whole file to the server.

taoqf avatar Mar 19 '24 09:03 taoqf