1import * as GCP from "@/GCP";
2import * as Effect from "effect/Effect";
3import { type Probe, serveProbes } from "../../bindingHost.ts";
4import { region, runVersionLifecycle } from "../common.ts";
5
6export const Classifier = GCP.ML.Model("Classifier", {
7 description: "binding probe",
8 regions: [region],
9});
10
11/** Needs a trained SavedModel at GCP_TEST_ML_DEPLOYMENT_URI. */
12export const V1 = Effect.gen(function* () {
13 const model = yield* Classifier;
14 return yield* GCP.ML.ModelsVersion("V1", {
15 model: model.name,
16 deploymentUri: process.env.GCP_TEST_ML_DEPLOYMENT_URI ?? "",
17 runtimeVersion: "2.11",
18 pythonVersion: "3.7",
19 framework: "TENSORFLOW",
20 });
21});
22
23/**
24 * Effect-native Cloud Run service exercising every AI Platform (legacy ML)
25 * binding as its own runtime service account. The version (and its
26 * GetVersion binding) is only declared when `runVersionLifecycle` is set.
27 * Deployed from {@link ../Bindings.test.ts}.
28 */
29export default class MlBindingsHost extends GCP.Function<MlBindingsHost>()(
30 "MlBindingsHost",
31 { main: import.meta.url, invokerIamDisabled: true },
32 Effect.gen(function* () {
33 const model = yield* Classifier;
34 const getModel = yield* GCP.ML.GetModel(model);
35 const predict = yield* GCP.ML.Predict(model);
36 const versionRoutes: Record<string, Probe> = runVersionLifecycle
37 ? { getVersion: (yield* GCP.ML.GetVersion(yield* V1))() }
38 : {};
39
40 return {
41 fetch: serveProbes({
42 getModel: getModel(),
43 predict: predict({
44 body: {
45 httpBody: {
46 contentType: "application/json",
47 data: btoa(JSON.stringify({ instances: [{ f1: 1 }] })),
48 },
49 },
50 }),
51 ...versionRoutes,
52 }),
53 };
54 }).pipe(
55 Effect.provide(GCP.ML.GetModelHttp),
56 Effect.provide(GCP.ML.PredictHttp),
57 Effect.provide(GCP.ML.GetVersionHttp),
58 ),
59) {}