Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,6 +26,55 @@ def preprocess_text(text):
|
|
| 26 |
|
| 27 |
return cleaned_chunks
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
cleaned_chunks = preprocess_text(knowledge_base)
|
| 31 |
|
|
|
|
| 26 |
|
| 27 |
return cleaned_chunks
|
| 28 |
|
| 29 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 30 |
+
|
| 31 |
+
def create_embeddings(text_chunks):
|
| 32 |
+
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list
|
| 33 |
+
|
| 34 |
+
print(chunk_embeddings)
|
| 35 |
+
|
| 36 |
+
print(chunk_embeddings.shape)
|
| 37 |
+
|
| 38 |
+
return chunk_embeddings
|
| 39 |
+
|
| 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("How does water get into the sky?", chunk_embeddings, cleaned_chunks) # Complete this line
|
| 75 |
+
|
| 76 |
+
print(top_results)
|
| 77 |
+
|
| 78 |
|
| 79 |
cleaned_chunks = preprocess_text(knowledge_base)
|
| 80 |
|