Skip to content

Commit

Permalink
Merge pull request #47 from bscholer/multiple-whisper-servers
Browse files Browse the repository at this point in the history
Sequential Whisper ASR Server URL Fallback
  • Loading branch information
djmango authored Feb 9, 2024
2 parents b8cc83c + e20bd45 commit 06fbe83
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
14 changes: 7 additions & 7 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface TranscriptionSettings {
translate: boolean;
language: string;
verbosity: number;
whisperASRUrl: string;
whisperASRUrls: string;
debug: boolean;
transcriptionEngine: string;
embedAdditionalFunctionality: boolean;
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}),
);
Expand Down
40 changes: 24 additions & 16 deletions src/transcribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,35 @@ 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 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,
};

const options: RequestUrlParam = {
method: "POST",
url: url,
contentType: `multipart/form-data; boundary=----${boundary_string}`,
body: request_body,
};
console.log("Options:", options);
console.log("Options:", options);

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<string> {
Expand Down

0 comments on commit 06fbe83

Please sign in to comment.