# Path: app.py
# def make_celery(app):
#         def __call__(self, *args, **kwargs):
# def create_app():
# def create_test_app():
#     class ContextTask(TaskBase):
# 
# Path: core/algo/scorer.py
# class Manager(object):
#     def __init__(self, question):
#         self.question = question
# 
#     def score_essay(self, essay):
#         text = essay.text
#         model = self.get_latest_model()
#         model_obj = joblib.load(os.path.join(settings.MODEL_PATH, model.path))
#         return model_obj.predict(text)
# 
#     def get_latest_model(self):
#         models = self.question.models
# 
#         if len(models) == 0:
#             raise NoModelException
# 
#         model = models[-1]
#         return model
# 
#     def create_model(self):
#         text = [e.text for e in self.question.essays if e.actual_score is not None]
#         scores = [e.actual_score for e in self.question.essays if e.actual_score is not None]
#         scorer = Scorer(text, scores)
#         scorer.train()
#         time = datetime.utcnow()
#         timestamp = calendar.timegm(time.utctimetuple())
#         path_string = "{0}_{1}.pickle".format(self.question.id, timestamp)
#         model = Model(
#             question=self.question,
#             error=scorer.cv_score,
#             path=path_string
#         )
# 
#         db.session.add(model)
# 
#         joblib.dump(scorer, os.path.join(settings.MODEL_PATH, path_string), compress=9)
#         db.session.commit()
# 
# Path: core/database/models.py
# class Question(db.Model):
#     __tablename__ = 'questions'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(STRING_MAX))
#     description = db.Column(db.Text)
#     prompt = db.Column(db.Text)
#     user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
# 
#     user = db.relationship("User", backref=db.backref('questions', order_by=id))
# 
#     created = db.Column(db.DateTime(timezone=True), default=datetime.utcnow)
#     updated = db.Column(db.DateTime(timezone=True), onupdate=datetime.utcnow)
# 
# class Essay(db.Model):
#     __tablename__ = 'essays'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     text = db.Column(db.Text)
#     actual_score = db.Column(db.Float)
#     info = db.Column(db.Text)
# 
#     predicted_score = db.Column(db.Float)
#     model_id = db.Column(db.Integer, db.ForeignKey('models.id'))
#     model = db.relationship("Model", backref=db.backref('essays', order_by=id))
# 
#     question_id = db.Column(db.Integer, db.ForeignKey('questions.id'))
#     question = db.relationship("Question", backref=db.backref('essays', order_by=id))
# 
#     created = db.Column(db.DateTime(timezone=True), default=datetime.utcnow)
#     updated = db.Column(db.DateTime(timezone=True), onupdate=datetime.utcnow)


from app import celery
from core.algo.scorer import Manager
from core.database.models import Question, Essay
        from app import db
    from app import cache
    from app import db, cache
    from app import db

class SqlAlchemyTask(celery.Task):
    """An abstract Celery Task that ensures that the connection the the
    database is closed on task completion"""
    abstract = True

    def after_return(self, status, retval, task_id, args, kwargs, einfo):
        db.session.remove()

@celery.task(base=SqlAlchemyTask)
def create_model(question_id):
    question = Question.query.filter_by(id=question_id).first()
    manager = Manager(question)
    manager.create_model()
    cache.delete('{0}_question_status')

@celery.task(base=SqlAlchemyTask)
def score_essay(essay_id):

# Path: app.py
# def make_celery(app):
#         def __call__(self, *args, **kwargs):
# def create_app():
# def create_test_app():
#     class ContextTask(TaskBase):
# 
# Path: core/algo/scorer.py
# class Manager(object):
#     def __init__(self, question):
#         self.question = question
# 
#     def score_essay(self, essay):
#         text = essay.text
#         model = self.get_latest_model()
#         model_obj = joblib.load(os.path.join(settings.MODEL_PATH, model.path))
#         return model_obj.predict(text)
# 
#     def get_latest_model(self):
#         models = self.question.models
# 
#         if len(models) == 0:
#             raise NoModelException
# 
#         model = models[-1]
#         return model
# 
#     def create_model(self):
#         text = [e.text for e in self.question.essays if e.actual_score is not None]
#         scores = [e.actual_score for e in self.question.essays if e.actual_score is not None]
#         scorer = Scorer(text, scores)
#         scorer.train()
#         time = datetime.utcnow()
#         timestamp = calendar.timegm(time.utctimetuple())
#         path_string = "{0}_{1}.pickle".format(self.question.id, timestamp)
#         model = Model(
#             question=self.question,
#             error=scorer.cv_score,
#             path=path_string
#         )
# 
#         db.session.add(model)
# 
#         joblib.dump(scorer, os.path.join(settings.MODEL_PATH, path_string), compress=9)
#         db.session.commit()
# 
# Path: core/database/models.py
# class Question(db.Model):
#     __tablename__ = 'questions'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(STRING_MAX))
#     description = db.Column(db.Text)
#     prompt = db.Column(db.Text)
#     user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
# 
#     user = db.relationship("User", backref=db.backref('questions', order_by=id))
# 
#     created = db.Column(db.DateTime(timezone=True), default=datetime.utcnow)
#     updated = db.Column(db.DateTime(timezone=True), onupdate=datetime.utcnow)
# 
# class Essay(db.Model):
#     __tablename__ = 'essays'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     text = db.Column(db.Text)
#     actual_score = db.Column(db.Float)
#     info = db.Column(db.Text)
# 
#     predicted_score = db.Column(db.Float)
#     model_id = db.Column(db.Integer, db.ForeignKey('models.id'))
#     model = db.relationship("Model", backref=db.backref('essays', order_by=id))
# 
#     question_id = db.Column(db.Integer, db.ForeignKey('questions.id'))
#     question = db.relationship("Question", backref=db.backref('essays', order_by=id))
# 
#     created = db.Column(db.DateTime(timezone=True), default=datetime.utcnow)
#     updated = db.Column(db.DateTime(timezone=True), onupdate=datetime.utcnow)


# Path: core/tasks/tasks.py
from app import celery
from core.algo.scorer import Manager
from core.database.models import Question, Essay
        from app import db
    from app import cache
    from app import db, cache
    from app import db


class SqlAlchemyTask(celery.Task):
    """An abstract Celery Task that ensures that the connection the the
    database is closed on task completion"""
    abstract = True

    def after_return(self, status, retval, task_id, args, kwargs, einfo):
        db.session.remove()

@celery.task(base=SqlAlchemyTask)
def create_model(question_id):
    question = Question.query.filter_by(id=question_id).first()
    manager = Manager(question)
    manager.create_model()
    cache.delete('{0}_question_status')

@celery.task(base=SqlAlchemyTask)
def score_essay(essay_id):

    essay = Essay.query.filter_by(id=essay_id).first()