Skip to content

Commit 397ab28

Browse files
author
JavaTypedScript
committed
new_mlflow_proj
1 parent 172a046 commit 397ab28

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+150902
-4586
lines changed

LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
1-
# React + Vite
1+
# How to run this application
22

3-
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
3+
## Make 3 terminals
4+
```
5+
cd Backend/back2
6+
```
7+
```
8+
cd Backend/back2
9+
```
10+
```
11+
cd frontend/ai-rec
12+
```
413

5-
Currently, two official plugins are available:
14+
## In first terminal run(uvicorn terminal (api))
15+
```
16+
pip install -r requirements.txt
17+
```
618

7-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react) uses [Babel](https://babeljs.io/) for Fast Refresh
8-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
19+
## In third terminal run(frontend/ai-rec)
20+
```
21+
npm init -y
22+
npm i
923
10-
## Expanding the ESLint configuration
24+
```
25+
## Running the application
1126

12-
If you are developing a production application, we recommend using TypeScript with type-aware lint rules enabled. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) for information on how to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.
27+
### In first terminal(uvicorn)
28+
```
29+
uvicorn saas_api:app --reload
30+
```
31+
32+
### In second terminal(mlflow)
33+
```
34+
mlflow ui --backend-store-uri sqlite:///mlflow.db --default-artifact-root ./mlflow_artifacts
35+
36+
```
37+
### In third terminal(frontend)
38+
```
39+
npm run dev
40+
```
41+
42+
## Go to localhost:5173 or frontend luanched port, to view mlflow go to localhost:5000
43+
44+
## Upload dataset and enter into required fields,select ready model and select required fields and click get recommendations
45+
46+
# Finally Peace

backend/README.md

Lines changed: 0 additions & 13 deletions
This file was deleted.
-1.93 KB
Binary file not shown.

backend/back2/Collaborative.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import pandas as pd
2+
import numpy as np
3+
from sklearn.decomposition import TruncatedSVD
4+
5+
class CollaborativeFilteringRecommender:
6+
"""
7+
Recommends items using TruncatedSVD after mean-centering the data
8+
to provide personalized recommendations.
9+
"""
10+
def __init__(self, n_components=50):
11+
self.n_components = n_components
12+
self.svd = TruncatedSVD(n_components=self.n_components, random_state=42)
13+
14+
self.user_features = None
15+
self.item_features = None
16+
17+
self.user_means = None
18+
self.item_ids = None
19+
self.user_ids = None
20+
21+
self.original_ratings_pivot = None
22+
23+
def fit(self, df, schema_map):
24+
"""
25+
Pivots the data, de-means it, and fits the SVD model.
26+
27+
Args:
28+
df (pd.DataFrame): Standardized dataframe.
29+
schema_map (dict): Dict with 'user_id', 'item_id', 'rating' keys.
30+
"""
31+
# 1. Create the user-item matrix *with NaNs*
32+
self.original_ratings_pivot = df.pivot_table(
33+
index=schema_map['user_id'],
34+
columns=schema_map['item_id'],
35+
values=schema_map['rating']
36+
)
37+
38+
# 2. Calculate the mean rating for each user
39+
self.user_means = self.original_ratings_pivot.mean(axis=1)
40+
41+
# 3. De-mean the data
42+
demeaned_ratings = self.original_ratings_pivot.subtract(self.user_means, axis=0)
43+
44+
# 4. NOW, fill the NaNs with 0
45+
demeaned_ratings_filled = demeaned_ratings.fillna(0)
46+
47+
# 5. Fit SVD on the de-meaned data
48+
self.user_features = self.svd.fit_transform(demeaned_ratings_filled)
49+
self.item_features = self.svd.components_ # (n_components, n_items)
50+
51+
# Store the user and item IDs in order
52+
self.user_ids = demeaned_ratings_filled.index
53+
self.item_ids = demeaned_ratings_filled.columns
54+
55+
print("✅ Collaborative Filtering (SVD) model fitted on de-meaned data.")
56+
57+
def recommend(self, user_id, n=10):
58+
"""
59+
Gets the top n item recommendations for a given user_id.
60+
"""
61+
if user_id not in self.user_ids:
62+
print(f"User '{user_id}' not found in Collaborative model.")
63+
return []
64+
65+
# 1. Get the user's row index
66+
user_index = self.user_ids.get_loc(user_id)
67+
68+
# 2. Get the user's latent feature vector
69+
user_vector = self.user_features[user_index]
70+
71+
# 3. Predict the *deviations*
72+
predicted_deviations = np.dot(user_vector, self.item_features)
73+
74+
# 4. Add the user's mean back
75+
user_mean = self.user_means.loc[user_id]
76+
predicted_ratings = predicted_deviations + user_mean
77+
78+
# 5. Convert to a Series
79+
predicted_series = pd.Series(predicted_ratings, index=self.item_ids)
80+
81+
# 6. Get items the user has already rated
82+
rated_items = self.original_ratings_pivot.loc[user_id].dropna().index
83+
84+
# 7. Filter out already-rated items and sort
85+
recommendations = predicted_series.drop(rated_items).sort_values(ascending=False)
86+
87+
return recommendations.head(n).index.tolist()

