File size: 1,936 Bytes
3059673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import gradio as gr
import numpy as np
from PIL import Image
import requests
import xgboost
import pandas as pd

from app_funcs import *

import hopsworks
import joblib

project = hopsworks.login()
fs = project.get_feature_store()

X_columns_to_drop = ["percentage_future", "cohort_first_month", "month",  
                     "cohort_first_product"]

feature_view = fs.get_feature_view(name="cohorts_fv", version=1)

mr = project.get_model_registry()
model = mr.get_model("cohort_model", version=1)
model_dir = model.download()
model = joblib.load(model_dir + "/xgb_.pkl")

# Get feature view in a dataframe
data, labels = feature_view.get_training_data(training_dataset_version=1)
data["percentage_future"] = labels

# Convert it to pandas datetime
data["cohort_first_month"] = pd.to_datetime(data["cohort_first_month"])
data["month"] = pd.to_datetime(data["month"])

# Sort and assert
data = data.sort_values(by=["cohort_first_product", "cohort_first_month", "month"])


def cohort_predict(start_date, cohort_start, product):
                
    input_list = []
    input_list.append(start_date, cohort_start, product)
    
    new_seq = generate_new_data(data, cohort_start, start_date, product, model, X_columns_to_drop, 12)
    hist_seq = get_sequence(data, cohort_start, product)

    fig = plot_example_from_case(hist_seq, new_seq, 25, product)
        
              
    return fig
        
demo = gr.Interface(
    fn=cohort_predict,
    title="Cohort Active Percentage Prediction",
    description="Predicts active user percentage in a future month for a cohort that started in specific date with specific product",
    allow_flagging="never",
    inputs=[
        gr.Textbox(default='2022-04-01', label="Cohort Start Date"),
        gr.Textbox(default='2022-04-01', label="Prediction Start Date"),
        gr.Textbox(default="3m", label="Product (1m, 3m, 4m)"),
        ],
    outputs=gr.Image(type="pil"))

demo.launch()