def predict_ffm():
    """
    Function to run prediction process:
    - Get all posts in a model
    - Get only new posts
    - Generate pairs with similar posts for each user
    - Load a model from disk
    - Get FFM predictions
    - Save recommendations to a mongo database
  """
    database_url = config['database_url']
    database = config['database_name']
    utils.log("FFM predict", "Prepare model...")
    model = ffm.read_model(config['model_path'] + "model.bin")
    mappings = joblib.load(config['model_path'] + "mappings.pkl")

    while True:
        utils.log("FFM predict", "Get posts...")
        posts = get_posts(database_url, database)
        utils.log("FFM predict", "Create dataset...")
        events = utils.get_events(database_url, database)
        dataset = create_dataset(posts, events)
        utils.log("FFM predict", "Extend events...")
        dataset = extend_events(dataset, posts)
        mappings, ffm_dataset_X, ffm_dataset_y = create_ffm_dataset(
            dataset, mappings)
        ffm_dataset = ffm.FFMData(ffm_dataset_X, ffm_dataset_y)
        dataset["prediction"] = model.predict(ffm_dataset)
        utils.log("FFM predict", "Save recommendations...")
        save_recommendations(
            dataset[["user_id", "post_permlink", "prediction"]], database_url,
            database)
def get_posts(url, database):
    events = utils.get_events(url, database)
    posts = utils.get_posts(url, database, events,
                            {'similar_posts': {
                                '$exists': True
                            }})
    return utils.preprocess_posts(posts)
def get_posts(url, database):
    events = utils.get_events(url, database)
    posts = utils.get_posts(url, database, events,
                            {'inferred_vector': {
                                '$exists': False
                            }})
    return utils.preprocess_posts(posts)
Beispiel #4
0
def get_posts(url, database):
    events = utils.get_events(url, database)
    posts = utils.get_posts(url, database, events,
                            {'inferred_vector': {
                                '$exists': True
                            }})
    return utils.preprocess_posts(posts, include_all_tags=True)
Beispiel #5
0
def train_ffm():
  """
    Function to train FFM model
    - Get all events from mongo database
    - Convert them to a set of unique user-post pairs with a coefficient depending on user sympathy
    - Extend events with a posts info
    - Convert events to a format for FFM algorithm
    - Build model with chosen train and test set
    - Save trained model
  """
  database_url = config['database_url']
  database = config['database_name']

  utils.log("FFM train", "Prepare events...")
  events = utils.get_events(database_url, database)

  events.to_csv(config['model_path'] + "prepared_events.csv")
  # events = pd.read_csv("prepared_events.csv").drop(["Unnamed: 0"], axis=1)

  utils.log("FFM train", "Prepare posts...")
  posts = get_posts(database_url, database, events)

  posts.to_csv(config['model_path'] + "prepared_posts.csv")
  # posts = pd.read_csv("prepared_posts.csv").drop(["Unnamed: 0"], axis=1)

  utils.log("FFM train", "Extend events...")
  events = extend_events(events, posts)

  utils.log("FFM train", "Save events...")
  events.to_csv(config['model_path'] + "extended_events.csv")

  # events = pd.read_csv("extended_events.csv").drop(["Unnamed: 0"], axis=1)

  utils.log("FFM train", "Create ffm dataset...")
  mappings, X, y = create_ffm_dataset(events)
  joblib.dump(X, config['model_path'] + "X.pkl")
  joblib.dump(y, config['model_path'] + "y.pkl")
  train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.3)
  utils.log("FFM train", "Build model...")
  model, train_auc_roc, test_auc_roc = build_model(train_X, train_y, test_X, test_y)
  utils.log("FFM train", train_auc_roc)
  utils.log("FFM train", test_auc_roc)
  os.system("ffm_predict stop")
  model.save_model(config['model_path'] + "model.bin")
  joblib.dump(mappings, config['model_path'] + "mappings.pkl")
  os.system("ffm_predict start")
Beispiel #6
0
from flask import Flask, jsonify, request, render_template
from .config import config as config_flask
import json
from flask_cors import CORS
import sys
from pymongo import MongoClient
import pandas as pd
import pdb
from golosio_recommendation_model.model.utils import get_events
from golosio_recommendation_model.config import config

database_url = config['database_url']
database_name = config['database_name']
events = get_events(database_url, database_name)

app = Flask(__name__)
port = 8080  # Use desired port


@app.route('/users')
def users():
    return jsonify(events[events["like"] >= 0.7]["user_id"].unique().tolist())


@app.route('/history')
def history():
    user = request.args.get("user")
    user_events = events[(events["user_id"] == user) & (events["like"] >= 0.7)]
    return jsonify(user_events["post_permlink"].unique().tolist())

def get_posts(url, database):
    events = utils.get_events(url, database)
    posts = utils.get_posts(url, database, events)
    return utils.preprocess_posts(posts)