> For the complete documentation index, see [llms.txt](https://ubiai.gitbook.io/ubiai-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ubiai.gitbook.io/ubiai-documentation/developer-documentation.md).

# Developer Documentation

## Getting started

Welcome to the UBIAI Developer Documentation! This guide is designed to help developers integrate and utilize UBIAI's APIs effectively. UBIAI provides a cutting-edge text labeling platform for natural language processing. By leveraging our APIs, developers can enhance their applications with advanced AI capabilities.

## Train model

{% openapi src="/files/g6UzlMobFS8mqGIx6J6a" path="/api\_v1/train\_model/{token}/{id}" method="post" %}
[spec.json](https://3073024999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0hqV0hLtifjaqlfkGT7n%2Fuploads%2FFZTIi9L9BvaAzIp8zoFC%2Fspec.json?alt=media\&token=d2719053-87bd-4d8f-8e58-91c9096b2152)
{% endopenapi %}

```python
import requests
import json


url ="https://api.ubiai.tools:8443/api_v1/train_model"
my_token = "put_your_access_token"

data = {
    "drop": 4,
    "max_batch": 4,
    "nb_iter": "8",
    "project": "8791",
    "selected_model": "blank",
    "selected_validation": "20",
    "model_type": "layoutlm",
    "with_annotate": False
    "allowed_labels": []
}


response = requests.post(url+ my_token ,data=data)
if response.status_code == 200:
    # Access the response content
    data = json.loads(response.content.decode("utf-8"))
    print("Response Data:", data)
else:
    # Handle the error
    print("Error:", response.status_code, response.text)
```

### Model types for training

| model types              | models                             |
| ------------------------ | ---------------------------------- |
| Spacy                    | en\_core\_web\_en                  |
| Bert                     | distilbert-base-cased              |
|                          | dslim/bert-base-NER                |
|                          | roberta-base                       |
|                          | allenai/scibert\_scivocab\_uncased |
|                          | alvaroalon2/biobert\_chemical\_ner |
| LayoutLM                 | LayoutLM-base                      |
| Template Form Recognizer | blank                              |

## Add project

{% openapi src="/files/g6UzlMobFS8mqGIx6J6a" path="/api\_v1/project" method="post" %}
[spec.json](https://3073024999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0hqV0hLtifjaqlfkGT7n%2Fuploads%2FFZTIi9L9BvaAzIp8zoFC%2Fspec.json?alt=media\&token=d2719053-87bd-4d8f-8e58-91c9096b2152)
{% endopenapi %}

```python
import requests
import json

url ="https://api.ubiai.tools:8443/api_v1/project"
headers = {"Authorization": "Token put_your_acess_token"}

project_name = ""

# must be one of these values:
# 'Chinese', 'Danish', 'Dutch', 'English', 'French'
# 'German', 'Greek', 'Italian', 'Lithuanian', 'Multi-language', 
# 'Norwegian Bokmål', 'Polish', 'Polish', 'Romanian', 'Spanish',
# 'Afrikaans', 'Albanian', 'Arabic', 'Armenian', 'Basque',
# 'Bengali', 'Bulgarian', 'Catalan', 'Croatian', 'Czech'
# 'Estonian', 'Finnish', 'Gujarati', 'Hebrew', 'Hindi', 'Hungarian', 'Tamil'
language = "English"

description = ""

# project type must be 'Text Annotation' for span based
# 'Character Based Annotation' for character based
# 'Native PDF Annotation' for ocr
# 'Image Classification'
project_type = "Text Annotation"

# label object must be in this format {'text': 'example', 'shortcut': '1'}
entities_labels = []

# label object must be in this format {'text': 'example', 'shortcut': '1'}
relations_labels = []

# classification_type must be 'binary' for positive or negative
# 'single' for single classification
# 'multi' for multi classifications
classification_type = "Binary"

# fill this only when classification type is different then binary
# label object must be in this format {'text': 'example', 'shortcut': '1'}
classifications_labels = []

project = {
    "name": project_name,
    "language": language,
    "description": description,
    "type": project_type,
    "entities_labels" : entities_labels,
    "relations_labels": relations_labels,
    "classification_type": classification_type,
    "classifications_labels": classifications_labels 
}

response = requests.post(url , json=project, headers=headers)
if response.status_code == 200:
    # Access the response content
    data = json.loads(response.content.decode("utf-8"))
    print("Response Data:", data)
else:
    # Handle the error
    print("Error:", response.status_code, response.text)
```

## Annotate project

{% openapi src="/files/g6UzlMobFS8mqGIx6J6a" path="/api\_v1/annotate\_project/{token}/{id}" method="post" %}
[spec.json](https://3073024999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0hqV0hLtifjaqlfkGT7n%2Fuploads%2FFZTIi9L9BvaAzIp8zoFC%2Fspec.json?alt=media\&token=d2719053-87bd-4d8f-8e58-91c9096b2152)
{% endopenapi %}

```python
import requests

# Define the API endpoint with token and file_type in the URL
api_url = "https://app.ubiai.tools:8443/api_v1/annotate_project/{token}/{project_id}"

# Replace placeholders with actual values
api_url = api_url.format(token="your_access_token", project_id="your_project_id")


# Make a POST request to the API with headers and files
response = requests.post(api_url)

# Check the response
if response.status_code == 200:
    # Access the response content
    data = json.loads(response.content.decode("utf-8"))
    print("Response Data:", data)
else:
    # Handle the error
    print("Error:", response.status_code, response.text)
```

## Annotate snippets

{% openapi src="/files/g6UzlMobFS8mqGIx6J6a" path="/api\_v1/annotate/{token}" method="post" %}
[spec.json](https://3073024999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0hqV0hLtifjaqlfkGT7n%2Fuploads%2FFZTIi9L9BvaAzIp8zoFC%2Fspec.json?alt=media\&token=d2719053-87bd-4d8f-8e58-91c9096b2152)
{% endopenapi %}

```python
import requests
import json


url ="https://api.ubiai.tools:8443/api_v1/annotate"
my_token = "/put-your_acess_token"


data = {
    
    # inputs is a list of text

    "inputs" : ["John works at Google.",
                "John works at Google." ],
    
    # entities is a list of list
    # Each list is a list of dict
    # Each dict must have this format :
    # {start : represent the offset of the start character
    # end : represent the offset of the end character + 1
    # label : represent the label of entity}
    "entities" : [
      [{'start': 0, 'end': 4, 'label': 'PER'}, 
       {'start': 14, 'end': 20, 'label': 'COMPANY'}], 
      [{'start': 0, 'end': 4, 'label': 'PER'}, 
       {'start': 14, 'end': 20, 'label': 'COMPANY'}]]}

response = requests.post(url+ my_token,json= data)
print(response.status_code)
res = json.loads(response.content.decode("utf-8"))
print(res)

```

## Export data

{% openapi src="/files/g6UzlMobFS8mqGIx6J6a" path="/api\_v1/download/{token}/{type}" method="get" %}
[spec.json](https://3073024999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0hqV0hLtifjaqlfkGT7n%2Fuploads%2FFZTIi9L9BvaAzIp8zoFC%2Fspec.json?alt=media\&token=d2719053-87bd-4d8f-8e58-91c9096b2152)
{% endopenapi %}

```python
import requests

# Define the API endpoint with token and file_type in the URL
api_url = "https://app.ubiai.tools:8443/api_v1/download/{token}/{type}"

# Replace placeholders with actual values
api_url = api_url.format(token="your_model_token", type="aws/Lists")
split_ratio = ""
params = {'splitRatio': split_ratio}
# Make a GETrequest to the API with headers and files
response = requests.get(api_url, params=params)

# Check the response
if response.status_code == 200:
    # Access the response content
    data = json.loads(response.content.decode("utf-8"))
    print("Response Data:", data)
else:
    # Handle the error
    print("Error:", response.status_code, response.text)
```

### Download options

<table><thead><tr><th>Type options</th></tr></thead><tbody><tr><td><pre><code>aws/Lists
</code></pre></td></tr><tr><td><pre><code>spacy/Json
</code></pre></td></tr><tr><td><pre><code>DocBin_NER/Json
</code></pre></td></tr><tr><td><pre><code>spacy_training/Json
</code></pre></td></tr><tr><td><pre><code>classification/Json
</code></pre></td></tr><tr><td><pre><code>ocr1,ocr2,ocr1
</code></pre></td></tr><tr><td><pre><code>stanford
</code></pre></td></tr><tr><td><pre><code>iob
</code></pre></td></tr><tr><td><pre><code>iob_pos
</code></pre></td></tr><tr><td><pre><code>iob_chatbot
</code></pre></td></tr></tbody></table>

## Download model

{% openapi src="/files/g6UzlMobFS8mqGIx6J6a" path="/api\_v1/download\_model/{token}/{model\_name}" method="get" %}
[spec.json](https://3073024999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0hqV0hLtifjaqlfkGT7n%2Fuploads%2FFZTIi9L9BvaAzIp8zoFC%2Fspec.json?alt=media\&token=d2719053-87bd-4d8f-8e58-91c9096b2152)
{% endopenapi %}

```python
import requests

# Define the API endpoint with token and file_type in the URL
api_url = "https://app.ubiai.tools:8443/api_v1/download_model/{token}/{model_name}"

# Replace placeholders with actual values
api_url = api_url.format(token="your_access_token", model_name="your_name")

# Make a POST request to the API with headers and files
response = requests.get(api_url)

# Check the response
if response.status_code == 200:
    # Access the response content
    data = json.loads(response.content.decode("utf-8"))
    print("Response Data:", data)
else:
    # Handle the error
    print("Error:", response.status_code, response.text)
```

## Perform OCR & layoutLM inference with API

{% openapi src="/files/g6UzlMobFS8mqGIx6J6a" path="/api\_v1/ocr\_layoutlm\_inference/{token}/{file\_type}" method="post" %}
[spec.json](https://3073024999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0hqV0hLtifjaqlfkGT7n%2Fuploads%2FFZTIi9L9BvaAzIp8zoFC%2Fspec.json?alt=media\&token=d2719053-87bd-4d8f-8e58-91c9096b2152)
{% endopenapi %}

```python
import requests

# Define the API endpoint with token and file_type in the URL
api_url = "https://app.ubiai.tools:8443/api_v1/ocr_layoutlm_inference/{token}/{file_type}"

# Replace placeholders with actual values
api_url = api_url.format(token="your_access_token", file_type="your_file_type")

# Define the file and other parameters to be sent with the request
file_paths = []    # add local files urls here
file_urls = []     # add urls of files online (must be public/accessible)

files = []
for path in file_paths:
   files.append(
           (
               "file",
               (os.path.basename(path), open(path, "rb"), mimetypes.guess_type(path)[0]),
           )
       )

data = {
   "ocr_engine": "DEFAULT",    # ocr engine to use 
   "filesUrls": file_urls,     # urls of files online
}

# Make a POST request to the API with headers and files
response = requests.post(api_url, body=body, headers=headers)

# Check the response
if response.status_code == 200:
    # Access the response content
    data = json.loads(response.content.decode("utf-8"))
    print("Response Data:", data)
else:
    # Handle the error
    print("Error:", response.status_code, response.text)
```

<table><thead><tr><th>OCR_ENGINES</th></tr></thead><tbody><tr><td><pre><code>DEFAULT
</code></pre></td></tr><tr><td><pre><code>ENGINE1
</code></pre></td></tr><tr><td><pre><code>ENGINE2
</code></pre></td></tr><tr><td><pre><code>ENGINE3
</code></pre></td></tr></tbody></table>

<table><thead><tr><th>type</th></tr></thead><tbody><tr><td><pre><code>pdf
</code></pre></td></tr><tr><td><pre><code>image
</code></pre></td></tr></tbody></table>

## Upload files

{% openapi src="/files/g6UzlMobFS8mqGIx6J6a" path="/api\_v1/upload/{token}/{file\_type}" method="post" %}
[spec.json](https://3073024999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0hqV0hLtifjaqlfkGT7n%2Fuploads%2FFZTIi9L9BvaAzIp8zoFC%2Fspec.json?alt=media\&token=d2719053-87bd-4d8f-8e58-91c9096b2152)
{% endopenapi %}

```python
import requests
import json
import mimetypes
import os

# Define the API endpoint with token and file_type in the URL
api_url = "https://app.ubiai.tools:8443/api/upload/{token}/{file_type}/"

# Replace placeholders with actual values
api_url = api_url.format(token="your_access_token", file_type="zip")

# Define the file and other parameters to be sent with the request
file_type = "/json"

list_of_file_path = ['']
urls = []
files = []
for file_path in list_of_file_path :
    files.append(('file',(os.path.basename(file_path ),open(file_path, 'rb'),mimetypes.guess_type(file_path)[0])))

data = {
  'autoAssignToCollab' :False,
  'taskType' :'TASK',
  'nbUsersPerDoc' :'',
  'selectedUsers' :'',
  'filesUrls' : urls
}

# Make a POST request to the API with headers and files
response = requests.post(api_url, files=files, data=data)

# Check the response
if response.status_code == 200:
    # Access the response content
    data = json.loads(response.content.decode("utf-8"))
    print("Response Data:", data)
else:
    # Handle the error
    print("Error:", response.status_code, response.text)
```
