Generate your client from Swager json

 

 

In this tutorial, you'll learn how to generate your client library using an Open API configuration. We're going to generate a python client but you can use the same library to generate your client in many languages.

 

Code generation

 

First you will need to download the latest swagger-codegen-cli version from maven central : 

https://repo1.maven.org/maven2/io/swagger/swagger-codegen-cli/.

 

Make sure to have java installed in your computer by runnning the following command :

 

foo@bar:~$ java -version

openjdk version "11.0.9.1" 2020-11-04
OpenJDK Runtime Environment (build 11.0.9.1+1-Ubuntu-0ubuntu1.18.04)
OpenJDK 64-Bit Server VM (build 11.0.9.1+1-Ubuntu-0ubuntu1.18.04, mixed mode, sharing)

 

Download the swagger.json file from the documentation page of your Mindee project.

 

 

 

 

Replace the "your_swagger.json" with the openai.json file you just downloaded and run the command :

 

foo@bar:~$ java -jar swagger-codegen-cli-version.jar generate -i your_swagger.json -l python -o your_api_client

 

 

You can now install your python sdk client :

 

  • Create a virtual env :
foo@bar:~$ python3 -m venv python_sdk_client_venv
foo@bar:~$ source python_sdk_client_venv/bin/activate

You can ignore this step if you are already using your own python3 virtual env.

 

  • Install your python sdk client via setuptools:
(python_sdk_client_venv) foo@bar:~$ cd your_api_client/
(python_sdk_client_venv) foo@bar:~$ python setup.py install

 

 

You are all set and ready to use your Python sdk client.

 

 

Usage

 

Create your api key from the API Keys page of your Mindee project: 

 

 

 

 

 

Using the predict endpoint

 

 

import swagger_client
from swagger_client.rest import ApiException
# replace XXX by your public api name in camel-case/ 
# for example, if your api is named api_name, replace XXXApi() by ApiNameApi()
api_instance = swagger_client.XXXApi()
authorization = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # your api key
version = "v1" # choose your version
file = "path/to/your/file"
feedback = "true"

try:
    api_response = api_instance.upload_file(
        authorization, version, file, feedback=feedback
    )
except ApiException as e:
    raise e

 

 

api_response is an object of type swagger_client.models.api_predict_response.ApiPredictResponse.

 

api_response.input_uuid # if you set feedback to 'true'
api_response.pages
api_response.predictions

 

 

api_response.pages.features is a list containing all the candidates the API found for ech field. Each item of the list corresponds to a page:

 

{
  'pages': {
    'candidates': [
      {
        'feature_1': [
          {
            'content': 'xxxx',
            'key': 'f0311d47',
            'segmentation': {
              'bounding_box': [
                [
                  0.2891,
                  0.0264
                ],
                [
                  0.4479,
                  0.0215
                ],
                [
                  0.4505,
                  0.0635
                ],
                [
                  0.2917,
                  0.0684
                ]
              ]
            }
          }
          }
          ....
        ],
        'feature_2': ....

        ]
      }
    }

 

 

api_response.predictions is a dict: containing the model's predictions for each feature. Note that if you're using the v0 version of your API, this object will be set to None as there is no trained model yet.

 

{
    'feature_1': [
                    {'content': 'xxx',
                     'key': 'c5d1daf7',
                     'page_id': 0,
                     'relative_vertices': [[0.0898, 0.0693],
                                           [0.2708, 0.0635],
                                           [0.2734, 0.1035],
                                           [0.0924, 0.1094]]
                    }, ....
                 ]
    'feature_2': ....
}

 

 

 

Using the feedback endpoint

 

 

Sending a feedback requires you to send an image to the /predict endpoint first with the param feedback=true . You'll then get a "input_uuid" key in your response that you need to feed in your /feedback call.

 

import swagger_client
from swagger_client.rest import ApiException
import json

input_uuid = 'input_uuid_example' # str | Input uuid from /predict endpoint to post the feedback on.
labels = dict() # dict feedback object.
# replace XXX by your public api name in camel-case/ 
# for example, if your api is named api_name, replace XXXApi() by ApiNameApi()
api_instance = swagger_client.XXXApi()
api_feedback_response = api_instance.send_feedback(authorization, input_uuid, json.dumps(labels))

 

 

labels must have the following format :

 

{
    'feature_1': {'page_id': 0, 'selected_keys': ['833b42a4', 'a70e7668']},
    'feature_2': ...
}