Skip to content

Commit

Permalink
use ttstool.com/fasttext to do language detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ken107 committed Apr 9, 2024
1 parent 0664055 commit 5933f9a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
14 changes: 12 additions & 2 deletions js/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,16 @@ function Doc(source, onEnd) {
})
}

function serverDetectLanguage(text) {
async function serverDetectLanguage(text) {
try {
const service = await rxjs.firstValueFrom(fasttextObservable)
if (!service) throw new Error("FastText service unavailable")
const [prediction] = await service.sendRequest("detectLanguage", {text})
return prediction?.language
}
catch (err) {
console.error(err)

return ajaxPost(config.serviceUrl + "/read-aloud/detect-language", {text: text}, "json")
.then(JSON.parse)
.then(function(res) {
Expand All @@ -297,6 +306,7 @@ function Doc(source, onEnd) {
console.error(err)
return null
})
}
}

async function getSpeech(texts) {
Expand Down Expand Up @@ -335,7 +345,7 @@ function Doc(source, onEnd) {
//method getState
function getState() {
if (activeSpeech) return activeSpeech.getState();
else return Promise.resolve(source.isWaiting() ? "LOADING" : "STOPPED");
else return "LOADING"
}

//method getActiveSpeech
Expand Down
4 changes: 2 additions & 2 deletions js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function makeDispatcher(myAddress, handlers) {
if (handlers[req.method]) {
Promise.resolve()
.then(() => handlers[req.method](req.args, sender))
.then(result => sendResponse({ type: "response", id: req.id, result, error: undefined }), error => sendResponse({ type: "response", id: req.id, result: undefined, error }));
.then(result => sendResponse({ to: req.from, type: "response", id: req.id, result, error: undefined }), error => sendResponse({ to: req.from, type: "response", id: req.id, result: undefined, error }));
//let caller know that sendResponse will be called asynchronously
return true;
}
Expand Down Expand Up @@ -187,7 +187,7 @@ function makeDispatcher(myAddress, handlers) {
else
pending.fulfill(res.result);
}
else {
else if (res.to == myAddress) {
console.error("Stray response", res);
}
}
Expand Down
47 changes: 41 additions & 6 deletions js/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const piperObservable = rxjs.defer(() => {
rxjs.shareReplay({bufferSize: 1, refCount: false})
)
const piperCallbacks = new rxjs.Subject()
const domDispatcher = makeDispatcher("piper-host", {
const piperDispatcher = makeDispatcher("piper-host", {
advertiseVoices({voices}, sender) {
updateSettings({piperVoices: voices})
piperSubject.next(sender)
Expand All @@ -25,16 +25,42 @@ const domDispatcher = makeDispatcher("piper-host", {
onEnd: args => piperCallbacks.next({type: "end", ...args}),
onError: args => piperCallbacks.next({type: "error", ...args}),
})


const fasttextSubject = new rxjs.Subject()
const fasttextObservable = rxjs.defer(() => {
createFasttextFrame()
return fasttextSubject
})
.pipe(
rxjs.startWith(null),
rxjs.shareReplay({bufferSize: 1, refCount: false})
)
const fasttextDispatcher = makeDispatcher("fasttext-host", {
onServiceReady(args, sender) {
fasttextSubject.next(sender)
}
})


window.addEventListener("message", event => {
const send = message => event.source.postMessage(message, {targetOrigin: event.origin})
const sender = {

piperDispatcher.dispatch(event.data, {
sendRequest(method, args) {
const id = String(Math.random())
send({to: "piper-service", type: "request", id, method, args})
return domDispatcher.waitForResponse(id)
send({from: "piper-host", to: "piper-service", type: "request", id, method, args})
return piperDispatcher.waitForResponse(id)
}
}
domDispatcher.dispatch(event.data, sender, send)
}, send)

fasttextDispatcher.dispatch(event.data, {
sendRequest(method, args) {
const id = String(Math.random())
send({from: "fasttext-host", to: "fasttext-service", type: "request", id, method, args})
return fasttextDispatcher.waitForResponse(id)
}
}, send)
})


Expand Down Expand Up @@ -346,3 +372,12 @@ function createPiperFrame() {
f.style.borderWidth = "0"
document.body.appendChild(f)
}

function createFasttextFrame() {
const f = document.createElement("iframe")
f.id = "fasttext-frame"
f.src = "https://ttstool.com/fasttext/index.html"
f.allow = "cross-origin-isolated"
f.style.display = "none"
document.body.appendChild(f)
}

0 comments on commit 5933f9a

Please sign in to comment.