ModuleNotFoundError在龙卷风中加载腌制的文件时

时间:2018-07-02 01:07:41

标签: python python-3.x tornado pickle

我正在使用龙卷风框架加载我的机器学习模型。我有一个受欢迎的课程

import numpy as np 
import pandas as pd
from pandas import DataFrame

class Popularity():
    users_data = pd.read_csv('~/Desktop/LatentCollaborativeFiltering/lib/seed_data/ratings.csv')
    movies_data = pd.read_csv('~/Desktop/LatentCollaborativeFiltering/lib/seed_data/movies.csv')
    data = pd.merge(users_data, movies_data, left_on="movieId", right_on="movieId")
    data = pd.DataFrame.sort_values(data, ['userId','movieId'],ascending=[0,1])

    def __init__(self):
        pass


    def favoriteMovies(self, activeUser,N):
        topMovies=pd.DataFrame.sort_values(self.data[self.data.userId==activeUser],['rating'],ascending=[0])[:N]
        # return the title corresponding to the movies in topMovies 
        return list(topMovies.title)

    def recommend_movies(self):
        return "No recommendation"

现在我还有另一个文件可以给此类的对象腌制build_model.py

from __future__ import print_function

import os

from sklearn.externals import joblib

import pandas as pd 
import numpy as np 

from popularity import Popularity

if __name__ == "__main__":
    popu = Popularity()


    _CUR_DIR = os.path.dirname(os.path.realpath(__file__))
    _SERIALIZATION_DIR = os.path.join(_CUR_DIR)

    if not os.path.exists(_SERIALIZATION_DIR):
        os.makedirs(_SERIALIZATION_DIR)
    model_filename = os.path.join(_SERIALIZATION_DIR, "model.pkl")

    joblib.dump(popu, model_filename)
    print("Successfully Built and Picked into models folder")

现在可以构建模型并将模型成功保存到与model.pkl文件相同的目录中。但是当我将模型加载到torando中时,会出现以下错误

    [I 180702 06:30:44 server:40] Loading Latent Collaborative Filtering model...
Traceback (most recent call last):
  File "run.py", line 7, in <module>
    server.main()
  File "/home/rabin/Desktop/LatentCollaborativeFiltering/movies-api/app/server.py", line 45, in main
    MODELS["recommender"] = pickle.load(infile)
ModuleNotFoundError: No module named 'Popularity'

我的server.py文件是

# !/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import logging
import logging.config

import tornado.ioloop
import tornado.web
from tornado.options import options

from sklearn.externals import joblib

from app.settings import MODEL_DIR, ROOT_DIR, _CUR_DIR
from app.handler import IndexHandler, IrisPredictionHandler
from app.popularity import Popularity
import pickle


MODELS = {}


def load_model(pickle_filename):
    return joblib.load(pickle_filename)


def main():

    # Get the Port and Debug mode from command line options or default in settings.py
    options.parse_command_line()


    # create logger for app
    logger = logging.getLogger('app')
    logger.setLevel(logging.INFO)

    FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(format=FORMAT)

    # Load ML Models
    logger.info("Loading Latent Collaborative Filtering model...")
   #MODELS["recommender"] = load_model(os.path.join(_CUR_DIR, 'model.pkl'))
    #MODELS["recommender"] = load_model('model.pkl')
    with open(os.path.join(_CUR_DIR, 'model.pkl'), 'rb') as infile:
        MODELS["recommender"] = pickle.load(infile)

    urls = [
        (r"/$", IndexHandler),
        (r"/api/recommender/(?P<action>[a-zA-Z]+)?", RecommenderHandler, # action is function in handler
            dict(model=MODELS["recommender"]))
    ]

    # Create Tornado application
    application = tornado.web.Application(
        urls,
        debug=options.debug,
        autoreload=options.debug)

    # Start Server
    logger.info("Starting App on Port: {} with Debug Mode: {}".format(options.port, options.debug))
    application.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

我的handler.py文件是

"""
Request Handlers
"""

import tornado.web
from tornado import concurrent
from tornado import gen
from concurrent.futures import ThreadPoolExecutor

from app.base_handler import BaseApiHandler
from app.settings import MAX_MODEL_THREAD_POOL
from app.popularity import Popularity

class IndexHandler(tornado.web.RequestHandler):
    """APP is live"""

    def get(self):
        self.write("Movie Recommendation System is Live")

    def head(self):
        self.finish()


class RecommenderHandler(BaseApiHandler):

    _thread_pool = ThreadPoolExecutor(max_workers=MAX_MODEL_THREAD_POOL)

    def initialize(self, model, *args, **kwargs):
        self.model = model
        super().initialize(*args, **kwargs)

    @concurrent.run_on_executor(executor='_thread_pool')
    def _blocking_predict(self, X):
        target_values = self.model.favoriteMovies(5,10) 
        return target_values


    @gen.coroutine
    def predict(self, data):
        if type(data) == dict:
            data = [data]

        X = []
        for item in data:
            record  = (item.get("user_id"))
            X.append(record)

        results = yield self._blocking_predict(X)
        self.respond(results)

我为该解决方案进行了过多搜索,但尚未找到对我有用的解决方案。 我也无法从控制台加载 enter image description here

0 个答案:

没有答案