Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,10 +10,12 @@ import tldextract
|
|
| 10 |
from ddgs import DDGS
|
| 11 |
from rapidfuzz.fuzz import ratio
|
| 12 |
|
| 13 |
-
from sentence_transformers import
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
from
|
| 17 |
|
| 18 |
# =====================================================
|
| 19 |
# APP
|
|
@@ -24,22 +26,22 @@ app = FastAPI(
|
|
| 24 |
)
|
| 25 |
|
| 26 |
# =====================================================
|
| 27 |
-
# EMBEDDING MODEL
|
| 28 |
# =====================================================
|
| 29 |
|
| 30 |
embedding_model = SentenceTransformer(
|
| 31 |
-
"
|
| 32 |
)
|
| 33 |
|
| 34 |
embedding_model.max_seq_length = 256
|
| 35 |
|
| 36 |
# =====================================================
|
| 37 |
-
# RERANKER
|
| 38 |
# =====================================================
|
| 39 |
|
| 40 |
-
reranker =
|
| 41 |
-
"
|
| 42 |
-
|
| 43 |
)
|
| 44 |
|
| 45 |
# =====================================================
|
|
@@ -310,7 +312,10 @@ def split_paragraphs(text):
|
|
| 310 |
|
| 311 |
lower = paragraph.lower()
|
| 312 |
|
| 313 |
-
if any(
|
|
|
|
|
|
|
|
|
|
| 314 |
continue
|
| 315 |
|
| 316 |
cleaned.append(paragraph)
|
|
@@ -332,9 +337,18 @@ def embedding_rank_paragraphs(
|
|
| 332 |
|
| 333 |
try:
|
| 334 |
|
| 335 |
-
query_embedding = embedding_model.encode(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
-
paragraph_embeddings = embedding_model.encode(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
similarities = cosine_similarity(
|
| 340 |
query_embedding,
|
|
@@ -376,7 +390,7 @@ def embedding_rank_paragraphs(
|
|
| 376 |
return paragraphs[:20]
|
| 377 |
|
| 378 |
# =====================================================
|
| 379 |
-
#
|
| 380 |
# =====================================================
|
| 381 |
|
| 382 |
def rerank_paragraphs(query, paragraphs):
|
|
@@ -391,7 +405,11 @@ def rerank_paragraphs(query, paragraphs):
|
|
| 391 |
for paragraph in paragraphs
|
| 392 |
]
|
| 393 |
|
| 394 |
-
scores = reranker.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
|
| 396 |
ranked = sorted(
|
| 397 |
zip(paragraphs, scores),
|
|
@@ -491,7 +509,11 @@ def rerank_facts(query, facts):
|
|
| 491 |
for fact in facts
|
| 492 |
]
|
| 493 |
|
| 494 |
-
scores = reranker.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 495 |
|
| 496 |
ranked = sorted(
|
| 497 |
zip(facts, scores),
|
|
|
|
| 10 |
from ddgs import DDGS
|
| 11 |
from rapidfuzz.fuzz import ratio
|
| 12 |
|
| 13 |
+
from sentence_transformers import (
|
| 14 |
+
SentenceTransformer,
|
| 15 |
+
CrossEncoder,
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 19 |
|
| 20 |
# =====================================================
|
| 21 |
# APP
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
# =====================================================
|
| 29 |
+
# LIGHTWEIGHT EMBEDDING MODEL
|
| 30 |
# =====================================================
|
| 31 |
|
| 32 |
embedding_model = SentenceTransformer(
|
| 33 |
+
"sentence-transformers/all-MiniLM-L6-v2"
|
| 34 |
)
|
| 35 |
|
| 36 |
embedding_model.max_seq_length = 256
|
| 37 |
|
| 38 |
# =====================================================
|
| 39 |
+
# LIGHTWEIGHT RERANKER
|
| 40 |
# =====================================================
|
| 41 |
|
| 42 |
+
reranker = CrossEncoder(
|
| 43 |
+
"cross-encoder/ms-marco-MiniLM-L-6-v2",
|
| 44 |
+
max_length=256,
|
| 45 |
)
|
| 46 |
|
| 47 |
# =====================================================
|
|
|
|
| 312 |
|
| 313 |
lower = paragraph.lower()
|
| 314 |
|
| 315 |
+
if any(
|
| 316 |
+
junk in lower
|
| 317 |
+
for junk in JUNK_PHRASES
|
| 318 |
+
):
|
| 319 |
continue
|
| 320 |
|
| 321 |
cleaned.append(paragraph)
|
|
|
|
| 337 |
|
| 338 |
try:
|
| 339 |
|
| 340 |
+
query_embedding = embedding_model.encode(
|
| 341 |
+
[query],
|
| 342 |
+
normalize_embeddings=True,
|
| 343 |
+
show_progress_bar=False,
|
| 344 |
+
)
|
| 345 |
|
| 346 |
+
paragraph_embeddings = embedding_model.encode(
|
| 347 |
+
paragraphs,
|
| 348 |
+
batch_size=16,
|
| 349 |
+
normalize_embeddings=True,
|
| 350 |
+
show_progress_bar=False,
|
| 351 |
+
)
|
| 352 |
|
| 353 |
similarities = cosine_similarity(
|
| 354 |
query_embedding,
|
|
|
|
| 390 |
return paragraphs[:20]
|
| 391 |
|
| 392 |
# =====================================================
|
| 393 |
+
# RERANK PARAGRAPHS
|
| 394 |
# =====================================================
|
| 395 |
|
| 396 |
def rerank_paragraphs(query, paragraphs):
|
|
|
|
| 405 |
for paragraph in paragraphs
|
| 406 |
]
|
| 407 |
|
| 408 |
+
scores = reranker.predict(
|
| 409 |
+
pairs,
|
| 410 |
+
batch_size=16,
|
| 411 |
+
show_progress_bar=False,
|
| 412 |
+
)
|
| 413 |
|
| 414 |
ranked = sorted(
|
| 415 |
zip(paragraphs, scores),
|
|
|
|
| 509 |
for fact in facts
|
| 510 |
]
|
| 511 |
|
| 512 |
+
scores = reranker.predict(
|
| 513 |
+
pairs,
|
| 514 |
+
batch_size=16,
|
| 515 |
+
show_progress_bar=False,
|
| 516 |
+
)
|
| 517 |
|
| 518 |
ranked = sorted(
|
| 519 |
zip(facts, scores),
|