Spaces:
Sleeping
Sleeping
| 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() | |