backend/back2/Content.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import pandas as pd
2+
from sklearn.feature_extraction.text import TfidfVectorizer
3+
from sklearn.metrics.pairwise import linear_kernel
4+
5+
class ContentBasedRecommender:
6+
"""Recommends items similar to a given item based on its content."""
7+
def __init__(self):
8+
self.cosine_sim = None
9+
self.indices = None
10+
self.df = None
11+
self.schema_map = {}
12+
13+
def fit(self, df, schema_map):
14+
"""
15+
Builds the TF-IDF and Cosine Similarity matrix from item features.
16+
17+
Args:
18+
df (pd.DataFrame): The standardized dataframe.
19+
schema_map (dict): A dict with 'item_id' and 'feature_cols' keys.
20+
"""
21+
self.df = df
22+
self.schema_map = schema_map
23+
content_feature_columns = schema_map.get('feature_cols', [])
24+
25+
# Create a "soup" of all content features in a single string
26+
df['soup'] = df[content_feature_columns].fillna('').astype(str).agg(' '.join, axis=1)
27+
28+
# Use TF-IDF to convert the text soup into a matrix of feature vectors
29+
tfidf = TfidfVectorizer(stop_words='english')
30+
tfidf_matrix = tfidf.fit_transform(df['soup'])
31+
32+
# Calculate the cosine similarity between all items
33+
self.cosine_sim = linear_kernel(tfidf_matrix, tfidf_matrix)
34+
35+
# Create a mapping from item_id to dataframe index for quick lookups
36+
# Create a mapping from item_title to dataframe index for quick lookups
37+
self.indices = pd.Series(df.index, index=df[schema_map['item_title']]).drop_duplicates()
38+
print("✅ Content-Based model fitted.")
39+
40+
def recommend(self, item_id, n=10):
41+
"""
42+
Gets the top n similar items for a given item_id.
43+
"""
44+
if item_id not in self.indices:
45+
print(f"Item '{item_id}' not found in Content model indices.")
46+
return []
47+
48+
# Get the index of the item that matches the ID
49+
idx = self.indices[item_id]
50+
51+
# Get the pairwise similarity scores of all items with that item
52+
sim_scores = list(enumerate(self.cosine_sim[idx]))
53+
54+
# Sort the items based on the similarity scores
55+
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
56+
57+
# Get the scores of the n most similar items (excluding the item itself)
58+
sim_scores = sim_scores[1:n+1]
59+
60+
# Get the item indices
61+
item_indices = [i[0] for i in sim_scores]
62+
63+
# Return the top n most similar item IDs
64+
return self.df[self.schema_map['item_id']].iloc[item_indices].tolist()

backend/back2/Hybrid.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pandas as pd
2+
class HybridRecommender:
3+
"""
4+
Combines recommendations from content-based and collaborative models
5+
to provide a more robust and diverse recommendation list.
6+
"""
7+
def __init__(self, content_model, collab_model):
8+
self.content_model = content_model
9+
self.collab_model = collab_model
10+
print("🤖 HybridRecommender initialized.")
11+
12+
def recommend(self, user_id, item_id_for_content, n=10):
13+
"""
14+
Generates a hybrid list of recommendations.
15+
"""
16+
# 1. Get content-based recommendations
17+
content_recs = self.content_model.recommend(item_id_for_content, n)
18+
19+
# 2. Get collaborative filtering recommendations
20+
collab_recs = self.collab_model.recommend(user_id, n)
21+
22+
# 3. Combine the lists (prioritizing content, filling with collaborative)
23+
combined_recs = list(dict.fromkeys(content_recs + collab_recs))
24+
25+
# 4. Return the final list
26+
return combined_recs[:n]
3.77 KB
Binary file not shown.
3.16 KB
Binary file not shown.
1.37 KB
Binary file not shown.

0 commit comments

Comments
 (0)