Skip to content

Commit

Permalink
Merge pull request #216 from hyperaudio/213-nova2
Browse files Browse the repository at this point in the history
213 nova2
  • Loading branch information
maboa authored Dec 6, 2023
2 parents 662807f + 9446911 commit d4624d5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 73 deletions.
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- (C) The Hyperaudio Project. AGPL 3.0 @license: https://www.gnu.org/licenses/agpl-3.0.en.html -->
<!-- Hyperaudio Lite Editor - Version 0.4 -->
<!-- Hyperaudio Lite Editor - Version 0.4.2 -->

<!-- Hyperaudio Lite Editor's source code is provided under a dual license model.
Expand Down Expand Up @@ -108,7 +108,7 @@
<select id="tier" name="tier" placeholder="tier" class="select select-bordered w-full max-w-xs">
<option value="base">Base</option>
<option value="enhanced">Enhanced (Better)</option>
<option value="nova">Nova (Best)</option>
<option value="nova">Nova-2 (Best)</option>
</select>

</div>
Expand Down
169 changes: 98 additions & 71 deletions js/hyperaudio-lite-editor-deepgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class DeepgramService extends HTMLElement {
"Phone call": "phonecall",
"Voicemail": "voicemail",
"Finance": "finance",
"Conversational AI": "conversationalai",
"Conversational AI": "conversationalai",
"Video": "video",
"Medical": "medical"
}

let counter = 0
Expand Down Expand Up @@ -83,6 +85,20 @@ class DeepgramService extends HTMLElement {
}
}

updateLanguageDropdown(event) {
let tier = document.querySelector('#deepgram-form #tier').value;

console.log(tier);

if (tier === "base" || tier === "enhanced" ){
populateLanguageDeepgram();
}

if (tier === "nova"){
populateLanguageDeepgramRestricted();
}
}

