Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,29 @@ for chunk in chunks:
|
|
| 16 |
cleaned_chunks.append(stripped_chunk)
|
| 17 |
print(cleaned_chunks)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
client = InferenceClient("google/gemma-3-27b-it")
|
| 20 |
|
| 21 |
def respond(message,history):
|
|
|
|
| 16 |
cleaned_chunks.append(stripped_chunk)
|
| 17 |
print(cleaned_chunks)
|
| 18 |
|
| 19 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 20 |
+
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
| 21 |
+
print(chunk_embeddings)
|
| 22 |
+
|
| 23 |
+
def get_top_chunks(query):
|
| 24 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 25 |
+
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 26 |
+
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 27 |
+
|
| 28 |
+
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
| 29 |
+
print(similarities)
|
| 30 |
+
|
| 31 |
+
top_indices = torch.topk(similarities, k=3).indices
|
| 32 |
+
print(top_indices)
|
| 33 |
+
|
| 34 |
+
top_chunks = []
|
| 35 |
+
|
| 36 |
+
for i in top_indices:
|
| 37 |
+
chunk = chunks[i]
|
| 38 |
+
top_chunks.append(chunk)
|
| 39 |
+
|
| 40 |
+
return top_chunks
|
| 41 |
+
|
| 42 |
client = InferenceClient("google/gemma-3-27b-it")
|
| 43 |
|
| 44 |
def respond(message,history):
|