- Delete all your search history on StudentMarket
-
-
-
-
-
-
-
-
-
-
-
- Browsing History
-
-
- Delete all your browsing history on StudentMarket
-
-
-
-
-
-
-
-
-
{/* Delete Account (Danger Zone) */}
-
Danger Zone
+
Danger Zone !!!
diff --git a/mysql-code/Schema.sql b/mysql-code/Schema.sql
index 3f36cf3..df51a58 100644
--- a/mysql-code/Schema.sql
+++ b/mysql-code/Schema.sql
@@ -61,7 +61,7 @@ CREATE TABLE Review (
),
Date DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (UserID) REFERENCES User (UserID),
- FOREIGN KEY (ProductID) REFERENCES Product (ProductID)
+ FOREIGN KEY (ProductID) REFERENCES Pprint(item[0])roduct (ProductID)
);
-- Transaction Entity (Many-to-One with User, Many-to-One with Product)
@@ -77,16 +77,17 @@ CREATE TABLE Transaction (
-- Recommendation Entity (Many-to-One with User, Many-to-One with Product)
CREATE TABLE Recommendation (
- RecommendationID_PK INT PRIMARY KEY,
+ RecommendationID_PK INT AUTO_INCREMENT PRIMARY KEY,
UserID INT,
RecommendedProductID INT,
+ Date DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (UserID) REFERENCES User (UserID),
FOREIGN KEY (RecommendedProductID) REFERENCES Product (ProductID)
);
-- History Entity (Many-to-One with User, Many-to-One with Product)
CREATE TABLE History (
- HistoryID INT PRIMARY KEY,
+ HistoryID INT AUTO_INCREMENT PRIMARY KEY,
UserID INT,
ProductID INT,
Date DATETIME DEFAULT CURRENT_TIMESTAMP,
diff --git a/recommondation-engine/__pycache__/app.cpython-313.pyc b/recommondation-engine/__pycache__/app.cpython-313.pyc
new file mode 100644
index 0000000..7d88e62
Binary files /dev/null and b/recommondation-engine/__pycache__/app.cpython-313.pyc differ
diff --git a/recommondation-engine/example1.py b/recommondation-engine/app.py
similarity index 73%
rename from recommondation-engine/example1.py
rename to recommondation-engine/app.py
index 621469d..95da178 100644
--- a/recommondation-engine/example1.py
+++ b/recommondation-engine/app.py
@@ -4,6 +4,7 @@ import mysql.connector
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import logging
+from unittest import result
def database():
db_connection = mysql.connector.connect(
@@ -83,27 +84,57 @@ def get_user_history(user_id):
return final
-def get_recommendations(user_id, top_n=40):
+def get_recommendations(user_id, top_n=10):
try:
# Get all products and user history with their category vectors
all_products = get_all_products()
user_history = get_user_history(user_id)
-
# if not user_history:
# # Cold start: return popular products
# return get_popular_products(top_n)
-
# Calculate similarity between all products and user history
user_profile = np.mean(user_history, axis=0) # Average user preferences
similarities = cosine_similarity([user_profile], all_products)
-
# finds the indices of the top N products that have the highest
# cosine similarity with the user's profile and sorted from most similar to least similar.
product_indices = similarities[0].argsort()[-top_n:][::-1]
print("product", product_indices)
+ # Get the recommended product IDs
+ recommended_products = [all_products[i][0] for i in product_indices] # Product IDs
+
+ # Upload the recommendations to the database
+ history_upload(user_id, product_indices) # Pass the indices directly to history_upload
+
# Return recommended product IDs
- return [all_products[i][0] for i in product_indices] # Product IDs
+ return recommended_products
except Exception as e:
logging.error(f"Recommendation error for user {user_id}: {str(e)}")
# return get_popular_products(top_n) # Fallback to popular products
+
+def history_upload(userID, anrr):
+ db_con = database()
+ cursor = db_con.cursor()
+
+ try:
+ for item in anrr:
+ #Product ID starts form index 1
+ item_value = item + 1
+ print(item_value)
+ # Use parameterized queries to prevent SQL injection
+ cursor.execute(f"INSERT INTO Recommendation (UserID, RecommendedProductID) VALUES ({userID}, {item_value});")
+
+ # Commit the changes
+ db_con.commit()
+
+ # If you need results, you'd typically fetch them after a SELECT query
+ # results = cursor.fetchall()
+ # print(results)
+
+ except Exception as e:
+ print(f"Error: {e}")
+ db_con.rollback()
+ finally:
+ # Close the cursor and connection
+ cursor.close()
+ db_con.close()
diff --git a/recommondation-engine/server.py b/recommondation-engine/server.py
index c371a3c..83dbe4d 100644
--- a/recommondation-engine/server.py
+++ b/recommondation-engine/server.py
@@ -1,6 +1,8 @@
from flask import Flask, request, jsonify
from flask_cors import CORS
-from example1 import get_recommendations
+from app import get_recommendations
+from app import history_upload
+
import time
@@ -18,8 +20,7 @@ def handle_session_data():
if not user_id or not email or is_authenticated is None:
return jsonify({'error': 'Invalid data'}), 400
- print(get_recommendations(user_id))
- time.sleep(2)
+ get_recommendations(user_id)
print(f"Received session data: User ID: {user_id}, Email: {email}, Authenticated: {is_authenticated}")
return jsonify({'message': 'Session data received successfully'})