gpt4 book ai didi

python - 加载保存的文件时,Flask 出现错误并显示 dill.dill 消息

转载 作者:行者123 更新时间:2023-11-30 09:17:28 30 4
gpt4 key购买 nike

我在 s3 中保存了两个文件(模型、矢量化器),并尝试从 s3 下载并使用我的 Flask 应用程序文件运行它。但是,当前返回 dill.dill (没有名为错误的模块)。有趣的是,我没有在任何地方导入“dill”,但看起来它是在内部导入并提示的。

    import flask
from flask import Flask, request, render_template, jsonify
from sklearn.externals import joblib
import numpy as np
from scipy import misc
from sklearn.feature_extraction.text import TfidfVectorizer
import boto3
import pickle

BUCKET_NAME = 'xxxx'
MODEL_FILE_NAME = 'text-classification-model-v1.pk'
VECTORIZER_FILE_NAME = 'word_vectorizer.pk'
ACCESS_KEY="QQQQ"
SECRET_KEY="ZZZZZ"
S3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY, region_name='us-west-2')
app = Flask(__name__)

@app.route('/', methods=['GET'])
def index():
payload = [request.args.get('description')]
prediction = predict_(payload)
return parse_data()

def predict_(payload):
transformed_text_features = load_vectorizer(payload)
return load_model(MODEL_FILE_NAME).predict(transformed_text_features)

def parse_data():
return 'xxx'

def load_vectorizer(payload):
with open("downloaded_vectorizer.pk", 'wb') as data:
S3.download_fileobj(BUCKET_NAME, VECTORIZER_FILE_NAME, data)
infile = open("downloaded_vectorizer.pk","rb")
loaded_vectorizer = joblib.load(infile)
infile.close()
transformed_text_features = loaded_vectorizer.transform(payload)
return transformed_text_features

def load_model(model):
with open("downloaded_model_textclassification.pkl", 'wb') as data:
S3.download_fileobj(BUCKET_NAME, MODEL_FILE_NAME, data)
infile = open("downloaded_model_textclassification.pkl","rb")
model = pickle.load(infile)
infile.close()
return model

==========
error message
==========
File "\flask\predict_commodity.py", line 32, in index
prediction = predict_(payload)
File "\flask\predict_commodity.py", line 38, in predict_
transformed_text_features = load_vectorizer(payload)
File "\flask\predict_commodity.py", line 60, in load_vectorize
loaded_vectorizer = joblib.load(infile)
File " \lib\site-packages\sklearn\externals\joblib\numpy_pickle.py", line 568, in load
obj = _unpickle(fobj)
File " \lib\site-packages\sklearn\externals\joblib\numpy_pickle.py", line 508, in _unpickle
obj = unpickler.load()
File " \anaconda3\Lib\pickle.py", line 1050, in load
dispatch[key[0]](self)
File "\Lib\pickle.py", line 1338, in load_global
klass = self.find_class(module, name)
File "\Lib\pickle.py", line 1388, in find_class
__import__(module, level=0)
ModuleNotFoundError: No module named 'dill.dill'

最佳答案

在您的代码中,您使用 picklesklearn.externals.joblib。这可能是一个问题。

此外,您可以尝试更新软件包:pip install dill --upgrade。这帮助我解决了同样的问题。

关于python - 加载保存的文件时,Flask 出现错误并显示 dill.dill 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51546019/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com