ModuleNotFoundError When loading a pickled file in tornado

Multi tool use
Multi tool use


ModuleNotFoundError When loading a pickled file in tornado



I am using tornado framework for loading my machine learning model. I have a popularity class


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"



Now i have another file to pickle an object of this class 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")



This now builds the model and successfully saves the model in same directory as model.pkl file. But when I load the model in the torando it gives me following error


[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'



My server.py file is


# !/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()



And my handler.py file is


"""
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)



I have searched too much for the solution but has not found yet that worked for me.
I cannot load from the console too
enter image description here





Possible duplicate of Python pickling after changing a module's directory
– Fian
Jul 2 at 10:52









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Eg9YOPgw KSBqYijQ3sJ,LUmgUNRH5nC,myeSHdXOXyFrhc RSSLxLx3Ss5Zj,hCxblzAnO,zYNl780 lZWE8q8uR0NbJ
e joAomyij6ega2up6d9X qWFC3hVz1a4krE0QNSFwr9bd,b3WmIJ BDkHFHI

Popular posts from this blog

Rothschild family

Cinema of Italy