From fca501dd8b7f86b74fee96e13c5236a1217e5685 Mon Sep 17 00:00:00 2001 From: practicer2015 Date: Tue, 18 Mar 2025 16:50:00 +0800 Subject: [PATCH] Tue Mar 18 16:50:00 CST 2025 inscode --- index.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- package.json | 15 +++++++++------ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 2d7e683..68211e6 100644 --- a/index.js +++ b/index.js @@ -1 +1,45 @@ -console.log("欢迎来到 InsCode"); \ No newline at end of file +const express = require('express'); +const multer = require('multer'); +const tencentcloud = require('tencentcloud-sdk-nodejs'); + +const app = express(); +const upload = multer({ dest: 'uploads/' }); + +// 腾讯云语音识别客户端 +const AsrClient = tencentcloud.asr.v20190614.Client; +const client = new AsrClient({ + credential: { + secretId: 'your-secret-id', // 替换为你的 SecretId + secretKey: 'your-secret-key' // 替换为你的 SecretKey + }, + region: 'ap-guangzhou', + profile: { + httpProfile: { + endpoint: 'asr.tencentcloudapi.com' + } + } +}); + +// 处理上传的录音文件 +app.post('/speech-to-text', upload.single('audio'), (req, res) => { + const filePath = req.file.path; + + // 调用腾讯云语音识别 API + const params = { + EngineModelType: '16k_zh', // 16k 中文普通话 + Url: filePath // 本地文件路径需要先上传到云存储 + }; + + client.CreateRecTask(params).then( + (data) => { + res.json({ code: 0, text: data.Result }); + }, + (err) => { + res.json({ code: 1, message: err.message }); + } + ); +}); + +app.listen(3000, () => { + console.log('Server is running on http://localhost:3000'); +}); \ No newline at end of file diff --git a/package.json b/package.json index 72caa17..45172df 100644 --- a/package.json +++ b/package.json @@ -4,15 +4,18 @@ "description": "", "main": "index.js", "scripts": { - "dev": "node index.js", - "test": "echo \"Error: no test specified\" && exit 1" + "dev": "node index.js", + "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { - "@types/node": "^18.0.6", - "node-fetch": "^3.2.6" + "@types/node": "^18.0.6", + "expres": "^0.0.5", + "express": "^4.21.2", + "multer": "^1.4.5-lts.1", + "node-fetch": "^3.2.6", + "tencentcloud-sdk-nodejs": "^4.0.1053" } - } - \ No newline at end of file +} -- GitLab