Use Conda Environments

Conda is an open-source package and environment management system that enables the use of environments containing third-party Python libraries.

Prerequisites

To run the OML4Py embedded Python execution REST APIs using conda environments, you need to save the Python scripts you want to run in the OML4Py script repository or have the Python scripts granted from other users. The conda environment also needs to be created and uploaded to Object Storage by the ADMIN user.

Environment Used in Examples

ADMIN user is required to create and manage environments. The following creates a mypyenv environment based on Python version 3.10 and uploads the environment to the Object Storage owned by the PDB. The data visualization library seaborn is installed in the environment.

create -n mypyenv python=3.10 seaborn 
upload mypyenv -t application OML4PY

Obtain token

Exchange database user credential for an access token using Oracle Machine Learning User Management Cloud Service REST endpoint /oauth2/v1/token. For more information, see Authenticate.

$ curl -X POST --header 'Content-Type: application/json' --header 'Accept:
application/json'
-d '{"grant_type":"password", "username":"'${username}'", "password":"'${password}'"}'
"<oml-cloud-service-location-url>/omlusers/api/oauth2/v1/token"

Data that Some Scripts Use

Table creation

The following Python code creates the IRIS table, which contains data that someof the scripts use.

%python

import oml
from sklearn.datasets import load_iris
import pandas as pd
iris = load_iris()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
x = pd.DataFrame(iris.data, columns = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width'])
y = pd.DataFrame(list(map(lambda x: {0: 'setosa', 1: 'versicolor', 2:'virginica'}[x], iris.target)), columns = ['Species'])
oml_iris = oml.create(pd.concat([x, y], axis=1), table = 'IRIS')
print("Shape of IRIS table:", oml_iris.shape)

Script Creation

test_seaborn_no inp

The script imports the seaborn library and creates a distribution plot. The following defines the script and stores it in the script repository.

begin
  sys.pyqScriptCreate('test_seaborn_noinp',
  'def fun_tab():
    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    data = np.random.multivariate_normal([0, 0], [[5, 2], [2,
    2]], size=2000)
    data = pd.DataFrame(data, columns=["x", "y"])
    sns.displot(data["x"])
    plt.title("Dist plot")
    plt.show()
    return "hello world"
  ',FALSE,TRUE);
end;
/

The following cURL command runs the script test_seaborn_noinp saved in the user's OML4Py script repository using the REST API for embedded Python execution. The conda environment name parameter envName is set to mypyenv, and the graphicsFlag is set to true because the script returns an image.

curl -i -k -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json' 
-d '{"envName": "mypyenv","graphicsFlag": true}' "<oml-cloud-service-location-url>/oml/api/py-scripts/v1/do-eval/test_seaborn_noinp"

The output contains both the PNG image and data returned by the script.

{
    "result": [
        {
            "IMAGE": "iVBORw0KGgoAAAANSUhEUgAAA......BJRU5ErkJggg==",
            "DATA": "\"hello world\"",
            "TITLE": "Lineplot",
            "ID": 1
        }
    ]
}

test_seaborn_inp

The script imports the seaborn library and creates a line plot with the input data. The following defines the script and stores it in the script repository.

begin
  sys.pyqScriptCreate('test_seaborn_inp',
  'def fun_tab(dat):
    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.lineplot(x="Sepal_Length", y="Sepal_Width", data=dat)
    plt.title("Iris plot")
    plt.show()
    return "hello world"
  ',FALSE,TRUE);
end;
/

The following cURL command runs the script test_seaborn_inp saved in the user's OML4Py script repository using the REST API for embedded Python execution. The conda environment name parameter envName is set to mypyenv, and the graphicsFlag is set to true because the script returns an image.

curl -i -k -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json' 
-d '{"input": "select * from IRIS", "rows": 50, "envName": "mypyenv", "graphicsFlag": true}' "<oml-cloud-service-location-url>/oml/api/py-scripts/v1/row-apply/test_seaborn_inp"

The output contains both the PNG image and data returned by the script.

{
    "result": {
        "0": {
            "IMAGE": "iVBORw0KGgoAAAANSUhEUg......AAABJRU5ErkJggg==",
            "DATA": "\"hello world\"",
            "TITLE": "Iris plot",
            "ID": 1,
            "CHUNK": 1
        },
        "1": {
            "IMAGE": "iVBORw0KGgoAAAANSUhEUg......tAAAAAElFTkSuQmCC",
            "DATA": "\"hello world\"",
            "TITLE": "Iris plot",
            "ID": 1,
            "CHUNK": 2
        },
        "2": {
            "IMAGE": "iVBORw0KGgoAAAANSUhEUg......AAABJRU5ErkJggg==",
            "DATA": "\"hello world\"",
            "TITLE": "Iris plot",
            "ID": 1,
            "CHUNK": 3
        }
    }
}

test_seaborn_idx

The script imports the seaborn library and produces a distribution plot with title. The following defines the script and stores it in the script repository.

begin
  sys.pyqScriptCreate('test_seaborn_idx',
  'def fun_tab(idx):
    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    data = np.random.multivariate_normal([0, 0], [[5, 2], [2,
    2]], size=2000)
    data = pd.DataFrame(data, columns=["x", "y"])
    sns.displot(data["x"])
    plt.title("Title {}".format(idx))
    plt.show()
    return idx
  ',FALSE,TRUE);
end;
/

The following cURL command runs the script test_seaborn_idx saved in the user's OML4Py script repository using the REST API for embedded Python execution. The conda environment name parameter envName is set to mypyenv, and the graphicsFlag is set to true because the script returns an image.

curl -i -k -X POST --header "Authorization: Bearer ${token}" --header 'Content-Type: application/json' --header 'Accept: application/json' 
-d '{"times": 2, "envName": "mypyenv", "graphicsFlag": true, "asyncFlag": true}' "<oml-cloud-service-location-url>/oml/api/py-scripts/v1/index-apply/test_seaborn_idx"

The output is similar to the following:

HTTP/1.1 201 Created
Date: Mon, 19 Sep 2022 19:48:38 GMT
Content-Length: 0
Connection: keep-alive
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1;mode=block
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Content-Security-Policy: frame-ancestors 'none'
Set-Cookie: JSESSIONID=node01emy4t8xt83rffyr498lwofji7.node0; Path=/oml; Secure; HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: https://<oml-service-url>/oml/api/py-scripts/v1/jobs/<job_id>