Merge branch 'worktree-agent-af2efb541856af61f'
This commit is contained in:
@@ -1,13 +1,630 @@
|
||||
#include "../GoogleDemos.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include "../../fw/Router.hpp"
|
||||
#include "../../fw/Templates.hpp"
|
||||
#include "google/cloudai/calls.h"
|
||||
#include "google/cloudai/caps.h"
|
||||
#include "google/cloudai/document_ai.h"
|
||||
#include "google/cloudai/long_audio.h"
|
||||
#include "google/cloudai/natural_language.h"
|
||||
#include "google/cloudai/speech_v2.h"
|
||||
#include "google/cloudai/translation.h"
|
||||
#include "google/cloudai/video_intelligence.h"
|
||||
#include "google/cloudai/vision.h"
|
||||
#include "google/credentials.h"
|
||||
|
||||
namespace molo::example::app {
|
||||
|
||||
// Translation, Vision, Natural Language, Video Intelligence, Document AI. Stub: the package agent fills this in.
|
||||
// Cloud AI: Translation v3, Vision, Natural Language, Video Intelligence,
|
||||
// Document AI, Speech v2 and long-audio Text-to-Speech over the real
|
||||
// src/google/cloudai part. GET routes need no credential. Every route that
|
||||
// calls Google is a POST: 422 on bad input (before anything else), 503
|
||||
// google_not_configured without a credential. Paid calls (translate, annotate,
|
||||
// analyze, process, recognize, synthesize) and every write or delete are dry
|
||||
// runs that show the request unless the body says "confirm":true. Long
|
||||
// running starts answer the operation and never wait.
|
||||
|
||||
namespace {
|
||||
|
||||
namespace ca = google::cloudai;
|
||||
using Json = clients::body::Json;
|
||||
|
||||
constexpr std::size_t kMaxBody = 12 * 1024 * 1024;
|
||||
constexpr std::size_t kMaxShownBytes = 1024 * 1024;
|
||||
constexpr std::size_t kMaxPreviewString = 200;
|
||||
|
||||
const char kPage[] = R"CLOUDAI({% extends "base.html" %}
|
||||
{% block content %}
|
||||
<h1>Cloud AI</h1>
|
||||
<p class="hint">Translation, Vision, Natural Language, Video Intelligence, Document AI,
|
||||
Speech v2 and long-audio Text-to-Speech. Reads run at once. Paid calls and every write are
|
||||
dry runs that show the request until the body says <code>"confirm":true</code>.</p>
|
||||
<p id="ca-config" class="hint">Checking Google credentials...</p>
|
||||
<section class="ca">
|
||||
<label>Call
|
||||
<select id="ca-route"></select></label>
|
||||
<label>Body (JSON object) <textarea id="ca-body" rows="9"></textarea></label>
|
||||
<div class="ca-actions"><button id="ca-send" type="button" class="primary">Send</button></div>
|
||||
<p id="ca-error" class="error" role="alert"></p>
|
||||
<pre id="ca-out" class="ca-out"></pre>
|
||||
</section>
|
||||
<style>
|
||||
.ca label{display:block;margin:.5rem 0}
|
||||
.ca select,.ca textarea{width:100%;box-sizing:border-box}
|
||||
.ca-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin:.5rem 0}
|
||||
.ca-out{overflow:auto;max-height:24rem;white-space:pre-wrap;word-break:break-word}
|
||||
</style>
|
||||
<script src="/embedded/google_cloudai.js"></script>
|
||||
{% endblock %}
|
||||
)CLOUDAI";
|
||||
|
||||
fw::Response json_out(const Json& j, int status = 200) {
|
||||
std::string dumped = j.dump();
|
||||
if (dumped.size() > kMaxShownBytes) {
|
||||
Json cut = Json::object();
|
||||
cut.set("truncated", Json(true));
|
||||
cut.set("bytes", Json(static_cast<double>(dumped.size())));
|
||||
cut.set("preview", Json(dumped.substr(0, 64 * 1024)));
|
||||
fw::Response res = fw::JsonResponse(cut.dump(), status);
|
||||
res.headers.set("Cache-Control", "no-store");
|
||||
return res;
|
||||
}
|
||||
fw::Response res = fw::JsonResponse(dumped, status);
|
||||
res.headers.set("Cache-Control", "no-store");
|
||||
return res;
|
||||
}
|
||||
|
||||
[[noreturn]] void rethrow(const google::GoogleError& e) {
|
||||
if (e.http_status() == 503 && e.status() == "NOT_CONFIGURED")
|
||||
throw fw::HttpError(503, "google_not_configured", e.message());
|
||||
if (e.service().empty() && e.status() == "INVALID_ARGUMENT")
|
||||
throw fw::HttpError(422, "validation_error", e.message());
|
||||
throw fw::HttpError(502, "google_error", e.status() + ": " + e.message());
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
auto guarded(F&& f) -> decltype(f()) {
|
||||
try {
|
||||
return f();
|
||||
} catch (const google::GoogleError& e) {
|
||||
rethrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
[[noreturn]] void bad(const std::string& why) { throw fw::HttpError(422, "validation_error", why); }
|
||||
|
||||
Json parse_body(const fw::Request& req) {
|
||||
if (req.body.size() > kMaxBody) throw fw::HttpError(413, "payload_too_large", "body too large");
|
||||
if (req.body.empty()) return Json::object();
|
||||
try {
|
||||
Json j = Json::parse(req.body);
|
||||
if (!j.is_object()) bad("body must be a JSON object");
|
||||
return j;
|
||||
} catch (const clients::body::JsonError&) {
|
||||
bad("body is not valid JSON");
|
||||
}
|
||||
}
|
||||
|
||||
std::string text(const Json& body, const char* key, bool required = false) {
|
||||
const Json* v = body.find(key);
|
||||
if (!v || v->is_null()) {
|
||||
if (required) bad(std::string(key) + " is required");
|
||||
return "";
|
||||
}
|
||||
if (!v->is_string()) bad(std::string(key) + " must be a string");
|
||||
return v->as_string();
|
||||
}
|
||||
|
||||
bool flag(const Json& body, const char* key, bool fallback = false) {
|
||||
const Json* v = body.find(key);
|
||||
if (!v || v->is_null()) return fallback;
|
||||
if (!v->is_bool()) bad(std::string(key) + " must be true or false");
|
||||
return v->as_bool();
|
||||
}
|
||||
|
||||
int number(const Json& body, const char* key, int lo, int hi, int fallback) {
|
||||
const Json* v = body.find(key);
|
||||
if (!v || v->is_null()) return fallback;
|
||||
if (!v->is_number()) bad(std::string(key) + " must be a number");
|
||||
int n = static_cast<int>(v->as_int());
|
||||
if (n < lo || n > hi) bad(std::string(key) + " must be " + std::to_string(lo) + ".." + std::to_string(hi));
|
||||
return n;
|
||||
}
|
||||
|
||||
std::string pick(const Json& body, const char* key, std::initializer_list<const char*> allowed) {
|
||||
std::string v = text(body, key, true);
|
||||
std::string list;
|
||||
for (const char* a : allowed) {
|
||||
if (v == a) return v;
|
||||
list += (list.empty() ? "" : ", ") + std::string(a);
|
||||
}
|
||||
bad(std::string(key) + " must be one of " + list);
|
||||
}
|
||||
|
||||
std::vector<std::string> strings(const Json& body, const char* key, bool required) {
|
||||
std::vector<std::string> out;
|
||||
const Json* v = body.find(key);
|
||||
if (!v || v->is_null()) {
|
||||
if (required) bad(std::string(key) + " is required");
|
||||
return out;
|
||||
}
|
||||
if (!v->is_array()) bad(std::string(key) + " must be an array of strings");
|
||||
for (const Json& s : v->as_array()) {
|
||||
if (!s.is_string()) bad(std::string(key) + " must be an array of strings");
|
||||
out.push_back(s.as_string());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// Long strings (base64 documents) are not echoed back in a dry run.
|
||||
Json redacted(const Json& j) {
|
||||
if (j.is_string() && j.as_string().size() > kMaxPreviewString)
|
||||
return Json("<" + std::to_string(j.as_string().size()) + " characters>");
|
||||
if (j.is_array()) {
|
||||
Json a = Json::array();
|
||||
for (const Json& x : j.as_array()) a.push(redacted(x));
|
||||
return a;
|
||||
}
|
||||
if (j.is_object()) {
|
||||
Json o = Json::object();
|
||||
for (const auto& [k, v] : j.as_object()) o.set(k, redacted(v));
|
||||
return o;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
Json dry_run(const char* call, const Json& request) {
|
||||
Json o = Json::object();
|
||||
o.set("dry_run", Json(true));
|
||||
o.set("call", Json(call));
|
||||
o.set("request", redacted(request));
|
||||
o.set("hint", Json("send confirm:true to run it"));
|
||||
return o;
|
||||
}
|
||||
|
||||
Json operation_json(const google::Operation& op) {
|
||||
Json o = Json::object();
|
||||
o.set("operation", Json(op.name));
|
||||
o.set("done", Json(op.done));
|
||||
if (op.failed()) o.set("error", Json(op.error_message));
|
||||
if (op.done && op.response.is_object()) o.set("response", op.response);
|
||||
return o;
|
||||
}
|
||||
|
||||
// Everything a route needs: the client plus the validated project / location.
|
||||
class Rig {
|
||||
public:
|
||||
explicit Rig(std::shared_ptr<google::GoogleClient> client) : client_(std::move(client)) {}
|
||||
|
||||
google::CloudContext context(const Json& body, const char* default_location) const {
|
||||
google::CloudContext c;
|
||||
c.project = google::Credentials::env("GOOGLE_CLOUD_PROJECT");
|
||||
c.location = text(body, "location");
|
||||
if (c.location.empty()) c.location = default_location;
|
||||
if (!c.valid()) bad("GOOGLE_CLOUD_PROJECT is missing or the location is malformed");
|
||||
return c;
|
||||
}
|
||||
template <typename S>
|
||||
S make(const Json& body, const char* default_location) const {
|
||||
return S(client_, context(body, default_location));
|
||||
}
|
||||
const std::shared_ptr<google::GoogleClient>& client() const { return client_; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<google::GoogleClient> client_;
|
||||
};
|
||||
|
||||
ca::NlDocument nl_document(const Json& b) {
|
||||
ca::NlDocument d;
|
||||
d.content = text(b, "content");
|
||||
d.gcs_uri = text(b, "gcsUri");
|
||||
std::string type = text(b, "type");
|
||||
if (!type.empty()) d.type = type;
|
||||
d.language = text(b, "language");
|
||||
return d;
|
||||
}
|
||||
|
||||
std::vector<ca::VisionFeature> vision_features(const Json& b) {
|
||||
std::vector<ca::VisionFeature> out;
|
||||
const Json* v = b.find("features");
|
||||
if (!v || !v->is_array() || v->as_array().empty()) bad("features is required (array of {type,maxResults})");
|
||||
for (const Json& f : v->as_array()) {
|
||||
if (f.is_string()) {
|
||||
out.push_back({f.as_string(), 0});
|
||||
continue;
|
||||
}
|
||||
if (!f.is_object()) bad("each feature is a type string or {type,maxResults}");
|
||||
out.push_back({text(f, "type", true), number(f, "maxResults", 0, ca::CloudAiCaps::kMaxResults, 0)});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void register_google_cloudai_demo(fw::Router& router, Services& services) {
|
||||
(void)router;
|
||||
(void)services;
|
||||
if (!services.google) services.google = google::GoogleClient::from_env();
|
||||
auto client = services.google;
|
||||
fw::Environment* env = services.templates.get();
|
||||
services.templates->add_template("google_cloudai.html", kPage);
|
||||
services.add_feature("google-cloudai", "/google/cloudai");
|
||||
auto rig = std::make_shared<Rig>(client);
|
||||
|
||||
router.get("/google/cloudai", "google-cloudai-page", [env](fw::Request req) {
|
||||
(void)req;
|
||||
fw::TemplateCtx ctx;
|
||||
ctx["title"] = std::string("Cloud AI");
|
||||
return fw::Response(200, env->render("google_cloudai.html", ctx), "text/html; charset=utf-8");
|
||||
});
|
||||
|
||||
router.get("/google/cloudai/status", "google-cloudai-status", [client](fw::Request req) {
|
||||
(void)req;
|
||||
Json o = Json::object();
|
||||
o.set("configured", Json(client->configured()));
|
||||
o.set("project_set", Json(!google::Credentials::env("GOOGLE_CLOUD_PROJECT").empty()));
|
||||
o.set("calls", Json(static_cast<int>(ca::call_count())));
|
||||
Json apis = Json::array();
|
||||
for (const char* a : {"translate v3", "vision v1", "language v2", "videointelligence v1",
|
||||
"documentai v1", "speech v2", "texttospeech v1"})
|
||||
apis.push(Json(a));
|
||||
o.set("apis", apis);
|
||||
o.set("not_in_discovery", Json("mediatranslation"));
|
||||
Json caps = Json::object();
|
||||
caps.set("max_texts", Json(static_cast<int>(ca::CloudAiCaps::kMaxTexts)));
|
||||
caps.set("max_text_chars", Json(static_cast<int>(ca::CloudAiCaps::kMaxTextChars)));
|
||||
caps.set("max_images", Json(static_cast<int>(ca::CloudAiCaps::kMaxImages)));
|
||||
caps.set("max_inline_bytes", Json(static_cast<int>(ca::CloudAiCaps::kMaxAudioBytes)));
|
||||
o.set("caps", caps);
|
||||
return json_out(o);
|
||||
});
|
||||
|
||||
// The Discovery-checked call table, for humans.
|
||||
router.get("/google/cloudai/calls", "google-cloudai-calls", [](fw::Request req) {
|
||||
(void)req;
|
||||
Json arr = Json::array();
|
||||
for (std::size_t i = 0; i < ca::call_count(); ++i) {
|
||||
const ca::CallSpec& s = ca::spec_at(i);
|
||||
Json row = Json::object();
|
||||
row.set("api", Json(std::string(s.api) + " " + s.version));
|
||||
row.set("http", Json(s.http_method));
|
||||
row.set("path", Json(s.path));
|
||||
row.set("rpc", Json(s.rpc_id));
|
||||
arr.push(row);
|
||||
}
|
||||
Json o = Json::object();
|
||||
o.set("calls", arr);
|
||||
return json_out(o);
|
||||
});
|
||||
|
||||
// --- Translation -----------------------------------------------------------
|
||||
router.post("/google/cloudai/translate", "google-cloudai-translate", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
ca::TranslateRequest r;
|
||||
r.texts = strings(b, "texts", false);
|
||||
if (r.texts.empty()) r.texts.push_back(text(b, "text", true));
|
||||
r.target = text(b, "target", true);
|
||||
r.source = text(b, "source");
|
||||
r.mime_type = text(b, "mimeType");
|
||||
r.model = text(b, "model");
|
||||
r.glossary = text(b, "glossary");
|
||||
r.glossary_ignore_case = flag(b, "glossaryIgnoreCase");
|
||||
bool confirm = flag(b, "confirm");
|
||||
auto t = rig->make<ca::Translation>(b, "global");
|
||||
return guarded([&] {
|
||||
Json body = t.translate_body(r);
|
||||
if (!confirm) return json_out(dry_run("translateText", body));
|
||||
Json arr = Json::array();
|
||||
for (const auto& x : t.translate(r)) {
|
||||
Json o = Json::object();
|
||||
o.set("text", Json(x.text));
|
||||
o.set("detectedLanguage", Json(x.detected_language));
|
||||
o.set("model", Json(x.model));
|
||||
arr.push(o);
|
||||
}
|
||||
Json o = Json::object();
|
||||
o.set("translations", arr);
|
||||
return json_out(o);
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/cloudai/translate/detect", "google-cloudai-detect", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string content = text(b, "text", true);
|
||||
bool confirm = flag(b, "confirm");
|
||||
auto t = rig->make<ca::Translation>(b, "global");
|
||||
return guarded([&] {
|
||||
if (!confirm) {
|
||||
Json body = Json::object();
|
||||
body.set("content", Json(content));
|
||||
return json_out(dry_run("detectLanguage", body));
|
||||
}
|
||||
return json_out(t.detect_language(content));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/cloudai/translate/list", "google-cloudai-translate-list", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string what = pick(b, "what", {"languages", "glossaries", "adaptiveDatasets", "models", "datasets", "locations"});
|
||||
std::string display = text(b, "displayLanguage");
|
||||
auto t = rig->make<ca::Translation>(b, "global");
|
||||
return guarded([&] {
|
||||
if (what == "languages") return json_out(t.supported_languages(display));
|
||||
Json o = Json::object();
|
||||
if (what == "glossaries") o.set("glossaries", t.list_glossaries());
|
||||
else if (what == "adaptiveDatasets") o.set("adaptiveMtDatasets", t.list_adaptive_datasets());
|
||||
else if (what == "models") o.set("models", t.list_models());
|
||||
else if (what == "datasets") o.set("datasets", t.list_datasets());
|
||||
else o.set("locations", t.list_locations());
|
||||
return json_out(o);
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/cloudai/translate/glossary", "google-cloudai-glossary", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string action = pick(b, "action", {"get", "create", "delete"});
|
||||
std::string id = text(b, "id", true);
|
||||
ca::GlossaryDraft d;
|
||||
d.id = id;
|
||||
d.input_uri = text(b, "inputUri");
|
||||
d.source_language = text(b, "source");
|
||||
d.target_language = text(b, "target");
|
||||
d.language_codes = strings(b, "languageCodes", false);
|
||||
bool confirm = flag(b, "confirm");
|
||||
auto t = rig->make<ca::Translation>(b, "global");
|
||||
return guarded([&] {
|
||||
if (action == "get") return json_out(t.get_glossary(id));
|
||||
if (action == "create") {
|
||||
Json body = t.glossary_body(d);
|
||||
if (!confirm) return json_out(dry_run("glossaries.create", body));
|
||||
return json_out(operation_json(t.create_glossary_start(d)), 202);
|
||||
}
|
||||
t.glossary_name(id);
|
||||
if (!confirm) {
|
||||
Json target = Json::object();
|
||||
target.set("glossary", Json(t.glossary_name(id)));
|
||||
return json_out(dry_run("glossaries.delete", target));
|
||||
}
|
||||
return json_out(operation_json(t.delete_glossary_start(id)), 202);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Vision ----------------------------------------------------------------
|
||||
router.post("/google/cloudai/vision/annotate", "google-cloudai-vision", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::vector<ca::VisionImage> images;
|
||||
const Json* arr = b.find("images");
|
||||
if (!arr || !arr->is_array() || arr->as_array().empty()) bad("images is required (array of {content|uri})");
|
||||
for (const Json& im : arr->as_array()) {
|
||||
if (!im.is_object()) bad("each image is {content|uri}");
|
||||
images.push_back({text(im, "content"), text(im, "uri")});
|
||||
}
|
||||
auto features = vision_features(b);
|
||||
bool confirm = flag(b, "confirm");
|
||||
auto v = rig->make<ca::Vision>(b, "us-west1");
|
||||
return guarded([&] {
|
||||
Json body = v.annotate_body(images, features);
|
||||
if (!confirm) return json_out(dry_run("images.annotate", body));
|
||||
return json_out(v.annotate(images, features));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/cloudai/vision/list", "google-cloudai-vision-list", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string what = pick(b, "what", {"productSets", "products", "referenceImages"});
|
||||
std::string product = what == "referenceImages" ? text(b, "productId", true) : "";
|
||||
auto v = rig->make<ca::Vision>(b, "us-west1");
|
||||
return guarded([&] {
|
||||
Json o = Json::object();
|
||||
if (what == "productSets") o.set("productSets", v.list_product_sets());
|
||||
else if (what == "products") o.set("products", v.list_products());
|
||||
else o.set("referenceImages", v.list_reference_images(product));
|
||||
return json_out(o);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Natural Language ------------------------------------------------------
|
||||
router.post("/google/cloudai/language/analyze", "google-cloudai-language", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string kind = pick(b, "kind", {"sentiment", "entities", "classify", "moderate", "annotate", "syntax", "entitySentiment"});
|
||||
ca::NlDocument d = nl_document(b);
|
||||
std::string encoding = text(b, "encoding");
|
||||
ca::NlFeatures f;
|
||||
if (kind == "annotate") {
|
||||
f.entities = flag(b, "extractEntities");
|
||||
f.sentiment = flag(b, "extractSentiment");
|
||||
f.classify = flag(b, "classifyText");
|
||||
f.moderate = flag(b, "moderateText");
|
||||
}
|
||||
bool confirm = flag(b, "confirm");
|
||||
auto nl = rig->make<ca::NaturalLanguage>(b, "global");
|
||||
return guarded([&] {
|
||||
Json doc = nl.document_json(d); // validates before any dry run
|
||||
if (kind == "annotate" && !f.entities && !f.sentiment && !f.classify && !f.moderate)
|
||||
bad("annotate needs one of extractEntities, extractSentiment, classifyText, moderateText");
|
||||
if (!confirm) {
|
||||
Json body = Json::object();
|
||||
body.set("document", doc);
|
||||
return json_out(dry_run(kind.c_str(), body));
|
||||
}
|
||||
if (kind == "sentiment") return json_out(nl.analyze_sentiment(d, encoding));
|
||||
if (kind == "entities") return json_out(nl.analyze_entities(d, encoding));
|
||||
if (kind == "classify") return json_out(nl.classify_text(d));
|
||||
if (kind == "moderate") return json_out(nl.moderate_text(d));
|
||||
if (kind == "syntax") return json_out(nl.analyze_syntax(d, encoding));
|
||||
if (kind == "entitySentiment") return json_out(nl.analyze_entity_sentiment(d, encoding));
|
||||
return json_out(nl.annotate_text(d, f, encoding));
|
||||
});
|
||||
});
|
||||
|
||||
// --- Video Intelligence ----------------------------------------------------
|
||||
router.post("/google/cloudai/video/annotate", "google-cloudai-video", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
ca::VideoRequest r;
|
||||
r.input_uri = text(b, "inputUri");
|
||||
r.input_content_base64 = text(b, "content");
|
||||
r.features = strings(b, "features", true);
|
||||
r.output_uri = text(b, "outputUri");
|
||||
r.location_id = text(b, "locationId");
|
||||
bool confirm = flag(b, "confirm");
|
||||
auto vi = rig->make<ca::VideoIntelligence>(b, "us-east1");
|
||||
return guarded([&] {
|
||||
Json body = vi.annotate_body(r);
|
||||
if (!confirm) return json_out(dry_run("videos.annotate", body));
|
||||
return json_out(operation_json(vi.annotate_start(r)), 202);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Document AI -----------------------------------------------------------
|
||||
router.post("/google/cloudai/documentai/process", "google-cloudai-docai-process", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
ca::ProcessRequest r;
|
||||
r.processor_id = text(b, "processorId", true);
|
||||
r.version_id = text(b, "versionId");
|
||||
r.content_base64 = text(b, "content");
|
||||
r.gcs_uri = text(b, "gcsUri");
|
||||
r.mime_type = text(b, "mimeType", true);
|
||||
r.skip_human_review = flag(b, "skipHumanReview", true);
|
||||
r.field_mask = text(b, "fieldMask");
|
||||
bool confirm = flag(b, "confirm");
|
||||
auto d = rig->make<ca::DocumentAi>(b, "us");
|
||||
return guarded([&] {
|
||||
Json body = d.process_body(r);
|
||||
d.process_target(r);
|
||||
if (!confirm) return json_out(dry_run("processors.process", body));
|
||||
return json_out(d.process(r));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/cloudai/documentai/list", "google-cloudai-docai-list", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string what = pick(b, "what", {"processors", "processorTypes", "fetchProcessorTypes", "versions", "schemas", "locations"});
|
||||
std::string processor = what == "versions" ? text(b, "processorId", true) : "";
|
||||
auto d = rig->make<ca::DocumentAi>(b, "us");
|
||||
return guarded([&] {
|
||||
Json o = Json::object();
|
||||
if (what == "processors") o.set("processors", d.list_processors());
|
||||
else if (what == "processorTypes") o.set("processorTypes", d.list_processor_types());
|
||||
else if (what == "fetchProcessorTypes") return json_out(d.fetch_processor_types());
|
||||
else if (what == "versions") o.set("processorVersions", d.list_versions(processor));
|
||||
else if (what == "schemas") o.set("schemas", d.list_schemas());
|
||||
else o.set("locations", d.list_locations());
|
||||
return json_out(o);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Speech v2 -------------------------------------------------------------
|
||||
router.post("/google/cloudai/speech/recognize", "google-cloudai-speech", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
ca::SpeechRequest r;
|
||||
std::string rec = text(b, "recognizer");
|
||||
if (!rec.empty()) r.recognizer = rec;
|
||||
r.content_base64 = text(b, "content");
|
||||
r.uri = text(b, "uri");
|
||||
std::string model = text(b, "model");
|
||||
if (!model.empty()) r.model = model;
|
||||
r.languages = strings(b, "languages", true);
|
||||
bool confirm = flag(b, "confirm");
|
||||
auto s = rig->make<ca::SpeechV2>(b, "us-central1");
|
||||
return guarded([&] {
|
||||
Json body = s.recognize_body(r);
|
||||
s.recognizer_name(r.recognizer);
|
||||
if (!confirm) return json_out(dry_run("recognizers.recognize", body));
|
||||
return json_out(s.recognize(r));
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/cloudai/speech/list", "google-cloudai-speech-list", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string what = pick(b, "what", {"recognizers", "phraseSets", "customClasses", "config", "locations"});
|
||||
auto s = rig->make<ca::SpeechV2>(b, "us-central1");
|
||||
return guarded([&] {
|
||||
Json o = Json::object();
|
||||
if (what == "recognizers") o.set("recognizers", s.list_recognizers());
|
||||
else if (what == "phraseSets") o.set("phraseSets", s.list_phrase_sets());
|
||||
else if (what == "customClasses") o.set("customClasses", s.list_custom_classes());
|
||||
else if (what == "config") return json_out(s.get_config());
|
||||
else o.set("locations", s.list_locations());
|
||||
return json_out(o);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Text-to-Speech long audio ---------------------------------------------
|
||||
router.post("/google/cloudai/tts/long", "google-cloudai-tts-long", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
ca::LongAudioRequest r;
|
||||
r.text = text(b, "text", true);
|
||||
r.ssml = flag(b, "ssml");
|
||||
r.language = text(b, "language", true);
|
||||
r.voice_name = text(b, "voice");
|
||||
std::string enc = text(b, "encoding");
|
||||
if (!enc.empty()) r.encoding = enc;
|
||||
r.output_uri = text(b, "outputUri", true);
|
||||
bool confirm = flag(b, "confirm");
|
||||
auto la = rig->make<ca::LongAudio>(b, "global");
|
||||
return guarded([&] {
|
||||
Json body = la.request_body(r);
|
||||
if (!confirm) return json_out(dry_run("synthesizeLongAudio", body));
|
||||
return json_out(operation_json(la.synthesize_start(r)), 202);
|
||||
});
|
||||
});
|
||||
|
||||
router.post("/google/cloudai/tts/voices", "google-cloudai-tts-voices", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string language = text(b, "language");
|
||||
auto la = rig->make<ca::LongAudio>(b, "global");
|
||||
return guarded([&] { return json_out(la.voices(language)); });
|
||||
});
|
||||
|
||||
// --- Operations (read) -----------------------------------------------------
|
||||
router.post("/google/cloudai/operation", "google-cloudai-operation", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string api = pick(b, "api", {"translate", "vision", "video", "documentai", "speech", "tts"});
|
||||
std::string name = text(b, "name", true);
|
||||
auto s = rig->make<ca::CloudAiService>(b, "global");
|
||||
ca::Call get = api == "translate" ? ca::Call::kTranslateLocationsOperationsGet
|
||||
: api == "vision" ? ca::Call::kVisionProjectsLocationsOperationsGet
|
||||
: api == "video" ? ca::Call::kVideoLocationsOperationsGet
|
||||
: api == "documentai" ? ca::Call::kDocumentaiLocationsOperationsGet
|
||||
: api == "speech" ? ca::Call::kSpeechLocationsOperationsGet
|
||||
: ca::Call::kTtsLocationsOperationsGet;
|
||||
return guarded([&] { return json_out(operation_json(s.operation(get, name))); });
|
||||
});
|
||||
|
||||
// --- Any row of the table --------------------------------------------------
|
||||
// Reads (GET) run at once; every other method is a dry run until confirm.
|
||||
router.post("/google/cloudai/call", "google-cloudai-call", [rig](fw::Request req) {
|
||||
Json b = parse_body(req);
|
||||
std::string rpc = text(b, "rpc", true);
|
||||
const ca::CallSpec* spec = ca::find_rpc(rpc);
|
||||
if (!spec) bad("unknown rpc; see GET /google/cloudai/calls");
|
||||
std::string name = text(b, "name");
|
||||
const Json* body = b.find("body");
|
||||
if (body && body->is_null()) body = nullptr;
|
||||
ca::Query query;
|
||||
if (const Json* q = b.find("query")) {
|
||||
if (!q->is_object()) bad("query must be an object of strings");
|
||||
for (const auto& [k, v] : q->as_object()) {
|
||||
if (!v.is_string()) bad("query values must be strings");
|
||||
query.emplace_back(k, v.as_string());
|
||||
}
|
||||
}
|
||||
bool confirm = flag(b, "confirm");
|
||||
ca::Call call = spec->call;
|
||||
auto s = rig->make<ca::CloudAiService>(b, "global");
|
||||
return guarded([&] {
|
||||
std::string path = ca::render(call, name) + ca::query_of(call, query);
|
||||
ca::check_body(call, body);
|
||||
bool read = std::string(spec->http_method) == "GET";
|
||||
if (!read && !confirm) {
|
||||
Json o = dry_run(spec->rpc_id, body ? *body : Json::object());
|
||||
o.set("http", Json(spec->http_method));
|
||||
o.set("url", Json(ca::endpoint_of(call, name).base_url + path));
|
||||
return json_out(o);
|
||||
}
|
||||
return json_out(s.invoke(call, name, body, query));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace molo::example::app
|
||||
|
||||
Reference in New Issue
Block a user