Home > database >  Azure ML model deployment fail: Module not found error
Azure ML model deployment fail: Module not found error

Time:01-12

I'm trying to deploy a model locally using Azure ML before deploying to AKS. I have a custom script that I want to import into my entry script (scoring script), but it's saying it is not found.

Here is the error: enter image description here

Here's my entry script with the custom script import on line 1:

import rake_refactored as rake
from operator import itemgetter
import pandas as pd
import datetime
import re
import operator
import numpy as np
import json

# Called when the deployed service starts
def init():
    global stopword_path
    # AZUREML_MODEL_DIR is an environment variable created during deployment.
    # It is the path to the model folder (./azureml-models/$MODEL_NAME/$VERSION)
    # For multiple models, it points to the folder containing all deployed models (./azureml-models)
    stopword_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'models/SmartStoplist.txt')
    # load models

def preprocess(df):
    df = rake.prepare_data(df)
    text = rake.process_response(df, "RESPNS")
    return text

# Use model to make predictions
def predict(df):
    text = preprocess(df)
    return rake.extract_keywords(stopword_path, text)

def run(data):
    try:
        # Find the data property of the JSON request
        df = pd.read_json(json.loads(data))
        prediction = predict(df)

        return json.dump(prediction)

    except Exception as e:
        return str(e)

And here is my model artifact directory in Azure ML showing that it is in the same directory as the entry script (rake_score.py).

What am I doing wrong? I had a similar issue before with a sklearn package that I was able to add to the pip-package list when I built the environment, but my custom script isn't a pip package.

enter image description here

CodePudding user response:

Not able to find rake_refactored in documentation and on the internet.

You can try below steps for importing rake.

Using pip

pip install rake-nltk

Directly from the repository

git clone https://github.com/csurfer/rake-nltk.git
python rake-nltk/setup.py install

Sample Code:

from rake_nltk import Rake

# Uses stopwords for english from NLTK, and all puntuation characters by
# default
r = Rake()

# Extraction given the text.
r.extract_keywords_from_text(<text to process>)

# Extraction given the list of strings where each string is a sentence.
r.extract_keywords_from_sentences(<list of sentences>)

# To get keyword phrases ranked highest to lowest.
r.get_ranked_phrases()

# To get keyword phrases ranked highest to lowest with scores.
r.get_ranked_phrases_with_scores()

Refer - https://github.com/csurfer/rake-nltk

CodePudding user response:

In order to access my custom script in my scoring script I needed to explicitly define the source directory in my inference configuration:

from azureml.core.model import InferenceConfig
inference_config = InferenceConfig(
    environment = env,
    entry_script = "rake_score.py",
    source_directory='./models'
)
  •  Tags:  
  • Related