Embedded Inputs

Embed inputs within inference requests sent to Modzy

The examples below demonstrate how to embed files, such as images, in inference submissions sent to models hosted by Modzy. Each of these examples submit the same input image to Modzy's Image-Based Geolocation model. Before you can use the examples below, you will need to make the following replacements:

  • Replace API_KEY with a valid API key string
  • If necessary, replace https://app.modzy.com/ with the URL of your instance of Modzy

Single Input

import json
from modzy import ApiClient
from modzy._util import file_to_bytes

#Initialize your Modzy client
client = ApiClient(base_url="https://app.modzy.com/api", api_key=API_KEY)

# Embedded input as a string in Base64
image_bytes = file_to_bytes('images/tower-bridge.jpg')
# Prepare the source dictionary
sources = {"tower-bridge": {"image": image_bytes}}

#Once you are ready, submit the job to v1.0.1 of the imaged-based geolocation model
job = client.jobs.submit_embedded("aevbu1h3yw", "1.0.1", sources)

#Print the response from your job submission
print(json.dumps(job))
const fs = require('fs');
const modzy = require("@modzy/modzy-sdk");

//Initialize the client
const modzyClient = new modzy.ModzyClient("https://app.modzy.com/api", API_KEY)

//Base64 encode an input and add it to the list of sources
const imageBytes = fs.readFileSync('images/tower-bridge.jpg');
let sources = { "tower-bridge": { "image": imageBytes } };

//Once you are ready, submit the job
modzyClient
  .submitJobEmbedded("aevbu1h3yw", "1.0.1", "application/octet-stream", sources)
  .then(
    (job)=>{
      console.log(JSON.stringify(job));
    }
  )
  .catch(
    (error)=>{
      console.error("Modzy job submission failed with code " + error.code + " and message " + error.message);
    }
  );
package main

import (
    "context"
    "log"
    "time"

    modzy "github.com/modzy/sdk-go"
)

func main() {
    ctx := context.TODO()
  // Replace BASE_URL and API_KEY with valid values
    baseURL := "https://app.modzy.com"
    apiKey := API_KEY

  // Initialize the API client
    client := modzy.NewClient(baseURL).WithAPIKey(apiKey)

    imagePath := "images/tower-bridge.jpg"
    mapSource := map[string]modzy.EmbeddedInputItem{
        "tower-bridge": {
            "image": modzy.URIEncodeFile(imagePath, "image/jpeg"),
        },
    }

    submitResponse, err := client.Jobs().SubmitJobEmbedded(ctx, &modzy.SubmitJobEmbeddedInput{
        ModelIdentifier: "aevbu1h3yw",
        ModelVersion:    "1.0.1",
        Inputs:          mapSource,
    })

  // Check on the status of your request to analyze the sentiment of your input once a second
    jobDetails, _ := submitResponse.WaitForCompletion(ctx, time.Second)

  //When the input is done processing, retrive the results from Modzy, and log them to your terminal
    if jobDetails.Details.Status == modzy.JobStatusCompleted {
        results, _ := submitResponse.GetResults(ctx)
        for key := range mapSource {
            if result, exists := results.Results.Results[key]; exists {
                log.Printf("    %s:\n", key)
                modelRes := result.Data["results.json"].(map[string]interface{})
                for key2, val2 := range modelRes {
                    log.Printf("    %s:%f\n", key2, val2)
                }
            }
        }
    }
}

Did this page help you?