Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,38 +40,29 @@ def create_embeddings(text_chunks):
|
|
| 40 |
chunk_embeddings = create_embeddings(cleaned_chunks)# Complete this line
|
| 41 |
|
| 42 |
def get_top_chunks(query, chunk_embeddings, text_chunks):
|
| 43 |
-
# Convert the query text into a vector embedding
|
| 44 |
query_embedding = model.encode(query, convert_to_tensor=True) # Complete this line
|
| 45 |
|
| 46 |
-
# Normalize the query embedding to unit length for accurate similarity comparison
|
| 47 |
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 48 |
|
| 49 |
-
# Normalize all chunk embeddings to unit length for consistent comparison
|
| 50 |
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 51 |
|
| 52 |
-
# Calculate cosine similarity between query and all chunks using matrix multiplication
|
| 53 |
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
|
| 54 |
|
| 55 |
-
# Print the similarities
|
| 56 |
print(similarities)
|
| 57 |
|
| 58 |
-
# Find the indices of the 3 chunks with highest similarity scores
|
| 59 |
top_indices = torch.topk(similarities, k=3).indices
|
| 60 |
|
| 61 |
-
# Print the top indices
|
| 62 |
print(top_indices)
|
| 63 |
|
| 64 |
-
# Create an empty list to store the most relevant chunks
|
| 65 |
top_chunks = []
|
| 66 |
|
| 67 |
-
# Loop through the top indices and retrieve the corresponding text chunks
|
| 68 |
for i in top_indices:
|
| 69 |
chunk = text_chunks[i]
|
| 70 |
top_chunks.append(chunk)
|
| 71 |
|
| 72 |
return top_chunks
|
| 73 |
|
| 74 |
-
top_results = get_top_chunks("
|
| 75 |
|
| 76 |
print(top_results)
|
| 77 |
|
|
|
|
| 40 |
chunk_embeddings = create_embeddings(cleaned_chunks)# Complete this line
|
| 41 |
|
| 42 |
def get_top_chunks(query, chunk_embeddings, text_chunks):
|
|
|
|
| 43 |
query_embedding = model.encode(query, convert_to_tensor=True) # Complete this line
|
| 44 |
|
|
|
|
| 45 |
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 46 |
|
|
|
|
| 47 |
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 48 |
|
|
|
|
| 49 |
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
|
| 50 |
|
|
|
|
| 51 |
print(similarities)
|
| 52 |
|
|
|
|
| 53 |
top_indices = torch.topk(similarities, k=3).indices
|
| 54 |
|
|
|
|
| 55 |
print(top_indices)
|
| 56 |
|
|
|
|
| 57 |
top_chunks = []
|
| 58 |
|
|
|
|
| 59 |
for i in top_indices:
|
| 60 |
chunk = text_chunks[i]
|
| 61 |
top_chunks.append(chunk)
|
| 62 |
|
| 63 |
return top_chunks
|
| 64 |
|
| 65 |
+
top_results = get_top_chunks("Your account has been compromised", chunk_embeddings, cleaned_chunks) # Complete this line
|
| 66 |
|
| 67 |
print(top_results)
|
| 68 |
|