File size: 2,363 Bytes
056373e
 
 
 
 
bc96d0d
056373e
 
 
 
331aefd
bc96d0d
056373e
 
 
 
 
 
 
 
331aefd
 
 
 
 
056373e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import joblib

data = pd.read_csv(r"data_final.csv")
model = joblib.load("KNN_Model.joblib")

def product_recommender(customer_id):
    list_predicted = []
    
    for id in data['product_id'].unique():
        preds = list(model.predict(customer_id, id))
        product_id = preds[1]
        product_score = preds[3]
    
        list_predicted.append((product_id, product_score))
        
    top_5_products_raw = sorted(list_predicted, key=lambda x:x[1], reverse=True)[:5]
    top_5_products = [product[0] for product in top_5_products_raw]

    product_1_category = data[data['product_id']==top_5_products[0]]['category'].values[0]
    product_2_category = data[data['product_id']==top_5_products[1]]['category'].values[0]
    product_3_category = data[data['product_id']==top_5_products[2]]['category'].values[0]
    product_4_category = data[data['product_id']==top_5_products[3]]['category'].values[0]
    product_5_category = data[data['product_id']==top_5_products[4]]['category'].values[0]

    result_1 = f"Recommendation Product ID {top_5_products[0]} with Category {product_1_category}"
    result_2 = f"Recommendation Product ID {top_5_products[1]} with Category {product_2_category}"
    result_3 = f"Recommendation Product ID {top_5_products[2]} with Category {product_3_category}"
    result_4 = f"Recommendation Product ID {top_5_products[3]} with Category {product_4_category}"
    result_5 = f"Recommendation Product ID {top_5_products[4]} with Category {product_5_category}"

    return result_1, result_2, result_3, result_4, result_5

demo = gr.Interface(
    title="Product Recommendation System",
    description="""This User Interface is Powered by Machine Learning to
                Predict the Top 5 of Product that customer likely to buy in the next purchase.
                All you need is to Input Customer ID and then the Recommendation will be appear.""",
    fn=product_recommender,
    inputs=[
        gr.Number(label="Input Customer ID")
    ],
    outputs=[
        gr.Textbox(label="Recommendation Product 1"),
        gr.Textbox(label="Recommendation Product 2"),
        gr.Textbox(label="Recommendation Product 3"),
        gr.Textbox(label="Recommendation Product 4"),
        gr.Textbox(label="Recommendation Product 5")
    ]
)

if __name__ == "__main__":
    demo.launch()