Last active
December 14, 2017 19:53
-
-
Save ramnov/6df470dc3d492439e5035f3e308ce78b to your computer and use it in GitHub Desktop.
Python code to Publish ManualTransmission Web Service and generate csharp client using swagger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----------------------------------------------------# | |
# STEP 1 : MODEL DEVELOPMENT | |
# ----------------------------------------------------# | |
# imports | |
from microsoftml.datasets.datasets import DataSetMtCars | |
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=DataSetMtCars().as_df()) | |
# -- provide some sample inputs to test the model | |
mydata = pd.DataFrame({ | |
'hp':[120], | |
'wt':[2.8] | |
}) | |
rx_predict(cars_model, data=mydata) | |
# ----------------------------------------------------# | |
# STEP 2 : PUBLISH WEBSERVICE | |
# ----------------------------------------------------# | |
# Define Function to deploy as webservice | |
def manualTransmission(hp, wt): | |
import pandas as pd | |
from revoscalepy import rx_predict | |
newData = pd.DataFrame({'hp':[hp], 'wt':[wt]}) | |
return rx_predict(cars_model, newData, type='response') | |
# Import the DeployClient and MLServer classes from the azureml-model-management-sdk package. | |
from azureml.deploy import DeployClient | |
from azureml.deploy.server import MLServer | |
from azureml.common.configuration import Configuration | |
# 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 = ('<username>', '<password>') | |
client = DeployClient(HOST, use=MLServer, auth=context) | |
# Deploy the web service | |
service_name = 'ManualTransmissionService' | |
service_version = '1.0.0' | |
service = client.service(service_name)\ | |
.version(service_version)\ | |
.code_fn(manualTransmission)\ | |
.inputs(hp=float, wt=float)\ | |
.outputs(answer=pd.DataFrame)\ | |
.models(cars_model=cars_model)\ | |
.description('Manual Transmission Service')\ | |
.deploy() | |
# Invoke the service with sample values | |
res = service.manualTransmission(120, 2.8) | |
# Pluck out the named output `answer` as defined during publishing and print | |
print(res.output('answer')) | |
# Save service swagger to json file | |
print(service.swagger()) | |
with open("manual-transmission-service-swagger.json", "w") as swagger_file: | |
swagger_file.write("%s" % service.swagger()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment