Transforming Images with Moderation
Project Setup
# Create a project directory.mkdir prodia-image-transformationcd prodia-image-transformation
Install Node (if not already installed):
brew install node# Close the current terminal and open a new one so that node is available.
apt install node# Close the current terminal and open a new one so that node is available.
winget install -e --id OpenJS.NodeJS.LTS# Close the current terminal and open a new one so that node is available.
Create project skeleton:
# Requires node --version >= 18# Initialize the project with npm.npm init -y
# Install the prodia-js library.npm install prodia --save
Install Python (if not already installed):
brew install python# Close the current terminal and open a new one so that python is available.
apt install python3 python3-venv python-is-python3# Close the current terminal and open a new one so that python is available.
winget install -e --id Python.Python.3.12# Close the current terminal and open a new one so that python is available.
# Requires python --version >= 3.12python -m venv venvsource venv/bin/activatepip install requests
Install curl (if not already installed):
brew install curl# Close the current terminal and open a new one so that curl is available.
apt install curl# Close the current terminal and open a new one so that curl is available.
# NOTE: Windows 10 and up have curl installed by default and this can be# skipped.winget install -e --id cURL.cURL# Close the current terminal and open a new one so that curl is available.
# Export your token so it can be used by the main code.export PRODIA_TOKEN=your-token-here
Create a main file for your project:
const { createProdia } = require("prodia/v2");
const prodia = createProdia({ token: process.env.PRODIA_TOKEN // get it from environment});
Create the following main.py
from requests.adapters import HTTPAdapter, Retryimport osimport requestsimport sys
prodia_token = os.getenv('PRODIA_TOKEN')prodia_url = 'https://inference.prodia.com/v2/job'
session = requests.Session()retries = Retry(status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)session.mount('http://', HTTPAdapter(max_retries=retries))session.mount('https://', HTTPAdapter(max_retries=retries))session.headers.update({'Authorization': f"Bearer {prodia_token}"})
set -euo pipefail
You’re now ready to make some API calls!
Transform an image (inside a workflow)
const { createProdia } = require("prodia/v2"); // v2 :)const fs = require("node:fs/promises"); // add this to imports at the top
const prodia = createProdia({ token: process.env.PRODIA_TOKEN, // get it from environment});
(async () => { // get input image const sunnyDay = await (await fetch("https://docs.prodia.com/sunny-day.jpg")).arrayBuffer();
const job = await prodia.job({ type: "workflow.serial.v1", config: { jobs: [ { type: "inference.flux.schnell.img2img.v1", config: { prompt: "rainy landscape, 4k", strength: 0.8, }, }, ], }, }, { inputs: [ sunnyDay ], });
const rainyDay = await job.arrayBuffer();
await fs.writeFile("rainy-day.jpg", new Uint8Array(rainyDay)); // open rainy-day.jpg})();
node main.js
from requests.adapters import HTTPAdapter, Retryfrom io import BytesIOimport jsonimport osimport requestsimport sys
prodia_token = os.getenv('PRODIA_TOKEN')prodia_url = 'https://inference.prodia.com/v2/job'
session = requests.Session()retries = Retry(status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)session.mount('http://', HTTPAdapter(max_retries=retries))session.mount('https://', HTTPAdapter(max_retries=retries))session.headers.update({'Authorization': f"Bearer {prodia_token}"})
try: with open('sunny-day.jpg', 'rb') as f: sunny_day = f.read()except FileNotFoundError: res = requests.get('https://docs.prodia.com/sunny-day.jpg') sunny_day = BytesIO(res.content) with open('sunny-day.jpg', 'wb') as f: f.write(res.content)except Exception as e: raise e
headers = { 'Accept': 'image/png',}
job = { 'type': 'workflow.serial.v1', 'config': { 'jobs': [ { 'type': 'inference.flux.schnell.img2img.v1', 'config': { 'prompt': 'rainy landscape, 4k', 'strength': 0.8, }, }, ], },}
files = [ ('job', ('job.json', BytesIO(json.dumps(job).encode('utf-8')), 'application/json')), ('input', ('sunny-day.jpg', sunny_day, 'image/jpeg')),]
res = session.post(prodia_url, headers=headers, files=files)print(f"Request ID: {res.headers['x-request-id']}")print(f"Status: {res.status_code}")
if res.status_code != 200: print(res.text) sys.exit(1)
with open('rainy-day.jpg', 'wb') as f: f.write(res.content)
python main.py
set -euo pipefail
cat <<EOF > job.json{ "type": "workflow.serial.v1", "config": { "jobs": [ { "type": "inference.flux.schnell.img2img.v1", "config": { "prompt": "rainy landscape, 4k", "strength": 0.8 } } ] }}EOF
if ! [[ -f sunny-day.jpg ]]; then curl -Lo sunny-day.jpg 'https://docs.prodia.com/sunny-day.jpg'fi
curl -sSf \ -H "Authorization: Bearer $PRODIA_TOKEN" \ -H 'Accept: image/jpeg' \ --output rainy-day.jpg \ --retry 3 \ https://inference.prodia.com/v2/job
bash main.sh
open rainy-day.jpg
xdg-open rainy-day.jpg
start rainy-day.jpg
Adding content moderation
const { createProdia } = require("prodia/v2"); // v2 :)const fs = require("node:fs/promises"); // add this to imports at the top
const prodia = createProdia({ token: process.env.PRODIA_TOKEN, // get it from environment});
(async () => { // get input image const sunnyDay = await (await fetch("https://docs.prodia.com/sunny-day.jpg")).arrayBuffer();
const job = await prodia.job({ type: "workflow.serial.v1", config: { jobs: [ { type: "workflow.moderate.v1", config: { threshold: 0.95, }, }, { type: "inference.flux.schnell.img2img.v1", config: { prompt: "rainy landscape, 4k", strength: 0.8, }, }, { type: "workflow.moderate.v1", config: { threshold: 0.95, }, }, ], }, }, { inputs: [ sunnyDay ], });
const rainyDay = await job.arrayBuffer();
await fs.writeFile("rainy-day.jpg", new Uint8Array(rainyDay)); // open rainy-day.jpg})();
node main.js
from requests.adapters import HTTPAdapter, Retryfrom io import BytesIOimport jsonimport osimport requestsimport sys
prodia_token = os.getenv('PRODIA_TOKEN')prodia_url = 'https://inference.prodia.com/v2/job'
session = requests.Session()retries = Retry(status_forcelist=Retry.RETRY_AFTER_STATUS_CODES)session.mount('http://', HTTPAdapter(max_retries=retries))session.mount('https://', HTTPAdapter(max_retries=retries))session.headers.update({'Authorization': f"Bearer {prodia_token}"})
try: with open('sunny-day.jpg', 'rb') as f: sunny_day = f.read()except FileNotFoundError: res = requests.get('https://docs.prodia.com/sunny-day.jpg') sunny_day = BytesIO(res.content) with open('sunny-day.jpg', 'wb') as f: f.write(res.content)except Exception as e: raise e
headers = { 'Accept': 'image/png',}
job = { 'type': 'workflow.serial.v1', 'config': { 'jobs': [ { 'type': 'workflow.moderate.v1', 'config': { 'threshold': 0.95, }, }, { 'type': 'inference.flux.schnell.img2img.v1', 'config': { 'prompt': 'rainy landscape, 4k', 'strength': 0.8, }, }, { 'type': 'workflow.moderate.v1', 'config': { 'threshold': 0.95, }, }, ], },}
files = [ ('job', ('job.json', BytesIO(json.dumps(job).encode('utf-8')), 'application/json')), ('input', ('sunny-day.jpg', sunny_day, 'image/jpeg')),]
res = session.post(prodia_url, headers=headers, files=files)print(f"Request ID: {res.headers['x-request-id']}")print(f"Status: {res.status_code}")
if res.status_code != 200: print(res.text) sys.exit(1)
with open('rainy-day.jpg', 'wb') as f: f.write(res.content)
python main.py
set -euo pipefail
cat <<EOF > job.json{ "type": "workflow.serial.v1", "config": { "jobs": [ { "type": "workflow.serial.v1", "config": { "threshold": 0.95 } }, { "type": "inference.flux.schnell.img2img.v1", "config": { "prompt": "rainy landscape, 4k", "strength": 0.8 } }, { "type": "workflow.serial.v1", "config": { "threshold": 0.95 } } ] }}EOF
if ! [[ -f sunny-day.jpg ]]; then curl -Lo sunny-day.jpg 'https://docs.prodia.com/sunny-day.jpg'fi
curl -sSf \ -H "Authorization: Bearer $PRODIA_TOKEN" \ -H 'Accept: image/jpeg' \ --output rainy-day.jpg \ --retry 3 \ https://inference.prodia.com/v2/job
bash main.sh
open rainy-day.jpg
xdg-open rainy-day.jpg
start rainy-day.jpg