updateTierDropdown(event) {

const deepgramModelCompatibility = {
Expand All @@ -92,23 +108,25 @@ class DeepgramService extends HTMLElement {
"da_general": ["enhanced", "base"],
"nl_general": ["enhanced", "base"],
"en_general": ["nova", "enhanced", "base"],
"en_meeting": ["enhanced", "base"],
"en_meeting": ["nova", "enhanced", "base"],
"en_phonecall": ["nova", "enhanced", "base"],
"en_voicemail": ["base"],
"en_finance": ["enhanced", "base"],
"en_conversationalai": ["base"],
"en_video": ["base"],
"en_voicemail": ["nova", "base"],
"en_finance": ["nova", "enhanced", "base"],
"en_conversationalai": ["nova", "base"],
"en_video": ["nova","base"],
"en_medical": ["nova"],
"en-AU_general": ["nova", "base"],
"en-GB_general": ["nova", "base"],
"en-IN_general": ["nova", "base"],
"en-NZ_general": ["nova", "base"],
"en-US_general": ["nova", "enhanced", "base"],
"en-US_meeting": ["enhanced", "base"],
"en-US_meeting": ["nova","enhanced", "base"],
"en-US_phonecall": ["nova", "enhanced", "base"],
"en-US_voicemail": ["base"],
"en-US_finance": ["enhanced", "base"],
"en-US_conversationalai": ["base"],
"en-US_video": ["base"],
"en-US_voicemail": ["nova","base"],
"en-US_finance": ["nova","enhanced", "base"],
"en-US_conversationalai": ["nova","base"],
"en-US_video": ["nova", "base"],
"en-US_medical": ["nova"],
"nl_general": ["enhanced", "base"],
"fr_general": ["enhanced" , "base"],
"fr-CA_general": ["base"],
Expand Down Expand Up @@ -221,22 +239,12 @@ function addModalEventListeners(modal) {
document.querySelector('#language-model').addEventListener('change', modal.updateDropdowns);
document.querySelector('#language-model').addEventListener('change', modal.updateTierDropdown);
document.querySelector('#language').addEventListener('change', modal.updateTierDropdown);
document.querySelector('#tier').addEventListener('change', modal.updateLanguageDropdown);
}

function fetchData(token, media, tier, language, model) {

let url = null;
let languageParam = `&language=${language}`;
if (language === "xx") {
//signifies autodetect
languageParam = "&detect_language=true";
}

if (model.startsWith("whisper")) { // no tier
url = `https://api.deepgram.com/v1/listen?model=${model}${languageParam}&diarize=true&summarize=true&detect_topics=true&smart_format=true`
} else {
url = `https://api.deepgram.com/v1/listen?model=${model}&tier=${tier}&diarize=true&summarize=true&detect_topics=true&language=${language}&smart_format=true`
}
let url = getApiUrl(tier, language, model);

fetch(url, {
method: 'POST',
Expand Down Expand Up @@ -267,44 +275,12 @@ function fetchData(token, media, tier, language, model) {
}
})
.catch(function (error) {
console.dir("error is : "+error);
error = error + "";

let errorDisplayed = displayError(error, tier);

if (error.indexOf("400") > 0 && tier === "enhanced") {
tier = "base";
fetchData(token, media, tier, language, model);
}

if (error.indexOf("400") > 0 && tier === "nova") {
tier = "enhanced";
fetchData(token, media, tier, language, model);
}

this.dataError = true;

if (errorDisplayed === false) {
displayGenericError();
}
displayAppropriateErrorMessage(error, token, file, tier, language, model, false);
})
}

function fetchDataLocal(token, file, tier, language, model) {


let url = null;
let languageParam = `&language=${language}`;
if (language === "xx") {
//signifies autodetect
languageParam = "&detect_language=true";
}

if (model.startsWith("whisper")) { // no tier
url = `https://api.deepgram.com/v1/listen?model=${model}${languageParam}&diarize=true&summarize=true&detect_topics=true&smart_format=true`
} else {
url = `https://api.deepgram.com/v1/listen?model=${model}&tier=${tier}&diarize=true&summarize=true&detect_topics=true&language=${language}&smart_format=true`
}
const apiKey = token;

// Create a new FileReader instance
Expand All @@ -321,6 +297,8 @@ function fetchDataLocal(token, file, tier, language, model) {
let player = document.querySelector("#hyperplayer");
player.src = URL.createObjectURL(blob);

let url = getApiUrl(tier, language, model);

// if the token is not present we just add the media to the player
if (token !== "") {
fetch(url, {
Expand Down Expand Up @@ -348,21 +326,7 @@ function fetchDataLocal(token, file, tier, language, model) {
}
})
.catch(function (error) {
console.dir("error is : "+error);
error = error + "";

let errorDisplayed = displayError(error, tier);

if (error.indexOf("400") > 0 && tier === "enhanced") {
tier = "base";
fetchDataLocal(token, file, tier, language, model);
}

this.dataError = true;

if (errorDisplayed === false) {
displayGenericError();
}
displayAppropriateErrorMessage(error, token, file, tier, language, model, true);
})
} else {
document.querySelector('#hypertranscript').innerHTML = '';
Expand All @@ -371,6 +335,65 @@ function fetchDataLocal(token, file, tier, language, model) {
});
}

function getApiUrl(tier, language, model) {
let url = null;
let languageParam = `&language=${language}`;
if (language === "xx") {
//signifies autodetect
languageParam = "&detect_language=true";
}

let novaPrefix = "";
if (tier === "nova") {
novaPrefix = "2-";
}

if (model.startsWith("whisper")) { // no tier
url = `https://api.deepgram.com/v1/listen?model=${model}${languageParam}&diarize=true&summarize=true&detect_topics=true&smart_format=true`
} else {
url = `https://api.deepgram.com/v1/listen?model=${novaPrefix}${model}&tier=${tier}&diarize=true&summarize=true&detect_topics=true&language=${language}&smart_format=true`
}
return (url);
}

function displayAppropriateErrorMessage(error, token, file, tier, language, model, isLocal) {
console.dir("error is : "+error);

error = error + "";

let errorDisplayed = displayError(error, tier);

if (error.indexOf("400") > 0 && tier === "enhanced") {
tier = "base";
if (isLocal === true) {
fetchDataLocal(token, file, tier, language, model);
} else {
fetchData(token, file, tier, language, model);
}

displayRetryError();
errorDisplayed = true;
}

if (error.indexOf("400") > 0 && tier === "nova") {
tier = "enhanced";
if (isLocal === true) {
fetchDataLocal(token, file, tier, language, model);
} else {
fetchData(token, file, tier, language, model);
}

displayRetryError();
errorDisplayed = true;
}

this.dataError = true;

if (errorDisplayed === false) {
displayGenericError();
}
}

function displayError(error, tier) {
if (error.indexOf("401") > 0 || (error.indexOf("400") > 0 && tier === "base")) {
document.querySelector('#hypertranscript').innerHTML = '<div class="vertically-centre"><img src="'+errorSvg+'" width="50" alt="error" style="margin: auto; display: block;"><br/><center>Sorry.<br/>It appears that the media URL does not exist<br/> or the token is invalid.</center></div>';
Expand All @@ -387,6 +410,10 @@ function displayGenericError() {
document.querySelector('#hypertranscript').innerHTML = '<div class="vertically-centre"><img src="'+errorSvg+'" width="50" alt="error" style="margin: auto; display: block;"><br/><center>Sorry.<br/>An unexpected error has occurred.</center></div>';
}

function displayRetryError() {
document.querySelector('#hypertranscript').innerHTML = '<div class="vertically-centre"><img src="'+transcribingSvg+'" width="50" alt="error" style="margin: auto; display: block;"><br/><center>There appears to be a language / model incompatibility. Retrying with a different model.</center></div>';
}

function displayNoWordsError() {
document.querySelector('#hypertranscript').innerHTML = '<div class="vertically-centre"><img src="'+errorSvg+'" width="50" alt="error" style="margin: auto; display: block;"><br/><center>Sorry.<br/>No words were detected.<br/>Please verify that audio contains speech.</center></div>';
}
Expand Down

0 comments on commit d4624d5

Please sign in to comment.