Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,7 +45,7 @@ login(token=hf_token)
|
|
| 45 |
# Define the model pipeline with additional generation parameters
|
| 46 |
model_pipeline = pipeline(
|
| 47 |
# model="meta-llama/Llama-3.2-1B",
|
| 48 |
-
model="
|
| 49 |
#use_auth_token=hf_token,
|
| 50 |
max_length=1000, # You can increase this if needed
|
| 51 |
max_new_tokens=500 # Limit how many tokens are generated
|
|
@@ -510,9 +510,10 @@ import numpy as np
|
|
| 510 |
from transformers import TextClassificationPipeline
|
| 511 |
from typing import List, Union, Any
|
| 512 |
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
|
|
|
| 516 |
|
| 517 |
def rerank_results(
|
| 518 |
results: List[Any],
|
|
@@ -520,56 +521,27 @@ def rerank_results(
|
|
| 520 |
reranker: Union[TextClassificationPipeline, Any]
|
| 521 |
) -> List[Any]:
|
| 522 |
"""
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
Args:
|
| 526 |
-
results: List of documents/results to rerank
|
| 527 |
-
query: Search query string
|
| 528 |
-
reranker: Either a HuggingFace TextClassificationPipeline or a custom reranker
|
| 529 |
-
with a rerank() method.
|
| 530 |
-
|
| 531 |
-
Returns:
|
| 532 |
-
List of reranked results
|
| 533 |
"""
|
| 534 |
if not results:
|
| 535 |
return results
|
| 536 |
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
else:
|
| 554 |
-
score = float(pred)
|
| 555 |
-
scores.append(score)
|
| 556 |
-
|
| 557 |
-
# Sort the results based on scores in descending order
|
| 558 |
-
reranked_idx = np.argsort(scores)[::-1]
|
| 559 |
-
|
| 560 |
-
# Return reranked results based on the sorted indices
|
| 561 |
-
return [results[i] for i in reranked_idx]
|
| 562 |
-
|
| 563 |
-
except Exception as e:
|
| 564 |
-
print(f"Warning: Reranking failed with error: {str(e)}")
|
| 565 |
-
return results
|
| 566 |
-
else:
|
| 567 |
-
# For custom rerankers with a dedicated rerank method
|
| 568 |
-
try:
|
| 569 |
-
return reranker.rerank(query, [doc.page_content for doc in results])
|
| 570 |
-
except Exception as e:
|
| 571 |
-
print(f"Warning: Custom reranking failed with error: {str(e)}")
|
| 572 |
-
return results
|
| 573 |
|
| 574 |
# Main Comparison Function
|
| 575 |
def compare_embeddings(file, query, embedding_models, custom_embedding_model, split_strategy, chunk_size, overlap_size, custom_separators, vector_store_type, search_type, top_k, expected_result=None, lang='german', apply_preprocessing=True, optimize_vocab=False, apply_phonetic=True, phonetic_weight=0.3, custom_tokenizer_file=None, custom_tokenizer_model=None, custom_tokenizer_vocab_size=10000, custom_tokenizer_special_tokens=None, use_query_optimization=False, query_optimization_model="google/flan-t5-base", use_reranking=False):
|
|
|
|
| 45 |
# Define the model pipeline with additional generation parameters
|
| 46 |
model_pipeline = pipeline(
|
| 47 |
# model="meta-llama/Llama-3.2-1B",
|
| 48 |
+
model="meta-llama/Llama-3.2-1B",
|
| 49 |
#use_auth_token=hf_token,
|
| 50 |
max_length=1000, # You can increase this if needed
|
| 51 |
max_new_tokens=500 # Limit how many tokens are generated
|
|
|
|
| 510 |
from transformers import TextClassificationPipeline
|
| 511 |
from typing import List, Union, Any
|
| 512 |
|
| 513 |
+
|
| 514 |
+
|
| 515 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 516 |
+
|
| 517 |
|
| 518 |
def rerank_results(
|
| 519 |
results: List[Any],
|
|
|
|
| 521 |
reranker: Union[TextClassificationPipeline, Any]
|
| 522 |
) -> List[Any]:
|
| 523 |
"""
|
| 524 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
"""
|
| 526 |
if not results:
|
| 527 |
return results
|
| 528 |
|
| 529 |
+
# Step 1: Encode the query and documents using SentenceTransformer
|
| 530 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 531 |
+
doc_contents = [doc.page_content for doc in results] # Assuming each result has a `page_content` attribute
|
| 532 |
+
doc_embeddings = model.encode(doc_contents, convert_to_tensor=True)
|
| 533 |
+
|
| 534 |
+
# Step 2: Compute cosine similarities between query and document embeddings
|
| 535 |
+
cosine_scores = util.cos_sim(query_embedding, doc_embeddings)[0] # Shape: (number of documents,)
|
| 536 |
+
|
| 537 |
+
# Step 3: Sort documents by similarity score in descending order
|
| 538 |
+
reranked_idx = np.argsort(cosine_scores.numpy())[::-1]
|
| 539 |
+
|
| 540 |
+
# Step 4: Return the reranked documents
|
| 541 |
+
reranked_results = [results[i] for i in reranked_idx]
|
| 542 |
+
|
| 543 |
+
return reranked_results
|
| 544 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
|
| 546 |
# Main Comparison Function
|
| 547 |
def compare_embeddings(file, query, embedding_models, custom_embedding_model, split_strategy, chunk_size, overlap_size, custom_separators, vector_store_type, search_type, top_k, expected_result=None, lang='german', apply_preprocessing=True, optimize_vocab=False, apply_phonetic=True, phonetic_weight=0.3, custom_tokenizer_file=None, custom_tokenizer_model=None, custom_tokenizer_vocab_size=10000, custom_tokenizer_special_tokens=None, use_query_optimization=False, query_optimization_model="google/flan-t5-base", use_reranking=False):
|