LogoLogo
Why Unitary?
  • Get started
    • 👋Step-by-step instructions
  • API References
    • ℹ️Features and requirements
    • ▶️API Authentication
    • ☑️Standard & Premium: Policy Classification endpoints
      • 🔃Add-on: synchronous endpoints
    • 🛂Virtual Moderator endpoint
    • #️⃣Items and Characteristics endpoints
      • *️⃣List of Item and Characteristics
    • ⏺️Detoxify endpoints
    • ↔️How to select thresholds
    • ↪️Integrating Webhooks
Powered by GitBook
On this page

Was this helpful?

  1. API References

Standard & Premium: Policy Classification endpoints

PreviousAPI AuthenticationNextAdd-on: synchronous endpoints

Last updated 1 year ago

Was this helpful?

The Policy Classification endpoints will serve you two products: Unitary Standard and Unitary Premium. The table below shows more details for each:

Product
Categories covered
How it works

Standard

• Adult & Sexual. Explicit sexual activity, nudity, detailed explanations of sexual encounters…

• Non-medical Drugs. Pills, coke, marihuana, minors drinking or smoking, paraphernalia…

• Violence and Injury. People killing or sexually assaulting other people, violent fights in uncontrolled settings, animal abuse, physical threats, suicide, self-injury or eating disorders and their glorification, graphic injuries...

• Weapons and Firearms. Firearms, ammunition, explosives, non-kitchen knives or homemade weapons in dangerous settings, instructional content on weapons…

• Hate Speech and Hate Symbols. Symbols from hate organisations, discrimination of people based on their identity…

The Standard product works "off the shelf". It does not require any input from your side. Please let us know if you need help analysing Untiary's model responses to set up decision thresholds.

To see improved performance over time, you can also get in contact to share false negatives and positives and make Unitary's models better for your use case.

Premium

All Standard categories tweaked based on your content guidelines + any other categories you may need e.g. cosmetic products.

The Premium product will give you greater accuracy by understanding your content guidelines and getting fine-tuned with your data. You can benefit from Unitary's Premium offering category by category. Please get in contact if you are interested in this.

For both the Standard and Premium products, Unitary will return in the classification results a raw score that you can use to set thresholds and determine if data is safe, harmful or ambiguous.

Please read How to select thresholdsif you need help analysing the Standard scores. We'll give you custom thresholds for any premium category we fine-tune to your policy or data.

You can contact if you wish to explore Unitary's Premium offering.

1. Authenticate

Please follow the API Authentication instructions first in order to authenticate with Unitary's API. This will generate a token that is valid for 24 hours and must be used in subsequent API requests.

2. Sending your first request

Depending on your use case, pick an image or a video and then use one of the following POST endpoints to send your first request.

POST endpoint reference

Example code

curl --location --request POST "https://api.unitary.ai/v1/classify/policy/image" \
--header "Authorization: Bearer {API_TOKEN}" \
--form "caption={CAPTION}" \
--form "url={RESOURCE_URL}"
import requests

api_token = "your_api_token_here"
file_path = "/path/to/your/file.jpg"
caption = "your_caption_here"
url = "https://api.unitary.ai/v1/classify/policy/image"

# For sending a URL
data = {
    'caption': caption,
    'url': 'your_resource_url_here'
}
headers = {'Authorization': f'Bearer {api_token}'}

response = requests.post(url, data=data, headers=headers)
job_id = resonse.text["job_id"]

print(job_id)
const fetch = require('node-fetch');
const FormData = require('form-data');
const fs = require('fs');

const apiToken = 'your_api_token_here';
const file_path = '/path/to/your/file.jpg';
const caption = 'your_caption_here';
const url = 'https://api.unitary.ai/v1/classify/image';

// For sending a URL
const dataUrl = new URLSearchParams();
dataUrl.append('caption', caption);
dataUrl.append('url', 'your_resource_url_here');

fetch(url, {
  method: 'POST',
  body: dataUrl,
  headers: {
    'Authorization': `Bearer ${apiToken}`,
    'Content-Type': 'application/x-www-form-urlencoded',
  },
})
.then(response => response.text())
.then(result => console.log(result["job_id"]))
.catch(error => console.log('error', error));

3. Get results back via the GET endpoint

Using the GET endpoint may be inadequate for large scale! The recommendation is using the "callback_url" parameter instead. Please check the Integrating Webhooks guide for more information.

GET endpoint reference

Example code

curl --location --request GET "https://api.unitary.ai/v1/classify/image/{JOB_ID}" \
--header "Authorization: Bearer {BEARER_TOKEN}"
import requests

BEARER_TOKEN = "{BEARER_TOKEN}"
JOB_ID = "{JOB_ID}
url = f"https://dev.api.unitary.ai/v1/classify/image/{JOB_ID}"

payload={}
headers = {
  'Authorization': f'Bearer {BEARER_TOKEN}'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response)
