<jupyter_start><jupyter_text>Deploy a Python model in Machine Learning Server with `azureml-model-management-sdk` Package                                 ***Applies to: Machine Learning Server 9.2***  1. Read in the mtcars datasetFrom your local machine, let's begin by reading in the data we will use to build our model. We will use the dataset `mtcars`.<jupyter_code># -- Import the dataset from the microsoftml package
from microsoftml.datasets.datasets import DataSetMtCars
mtcars = DataSetMtCars()

# -- Represent the dataset as a dataframe.
mtcars = mtcars.as_df()

# -- print top rows of data to inspect the data
mtcars.head()<jupyter_output><empty_output><jupyter_text>2. Authenticate and initiate the  `DeployClient`There are several ways to authentication against Machine Learning Server from your local machine. The method you choose should match the authentication defined by your administrator. Please contact your administrator for authentication credentials. For simplicity, this example uses the local 'admin' account for authentication.  1. Begin by importing the DeployClient and MLServer classes from the [azureml-model-management-sdk package](https://docs.microsoft.com/en-us/r-server/python-reference/azureml-model-management-sdk/azureml-model-management-sdk) so you can connect to Machine Learning Server (`use=MLServer`).1. Then, **fill in your own connection details** for the host and context into the corresponding fields. Learn more in the article "[Connecting to Machine Learning Server in Python](https://docs.microsoft.com/en-us/r-server/operationalize/python/how-to-authenticate-in-python )."<jupyter_code># -- Import the DeployClient and MLServer classes from the azureml-model-management-sdk package.
from azureml.deploy import DeployClient
from azureml.deploy.server import MLServer

# -- Define the location of the ML Server --
# -- for local onebox for Machine Learning Server: http://localhost:12800
# -- Replace with connection details to your instance of ML Server. 
HOST = 'http://localhost:12800'
context = ('admin', 'YOUR_ADMIN_PASSWORD')
client = DeployClient(HOST, use=MLServer, auth=context)<jupyter_output><empty_output><jupyter_text>You are now authenticated. The **DeployClient** can interact with the web service management APIs to deploy, list, consume and so on.   3. Create and run a linear model locallyNow that you have built the authentication logic into your application, you can interact with the core APIs using functions in azureml-model-management-sdk to create a model and publish it as a web service.Create a GLM model called `cars_model` using the dataset `mtcars` we imported before. Using horsepower (hp) and weight (wt), this model estimates the probability that a vehicle has been fitted with a manual transmission.We use the [rx_lin_mod](https://docs.microsoft.com/en-us/r-server/python-reference/revoscalepy/rx-lin-mod) function from the [revoscalepy package](https://docs.microsoft.com/en-us/r-server/python-reference/revoscalepy/revoscalepy-package) to build the model.<jupyter_code># -- import the needed classes and functions
import pandas as pd
from revoscalepy import rx_lin_mod, rx_predict

# -- using rx_lin_mod from revoscalepy package
# -- create glm model with `mtcars` dataset
cars_model = rx_lin_mod(
    formula='am ~ hp + wt',
    data=mtcars)

# -- provide some sample inputs to test the model
mydata = pd.DataFrame({
    'hp':[120],
    'wt':[2.8]
})
mydata

# -- predict locally
rx_predict(cars_model, data=mydata)<jupyter_output>Rows Read: 32, Total Rows Processed: 32, Total Chunk Time: Less than .001 seconds 
Computation time: 0.005 seconds.
Rows Read: 1, Total Rows Processed: 1, Total Chunk Time: Less than .001 seconds<jupyter_text>Examine the results of the locally executed code. You can compare these results to the results of the web service in this next step.  4. Publish the model as a web serviceNow let's:+ Define an initialization function + Produce a prediction function+ Publish the linear model as a Python web serviceYou can define an 'init' function to be bootstrapped to the web service. This 'init' function handles service initialization. Use it to load the packages, datasets, and global variables you need when the service is called the first time. One caveat, however, is that all imports are scoped to the 'init' function and not to the global namespace. Consequently, you must still import the modules in each run or consume function.<jupyter_code># --Define an `init` function to handle service initialization --
def init():
    import pandas as pd
    from revoscalepy import rx_predict<jupyter_output><empty_output><jupyter_text>Produce a prediction function called `manualTransmission` that can use the `cars_model` model. This function will be part of the code `code_fn` supplied when the service is published.<jupyter_code>def manualTransmission(hp, wt):
    import pandas as pd
    from revoscalepy import rx_predict
    
    # -- make the prediction use model `cars_model` and input data --
    newData = pd.DataFrame({'hp':[hp], 'wt':[wt]})
    answer = rx_predict(cars_model, newData, type='response')
    
    # -- save some files to demonstrate the ability to return file artifacts --
    answer.to_csv('answer.csv')
    # return prediction
    return answer<jupyter_output><empty_output><jupyter_text>Now we can publish the model as a web service called `"cars_model"` to Machine Learning Server. This service uses the model `carsModel`, the function `manualTransmission`, and the `init` function. As an input, the service takes a list of vehicle horsepower and vehicle weight represented as a float. As an output, a percentage as a dataframe for the probability each vehicle has of being fitted with a manual transmission.When publishing a service, you can specify its name and version, the code functions, the inputs, and the outputs needed for application integration as well as other parameters. NOTE ON THE BASICS OF SERVICE PUBLISHING API:The fluent APIS are designed for optional configurations where the readability of the invocation is close to that of the ordinary written prose (grammatical structure). A publish can be initiated by invoking the `client.service(name)` object then calling `.deploy()`  to send the publish request.<jupyter_code>service_name = 'TxService'

service = client.service(service_name)\
        .version('1.0')\
        .code_fn(manualTransmission, init)\
        .inputs(hp=float, wt=float)\
        .outputs(answer=pd.DataFrame)\
        .models(cars_model=cars_model)\
        .description('My first python model')\
        .artifacts(['answer.csv'])\
        .deploy()<jupyter_output><empty_output><jupyter_text>5. Explore and consume the published web serviceNow let's:+ Use the help function to explore the published service. You can call the `help` function on any `azureml-model-management-sdk` functions, even those we dynamically generated ones to learn more about them.+ Print the capabilities that define the service holdings: service name, version, descriptions, inputs, outputs, and the name of the function to be consumed.+ Predict an outcome.+ Download the Swagger-based JSON file.  This file is auto-generated at deploy time.  You can share it with any authenticated users so they can test and consume the service.   **You can share the resulting file with application developers or others testing your service.** Learn more about [exploring and consuming in this notebook](https://github.com/Microsoft/ML-Server-Python-Samples/blob/master/web-services/deploy-consume/Explore_Consume_Python_Web_Services.ipynb).<jupyter_code>print(help(service))<jupyter_output>Help on TxserviceService in module azureml.deploy.server.service object:

class TxserviceService(Service)
 |  Service object from metadata.
 |  
 |  Method resolution order:
 |      TxserviceService
 |      Service
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __init__(self, service, http_client)
 |      Constructor
 |      
 |      :param service:
 |      :param http_client:
 |  
 |  __str__(self)
 |      Return str(self).
 |  
 |  batch(self, records, parallel_count=10)
 |      Register a set of input records for batch execution on this service.
 |      
 |      :param records: The `data.frame` or `list` of
 |             input records to execute.
 |      :param parallel_count: Number of threads used to process entries in
 |             the batch. Default value is 10. Please make sure not to use too
 |             high of a number because it might negatively impact performance.
 |      :return: The `Batch` object to control service batching
 |              lifecycle[...]<jupyter_text>Explore all available functions on the service object by calling `capabilities`.<jupyter_code>service.capabilities()<jupyter_output><empty_output><jupyter_text>Since you are in the same session as the one you in which you deployed, you can consume it using the _service api_ object returned from `.deploy()`. You can verify that the results are as expected and that they match the results obtained when the model was run locally earlier.<jupyter_code>res = service.manualTransmission(120, 2.8)

# -- Pluck out the named output `answer` as defined during publishing and print --
print(res.output('answer'))<jupyter_output>am_Pred
0  0.533267<jupyter_text>Now you can grab the Swagger-based JSON file, which defines the service.<jupyter_code># -- Retrieve the URL of the swagger file for this service.
cap = service.capabilities()
swagger_URL = cap['swagger']
print(swagger_URL)<jupyter_output>http://localhost:12800/api/TxService/1.0/swagger.json<jupyter_text>Learn more about how to [list, get, explore and consume web services in this notebook](https://github.com/Microsoft/ML-Server-Python-Samples/blob/master/operationalize/Explore_Consume_Python_Web_Services.ipynb).  6. Delete servicesYou can call `delete_service` on the `DeployClient` object to delete a specific service on the running Machine Learning Server.<jupyter_code>client.delete_service('TxService', version='1.0')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07922954725972994, 'fraction_numerical': 0.006850675138999206, 'mean_word_length': 4.459620596205962, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '447368', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2685, 'n_words': 1131, 'path': 'operationalize/Quickstart_Publish_Python_Web_Service.ipynb', 'repo_name': 'mounirHai/ML-Server-Python-Samples', 'size': '10072'}