text: const path = require("path");
  const tf = require("@tensorflow/tfjs");
  const mobilenet = require("@tensorflow-models/mobilenet");
  const http = require("http");
  const multer = require("multer");
  const jpeg = require("jpeg-js");
  
  // required because tfjs assumes a browser
  // environment and therefore the availability of fetch()
  global.fetch = require("node-fetch");
  
  const TF_NUM_CHANNELS = 3;
  
  const storage = multer.memoryStorage();
  const upload = multer({
    storage,
    limits: {
      files: 1,
      fileSize: 1024 * 1024,
    },
  });
  
  const imageByteArray = (image, numChannels) => {
    const pixels = image.data;
    const numPixels = image.width * image.height;
    const values = new Int32Array(numPixels * numChannels);
  
    for (let i = 0; i < numPixels; i++) {
      for (let channel = 0; channel < numChannels; ++channel) {
        values[i * numChannels + channel] = pixels[i * 4 + channel];
      }
    }
  
    return values;
  };
  
  const imageToInput = (image, numChannels) => {
    const values = imageByteArray(image, numChannels);
    const outShape = [image.height, image.width, numChannels];
    const input = tf.tensor3d(values, outShape, "int32");
  
    return input;
  };
  
  (async () => {
    // Local file:// urls are not supported by tfjs, although
    // the library could be extended to support them.
    const model = await mobilenet.load({
      inputRange: [0, 1],
      version: 1,
      alpha: 1.0,
      modelUrl: `https://storage.googleapis.com/mobilenet_v2/checkpoints/mobilenet_v2_1.0_224.tgz`,
    });
  
    http
      .createServer(async (req, res) => {
        // Execute multer middleware manually since we don't have express
        // for this example PoC.
        await upload.single("file")(req, res, async (err) => {
          try {
            if (!req.file) throw new Error("No file");
            const tfInput = imageToInput(
              jpeg.decode(req.file.buffer),
              TF_NUM_CHANNELS
            );
            const prediction = await model.classify(tfInput);
  
            res.writeHead(200, { "Content-Type": "application/json" });
            res.end(JSON.stringify(prediction));
          } catch (err) {
            console.error(err);
            res.writeHead(400, { "Content-Type": "application/json" });
            res.end(JSON.stringify(err));
          }
        });
      })
      .listen(8080, "0.0.0.0");
  
    console.info("Server running at http://127.0.0.1:8080/");
  })();
  