def deploy_new_web_service(workspace, service_name, aciconfig, image_config, model): service = Webservice.deploy_from_model(workspace=workspace, name=service_name, deployment_config=aciconfig, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) print('The URI to access the web service is: ', service.scoring_uri)
def deployModelAsWebService( ws, model_folder_path="models", model_name="component_compliance", scoring_script_filename="scoring_service.py", conda_packages=['numpy', 'pandas'], pip_packages=['azureml-sdk', 'onnxruntime'], conda_file="dependencies.yml", runtime="python", cpu_cores=1, memory_gb=1, tags={'name': 'scoring'}, description='Compliance classification web service.', service_name="complianceservice"): # notice for the model_path, we supply the name of the outputs folder without a trailing slash # this will ensure both the model and the customestimators get uploaded. print("Registering and uploading model...") registered_model = Model.register(model_path=model_folder_path, model_name=model_name, workspace=ws) # create a Conda dependencies environment file print("Creating conda dependencies file locally...") from azureml.core.conda_dependencies import CondaDependencies mycondaenv = CondaDependencies.create(conda_packages=conda_packages, pip_packages=pip_packages) with open(conda_file, "w") as f: f.write(mycondaenv.serialize_to_string()) # create container image configuration print("Creating container image configuration...") from azureml.core.image import ContainerImage image_config = ContainerImage.image_configuration( execution_script=scoring_script_filename, runtime=runtime, conda_file=conda_file) # create ACI configuration print("Creating ACI configuration...") from azureml.core.webservice import AciWebservice, Webservice aci_config = AciWebservice.deploy_configuration(cpu_cores=cpu_cores, memory_gb=memory_gb, tags=tags, description=description) # deploy the webservice to ACI print("Deploying webservice to ACI...") webservice = Webservice.deploy_from_model(workspace=ws, name=service_name, deployment_config=aci_config, models=[registered_model], image_config=image_config) webservice.wait_for_deployment(show_output=True) return webservice
def run(model_path, model_name): auth_args = { 'tenant_id': os.environ['TENANT_ID'], 'service_principal_id': os.environ['SERVICE_PRINCIPAL_ID'], 'service_principal_password': os.environ['SERVICE_PRINCIPAL_PASSWORD'] } ws_args = { 'auth': ServicePrincipalAuthentication(**auth_args), 'subscription_id': os.environ['SUBSCRIPTION_ID'], 'resource_group': os.environ['RESOURCE_GROUP'] } ws = Workspace.get(os.environ['WORKSPACE_NAME'], **ws_args) print(ws.get_details()) print('\nSaving model {} to {}'.format(model_path, model_name)) model = Model.register(ws, model_name=model_name, model_path=model_path) print('Done!') print('Checking for existing service {}'.format(model_name)) service_name = 'simplemnist-svc' if model_name in ws.webservices: print('Found it!\nRemoving Existing service...') ws.webservices[model_name].delete() print('Done!') else: print('Not found, creating new one!') # image configuration image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="environment.yml") # deployement configuration aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, description=model_name) # deploy service = Webservice.deploy_from_model(workspace=ws, name=model_name, models=[model], image_config=image_config, deployment_config=aciconfig) service.wait_for_deployment(show_output=True) #print logs print(service.get_logs()) print('Done!')
def deploy(aciconfig, envfile, name, model): # configure the image image_config = ContainerImage.image_configuration( execution_script="./score.py", runtime="python", conda_file=envfile) service = Webservice.deploy_from_model(workspace=ws, name=name, deployment_config=aciconfig, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) print(service.scoring_uri)
def deployWebservice(ws, args, folders): # this section requries that the processing is done in the directory where the execution script and the conda_file resides os.chdir(folders.script_folder) model = Model(ws, args.modelName) aciconfig = AciWebservice.deploy_configuration(cpu_cores=args.cpuCores, memory_gb=args.memoryGB) # configure the image image_config = ContainerImage.image_configuration( execution_script=args.scoringScript, runtime="python", conda_file=args.environmentFileName) service = Webservice.deploy_from_model(workspace=ws, name=args.webserviceName, deployment_config=aciconfig, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) return service.scoring_uri
def deploy_service(execution_script, conda_file, aciconfig, service_name, model, workspace, runtime="python"): image_config = ContainerImage.image_configuration( execution_script=execution_script, runtime=runtime, conda_file=conda_file) service = Webservice.deploy_from_model(workspace=workspace, name=service_name, deployment_config=aciconfig, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) print(service.scoring_uri) return service
myenv.add_conda_package("scikit-learn") with open("myenv.yml", "w") as f: f.write(myenv.serialize_to_string()) print("Finished Writing Conda File") print("Defined deploy configuration for ACI") aciconfig = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, tags={ "data": "MNIST", "method": "sklearn" }, description='Predict MNIST with sklearn') print("Configuring Image") # configure the image image_config = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="myenv.yml") service = Webservice.deploy_from_model(workspace=ws, name='sklearn-mnist-svc', deployment_config=aciconfig, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) print(service.scoring_uri)
aks_config = AksWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, #collect_model_data=True, enable_app_insights=True, tags={ "data": "flower_photos", "method": "TensorFlow" }, description='Predict flowers with TensorFlow') print("Creating the image and deploy as web service...") service = Webservice.deploy_from_model(workspace=ws, name=service_name, deployment_config=aks_config, deployment_target=aks_target, models=[model_graph, model_labels], image_config=image_config) service.wait_for_deployment(show_output=True) print(service.state) print("Service URI:", service.scoring_uri) print("Testings web service via SDK...") file_name = "./resources/test-images/Daisy1.jpg" for dirpath, dnames, fnames in os.walk("./resources/test-images/"): for f in fnames: file_name = os.path.join(dirpath, f) # load image
# <retrieveModel> from azureml.core.model import Model model_name = "sklearn_mnist" model = Model(ws, model_name) # </retrieveModel> # ## DEPLOY FROM REGISTERED MODEL # <option2Deploy> from azureml.core.webservice import Webservice service_name = 'aci-mnist-2' service = Webservice.deploy_from_model( deployment_config=aciconfig, image_config=image_config, models=[model], # this is the registered model object name=service_name, workspace=ws) service.wait_for_deployment(show_output=True) print(service.state) # </option2Deploy> service.delete() # ## DEPLOY FROM IMAGE # <option3CreateImage> from azureml.core.image import ContainerImage image = ContainerImage.create( name="myimage1",
from azureml.core.webservice import AciWebservice, Webservice aci_config = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, tags={'name': 'Azure ML ACI'}, description='This is a great example.') # Step 7 -Deploy the webservice to ACI ###################################### service_name = "usedcarsmlservice01" webservice = Webservice.deploy_from_model( workspace=ws, name=service_name, deployment_config=aci_config, models=[registered_model], image_config=image_config, ) webservice.wait_for_deployment(show_output=True) # Step 8 - Test the ACI deployed webservice ########################################### import json age = 60 km = 40000 test_data = json.dumps([[age, km]]) test_data result = webservice.run(input_data=test_data) print(result)
#conda_dependency = CondaDependencies.create(pip_packages=['azureml-defaults', 'tensorflow']) with open('myenv.yml', 'w') as f: f.write(conda_dependency.serialize_to_string()) # Create image configuration image_conf = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="myenv.yml") #%% [markdown] # Deploy as a web service ! #%% svc = Webservice.deploy_from_model(name='my-mnist-service', deployment_config=aci_conf, models=[registered_model], image_config=image_conf, workspace=ws) svc.wait_for_deployment(show_output=True) #%% # See details, if error has occured print(svc.get_logs()) #%% [markdown] # Check service url #%% svc.scoring_uri #%% [markdown]
from sklearn.externals import joblib mods = Model.list(ws, name=None, tags=None, properties=None, run_id=None, latest=False) print(mods) model_path = Model.get_model_path('diabetes', _workspace=ws) print(model_path) model = joblib.load(model_path) from azureml.core.webservice import AciWebservice aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, tags={"data": "diabetes", "method": "sklearn"}, description='Predict diabetes with sklearn') # configure the image image_config = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="myenv.yml") service = Webservice.deploy_from_model(workspace=ws, name='diabetes-svc', deployment_config=aciconfig, models=[mods[0]], image_config=image_config) service.wait_for_deployment(show_output=True) print(service.scoring_uri)
# deploy to aci from azureml.core.webservice import Webservice from azureml.core.image import ContainerImage # configure the image image_config = ContainerImage.image_configuration( execution_script="weather_data_extractor.py", runtime="python", conda_file="weather_env_file.yml", description="Weather Data Extractor", tags={"data": "Weather"}) service = Webservice.deploy_from_model(workspace=ws, name='weatherdataextractor', deployment_config=myaci_config, models=[], image_config=image_config) service.wait_for_deployment(show_output=True) # print the uri of the web service print(service.scoring_uri) #### test the web service import requests import json data = requests.post(service.scoring_uri, manager.weather_api_token, headers={'Content-Type': 'application/json'})
from azureml.core.image import ContainerImage from azureml.core.model import Model from azureml.core.webservice import Webservice from azureml.core import Workspace image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="predictor_env.yml", description="Environment definitions") service = Webservice.deploy_from_model(workspace=ws, name='Agrix-Predictions', models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) print(service.scoring_uri)
from azureml.core.webservice import AciWebservice, Webservice from azureml.core.image import ContainerImage # Define the configuration of compute: ACI with 1 cpu core and 1 gb of memory. aci_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1) # Specify the configuration of image: scoring script, Python runtime (PySpark is the other option), and conda file of library dependencies. image_config = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="myenv.yml") # Deploy the web service as an image containing the registered model. service = Webservice.deploy_from_model(name="area-calculator", deployment_config=aci_config, models=[model], image_config=image_config, workspace=ws) # The service deployment can take several minutes: wait for completion. service.wait_for_deployment(show_output=True) # You can try out the web service by passing in data as json-formatted request. Run the cell below and move the slider around to see real-time responses. # In[ ]: from ipywidgets import interact def get_area(radius): request = json.dumps({"radius": radius})
%%time image_config = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="Version3/ta_env.yml") aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, tags={"data": "lengthcount", "method" : "lencount"}, description='Predict Sentence Length') service = Webservice.deploy_from_model(workspace=ws, name='lengthcount', deployment_config=aciconfig, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) service.scoring_uri !curl -X POST \ -H 'Content-Type':'application/json' \ -d 'Hello, you fool, I love you, wanna go on a joy ride'\ http://20.42.26.191:80/score
# store model id in json file update_json(options.params,{"model_id": model_id}) else: # from list of models, pick newest one with the provided name models = [x for x in ws.models() if x.name==model_name] import dateutil.parser model_id = sorted(models,key=lambda x: dateutil.parser.parse(x.created_time))[-1].id # store model id in json file update_json(options.params,{"model_id": model_id}) else: # use stored model id model_id = get_from_json(options.params,"model_id") service = None if len(service_name)>0: try: service = Webservice(ws,service_name) if options.delete: print("Deleting: "+str(service.id)) service.delete() service = None except WebserviceException: if not options.delete: service = Webservice.deploy_from_model(ws, service_name, [Model(ws,id=model_id)], BrainwaveImage.image_configuration(), BrainwaveWebservice.deploy_configuration()) service.wait_for_deployment(True) if service is not None: update_json(options.params, {"address": service.ip_address, "port": service.port})
elif CONDA_FILE_URL is not '' and DOCKER_FILE_URL == '': wget.download(CONDA_FILE_URL, CONDA_FILE_PATH) image_config = ContainerImage.image_configuration( execution_script=EXECUTION_SCRIPT_PATH, runtime="python", conda_file=CONDA_FILE_PATH) elif DOCKER_FILE_URL is not '': wget.download(DOCKER_FILE_URL, DOCKER_FILE_PATH) image_config = ContainerImage.image_configuration( execution_script=EXECUTION_SCRIPT_PATH, runtime="python", docker_file=DOCKER_FILE_PATH) #Deploy the service service = Webservice.deploy_from_model(workspace=ws, name=SERVICE_NAME, deployment_config=aciconfig, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) print("SERVICE_ENDPOINT", service.scoring_uri) variables.put("SCORING_URI", service.scoring_uri) if AUTH_ENABLED: primary, secondary = service.get_keys() variables.put("SERVICE_KEY", primary) print("END " + __file__)
print("delete " + par_service_name + " before creating new one") oldservice.delete() except: print(par_service_name + " does not exist, create new one") # COMMAND ---------- from azureml.core.image import ContainerImage from azureml.core.webservice import AciWebservice, Webservice image_config = ContainerImage.image_configuration( execution_script="score_deeplearning.py", runtime="python", conda_file="deeplearningenv.yml") aci_config = AciWebservice.deploy_configuration( cpu_cores=2, memory_gb=4, tags={'name': 'Databricks ALM ACI'}, description='AML Deployment Production') # COMMAND ---------- service = Webservice.deploy_from_model(workspace=ws, name=par_service_name, deployment_config=aci_config, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True)
from azureml.core.image import ContainerImage # configure the image image_config = ContainerImage.image_configuration( execution_script="web_service_score.py", runtime="python", conda_file="myenv.yml", description="Auto ML model", tags={ "data": "titanic", "type": "classification" }) service = Webservice.deploy_from_model(workspace=ws, name='automlwebservice', deployment_config=myaci_config, models=[mymodel], image_config=image_config) service.wait_for_deployment(show_output=True) # print the uri of the web service print(service.scoring_uri) #### test the web service import requests import json # we don't want to send nan to our webservice. Replace with 0. test_data = pd.read_csv("data/titanic_test.csv").fillna(value=0).values # send a random row from the test set to score
%%time image_config = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="ta_env.yml") aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1, tags={"data": "textanalytics", "method" : "textblob"}, description='Predict Sentiment of Sentences') service = Webservice.deploy_from_model(workspace=ws, name='ta-textblob', deployment_config=aciconfig, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) service.scoring_uri !curl -X POST \ -H 'Content-Type':'application/json' \ -d 'Hello, you fool, I love you, wanna go on a joy ride'\ http://20.42.27.28:80/score
print('Complete') #%% deploy %%time from azureml.core.webservice import Webservice from azureml.core.image import ContainerImage # configure the image image_config = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="mymodelenv.yml") service = Webservice.deploy_from_model(workspace=ws, name='dengdigideng3', deployment_config=aciconfig, models=['.\iq_best_model.pkl', '.\sj_best_model.pkl',], image_config=image_config) service.wait_for_deployment(show_output=True) print('Complete') #%% print swervice logs print(service.get_logs()) #%% print scoring uri print(service.scoring_uri) #%% test service object import json
service_name = "smsspam" runtime = "spark-py" driver_file = "score_sparkml.py" my_conda_file = "mydeployenv.yml" # image creation from azureml.core.image import ContainerImage myimage_config = ContainerImage.image_configuration( execution_script=driver_file, runtime=runtime, conda_file=my_conda_file) # COMMAND ---------- # Webservice creation myservice = Webservice.deploy_from_model(workspace=ws, name=service_name, deployment_config=myaci_config, models=[mymodel], image_config=myimage_config) myservice.wait_for_deployment(show_output=True) # COMMAND ---------- #for using the Web HTTP API print(myservice.scoring_uri) # COMMAND ---------- json_ex = """[ { \"cleantext\": \"Incredible! You won a 1 month FREE membership in our prize ruffle! Text us at 09061701461 to claim \" }, { \"cleantext\": \"Hi darling, this is a good message and I think you will receive it. Love you, see you later!\" }]"""
# In[7]: #Deploy the model in Azure %%time from azureml.core.webservice import Webservice from azureml.core.image import ContainerImage # configure the image image_config = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="myenv.yml") service = Webservice.deploy_from_model(workspace=ws, name='emp-churn-model-ci', deployment_config=aciconfig, models=[model], image_config=image_config) service.wait_for_deployment(show_output=True) # In[ ]: print(service.get_logs()) # In[8]: