无法在ai-platform自定义预测中使用configparser

Unable to use configparser in ai-platform custom prediction

我正在寻找一种在我的自定义预测例程的预测代码中使用 configparser 的方法。 我尝试了以下代码片段

common.cfg

[MODEL]
VERSION=config-true

setup.py

from setuptools import setup

REQUIRED_PACKAGES = [
    'joblib==0.13.0'
]

setup(
    name='test',
    description='Custom prediction routine',
    version=0.1,
    install_requires=REQUIRED_PACKAGES,
    scripts=['src/predictor.py', 'config/common.cfg']
)

predictor.py

import os
import joblib
import subprocess
import configparser

class CustomPredictor(object):
    def __init__(self, model, config):
        self._model = model
        self._config = config

    def predict(self, instances, **kwargs):
        version_value = self._config.get('MODEL', 'VERSION', fallback='config-false')
        print(f'version value = {version_value}', flush=True) # printing config-false

        preprocessed_input = self._preprocess(instances)

        score = self._model.predict(preprocessed_input)

        print(f'predicted score {score}', flush=True)
        return score.to_list()

    @classmethod
    def from_path(cls, model_dir):
        config = configparser.RawConfigParser()
        result = config.read('config/common.cfg')
        print(f'read config result: {result}', flush=True) # empty
        print(f'config sections: {config.sections()}', flush=True) # empty

        subprocess.run(["ls", "-l"]) # don't see the config file or folder

        model_path = os.path.join(model_dir, "model.joblib")
        model = joblib.load(model_path)

        return cls(model, config)

对我做错或遗漏的事情有什么建议吗?

我运行这段代码,它打印了预期的结果:

config = configparser.RawConfigParser()
result = config.read('common.cfg')
print(f'read config result: {result}', flush=True) # empty
print(f'config sections: {config.sections()}', flush=True) # empty

输出是

read config result: ['common.cfg']
config sections: ['MODEL']

所以可能的错误可能是您的 common.cfg 文件的路径。

当我复制你的代码并 运行 它显示空列表作为输出,因为文件在同一个目录中而不在配置目录中,但是一旦我更正路径它打印预期结果。

您有 2 个选择:

  1. 配置 setup.py 并在其中添加 configparser
REQUIRED_PACKAGES = [
    'joblib==0.13.0',
    'configparser'
]
  1. 创建模型时,使用 package-uris 将配置解析器作为包传递。从 pypi 我已经找到了 tar.gz 文件。 请看一下 this 示例,当我们传递 PyTorch 包时,在您的情况下,从 pypi 下载文件并将其放入 GCS Bucket,在创建模型时从那里定义它:
gcloud beta ai-platform versions create {MODEL_VERSION} --model {MODEL_NAME} \
 --origin=gs://{BUCKET_NAME}/{MODEL_DIR}/ \
 --python-version=3.7 \
 --runtime-version={RUNTIME_VERSION} \
 --package-uris=gs://{BUCKET_NAME}/{PACKAGES_DIR}/text_classification-0.1.tar.gz,
gs://{BUCKET_NAME}/configparser-5.0.0.tar.gz  \
 --machine-type=mls1-c4-m4 \
 --prediction-class=model.CustomModelPrediction

我发现了我做的错误,在部署自定义预测例程时,ai-platform 将文件保留在/tmp/custom_lib/bin/ 下,并将执行路径保留为根目录。因此,在我的代码中,我已将配置路径更新为类似这样的内容

config_file = pathlib.Path(__file__).parent.absolute() / 'common.cfg'
config.read(config_file)

这解决了问题!

注意:我还认为我们需要将配置文件保存在 scripts 标签下,因为当部署逻辑安装包时,setuptools 会将脚本复制到 ai-platform 定义的 PATH 并使它可用。