示例#1
0
def get_conda_depencies():
    conda_dep = CondaDependencies()
    conda_dep.set_python_version("3.6.2")

    with open('compute_pip_requirements.txt') as f:
        for requirement in f:
            conda_dep.add_pip_package(str(requirement))

    with open('compute_conda_requirements.txt') as f:
        for requirement in f:
            conda_dep.add_conda_package(str(requirement))

    return conda_dep
# ***************** 1. CREATE PythonScriptStep1: DATA PREPARATION **************************

# ...which needs a RunConfiguration object that specifies the right Conda/Pip dependencies. So we proceed as follows in 3 steps:

# 1.A) Create PipelineData Object datapreparation_output
from azureml.pipeline.core import PipelineData
is_directory = False  # it's a file where we save the prepared dataframe
default_datastore = ws.get_default_datastore()
datapreparation_output = PipelineData('datapreparation_output',
                                      datastore=default_datastore,
                                      is_directory=is_directory)

# 1.B) Create the dependency object with mlextend package https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.conda_dependencies.condadependencies?view=azure-ml-py:
from azureml.core.environment import CondaDependencies
conda_dep_prep = CondaDependencies()
conda_dep_prep.add_pip_package(
    "mlxtend==0.17.2")  # or conda_dep.add_conda_package("mlxtend==0.17.2")

# 1.C) Create the RunConfiguration object:
from azureml.core import RunConfiguration
run_config_prep = RunConfiguration(conda_dependencies=conda_dep_prep)

# 1.D) Create the PythonScriptStep
from azureml.pipeline.steps import PythonScriptStep
data_preparation_step = PythonScriptStep(
    name="1: Data preparation",
    script_name="1-data_preparation.py",
    compute_target=compute_target,
    runconfig=run_config_prep,
    arguments=[
        "--remoteDataFolder", remote_data_folder, "--localDataFolder",
        local_data_to_download_folder, "--datapreparation_output",
#création du path complet dans le blob storage
path_model_datastore = "azureml/" + step1_runid + "/outputs/"
#download en local du modele
def_data_store.download(target_path='datastore', prefix=path_model_datastore)
#chargement dans la workspace a partir du modele download en local
model = Model.register(workspace=ws,
                       model_path='datastore/' + path_model_datastore +
                       model_pklname + '.pkl',
                       model_name=model_basename)

#Create environment file
from azureml.core.conda_dependencies import CondaDependencies

myenv = CondaDependencies()
myenv.add_conda_package("scikit-learn")
myenv.add_pip_package("azureml-defaults")
myenv.add_conda_package("numpy")
myenv.add_conda_package("pandas")

# import os
# old_wd = os.getcwd()
# os.chdir(old_wd+"/source")

with open("myenv.yml", "w") as f:
    f.write(myenv.serialize_to_string())

#Review the content of the myenv.yml file:
with open("myenv.yml", "r") as f:
    print(f.read())

# register image
# In[5]:

get_ipython().run_cell_magic(
    'writefile', 'summarizer_service.py',
    '\nimport re\nimport nltk\nimport unicodedata\nfrom gensim.summarization import summarize, keywords\n\ndef clean_and_parse_document(document):\n    if isinstance(document, str):\n        document = document\n    elif isinstance(document, unicode):\n        return unicodedata.normalize(\'NFKD\', document).encode(\'ascii\', \'ignore\')\n    else:\n        raise ValueError("Document is not string or unicode.")\n    document = document.strip()\n    sentences = nltk.sent_tokenize(document)\n    sentences = [sentence.strip() for sentence in sentences]\n    return sentences\n\ndef summarize_text(text, summary_ratio=None, word_count=30):\n    sentences = clean_and_parse_document(text)\n    cleaned_text = \' \'.join(sentences)\n    summary = summarize(cleaned_text, split=True, ratio=summary_ratio, word_count=word_count)\n    return summary \n\ndef init():  \n    nltk.download(\'all\')\n    return\n\ndef run(input_str):\n    try:\n        return summarize_text(input_str)\n    except Exception as e:\n        return (str(e))'
)

# In[7]:

from azureml.core import Environment
from azureml.core.environment import CondaDependencies

myenv = Environment(name="myenv")
conda_dep = CondaDependencies()
conda_dep.add_pip_package("gensim")
conda_dep.add_pip_package("nltk")
myenv.python.conda_dependencies = conda_dep

# In[8]:

myenv.register(workspace=ws)

# In[9]:

from azureml.core.model import InferenceConfig
from azureml.core.webservice import AciWebservice

inference_config = InferenceConfig(entry_script='summarizer_service.py',
                                   environment=myenv)
aci_config = AciWebservice.deploy_configuration(cpu_cores=1,
示例#5
0
pickle.dump(model, open(filename, 'wb'))

# register the dummy model
ws = Workspace.from_config()
registration_params = {
    'model_path': "dummy.pkl",
    'model_name': "dummy-model",
    'description': "mock test deployment",
    'workspace': ws
}
model = register_model(**registration_params)

myenv = Environment(name='my_env')
myenv.get(workspace=ws, name='ls-ds-ml-env')
conda_dep = CondaDependencies()
conda_dep.add_pip_package("azureml-defaults==1.3.0")
conda_dep.add_pip_package("joblib")
conda_dep.add_pip_package("json")
myenv.python.conda_dependencies = conda_dep
inference_config = InferenceConfig(entry_script="src/dummy_score.py",
                                   environment=myenv)
aci_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1)

service_name = 'dummy-service'
try:
    service = Model.deploy(ws,
                           service_name, [model],
                           inference_config,
                           deployment_config=aci_config)
    service.wait_for_deployment(True)
    logs = service.get_logs()