update
Browse files
app.py
CHANGED
|
@@ -30,9 +30,14 @@ async def rerank(request: RerankerRequest):
|
|
| 30 |
if not query or not documents:
|
| 31 |
raise HTTPException(status_code=400, detail="Query and documents must be provided.")
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Create a list of dictionaries containing the document and its score
|
| 38 |
results = [{"document": doc, "score": score} for doc, score in zip(documents, scores)]
|
|
|
|
| 30 |
if not query or not documents:
|
| 31 |
raise HTTPException(status_code=400, detail="Query and documents must be provided.")
|
| 32 |
|
| 33 |
+
from sentence_transformers import util
|
| 34 |
+
|
| 35 |
+
# Calculate embeddings for the query and documents
|
| 36 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 37 |
+
document_embeddings = model.encode(documents, convert_to_tensor=True)
|
| 38 |
+
|
| 39 |
+
# Calculate cosine similarity between the query and each document
|
| 40 |
+
scores = util.cos_sim(query_embedding, document_embeddings)[0].tolist()
|
| 41 |
|
| 42 |
# Create a list of dictionaries containing the document and its score
|
| 43 |
results = [{"document": doc, "score": score} for doc, score in zip(documents, scores)]
|