|
import pandas as pd |
|
import numpy as np |
|
import requests |
|
from flask import Flask, jsonify, request |
|
import streamlit as st |
|
from sklearn.metrics.pairwise import cosine_similarity |
|
from sklearn.feature_extraction.text import TfidfVectorizer |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
df = pd.read_csv("ecommerce_dataset_updated.csv") |
|
df['Category'] = df['Category'].str.replace('&', ' and ') |
|
df['Purchase_Date'] = pd.to_datetime(df['Purchase_Date'], format='%d-%m-%Y') |
|
df.dropna(inplace=True) |
|
|
|
|
|
interaction_matrix = df.pivot_table( |
|
index='User_ID', |
|
columns='Product_ID', |
|
values='Final_Price(Rs.)', |
|
aggfunc='sum', |
|
fill_value=0 |
|
) |
|
|
|
|
|
user_similarity = cosine_similarity(interaction_matrix) |
|
user_similarity_df = pd.DataFrame(user_similarity, index=interaction_matrix.index, columns=interaction_matrix.index) |
|
|
|
|
|
tfidf = TfidfVectorizer(stop_words='english') |
|
tfidf_matrix = tfidf.fit_transform(df['Category']) |
|
product_similarity = cosine_similarity(tfidf_matrix, tfidf_matrix) |
|
product_similarity_df = pd.DataFrame(product_similarity, index=df['Product_ID'], columns=df['Product_ID']) |
|
|
|
|
|
def recommend_collaborative(user_id, top_n=5): |
|
user_idx = interaction_matrix.index.get_loc(user_id) |
|
sim_scores = user_similarity_df.iloc[user_idx] |
|
weighted_scores = np.dot(sim_scores, interaction_matrix.values) |
|
product_indices = np.argsort(weighted_scores)[::-1][:top_n] |
|
recommended_products = interaction_matrix.columns[product_indices] |
|
return recommended_products.tolist() |
|
|
|
|
|
def recommend_content_based(product_id, top_n=5): |
|
sim_scores = product_similarity_df.loc[product_id].sort_values(ascending=False) |
|
recommended_products = sim_scores.index[1:top_n+1] |
|
return recommended_products.tolist() |
|
|
|
|
|
def recommend_hybrid(user_id, product_id, top_n=5): |
|
collab_recs = recommend_collaborative(user_id, top_n) |
|
content_recs = recommend_content_based(product_id, top_n) |
|
hybrid_recs = list(set(collab_recs + content_recs))[:top_n] |
|
return hybrid_recs |
|
|
|
|
|
@app.route('/recommend', methods=['GET']) |
|
def recommend(): |
|
user_id = request.args.get('User_ID') |
|
product_id = request.args.get('Product_ID') |
|
|
|
if user_id and product_id: |
|
recommendations = recommend_hybrid(user_id, product_id) |
|
return jsonify({'recommendations': recommendations}) |
|
else: |
|
return jsonify({'error': 'Invalid parameters. Please provide both User_ID and Product_ID.'}), 400 |
|
|
|
|
|
if __name__ == '__main__': |
|
import threading |
|
|
|
|
|
def run_flask(): |
|
app.run(debug=True, use_reloader=False) |
|
|
|
|
|
threading.Thread(target=run_flask).start() |
|
|
|
|
|
st.title("Real-Time E-Commerce Recommendation System") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose a file") |
|
if uploaded_file is not None: |
|
df = pd.read_csv(uploaded_file) |
|
st.write(df) |
|
|
|
|
|
df['Category'] = df['Category'].str.replace('&', ' and ') |
|
user_id = st.selectbox("Select User ID", df['User_ID'].unique()) |
|
product_id = st.selectbox("Select Product ID", df['Product_ID'].unique()) |
|
|
|
|
|
url = f'http://localhost:5000/recommend?User_ID={user_id}&Product_ID={product_id}' |
|
response = requests.get(url) |
|
|
|
if response.status_code == 200: |
|
recommendations = response.json()['recommendations'] |
|
st.write("Recommended Products:", recommendations) |
|
else: |
|
st.write("Error:", response.json()) |
|
|