waseoke commited on
Commit
30aed04
·
verified ·
1 Parent(s): d0ee258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -1
app.py CHANGED
@@ -2,6 +2,8 @@ from pymongo import MongoClient
2
  from transformers import BertTokenizer, BertModel
3
  import torch
4
  from torch.nn import Embedding
 
 
5
 
6
  # MongoDB Atlas 연결 설정
7
  client = MongoClient("mongodb+srv://waseoke:[email protected]/test?retryWrites=true&w=majority&tls=true&tlsAllowInvalidCertificates=true")
@@ -107,4 +109,49 @@ for user_data in all_users:
107
  )
108
  print(f"Embedding saved to MongoDB Atlas for user_id {user_data['user_id']}.")
109
  except ValueError as e:
110
- print(f"Skipping user_id {user_data['user_id']} due to error: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from transformers import BertTokenizer, BertModel
3
  import torch
4
  from torch.nn import Embedding
5
+ import numpy as np
6
+ from sklearn.metrics.pairwise import cosine_similarity
7
 
8
  # MongoDB Atlas 연결 설정
9
  client = MongoClient("mongodb+srv://waseoke:[email protected]/test?retryWrites=true&w=majority&tls=true&tlsAllowInvalidCertificates=true")
 
109
  )
110
  print(f"Embedding saved to MongoDB Atlas for user_id {user_data['user_id']}.")
111
  except ValueError as e:
112
+ print(f"Skipping user_id {user_data['user_id']} due to error: {e}")
113
+
114
+ # 사용자 맞춤 추천 함수
115
+ def recommend_products_for_user(user_id, top_n=1):
116
+ try:
117
+ # MongoDB에서 사용자 임베딩 가져오기
118
+ user_embedding_data = user_embedding_collection.find_one({"user_id": user_id})
119
+ if not user_embedding_data:
120
+ print(f"User ID {user_id} embedding not found.")
121
+ return []
122
+
123
+ user_embedding = np.array(user_embedding_data["embedding"]).reshape(1, -1)
124
+
125
+ # 모든 상품 임베딩 가져오기
126
+ all_product_embeddings = list(product_embedding_collection.find())
127
+
128
+ # 상품 ID 및 임베딩 추출
129
+ product_ids = []
130
+ product_embeddings = []
131
+ for product_data in all_product_embeddings:
132
+ product_ids.append(product_data["product_id"])
133
+ product_embeddings.append(np.array(product_data["embedding"]))
134
+
135
+ product_embeddings = np.array(product_embeddings)
136
+
137
+ # Cosine Similarity 계산
138
+ similarities = cosine_similarity(user_embedding, product_embeddings).flatten()
139
+
140
+ # 유사도 정렬 및 상위 N개 선택
141
+ top_indices = similarities.argsort()[::-1][:top_n]
142
+ recommended_products = [(product_ids[i], similarities[i]) for i in top_indices]
143
+
144
+ print(f"Top {top_n} recommendations for User ID {user_id}:")
145
+ for product_id, similarity in recommended_products:
146
+ print(f"Product ID: {product_id}, Similarity: {similarity:.4f}")
147
+
148
+ return recommended_products
149
+
150
+ except Exception as e:
151
+ print(f"Error during recommendation for User ID {user_id}: {e}")
152
+ return []
153
+
154
+ # 사용자 맞춤 추천 실행
155
+ user_id_to_recommend = 1 # 추천할 사용자 ID
156
+ top_n_recommendations = 1 # 추천 상품 개수
157
+ recommended_products = recommend_products_for_user(user_id_to_recommend, top_n=top_n_recommendations)