Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,15 +13,9 @@ Equilibrium is the point where supply equals demand.
|
|
| 13 |
Price acts as a signal for both producers and consumers.
|
| 14 |
Market dynamics are influenced by shifts in supply and demand.
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
cleaned_text = supply_demand_text.strip()
|
| 18 |
chunks = cleaned_text.split("\n")
|
| 19 |
-
cleaned_chunks = []
|
| 20 |
-
for chunk in chunks:
|
| 21 |
-
stripped_chunk = chunk.strip()
|
| 22 |
-
if stripped_chunk:
|
| 23 |
-
cleaned_chunks.append(stripped_chunk)
|
| 24 |
-
|
| 25 |
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
| 26 |
|
| 27 |
def get_top_chunks(query):
|
|
@@ -29,7 +23,8 @@ def get_top_chunks(query):
|
|
| 29 |
query_embedding = query_embedding / query_embedding.norm()
|
| 30 |
normalized_chunks = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 31 |
similarities = torch.matmul(normalized_chunks, query_embedding)
|
| 32 |
-
|
|
|
|
| 33 |
top_chunks = [cleaned_chunks[i] for i in top_indices]
|
| 34 |
return top_chunks
|
| 35 |
|
|
|
|
| 13 |
Price acts as a signal for both producers and consumers.
|
| 14 |
Market dynamics are influenced by shifts in supply and demand.
|
| 15 |
"""
|
|
|
|
| 16 |
cleaned_text = supply_demand_text.strip()
|
| 17 |
chunks = cleaned_text.split("\n")
|
| 18 |
+
cleaned_chunks = [chunk.strip() for chunk in chunks if chunk.strip()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
|
| 20 |
|
| 21 |
def get_top_chunks(query):
|
|
|
|
| 23 |
query_embedding = query_embedding / query_embedding.norm()
|
| 24 |
normalized_chunks = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 25 |
similarities = torch.matmul(normalized_chunks, query_embedding)
|
| 26 |
+
top_k = min(3, similarities.size(0)) # safer if fewer than 3 chunks
|
| 27 |
+
top_indices = torch.topk(similarities, k=top_k).indices
|
| 28 |
top_chunks = [cleaned_chunks[i] for i in top_indices]
|
| 29 |
return top_chunks
|
| 30 |
|