Skip to main content
In this quickstart, we’ll:
  • Set up our environment
  • Get an image
  • Run inference of MechaNet.

Set up your environment variables

Generate a .env file with the following environment variables in your local development setting.
.env
MECHA_ENDPOINT=
MECHA_TOKEN=

Set up your environment

Install the required dependencies to make an API request.
pip install requests python-dotenv pillow

Download a test image

# coding=utf-8
# python download_image.py
import requests
from PIL import Image

base_url = "https://upload.wikimedia.org/wikipedia/commons"
image_url = f"{base_url}/7/7a/Cardiomegally.PNG"
headers = {"User-Agent": "Mecha-Health"}
response = requests.get(image_url, headers=headers, stream=True)
im = Image.open(response.raw)
im.save("./test_image.png")

Make an API Request

Make an API request by passing the image to our API.
 # coding=utf-8

import os
import time
import requests
import base64
import dotenv

dotenv.load_dotenv()

API_URL = os.getenv("MECHA_ENDPOINT")

data = "./test_image.png"

with open(data, "rb") as f:
    image_bytes = f.read()
    data = base64.b64encode(image_bytes).decode('utf-8')

request = {
    "inputs": [
        {
            "name": "IMAGE",
            "data": data
        }
    ],
    "language": "es" # one of ["es", "en"]
}

start_time = time.time()
response = requests.post(API_URL,
                         json=request,
                         headers={"Authorization": f"Bearer {os.getenv('MECHA_TOKEN')}"})
end_time = time.time()
print(f"Time taken: {end_time - start_time} seconds")
print(response.json())

Make an API request with additional information

In addition to the image, you may want to pass auxiliary information such as the indication for the scan, and potentially past reports for the same patient. In this case, you can optionally pass this data to the inputs body as dictionaries. You can pass both, or either data to the body of the request.
 # coding=utf-8

import os
import time
import requests
import base64
import dotenv

dotenv.load_dotenv()

API_URL = os.getenv("MECHA_ENDPOINT")

data = "./test_image.png"

with open(data, "rb") as f:
    image_bytes = f.read()
    data = base64.b64encode(image_bytes).decode('utf-8')

request = {
    "inputs": [
        {
            "name": "IMAGE",
            "data": data
        },
        {
            "name": "INDICATION",
            "data": "Evaluate the image for pneumothorax."
        },
        {
            "name": "PAST_REPORTS_DATES_TIMES",
            "data": [
                ("The image shows a large right sided pleural effusion with complete opacification of the right lung.",
                 0, 4, 3, 2), # YYYY, DD, H, S from current date: 0 years, 4 days, 3 hours, and 2 minutes ago.
                ("The image shows clear lung fields. No osseous abnormalities.",
                 10, 4, 3, 1)  # YYYY, DD, H, S from current date: 10 years, 4 days, 3 hours, and 1 minutes ago.
            ]
        }
    ],
    "language": "es" # one of ["es", "en"]
}

start_time = time.time()
response = requests.post(API_URL,
                         json=request,
                         headers={"Authorization": f"Bearer {os.getenv('MECHA_TOKEN')}"})
end_time = time.time()
print(f"Time taken: {end_time - start_time} seconds")
print(response.json())
The PAST_REPORTS_DATES_TIMES data type is list of tuples, each of length 5, where the first element is the report text (string) and the remaining four elements represent the time delta from the current date in the format (YYYY, DD, HH, MM) - years, days, hours, and minutes ago respectively.