67 lines
3.7 KiB
JavaScript
67 lines
3.7 KiB
JavaScript
// Cloud AI page: thin client over /google/cloudai/* (all limits server side).
|
|||
|
|
(function () {
|
||
|
|
"use strict";
|
||
|
|
var $ = function (id) { return document.getElementById(id); };
|
||
|
|
var out = $("ca-out"), err = $("ca-error"), sel = $("ca-route"), box = $("ca-body");
|
||
|
|
|
||
|
|
// One example body per route. Paid calls and writes stay dry runs until
|
||
|
|
// "confirm": true is added.
|
||
|
|
var routes = [
|
||
|
|
["translate", "Translate text (paid)", { texts: ["Hello world"], target: "nl" }],
|
||
|
|
["translate/detect", "Detect language (paid)", { text: "Bonjour tout le monde" }],
|
||
|
|
["translate/list", "Translation: languages, glossaries, models, datasets", { what: "languages", displayLanguage: "en" }],
|
||
|
|
["translate/glossary", "Translation: glossary get, create, delete", { action: "get", id: "GLOSSARY_ID" }],
|
||
|
|
["vision/annotate", "Vision: annotate images (paid)", { images: [{ uri: "gs://BUCKET/image.png" }], features: [{ type: "LABEL_DETECTION", maxResults: 5 }] }],
|
||
|
|
["vision/list", "Vision: product sets, products", { what: "productSets" }],
|
||
|
|
["language/analyze", "Natural Language: analyze text (paid)", { kind: "sentiment", content: "I love this product." }],
|
||
|
|
["video/annotate", "Video Intelligence: annotate (paid, starts an operation)", { inputUri: "gs://BUCKET/video.mp4", features: ["LABEL_DETECTION"] }],
|
||
|
|
["documentai/process", "Document AI: process (paid)", { processorId: "PROCESSOR_ID", gcsUri: "gs://BUCKET/doc.pdf", mimeType: "application/pdf" }],
|
||
|
|
["documentai/list", "Document AI: processors, types, versions", { what: "processors" }],
|
||
|
|
["speech/recognize", "Speech v2: recognize (paid)", { languages: ["en-US"], uri: "gs://BUCKET/audio.wav", model: "chirp_2" }],
|
||
|
|
["speech/list", "Speech v2: recognizers, phrase sets", { what: "recognizers" }],
|
||
|
|
["tts/long", "Text-to-Speech: long audio (paid, starts an operation)", { text: "Hello there.", language: "en-US", outputUri: "gs://BUCKET/out.wav" }],
|
||
|
|
["tts/voices", "Text-to-Speech: voices", { language: "en-US" }],
|
||
|
|
["operation", "Read an operation", { api: "video", name: "projects/P/locations/us-east1/operations/ID" }],
|
||
|
|
["call", "Any call of the table (see /google/cloudai/calls)", { rpc: "translate.projects.locations.glossaries.list", name: "projects/P/locations/global" }]
|
||
|
|
];
|
||
|
|
routes.forEach(function (r) {
|
||
|
|
var o = document.createElement("option");
|
||
|
|
o.value = r[0];
|
||
|
|
o.textContent = r[1];
|
||
|
|
sel.appendChild(o);
|
||
|
|
});
|
||
|
|
function fill() {
|
||
|
|
for (var i = 0; i < routes.length; i++) {
|
||
|
|
if (routes[i][0] === sel.value) { box.value = JSON.stringify(routes[i][2], null, 1); return; }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
sel.onchange = fill;
|
||
|
|
fill();
|
||
|
|
|
||
|
|
fetch("/google/cloudai/status").then(function (r) { return r.json(); }).then(function (s) {
|
||
|
|
$("ca-config").textContent = s.configured
|
||
|
|
? "Google credential found" + (s.project_set ? "." : "; set GOOGLE_CLOUD_PROJECT.")
|
||
|
|
: "No Google credential: calls answer 503 until one is configured.";
|
||
|
|
});
|
||
|
|
|
||
|
|
$("ca-send").onclick = function () {
|
||
|
|
var body;
|
||
|
|
err.textContent = "";
|
||
|
|
try { body = JSON.parse(box.value || "{}"); }
|
||
|
|
catch (e) { err.textContent = "Body is not valid JSON"; return; }
|
||
|
|
out.textContent = "...";
|
||
|
|
fetch("/google/cloudai/" + sel.value, {
|
||
|
|
method: "POST",
|
||
|
|
headers: { "Content-Type": "application/json" },
|
||
|
|
body: JSON.stringify(body)
|
||
|
|
}).then(function (res) {
|
||
|
|
return res.text().then(function (t) {
|
||
|
|
var j;
|
||
|
|
try { j = JSON.parse(t); } catch (e) { j = { message: t }; }
|
||
|
|
if (!res.ok) { err.textContent = j.message || ("HTTP " + res.status); out.textContent = ""; }
|
||
|
|
else { out.textContent = JSON.stringify(j, null, 2); }
|
||
|
|
});
|
||
|
|
}).catch(function (e) { err.textContent = String(e); out.textContent = ""; });
|
||
|
|
};
|
||
|
|
})();
|