Rust and WebAssembly Serverless functions in Vercel

In this article, we will show you two serverless functions in Rust and WasmEdge deployed on Vercel. One is the image processing function, the other one is the TensorFlow inference function.

For more insights on why WasmEdge on Vercel, please refer to the article Rust and WebAssembly Serverless Functions in Vercel.

Prerequisite

Since our demo WebAssembly functions are written in Rust, you will need a Rust compiler. Make sure that you install the wasm32-wasi compiler target as follows, in order to generate WebAssembly bytecode.

rustup target add wasm32-wasi

The demo application front end is written in Next.js, and deployed on Vercel. We will assume that you already have the basic knowledge of how to work with Vercel.

Example 1: Image processing

Our first demo application allows users to upload an image and then invoke a serverless function to turn it into black and white. A live demo deployed on Vercel is available.

Fork the demo application’s GitHub repo to get started. To deploy the application on Vercel, just import the Github repo from Vercel for Github web page.

This repo is a standard Next.js application for the Vercel platform. The backend serverless function is in the api/functions/image_grayscale folder. The src/main.rs file contains the Rust program’s source code. The Rust program reads image data from the STDIN, and then outputs the black-white image to the STDOUT.

  1. use hex;
  2. use std::io::{self, Read};
  3. use image::{ImageOutputFormat, ImageFormat};
  4. fn main() {
  5. let mut buf = Vec::new();
  6. io::stdin().read_to_end(&mut buf).unwrap();
  7. let image_format_detected: ImageFormat = image::guess_format(&buf).unwrap();
  8. let img = image::load_from_memory(&buf).unwrap();
  9. let filtered = img.grayscale();
  10. let mut buf = vec![];
  11. match image_format_detected {
  12. ImageFormat::Gif => {
  13. filtered.write_to(&mut buf, ImageOutputFormat::Gif).unwrap();
  14. },
  15. _ => {
  16. filtered.write_to(&mut buf, ImageOutputFormat::Png).unwrap();
  17. },
  18. };
  19. io::stdout().write_all(&buf).unwrap();
  20. io::stdout().flush().unwrap();
  21. }

You can use Rust’s cargo tool to build the Rust program into WebAssembly bytecode or native code.

cd api/functions/image-grayscale/ cargo build --release --target wasm32-wasi

Copy the build artifacts to the api folder.

cp target/wasm32-wasi/release/grayscale.wasm ../../

Vercel runs api/pre.sh upon setting up the serverless environment. It installs the WasmEdge runtime, and then compiles each WebAssembly bytecode program into a native so library for faster execution.

The api/hello.js file conforms Vercel serverless specification. It loads the WasmEdge runtime, starts the compiled WebAssembly program in WasmEdge, and passes the uploaded image data via STDIN. Notice api/hello.js runs the compiled grayscale.so file generated by api/pre.sh for better performance.

const fs = require('fs'); const { spawn } = require('child_process'); const path = require('path'); module.exports = (req, res) => { const wasmedge = spawn( path.join(__dirname, 'wasmedge'), [path.join(__dirname, 'grayscale.so')]); let d = []; wasmedge.stdout.on('data', (data) => { d.push(data); }); wasmedge.on('close', (code) => { let buf = Buffer.concat(d); res.setHeader('Content-Type', req.headers['image-type']); res.send(buf); }); wasmedge.stdin.write(req.body); wasmedge.stdin.end(''); }

That’s it. Deploy the repo to Vercel and you now have a Vercel Jamstack app with a high-performance Rust and WebAssembly based serverless backend.

Example 2: AI inference

The second demo application allows users to upload an image and then invoke a serverless function to classify the main subject on the image.

It is in the same GitHub repo as the previous example but in the tensorflow branch. Note: when you import this GitHub repo on the Vercel website, it will create a preview URL for each branch. The tensorflow branch would have its own deployment URL.

The backend serverless function for image classification is in the api/functions/image-classification folder in the tensorflow branch. The src/main.rs file contains the Rust program’s source code. The Rust program reads image data from the STDIN, and then outputs the text output to the STDOUT. It utilizes the WasmEdge Tensorflow API to run the AI inference.

  1. pub fn main() {
  2. // Step 1: Load the TFLite model
  3. let model_data: &[u8] = include_bytes!("models/mobilenet_v1_1.0_224/mobilenet_v1_1.0_224_quant.tflite");
  4. let labels = include_str!("models/mobilenet_v1_1.0_224/labels_mobilenet_quant_v1_224.txt");
  5. // Step 2: Read image from STDIN
  6. let mut buf = Vec::new();
  7. io::stdin().read_to_end(&mut buf).unwrap();
  8. // Step 3: Resize the input image for the tensorflow model
  9. let flat_img = wasmedge_tensorflow_interface::load_jpg_image_to_rgb8(&buf, 224, 224);
  10. // Step 4: AI inference
  11. let mut session = wasmedge_tensorflow_interface::Session::new(&model_data, wasmedge_tensorflow_interface::ModelType::TensorFlowLite);
  12. session.add_input("input", &flat_img, &[1, 224, 224, 3])
  13. .run();
  14. let res_vec: Vec<u8> = session.get_output("MobilenetV1/Predictions/Reshape_1");
  15. // Step 5: Find the food label that responds to the highest probability in res_vec
  16. // ... ...
  17. let mut label_lines = labels.lines();
  18. for _i in 0..max_index {
  19. label_lines.next();
  20. }
  21. // Step 6: Generate the output text
  22. let class_name = label_lines.next().unwrap().to_string();
  23. if max_value > 50 {
  24. println!("It {} a <a href='https://www.google.com/search?q={}'>{}</a> in the picture", confidence.to_string(), class_name, class_name);
  25. } else {
  26. println!("It does not appears to be any food item in the picture.");
  27. }
  28. }

You can use the cargo tool to build the Rust program into WebAssembly bytecode or native code.

cd api/functions/image-classification/ cargo build --release --target wasm32-wasi

Copy the build artifacts to the api folder.

cp target/wasm32-wasi/release/classify.wasm ../../

Again, the api/pre.sh script installs WasmEdge runtime and its Tensorflow dependencies in this application. It also compiles the classify.wasm bytecode program to the classify.so native shared library at the time of deployment.

The api/hello.js file conforms Vercel serverless specification. It loads the WasmEdge runtime, starts the compiled WebAssembly program in WasmEdge, and passes the uploaded image data via STDIN. Notice api/hello.js runs the compiled classify.so file generated by api/pre.sh for better performance.

const fs = require('fs'); const { spawn } = require('child_process'); const path = require('path'); module.exports = (req, res) => { const wasmedge = spawn( path.join(__dirname, 'wasmedge-tensorflow-lite'), [path.join(__dirname, 'classify.so')], {env: {'LD_LIBRARY_PATH': __dirname}} ); let d = []; wasmedge.stdout.on('data', (data) => { d.push(data); }); wasmedge.on('close', (code) => { res.setHeader('Content-Type', `text/plain`); res.send(d.join('')); }); wasmedge.stdin.write(req.body); wasmedge.stdin.end(''); }

You can now deploy your forked repo to Vercel and have a web app for subject classification.

Next, it’s your turn to use the vercel-wasm-runtime repo as a template to develop your own Rust serverless functions in Vercel. Looking forward to your great work.