Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,33 +17,33 @@ class ModeratelySimplifiedGATConvModel(torch.nn.Module):
|
|
| 17 |
|
| 18 |
def forward(self, x, edge_index, edge_attr=None):
|
| 19 |
x = self.conv1(x, edge_index, edge_attr)
|
| 20 |
-
x
|
| 21 |
-
x
|
| 22 |
-
x
|
| 23 |
return x
|
| 24 |
|
| 25 |
# Load the dataset and the GATConv model
|
| 26 |
-
data
|
| 27 |
|
| 28 |
# Load the BERT-based sentence transformer model
|
| 29 |
-
model_bert
|
| 30 |
|
| 31 |
# Ensure the DataFrame is loaded properly
|
| 32 |
try:
|
| 33 |
-
df
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error reading JSON file: {e}")
|
| 36 |
|
| 37 |
# Generate GNN-based embeddings
|
| 38 |
with torch.no_grad():
|
| 39 |
-
all_video_embeddings
|
| 40 |
|
| 41 |
# Function to find the most similar video and recommend the top 10 based on GNN embeddings
|
| 42 |
def get_similar_and_recommend(input_text):
|
| 43 |
# Find the most similar video based on input text
|
| 44 |
-
embeddings_matrix
|
| 45 |
-
input_embedding
|
| 46 |
-
similarities
|
| 47 |
|
| 48 |
# Modify the similarity scores based on user input
|
| 49 |
user_keywords = input_text.split() # Create a list of keywords from user input
|
|
@@ -66,12 +66,12 @@ def get_similar_and_recommend(input_text):
|
|
| 66 |
torch.dot(all_video_embeddings[given_video_index], all_video_embeddings[i])
|
| 67 |
for i in range(all_video_embeddings.shape[0])
|
| 68 |
]
|
| 69 |
-
dot_products[given_video_index]
|
| 70 |
|
| 71 |
-
top_10_indices
|
| 72 |
return [df.iloc[idx].to_dict() for idx in top_10_indices]
|
| 73 |
|
| 74 |
-
top_10_recommended_videos_features
|
| 75 |
|
| 76 |
# Exclude unwanted features for recommended videos
|
| 77 |
for recommended_video in top_10_recommended_videos_features:
|
|
|
|
| 17 |
|
| 18 |
def forward(self, x, edge_index, edge_attr=None):
|
| 19 |
x = self.conv1(x, edge_index, edge_attr)
|
| 20 |
+
x = torch.relu(x)
|
| 21 |
+
x = dropout1(x)
|
| 22 |
+
x = self.conv2(x, edge_index, edge_attr)
|
| 23 |
return x
|
| 24 |
|
| 25 |
# Load the dataset and the GATConv model
|
| 26 |
+
data = torch.load("graph_data.pt", map_location=torch.device("cpu"))
|
| 27 |
|
| 28 |
# Load the BERT-based sentence transformer model
|
| 29 |
+
model_bert = SentenceTransformer("all-mpnet-base-v2")
|
| 30 |
|
| 31 |
# Ensure the DataFrame is loaded properly
|
| 32 |
try:
|
| 33 |
+
df = pd.read_json("combined_data.json.gz", orient='records', lines=True, compression='gzip')
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Error reading JSON file: {e}")
|
| 36 |
|
| 37 |
# Generate GNN-based embeddings
|
| 38 |
with torch.no_grad():
|
| 39 |
+
all_video_embeddings = gatconv_model(data.x, data.edge_index, data.edge_attr).cpu()
|
| 40 |
|
| 41 |
# Function to find the most similar video and recommend the top 10 based on GNN embeddings
|
| 42 |
def get_similar_and_recommend(input_text):
|
| 43 |
# Find the most similar video based on input text
|
| 44 |
+
embeddings_matrix = np.array(df["embeddings"].tolist())
|
| 45 |
+
input_embedding = model_bert.encode([input_text])[0]
|
| 46 |
+
similarities = cosine_similarity([input_embedding], embeddings_matrix)[0]
|
| 47 |
|
| 48 |
# Modify the similarity scores based on user input
|
| 49 |
user_keywords = input_text.split() # Create a list of keywords from user input
|
|
|
|
| 66 |
torch.dot(all_video_embeddings[given_video_index], all_video_embeddings[i])
|
| 67 |
for i in range(all_video_embeddings.shape[0])
|
| 68 |
]
|
| 69 |
+
dot_products[given_video_index] = -float("inf")
|
| 70 |
|
| 71 |
+
top_10_indices = np.argsort(dot_products)[::-1][:10]
|
| 72 |
return [df.iloc[idx].to_dict() for idx in top_10_indices]
|
| 73 |
|
| 74 |
+
top_10_recommended_videos_features = recommend_next_10_videos(most_similar_index, all_video_embeddings)
|
| 75 |
|
| 76 |
# Exclude unwanted features for recommended videos
|
| 77 |
for recommended_video in top_10_recommended_videos_features:
|