const fetch = require("node-fetch"); 
const BEARER_TOKEN = "{BEARER_TOKEN}";
const JOB_ID = "{JOB_ID}";
const url = `https://dev.api.unitary.ai/v1/classify/image/${JOB_ID}`;

fetch(url, {
  method: "GET",
  headers: {
    'Authorization': `Bearer ${BEARER_TOKEN}`,
  },
})
.then(response => response.json())
.then(data => {
  const results = data["results"];
  console.log(results);
})
.catch((error) => {
  console.error('Error:', error);
});

Example responses

Depending on the processing status of the job at the time of request, you can get any of the following as the response from the GET endpoints:

{
  "status": "done",
  "results": {
    "policy_categories": [
      {
        "name": "{CATEGORY_NAME}",
        "description": "{CATEGORY_DESCRIPTION}",
        "score": 0.99
      }
    ],
    "url": "{RESOURCE_URL}"
  }
}
{
  "status": "queued"
}
{
  "status": "failed",
  "msg": "the error message for the request"
}

4. Webhooks

(optional but encouraged)

In order to have a scalable end-to-end integration, the last step is to set up the receiving of webhooks as described in the following guide: Integrating Webhooks

☑️
support@unitary.ai

Retrieve the results of a queued Policy classification job

get
Authorizations
Path parameters
job_idstring · uuid4Required
Responses
200
Successful Response
application/json
404
Not found
422
Validation Error
application/json
get
GET /classify/policy/image/{job_id} HTTP/1.1
Host: 
Authorization: Bearer JWT
Accept: */*
{
  "status": "done",
  "results": {
    "url": "https://example.com",
    "policy_categories": [
      {
        "name": "ARMS_AMMO",
        "description": "Arms & Ammunition",
        "risk_level": "high",
        "score": 0.8
      }
    ]
  },
  "msg": "text"
}

Retrieve the results of a queued Policy classification job

get
Authorizations
Path parameters
job_idstring · uuid4Required
Responses
200
Successful Response
application/json
404
Not found
422
Validation Error
application/json
get
GET /classify/policy/video/{job_id} HTTP/1.1
Host: 
Authorization: Bearer JWT
Accept: */*
{
  "status": "done",
  "results": {
    "url": "https://example.com",
    "policy_categories": [
      {
        "name": "ARMS_AMMO",
        "description": "Arms & Ammunition",
        "risk_level": "high",
        "score": 0.8
      }
    ],
    "metadata": {
      "width": 1,
      "height": 1,
      "fps": 1,
      "duration": 1,
      "seconds_processed": 1
    }
  },
  "msg": "text"
}
  • 1. Authenticate
  • 2. Sending your first request
  • POST endpoint reference
  • POSTQueue an image for Policy classification
  • POSTQueue a video for Policy classification
  • Example code
  • 3. Get results back via the GET endpoint
  • GET endpoint reference
  • GETRetrieve the results of a queued Policy classification job
  • GETRetrieve the results of a queued Policy classification job
  • Example code
  • Example responses
  • 4. Webhooks

Queue an image for Policy classification

post

This endpoint predicts the policy categories of a single image.

The request must contain a url form field (a string containing a URL to download the image file from).

Example CURL:

curl --location --request POST "https://api.unitary.ai/v1/classify/policy/image" --header "Authorization: Bearer {API_TOKEN}" --header "Content-Type: multipart/form-data" --form "url={RESOURCE_URL}"
Authorizations
Body
callback_urlstring · uri · min: 1 · max: 2083Optional
captionstringOptional
urlstring · uri · min: 1 · max: 65536Optional
Responses
200
Successful Response
application/json
404
Not found
422
Validation Error
application/json
post
POST /classify/policy/image HTTP/1.1
Host: 
Authorization: Bearer JWT
Content-Type: application/x-www-form-urlencoded
Accept: */*
Content-Length: 83

"callback_url='https://example.com'&caption='text'&url='https://example.com'"
{
  "job_id": "text"
}

Queue a video for Policy classification

post

This endpoint predicts the policy categories of a single video.

The request must contain a url form field (a string containing a URL to download the video file from).

Example CURL:

curl --location --request POST "https://api.unitary.ai/v1/classify/policy/video" --header "Authorization: Bearer {API_TOKEN}" --header "Content-Type: multipart/form-data" --form "url={RESOURCE_URL}"
Authorizations
Body
callback_urlstring · uri · min: 1 · max: 2083Optional
captionstringOptional
urlstring · uri · min: 1 · max: 65536Optional
Responses
200
Successful Response
application/json
404
Not found
422
Validation Error
application/json
post
POST /classify/policy/video HTTP/1.1
Host: 
Authorization: Bearer JWT
Content-Type: application/x-www-form-urlencoded
Accept: */*
Content-Length: 83

"callback_url='https://example.com'&caption='text'&url='https://example.com'"
{
  "job_id": "text"
}