Spaces:
Sleeping
Sleeping
| const plugin = requirePlugin('WechatSI'); | |
| const innerAudioContext = wx.createInnerAudioContext(); | |
| const manager = plugin.getRecordRecognitionManager(); | |
| Page({ | |
| data: { | |
| languages: { | |
| 'zh': { name: '中文', flag: 'cn', code: 'zh_CN' }, | |
| 'en': { name: 'English', flag: 'us', code: 'en_US' }, | |
| 'ja': { name: '日本語', flag: 'jp', code: 'ja_JP' }, | |
| 'ko': { name: '한국어', flag: 'kr', code: 'ko_KR' } | |
| }, | |
| langCodes: ['zh', 'en', 'ja', 'ko'], | |
| sourceLang: 'zh', | |
| targetLang: 'en', | |
| transcript: '', | |
| outputText: '', | |
| isRecording: false, | |
| sourceLanguages: [], | |
| targetLanguages: [], | |
| hfSpaceUrl: 'https://dazaozi-wechat-translator-app.hf.space', | |
| currentMode: '' // 'plugin-translate' or 'hf-bridge' | |
| }, | |
| onLoad: function () { | |
| this.initializeLanguages(); | |
| this.initRecorderManager(); | |
| }, | |
| // --- Language Selection Logic --- | |
| initializeLanguages: function () { | |
| const { langCodes, languages, sourceLang, targetLang } = this.data; | |
| this.setData({ | |
| sourceLanguages: langCodes.map(c => ({ ...languages[c], langCode: c, selected: c === sourceLang })), | |
| targetLanguages: langCodes.map(c => ({ ...languages[c], langCode: c, selected: c === targetLang })) | |
| }); | |
| }, | |
| selectSourceLanguage: function(e) { this.setData({ sourceLang: e.currentTarget.dataset.langCode }, this.initializeLanguages); }, | |
| selectTargetLanguage: function(e) { this.setData({ targetLang: e.currentTarget.dataset.langCode }, this.initializeLanguages); }, | |
| swapLanguages: function() { | |
| this.setData({ | |
| sourceLang: this.data.targetLang, | |
| targetLang: this.data.sourceLang, | |
| transcript: this.data.outputText, | |
| outputText: this.data.transcript | |
| }, this.initializeLanguages); | |
| }, | |
| // --- Unified Recording Logic --- | |
| initRecorderManager: function() { | |
| manager.onStart = (res) => { | |
| this.setData({ transcript: '正在聆听...', outputText: '' }); | |
| }; | |
| manager.onRecognize = (res) => { | |
| this.setData({ transcript: res.result }); | |
| }; | |
| manager.onTranslate = (res) => { | |
| this.setData({ outputText: res.result }); | |
| }; | |
| manager.onStop = (res) => { | |
| this.setData({ isRecording: false }); | |
| if (this.data.currentMode === 'plugin-translate') { | |
| this.setData({ transcript: res.result, outputText: res.translateResult }); | |
| } else if (this.data.currentMode === 'hf-bridge') { | |
| // For HF mode, we need the audio file to upload. | |
| if (res.tempFilePath) { | |
| this.uploadAudioForASR(res.tempFilePath); | |
| } else { | |
| this.setData({ transcript: '录音文件获取失败' }); | |
| } | |
| } | |
| }; | |
| manager.onError = (res) => { | |
| this.setData({ isRecording: false, transcript: '识别失败', outputText: res.msg }); | |
| }; | |
| }, | |
| startRecording: function () { | |
| const { sourceLang, targetLang } = this.data; | |
| const isChineseEnglish = (sourceLang === 'zh' && targetLang === 'en') || (sourceLang === 'en' && targetLang === 'zh'); | |
| this.setData({ isRecording: true }); | |
| if (isChineseEnglish) { | |
| // Mode 1: Fast, all-in-one ASR and Translation by the plugin | |
| this.setData({ currentMode: 'plugin-translate' }); | |
| manager.start({ lang: this.data.languages[sourceLang].code, translate: true, lto: this.data.languages[targetLang].code }); | |
| } else { | |
| // Mode 2: HF Bridge. Use plugin for ASR only, then our backend for translation. | |
| this.setData({ currentMode: 'hf-bridge' }); | |
| // We ask the plugin to record, but NOT to translate. | |
| manager.start({ lang: this.data.languages[sourceLang].code, translate: false }); | |
| } | |
| }, | |
| stopRecording: function () { | |
| manager.stop(); | |
| }, | |
| // --- HF Bridge Translation Flow --- | |
| uploadAudioForASR: function (filePath) { | |
| this.setData({ transcript: '正在识别 (1/3)...' }); | |
| const fileSystemManager = wx.getFileSystemManager(); | |
| fileSystemManager.readFile({ filePath, encoding: 'base64', success: (res) => { | |
| wx.request({ | |
| url: `${this.data.hfSpaceUrl}/api/asr`, | |
| method: 'POST', | |
| header: { 'Content-Type': 'application/json' }, | |
| data: { "audio_data_uri": `data:audio/mp3;base64,${res.data}` }, | |
| timeout: 60000, | |
| success: (asrRes) => { | |
| if (asrRes.statusCode === 200 && asrRes.data.transcript) { | |
| const transcript = asrRes.data.transcript; | |
| this.setData({ transcript }); | |
| this.fullBackendBridge(transcript, this.data.sourceLang, this.data.targetLang); | |
| } else { this.setData({ transcript: 'HF识别失败' }); } | |
| }, | |
| fail: () => { this.setData({ transcript: 'HF识别请求失败' }); } | |
| }); | |
| }}); | |
| }, | |
| fullBackendBridge: function(text, sourceLang, targetLang) { | |
| this.setData({ outputText: '翻译中 (2/3)..' }); | |
| this.translateViaHF(text, sourceLang, 'en', (englishResult) => { | |
| if (englishResult) { | |
| this.setData({ outputText: '翻译中 (3/3)..' }); | |
| this.translateViaHF(englishResult, 'en', targetLang, (finalResult) => { | |
| if (finalResult) { | |
| this.setData({ outputText: finalResult }); | |
| } | |
| }); | |
| } | |
| }); | |
| }, | |
| translateViaHF: function(text, sourceLang, targetLang, callback) { | |
| wx.request({ | |
| url: `${this.data.hfSpaceUrl}/api/translate`, | |
| method: 'POST', | |
| header: { 'Content-Type': 'application/json' }, | |
| data: { "text": text, "source_lang": sourceLang, "target_lang": targetLang }, | |
| timeout: 30000, | |
| success: (res) => { | |
| if (res.statusCode === 200 && res.data.translated_text) { | |
| callback(res.data.translated_text); | |
| } else { | |
| this.setData({ outputText: `HF翻译失败 (${sourceLang}->${targetLang})` }); | |
| callback(null); | |
| } | |
| }, | |
| fail: () => { | |
| this.setData({ outputText: `HF翻译请求失败 (${sourceLang}->${targetLang})` }); | |
| callback(null); | |
| } | |
| }); | |
| } | |
| }); |