- 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
mkdir mecha-inference
cd mecha-inference
npm init -y
npm install node-fetch@2 dotenv
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")
// node download_image.js
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
// Downloads an image from Wikimedia, which is CC, and saves it locally.
async function downloadImage() {
const baseUrl = "https://upload.wikimedia.org/wikipedia/commons";
const imageUrl = `${baseUrl}/7/7a/Cardiomegally.PNG`;
const headers = { "User-Agent": "Mecha-Health" };
const destinationPath = path.join(__dirname, 'test_image.png');
try {
const response = await fetch(imageUrl, { headers, compress: true });
if (!response.ok) {
throw new Error(`Failed to fetch image: ${response.statusText}`);
}
const fileStream = fs.createWriteStream(destinationPath);
await new Promise((resolve, reject) => {
response.body.pipe(fileStream);
response.body.on("error", (err) => {
reject(err);
});
fileStream.on("finish", resolve);
});
console.log(`Image successfully downloaded to ${destinationPath}`);
} catch (error) {
console.error(`Error downloading the image: ${error.message}`);
}
}
// Invoke the function to download the image
downloadImage();
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())
// test_inference.js
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const fetch = require('node-fetch');
// Load environment variables from .env file
dotenv.config();
const API_URL = process.env.MECHA_ENDPOINT;
const MECHA_TOKEN = process.env.MECHA_TOKEN;
const IMAGE_PATH = path.join(__dirname, 'test_image.png');
async function makeApiRequest() {
try {
// Read and encode the image
const imageBuffer = fs.readFileSync(IMAGE_PATH);
const encodedImage = Buffer.from(imageBuffer).toString('base64');
const requestBody = {
inputs: [
{
name: "IMAGE",
data: encodedImage
}
],
language: "es" // one of ["es", "en"]
};
const startTime = Date.now();
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${MECHA_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
const endTime = Date.now();
const timeTaken = (endTime - startTime) / 1000;
const responseData = await response.json();
console.log(`Time taken: ${timeTaken} seconds`);
console.log(responseData);
} catch (error) {
console.error('Error making API request:', error);
}
}
makeApiRequest();
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 theinputs 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())
// test_inference.js
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const fetch = require('node-fetch');
// Load environment variables from .env file
dotenv.config();
const API_URL = process.env.MECHA_ENDPOINT;
const MECHA_TOKEN = process.env.MECHA_TOKEN;
const IMAGE_PATH = path.join(__dirname, 'test_image.png');
async function makeApiRequest() {
try {
// Read and encode the image
const imageBuffer = fs.readFileSync(IMAGE_PATH);
const encodedImage = Buffer.from(imageBuffer).toString('base64');
const requestBody = {
inputs: [
{
name: "IMAGE",
data: encodedImage
},
{
"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"]
};
const startTime = Date.now();
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${MECHA_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
const endTime = Date.now();
const timeTaken = (endTime - startTime) / 1000;
const responseData = await response.json();
console.log(`Time taken: ${timeTaken} seconds`);
console.log(responseData);
} catch (error) {
console.error('Error making API request:', error);
}
}
makeApiRequest();
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.