Adding images to test cases (for multi-modal)
If you're evaluating multi-modal pipelines, you can upload images to Gentrace and use them as inputs to your test cases.
Uploading files
If your pipeline depends on file inputs (e.g. images, PDFs), you can upload your files to our object storage. We then return an authenticated URL to add to your test case inputs.
- TypeScript
- Python
python
import osimport gentracefrom dotenv import load_dotenvload_dotenv()gentrace.init(api_key=os.getenv("GENTRACE_API_KEY"), host="http://localhost:3000/api/v1")GENTRACE_PIPELINE_SLUG = "main"with open("/home/user/gentrace-icon.png", "rb") as f:# This SDK method receives a file handle and returns an authenticated URLurl = gentrace.upload_file(f)print("Gentrace file URL: ", url)gentrace.create_test_case(# Pipeline slugGENTRACE_PIPELINE_SLUG,{"name": 'Gentrace Icon','inputs': {# Any Gentrace-uploaded URL will be rendered in a pretty format in the UI'iconUrl': url,},"expectedOutputs": {"value": "Gentrace logo"}})
When viewing the test cases with Gentrace file URLs, we detect image extensions and render them directly in the UI.
Uploading file content bytes
If your file does not have a presence on the filesystem, you can directly upload the bytes to Gentrace. This approach requires you to name your file.
Uploading file content bytes is only supported in Python at this time.
- Python
python
import gentracegentrace.init(api_key=os.getenv("GENTRACE_API_KEY"))with open("examples/files/gentrace-icon.png", "rb") as f:# Remember to specify the file extension! The Gentrace UI relies on the file# extension to render file contents correctly.url = gentrace.upload_bytes("gentrace-icon.png", f.read())print("Uploaded URL: ", url)gentrace.create_test_case("main",{"name": 'Gentrace Icon','inputs': {'imageUrl': url,},"expectedOutputs": {"value": "Gentrace logo"}})
typescript
import {init ,uploadBuffer ,createTestCase } from "@gentrace/core";importfs from "fs/promises";init ({apiKey :process .env .GENTRACE_API_KEY ,});async functionupload () {constbuffer = awaitfs .readFile ("./icon.png");// This SDK method receives a file name + buffer and returns an authenticated URLconsturl = awaituploadBuffer ("icon.png",buffer );constcaseId = awaitcreateTestCase ({pipelineSlug : "main",name : "Gentrace Icon",inputs : {// Any Gentrace-uploaded URL will be rendered in a pretty format in the UIiconUrl :url },expectedOutputs : {value : "Gentrace Icon"},});console .log ("Case ID",caseId );}upload ();
When viewing the test cases with Gentrace file URLs, we detect image extensions and render them directly in the UI.
Retrieving files
All uploaded files require authentication by an API key. When pulling test cases for a Gentrace pipeline, you need to construct an authenticated HTTP request to download the files associated with each case.
Here's a script that:
- Pulls test cases for a Gentrace pipeline
- Downloads the Gentrace-uploaded images from the input URL
- Runs the image data through our AI business logic
- Submits the outputs for grading
- TypeScript
- Python
python
import osfrom urllib.parse import urlparseimport gentraceimport requestsfrom dotenv import load_dotenv# Import your AI pipelinefrom ai.pipelines import image_to_wordsGENTRACE_PIPELINE_SLUG = "main"load_dotenv()gentrace.init(api_key=os.getenv("GENTRACE_API_KEY"))cases = gentrace.get_test_cases(pipeline_slug=GENTRACE_PIPELINE_SLUG)outputs = []for case in cases:image_url = case.get("inputs").get("imageUrl")# Image URLs are authenticated. You must provide an API key as the bearer token.headers = {'Authorization': 'Bearer {}'.format(os.getenv("GENTRACE_API_KEY"))}response = requests.get(image_url, headers=headers)# Run the AI pipeline on the raw image contentimage_description = image_to_words(response.content)outputs.append({"value": image_description})response = gentrace.submit_test_result(GENTRACE_PIPELINE_SLUG, cases, outputs)print(response["resultId"])
typescript
import {init ,submitTestResult ,getTestCases } from "@gentrace/core";import {imageToWords } from "../api/pipelines";constGENTRACE_PIPELINE_SLUG = "main";constGENTRACE_API_KEY =process .env .GENTRACE_API_KEY ;init ({apiKey :GENTRACE_API_KEY ,});async functionrunTest () {constcases = awaitgetTestCases (GENTRACE_PIPELINE_SLUG );constoutputs :Record <string, any>[] = [];for (consttestCase ofcases ) {consturl =testCase .inputs .imageUrl ;constresponse = awaitfetch (url , {method : "GET",headers : {Authorization : `Bearer ${GENTRACE_API_KEY }`,},});constblob = awaitresponse .blob ();constwords = awaitimageToWords (blob );outputs .push ({value :words ,});}constresponse = awaitsubmitTestResult (GENTRACE_PIPELINE_SLUG ,cases ,outputs ,);console .log ("Result ID:",response .resultId );}runTest ();