Skip to content

Commit

Permalink
feat: add Microsoft Azure Speech to Text API
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed Dec 8, 2018
1 parent 0c9a824 commit f8c1dde
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 19 deletions.
52 changes: 51 additions & 1 deletion src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
},

"optionValue_speechService_ibmSpeechApi": {
"message": "IBM Speech to Text",
"message": "IBM Watson Speech to Text API",
"description": "Value of the option."
},

"optionValue_speechService_microsoftSpeechApi": {
"message": "Microsoft Azure Speech to Text API",
"description": "Value of the option."
},

Expand Down Expand Up @@ -64,6 +69,51 @@
"description": "Value of the option."
},

"optionTitle_microsoftSpeechApiLoc": {
"message": "API location",
"description": "Title of the option."
},

"optionValue_microsoftSpeechApiLoc_eastUs": {
"message": "East US",
"description": "Value of the option."
},

"optionValue_microsoftSpeechApiLoc_eastUs2": {
"message": "East US 2",
"description": "Value of the option."
},

"optionValue_microsoftSpeechApiLoc_westUs": {
"message": "West US",
"description": "Value of the option."
},

"optionValue_microsoftSpeechApiLoc_westUs2": {
"message": "West US 2",
"description": "Value of the option."
},

"optionValue_microsoftSpeechApiLoc_eastAsia": {
"message": "East Asia",
"description": "Value of the option."
},

"optionValue_microsoftSpeechApiLoc_southeastAsia": {
"message": "Southeast Asia",
"description": "Value of the option."
},

"optionValue_microsoftSpeechApiLoc_westEu": {
"message": "West Europe",
"description": "Value of the option."
},

"optionValue_microsoftSpeechApiLoc_northEu": {
"message": "North Europe",
"description": "Value of the option."
},

"inputLabel_apiKey": {
"message": "API key",
"description": "Placeholder of the input."
Expand Down
30 changes: 28 additions & 2 deletions src/options/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@
:label="getText('inputLabel_apiKey')">
</v-textfield>
</div>
<div class="option select"
v-if="options.speechService === 'microsoftSpeechApi'">
<v-select :label="getText('optionTitle_microsoftSpeechApiLoc')"
v-model="options.microsoftSpeechApiLoc"
:options="selectOptions.microsoftSpeechApiLoc">
</v-select>
</div>
<div class="option text-field"
v-if="options.speechService === 'microsoftSpeechApi'">
<v-textfield v-model="options.microsoftSpeechApiKey"
:label="getText('inputLabel_apiKey')">
</v-textfield>
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -58,22 +71,35 @@ export default {
speechService: [
'googleSpeechApiDemo',
'googleSpeechApi',
'ibmSpeechApi'
'ibmSpeechApi',
'microsoftSpeechApi'
],
ibmSpeechApiLoc: [
'frankfurt',
'dallas',
'washington',
'sydney',
'tokyo'
],
microsoftSpeechApiLoc: [
'eastUs',
'eastUs2',
'westUs',
'westUs2',
'eastAsia',
'southeastAsia',
'westEu',
'northEu'
]
}),
options: {
speechService: '',
googleSpeechApiKey: '',
ibmSpeechApiLoc: '',
ibmSpeechApiKey: ''
ibmSpeechApiKey: '',
microsoftSpeechApiLoc: '',
microsoftSpeechApiKey: ''
}
};
},
Expand Down
54 changes: 48 additions & 6 deletions src/solve/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {getText, waitForElement, arrayBufferToBase64} from 'utils/common';
import {
captchaGoogleSpeechApiLangCodes,
captchaIbmSpeechApiLangCodes,
ibmSpeechApiUrls
captchaMicrosoftSpeechApiLangCodes,
ibmSpeechApiUrls,
microsoftSpeechApiUrls
} from 'utils/data';

let solverWorking = false;
Expand Down Expand Up @@ -199,16 +201,56 @@ async function solve() {
});
return;
}
const apiUrl = ibmSpeechApiUrls[apiLoc];
const model = captchaIbmSpeechApiLangCodes[lang] || 'en-US_BroadbandModel';

const rsp = await fetch(`${apiUrl}?model=${model}&profanity_filter=false`, {
referrer: '',
mode: 'cors',
method: 'POST',
headers: {
Authorization: 'Basic ' + window.btoa('apiKey:' + apiKey)
},
body: new Blob([audioContent], {type: 'audio/wav'})
});

if (rsp.status !== 200) {
throw new Error(`API response: ${rsp.status}, ${await rsp.text()}`);
}

const results = (await rsp.json()).results;
if (results && results.length) {
solution = results[0].alternatives[0].transcript.trim();
}
}

