amburp commited on
Commit
116810a
·
verified ·
1 Parent(s): 44a3c5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -3
app.py CHANGED
@@ -24,8 +24,15 @@ cleaned_chunks = preprocess_text(info_text)
24
  model = SentenceTransformer('all-MiniLM-L6-v2')
25
 
26
  def create_embeddings(text_chunks):
27
- chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list
28
- return chunk_embeddings
 
 
 
 
 
 
 
29
 
30
  chunk_embeddings = create_embeddings(cleaned_chunks) # Complete this line
31
 
@@ -35,7 +42,12 @@ def get_top_chunks(query, chunk_embeddings, text_chunks):
35
  query_embedding_normalized = query_embedding / query_embedding.norm()
36
  chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
37
  similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
38
- top_indices = torch.topk(similarities, k=3).indices
 
 
 
 
 
39
  top_chunks = []
40
 
41
  for i in top_indices:
 
24
  model = SentenceTransformer('all-MiniLM-L6-v2')
25
 
26
  def create_embeddings(text_chunks):
27
+ embeddings = model.encode(text_chunks, convert_to_tensor=True)
28
+
29
+ if embeddings.dim() == 1:
30
+ embeddings = embeddings.unsqueeze(0)
31
+
32
+ return embeddings
33
+
34
+ # chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list
35
+ # return chunk_embeddings
36
 
37
  chunk_embeddings = create_embeddings(cleaned_chunks) # Complete this line
38
 
 
42
  query_embedding_normalized = query_embedding / query_embedding.norm()
43
  chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
44
  similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
45
+
46
+ # top_indices = torch.topk(similarities, k=3).indices
47
+
48
+ k = min(3, len(text_chunks))
49
+ top_indices = torch.topk(similarities, k=k).indices
50
+
51
  top_chunks = []
52
 
53
  for i in top_indices: