Skip to content

Python SDK Tutorial Part 2

Albert Suarez edited this page Oct 27, 2022 · 6 revisions

Tutorial Intro & Setup

So now that you already know how to use our API for calling one solution, let's see how to power up this calling multiple solutions with just one API request.

API Request Breakdown

To start with, let's see what an Multipredict API request looks like; here's an example:

https://api-us.restb.ai/vision/v2/multipredict?client_key=YOUR_CLIENT_KEY_HERE&model_id=re_roomtype_global_v2&image_url=https://demo.restb.ai/images/demo/demo-1.jpg

What's the difference? The API endpoint:

multipredict

This is the API endpoint which allows the user to use multiple Computer Vision solutions that Restb.ai has.

Parameters:

client_key=YOUR_CLIENT_KEY_HERE
model_id=re_roomtype_global_v2,re_features_v4,re_logo,re_compliance_v2,re_condition_r1r6_global
image_url=https://demo.restb.ai/images/demo/demo-6.jpg

These are the required parameters that are necessary for every multipredict API request. They are the same as before (predict endpoint) but adding a small difference in the model_id parameter:

  • model_id - to specify multiple solutions, there are two ways to facilitate this:
    • multiple model_id parameters: the easiest way to specify multiple solutions is to simply specify multiple model_id parameters. For example, instead of simply passing in &model_id=re_roomtype_global_v2, developers can now pass in &model_id=re_roomtype_global_v2&model_id=re_features_v4&model_id=re_compliance_v2. As many model_id parameters can be provided as desired, although note that duplicates will not be processed.
    • comma-separated list: the other supported way to specify multiple model_id parameters is to specify all desired solutions via a comma-separated list as the parameter value. For example: model_id=re_roomtype_global_v2,re_features_v4,re_compliance_v2. This is useful when utilizing a library or programming language that doesn't support passing in multiple HTTP parameters (GET or POST) with the same name. Note that there should be no whitespace in the parameter value.

Implementing a basic, single-threaded client

Skipping all the steps that we did in the tutorial before, we start creating a new example where we are gonna test the multipredict endpoint.

Next, we will need to define a function to actually facilitate our test; let's call it test_api:

from restb.sdk import *
from restb.sdk.api import service


def test_api(client_key):

    # note the module variables as defined in restb/sdk/__init__.py
    params = __PARAMS.copy()

    # 1. Pick which API endpoint to use (US vs. EU)
    url = __URL_US

    # 2. Determine which solution (API and model) to use
    model_id = ','.join(__MODELS)
    endpoint = __ENDPOINT_MULTIPREDICT
    params['model_id'] = model_id

    # 3. Insert in your client_key
    params['client_key'] = client_key

    # 4. Pick an image_url
    image_url = 'https://demo.restb.ai/images/demo/demo-1.jpg'
    params['image_url'] = image_url

    # 5. Call the API
    return service(url=url, endpoint=endpoint, params=params)


def run(client_key):
    response = test_api(client_key)
    print(response.text)

Let's explain the difference between the predict and multipredict scenarios:

  • Using the __MODELS global variable, we are joining this array (which contains all the solutions that our API has) in a single string comma-separated.
  • Instead of using the __ENDPOINT variable, we've switched to __ENDPOINT_MULTIPREDICT.

Then, the last missing step is invoking it from the previously created run.py script:

if __name__ == '__main__':
    client_key = 'YOUR_CLIENT_KEY_HERE'
    print('1. running basic example')
    basic.run(client_key)
    print('2. running multipredict example')
    multipredict.run(client_key)

And finally to run this, just execute the following command again:

python3 -m restb.examples.run

And that's it! At this point, you have a functional client that can successfully integrate with our API calling! Navigate onwards to part 3 to learn how to enhance this basic client with concurrent rate limiting.