From d6099ba80c9cd8cf2d1adbe9e162f68d5a7c8ef2 Mon Sep 17 00:00:00 2001 From: Ben Scholer Date: Fri, 9 Feb 2024 14:47:28 -0500 Subject: [PATCH 1/2] add support for multiple ASR servers Issue #13 --- src/settings.ts | 14 +++++++------- src/transcribe.ts | 40 +++++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/settings.ts b/src/settings.ts index 3dc2d5f..176a15e 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -7,7 +7,7 @@ interface TranscriptionSettings { translate: boolean; language: string; verbosity: number; - whisperASRUrl: string; + whisperASRUrls: string; debug: boolean; transcriptionEngine: string; embedAdditionalFunctionality: boolean; @@ -33,7 +33,7 @@ const DEFAULT_SETTINGS: TranscriptionSettings = { translate: false, language: "auto", verbosity: 1, - whisperASRUrl: "http://localhost:9000", + whisperASRUrls: "http://localhost:9000", debug: false, transcriptionEngine: "swiftink", embedAdditionalFunctionality: true, @@ -422,17 +422,17 @@ class TranscriptionSettingTab extends PluginSettingTab { .setHeading(); new Setting(containerEl) - .setName("Whisper ASR URL") + .setName("Whisper ASR URLs") .setDesc( - "The URL of the Whisper ASR server: https://github.com/ahmetoner/whisper-asr-webservice", + "The URL of the Whisper ASR server: https://github.com/ahmetoner/whisper-asr-webservice. Provide multiple URLs separated by semi-colons in case one is offline or not accessible. Tried in order.", ) .setClass("whisper-asr-settings") .addText((text) => text - .setPlaceholder(DEFAULT_SETTINGS.whisperASRUrl) - .setValue(this.plugin.settings.whisperASRUrl) + .setPlaceholder(DEFAULT_SETTINGS.whisperASRUrls) + .setValue(this.plugin.settings.whisperASRUrls) .onChange(async (value) => { - this.plugin.settings.whisperASRUrl = value; + this.plugin.settings.whisperASRUrls = value; await this.plugin.saveSettings(); }), ); diff --git a/src/transcribe.ts b/src/transcribe.ts index 917577a..3b82f73 100644 --- a/src/transcribe.ts +++ b/src/transcribe.ts @@ -94,27 +94,33 @@ export class TranscriptionEngine { if (this.settings.language != "auto") args += `&language=${this.settings.language}`; - const url = `${this.settings.whisperASRUrl}/asr?${args}`; - console.log("URL:", url); - - const options: RequestUrlParam = { - method: "POST", - url: url, - contentType: `multipart/form-data; boundary=----${boundary_string}`, - body: request_body, - }; - console.log("Options:", options); + const urls = this.settings.whisperASRUrls + .split(";") + .filter(Boolean); // Remove empty strings + + for (const baseUrl of urls) { + const url = `${baseUrl}/asr?${args}`; + console.log("Trying URL:", url); + + const options: RequestUrlParam = { + method: "POST", + url: url, + contentType: `multipart/form-data; boundary=----${boundary_string}`, + body: request_body, + }; - return requestUrl(options) - .then(async (response) => { + try { + const response = await requestUrl(options); if (this.settings.debug) console.log(response); if (typeof response.text === "string") return response.text; else return response.json.text; - }) - .catch((error) => { - if (this.settings.debug) console.error(error); - return Promise.reject(error); - }); + } catch (error) { + if (this.settings.debug) console.error("Error with URL:", url, error); + // Don't return or throw yet, try the next URL + } + } + // If all URLs fail, reject the promise with a generic error or the last specific error caught + return Promise.reject("All Whisper ASR URLs failed"); } async getTranscriptionSwiftink(file: TFile): Promise { From e20bd45292680517e7035c1e94be8447d4757c05 Mon Sep 17 00:00:00 2001 From: Ben Scholer Date: Fri, 9 Feb 2024 15:04:15 -0500 Subject: [PATCH 2/2] add options debug console log back in --- src/transcribe.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/transcribe.ts b/src/transcribe.ts index 3b82f73..3b048d4 100644 --- a/src/transcribe.ts +++ b/src/transcribe.ts @@ -109,6 +109,8 @@ export class TranscriptionEngine { body: request_body, }; + console.log("Options:", options); + try { const response = await requestUrl(options); if (this.settings.debug) console.log(response);