if (speechService === 'microsoftSpeechApi') {
const {
microsoftSpeechApiLoc: apiLoc,
microsoftSpeechApiKey: apiKey
} = await storage.get(
['microsoftSpeechApiLoc', 'microsoftSpeechApiKey'],
'sync'
);
if (!apiKey) {
browser.runtime.sendMessage({
id: 'notification',
messageId: 'error_missingApiKey'
});
return;
}
const apiUrl = microsoftSpeechApiUrls[apiLoc];
const language = captchaMicrosoftSpeechApiLangCodes[lang] || 'en-US';

const rsp = await fetch(
`${ibmSpeechApiUrls[apiLoc]}?model=${model}&profanity_filter=false`,
`${apiUrl}?language=${language}&format=detailed&profanity=raw`,
{
referrer: '',
mode: 'cors',
method: 'POST',
headers: {
Authorization: 'Basic ' + window.btoa('apiKey:' + apiKey)
'Ocp-Apim-Subscription-Key': apiKey,
'Content-type': 'audio/wav; codec=audio/pcm; samplerate=16000'
},
body: new Blob([audioContent], {type: 'audio/wav'})
}
Expand All @@ -218,9 +260,9 @@ async function solve() {
throw new Error(`API response: ${rsp.status}, ${await rsp.text()}`);
}

const results = (await rsp.json()).results;
if (results && results.length) {
solution = results[0].alternatives[0].transcript.trim();
const results = (await rsp.json()).NBest;
if (results) {
solution = results[0].Lexical.trim();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/storage/versions/local/ONiJBs00o.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill';

const message = 'Add IBM Speech to Text';
const message = 'Add IBM Watson Speech to Text API';

const revision = 'ONiJBs00o';
const downRevision = 'UoT3kGyBH';
Expand All @@ -9,7 +9,7 @@ const storage = browser.storage.local;

async function upgrade() {
const changes = {
ibmSpeechApiLoc: 'frankfurt', // frankfurt, dallas, washington, sydney, tokyo
ibmSpeechApiLoc: 'frankfurt', // 'frankfurt', 'dallas', 'washington', 'sydney', 'tokyo'
ibmSpeechApiKey: ''
};

Expand Down
28 changes: 28 additions & 0 deletions src/storage/versions/local/UidMDYaYA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import browser from 'webextension-polyfill';

const message = 'Add Microsoft Azure Speech to Text API';

const revision = 'UidMDYaYA';
const downRevision = 'ONiJBs00o';

const storage = browser.storage.local;

async function upgrade() {
const changes = {
microsoftSpeechApiLoc: 'eastUs', // 'eastUs', 'eastUs2', 'westUs', 'westUs2', 'eastAsia', 'southeastAsia', 'westEu', 'northEu'
microsoftSpeechApiKey: ''
};

changes.storageVersion = revision;
return storage.set(changes);
}

async function downgrade() {
const changes = {};
await storage.remove(['microsoftSpeechApiLoc', 'microsoftSpeechApiKey']);

changes.storageVersion = downRevision;
return storage.set(changes);
}

export {message, revision, upgrade, downgrade};
2 changes: 1 addition & 1 deletion src/storage/versions/local/UoT3kGyBH.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const storage = browser.storage.local;

async function upgrade() {
const changes = {
speechService: 'googleSpeechApiDemo', // 'googleSpeechApiDemo', 'googleSpeechApi'
speechService: 'googleSpeechApiDemo', // 'googleSpeechApiDemo', 'googleSpeechApi', 'ibmSpeechApi', 'microsoftSpeechApi'
googleSpeechApiKey: '',
installTime: new Date().getTime(),
useCount: 0
Expand Down
2 changes: 1 addition & 1 deletion src/storage/versions/local/versions.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"versions": ["UoT3kGyBH", "ONiJBs00o"]}
{"versions": ["UoT3kGyBH", "ONiJBs00o", "UidMDYaYA"]}
4 changes: 2 additions & 2 deletions src/storage/versions/sync/ONiJBs00o.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import browser from 'webextension-polyfill';

const message = 'Add IBM Speech to Text';
const message = 'Add IBM Watson Speech to Text API';

const revision = 'ONiJBs00o';
const downRevision = 'UoT3kGyBH';
Expand All @@ -9,7 +9,7 @@ const storage = browser.storage.sync;

async function upgrade() {
const changes = {
ibmSpeechApiLoc: 'frankfurt', // frankfurt, dallas, washington, sydney, tokyo
ibmSpeechApiLoc: 'frankfurt', // 'frankfurt', 'dallas', 'washington', 'sydney', 'tokyo'
ibmSpeechApiKey: ''
};

Expand Down
28 changes: 28 additions & 0 deletions src/storage/versions/sync/UidMDYaYA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import browser from 'webextension-polyfill';

const message = 'Add Microsoft Azure Speech to Text API';

const revision = 'UidMDYaYA';
const downRevision = 'ONiJBs00o';

const storage = browser.storage.sync;

async function upgrade() {
const changes = {
microsoftSpeechApiLoc: 'eastUs', // 'eastUs', 'eastUs2', 'westUs', 'westUs2', 'eastAsia', 'southeastAsia', 'westEu', 'northEu'
microsoftSpeechApiKey: ''
};

changes.storageVersion = revision;
return storage.set(changes);
}

async function downgrade() {
const changes = {};
await storage.remove(['microsoftSpeechApiLoc', 'microsoftSpeechApiKey']);

changes.storageVersion = downRevision;
return storage.set(changes);
}

export {message, revision, upgrade, downgrade};
2 changes: 1 addition & 1 deletion src/storage/versions/sync/UoT3kGyBH.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const storage = browser.storage.sync;

async function upgrade() {
const changes = {
speechService: 'googleSpeechApiDemo', // 'googleSpeechApiDemo', 'googleSpeechApi'
speechService: 'googleSpeechApiDemo', // 'googleSpeechApiDemo', 'googleSpeechApi', 'ibmSpeechApi', 'microsoftSpeechApi'
googleSpeechApiKey: '',
installTime: new Date().getTime(),
useCount: 0
Expand Down
2 changes: 1 addition & 1 deletion src/storage/versions/sync/versions.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"versions": ["UoT3kGyBH", "ONiJBs00o"]}
{"versions": ["UoT3kGyBH", "ONiJBs00o", "UidMDYaYA"]}
Loading

0 comments on commit f8c1dde

Please sign in to comment.