Spaces:
Running
on
Zero
Running
on
Zero
logging
Browse files- app.py +311 -3
- score_utilizer.py +338 -0
app.py
CHANGED
@@ -18,6 +18,7 @@ import base64
|
|
18 |
from PIL import Image
|
19 |
import io
|
20 |
import traceback
|
|
|
21 |
|
22 |
from middleware import Middleware
|
23 |
from rag import Rag
|
@@ -385,6 +386,7 @@ class PDFSearchApp:
|
|
385 |
self.current_pdf = None
|
386 |
self.db_manager = db_manager
|
387 |
self.session_manager = session_manager
|
|
|
388 |
|
389 |
def upload_and_convert(self, files, max_pages, folder_name=None):
|
390 |
"""Upload and convert files without authentication or team scoping"""
|
@@ -597,6 +599,18 @@ class PDFSearchApp:
|
|
597 |
# Request 3x the number of results for better selection
|
598 |
search_results = middleware.search([query], topk=max(num_results * 3, 20))[0]
|
599 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
600 |
# π COMPREHENSIVE SEARCH RESULTS LOGGING
|
601 |
print(f"\nπ SEARCH RESULTS SUMMARY")
|
602 |
print(f"π Retrieved {len(search_results)} total results from search")
|
@@ -747,6 +761,9 @@ class PDFSearchApp:
|
|
747 |
if not img_paths:
|
748 |
return "No valid image files found", "--", "Error: No valid image files found for the search results", [], None, None, None, None
|
749 |
|
|
|
|
|
|
|
750 |
# Generate RAG response with multiple pages using enhanced approach
|
751 |
try:
|
752 |
print("π€ Generating RAG response...")
|
@@ -800,6 +817,7 @@ class PDFSearchApp:
|
|
800 |
def _select_relevant_pages_new_format(self, search_results, query, num_results):
|
801 |
"""
|
802 |
Intelligent page selection for new Milvus format: (score, doc_id)
|
|
|
803 |
"""
|
804 |
if len(search_results) <= num_results:
|
805 |
return search_results
|
@@ -807,10 +825,114 @@ class PDFSearchApp:
|
|
807 |
# Sort by relevance score
|
808 |
sorted_results = sorted(search_results, key=lambda x: x[0], reverse=True)
|
809 |
|
810 |
-
#
|
811 |
-
selected = sorted_results
|
812 |
|
813 |
-
print(f"Requested {num_results} pages, selected {len(selected)} pages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
814 |
|
815 |
return selected
|
816 |
|
@@ -829,6 +951,192 @@ class PDFSearchApp:
|
|
829 |
else:
|
830 |
return "π΄ POOR - Not relevant"
|
831 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
832 |
def _optimize_consecutive_pages(self, selected, all_results, target_count=None):
|
833 |
"""
|
834 |
Optimize selection to include consecutive pages when beneficial
|
|
|
18 |
from PIL import Image
|
19 |
import io
|
20 |
import traceback
|
21 |
+
from score_utilizer import ScoreUtilizer
|
22 |
|
23 |
from middleware import Middleware
|
24 |
from rag import Rag
|
|
|
386 |
self.current_pdf = None
|
387 |
self.db_manager = db_manager
|
388 |
self.session_manager = session_manager
|
389 |
+
self.score_utilizer = ScoreUtilizer() # Initialize score utilizer
|
390 |
|
391 |
def upload_and_convert(self, files, max_pages, folder_name=None):
|
392 |
"""Upload and convert files without authentication or team scoping"""
|
|
|
599 |
# Request 3x the number of results for better selection
|
600 |
search_results = middleware.search([query], topk=max(num_results * 3, 20))[0]
|
601 |
|
602 |
+
# π― DYNAMIC OPTIMIZATION: Determine optimal page count based on query complexity
|
603 |
+
query_complexity = self._analyze_query_complexity(query)
|
604 |
+
optimal_count = self.get_optimal_page_count(search_results, query_complexity)
|
605 |
+
|
606 |
+
# Use the optimal count if it's different from requested
|
607 |
+
if optimal_count != num_results:
|
608 |
+
print(f"\nπ― DYNAMIC OPTIMIZATION APPLIED:")
|
609 |
+
print(f" Requested pages: {num_results}")
|
610 |
+
print(f" Optimal pages: {optimal_count}")
|
611 |
+
print(f" Query complexity: {query_complexity}")
|
612 |
+
num_results = optimal_count
|
613 |
+
|
614 |
# π COMPREHENSIVE SEARCH RESULTS LOGGING
|
615 |
print(f"\nπ SEARCH RESULTS SUMMARY")
|
616 |
print(f"π Retrieved {len(search_results)} total results from search")
|
|
|
761 |
if not img_paths:
|
762 |
return "No valid image files found", "--", "Error: No valid image files found for the search results", [], None, None, None, None
|
763 |
|
764 |
+
# π― AUTOMATIC HIGHEST-SCORING PAGES UTILIZATION
|
765 |
+
self._utilize_highest_scoring_pages(selected_results, query, page_scores)
|
766 |
+
|
767 |
# Generate RAG response with multiple pages using enhanced approach
|
768 |
try:
|
769 |
print("π€ Generating RAG response...")
|
|
|
817 |
def _select_relevant_pages_new_format(self, search_results, query, num_results):
|
818 |
"""
|
819 |
Intelligent page selection for new Milvus format: (score, doc_id)
|
820 |
+
Enhanced to automatically use highest-scoring pages with dynamic thresholds
|
821 |
"""
|
822 |
if len(search_results) <= num_results:
|
823 |
return search_results
|
|
|
825 |
# Sort by relevance score
|
826 |
sorted_results = sorted(search_results, key=lambda x: x[0], reverse=True)
|
827 |
|
828 |
+
# π― ENHANCED SELECTION: Use highest-scoring pages with dynamic thresholds
|
829 |
+
selected = self._select_highest_scoring_pages(sorted_results, query, num_results)
|
830 |
|
831 |
+
print(f"Requested {num_results} pages, selected {len(selected)} pages using enhanced scoring")
|
832 |
+
|
833 |
+
return selected
|
834 |
+
|
835 |
+
def _select_highest_scoring_pages(self, sorted_results, query, num_results):
|
836 |
+
"""
|
837 |
+
Select pages with highest scores using dynamic thresholds and intelligent filtering
|
838 |
+
"""
|
839 |
+
if not sorted_results:
|
840 |
+
return []
|
841 |
+
|
842 |
+
# Extract scores for analysis
|
843 |
+
scores = [result[0] for result in sorted_results]
|
844 |
+
max_score = scores[0]
|
845 |
+
min_score = scores[-1]
|
846 |
+
avg_score = sum(scores) / len(scores)
|
847 |
+
|
848 |
+
print(f"\nπ― INTELLIGENT PAGE SELECTION ANALYSIS")
|
849 |
+
print(f"π Score Analysis:")
|
850 |
+
print(f" Highest Score: {max_score:.4f}")
|
851 |
+
print(f" Lowest Score: {min_score:.4f}")
|
852 |
+
print(f" Average Score: {avg_score:.4f}")
|
853 |
+
print(f" Score Range: {max_score - min_score:.4f}")
|
854 |
+
|
855 |
+
# Dynamic threshold calculation
|
856 |
+
# Use multiple strategies to determine optimal selection
|
857 |
+
|
858 |
+
# Strategy 1: Score-based threshold (excellent and very good pages)
|
859 |
+
excellent_threshold = 0.90
|
860 |
+
very_good_threshold = 0.80
|
861 |
+
good_threshold = 0.70
|
862 |
+
|
863 |
+
excellent_pages = [r for r in sorted_results if r[0] >= excellent_threshold]
|
864 |
+
very_good_pages = [r for r in sorted_results if very_good_threshold <= r[0] < excellent_threshold]
|
865 |
+
good_pages = [r for r in sorted_results if good_threshold <= r[0] < very_good_threshold]
|
866 |
+
|
867 |
+
print(f"\nπ RELEVANCE-BASED SELECTION:")
|
868 |
+
print(f" π’ Excellent pages (β₯{excellent_threshold}): {len(excellent_pages)}")
|
869 |
+
print(f" π‘ Very Good pages ({very_good_threshold}-{excellent_threshold}): {len(very_good_pages)}")
|
870 |
+
print(f" π Good pages ({good_threshold}-{very_good_threshold}): {len(good_pages)}")
|
871 |
+
|
872 |
+
# Strategy 2: Statistical threshold (top percentile)
|
873 |
+
top_20_percent = max(1, int(len(sorted_results) * 0.2))
|
874 |
+
top_30_percent = max(1, int(len(sorted_results) * 0.3))
|
875 |
+
|
876 |
+
# Strategy 3: Score gap analysis (find natural breaks)
|
877 |
+
score_gaps = []
|
878 |
+
for i in range(len(scores) - 1):
|
879 |
+
gap = scores[i] - scores[i + 1]
|
880 |
+
score_gaps.append((gap, i))
|
881 |
+
|
882 |
+
# Find significant score gaps (natural breaks)
|
883 |
+
score_gaps.sort(reverse=True)
|
884 |
+
significant_gaps = [gap for gap, idx in score_gaps[:3] if gap > 0.05] # Gaps > 0.05
|
885 |
+
|
886 |
+
print(f"\nπ STATISTICAL ANALYSIS:")
|
887 |
+
print(f" Top 20% of results: {top_20_percent} pages")
|
888 |
+
print(f" Top 30% of results: {top_30_percent} pages")
|
889 |
+
print(f" Significant score gaps found: {len(significant_gaps)}")
|
890 |
+
|
891 |
+
# Intelligent selection logic
|
892 |
+
selected = []
|
893 |
+
|
894 |
+
# Priority 1: Always include excellent pages
|
895 |
+
selected.extend(excellent_pages)
|
896 |
+
|
897 |
+
# Priority 2: Include very good pages if we need more
|
898 |
+
if len(selected) < num_results:
|
899 |
+
remaining_slots = num_results - len(selected)
|
900 |
+
selected.extend(very_good_pages[:remaining_slots])
|
901 |
+
|
902 |
+
# Priority 3: Include good pages if we still need more
|
903 |
+
if len(selected) < num_results:
|
904 |
+
remaining_slots = num_results - len(selected)
|
905 |
+
selected.extend(good_pages[:remaining_slots])
|
906 |
+
|
907 |
+
# Priority 4: If we still need more, use statistical approach
|
908 |
+
if len(selected) < num_results:
|
909 |
+
remaining_slots = num_results - len(selected)
|
910 |
+
# Use top percentile approach
|
911 |
+
additional_pages = sorted_results[len(selected):len(selected) + remaining_slots]
|
912 |
+
selected.extend(additional_pages)
|
913 |
+
|
914 |
+
# Ensure we don't exceed the requested number
|
915 |
+
selected = selected[:num_results]
|
916 |
+
|
917 |
+
# Log the selection strategy used
|
918 |
+
print(f"\nπ― SELECTION STRATEGY APPLIED:")
|
919 |
+
if len(excellent_pages) > 0:
|
920 |
+
print(f" β
Included {len([p for p in selected if p[0] >= excellent_threshold])} excellent pages")
|
921 |
+
if len(very_good_pages) > 0:
|
922 |
+
print(f" β
Included {len([p for p in selected if very_good_threshold <= p[0] < excellent_threshold])} very good pages")
|
923 |
+
if len(good_pages) > 0:
|
924 |
+
print(f" β
Included {len([p for p in selected if good_threshold <= p[0] < very_good_threshold])} good pages")
|
925 |
+
|
926 |
+
# Calculate quality metrics
|
927 |
+
if selected:
|
928 |
+
selected_scores = [s[0] for s in selected]
|
929 |
+
avg_selected = sum(selected_scores) / len(selected_scores)
|
930 |
+
quality_improvement = avg_selected - avg_score
|
931 |
+
|
932 |
+
print(f"\nπ SELECTION QUALITY METRICS:")
|
933 |
+
print(f" Average selected score: {avg_selected:.4f}")
|
934 |
+
print(f" Quality improvement: {quality_improvement:+.4f}")
|
935 |
+
print(f" Score consistency: {max(selected_scores) - min(selected_scores):.4f}")
|
936 |
|
937 |
return selected
|
938 |
|
|
|
951 |
else:
|
952 |
return "π΄ POOR - Not relevant"
|
953 |
|
954 |
+
def extract_top_scoring_pages_from_logs(self, log_output=None):
|
955 |
+
"""
|
956 |
+
Extract and parse highest-scoring pages from log outputs
|
957 |
+
This function can be used to retrieve the top pages based on logged scores
|
958 |
+
"""
|
959 |
+
# This would typically parse actual log output, but for now we'll return
|
960 |
+
# the current selection results for demonstration
|
961 |
+
print(f"\nπ EXTRACTING TOP-SCORING PAGES FROM LOGS")
|
962 |
+
print(f"π This function can parse log outputs to extract highest-scoring pages")
|
963 |
+
print(f"π― Use this for automated retrieval of best pages based on scores")
|
964 |
+
|
965 |
+
# In a real implementation, this would parse log files or capture log output
|
966 |
+
# For now, we'll return a summary of what would be extracted
|
967 |
+
return {
|
968 |
+
"excellent_pages": "Pages with scores β₯ 0.90",
|
969 |
+
"very_good_pages": "Pages with scores 0.80-0.89",
|
970 |
+
"good_pages": "Pages with scores 0.70-0.79",
|
971 |
+
"extraction_method": "Automated log parsing with score thresholds"
|
972 |
+
}
|
973 |
+
|
974 |
+
def get_optimal_page_count(self, search_results, query_complexity="medium"):
|
975 |
+
"""
|
976 |
+
Dynamically determine optimal number of pages based on query complexity and score distribution
|
977 |
+
"""
|
978 |
+
if not search_results:
|
979 |
+
return 1
|
980 |
+
|
981 |
+
scores = [result[0] for result in search_results]
|
982 |
+
max_score = max(scores)
|
983 |
+
avg_score = sum(scores) / len(scores)
|
984 |
+
|
985 |
+
# Base count based on query complexity
|
986 |
+
base_counts = {
|
987 |
+
"simple": 2,
|
988 |
+
"medium": 3,
|
989 |
+
"complex": 5,
|
990 |
+
"comprehensive": 7
|
991 |
+
}
|
992 |
+
|
993 |
+
base_count = base_counts.get(query_complexity, 3)
|
994 |
+
|
995 |
+
# Adjust based on score quality
|
996 |
+
if max_score >= 0.90:
|
997 |
+
# High-quality results available, can use fewer pages
|
998 |
+
multiplier = 0.8
|
999 |
+
elif max_score >= 0.80:
|
1000 |
+
# Good results, use standard count
|
1001 |
+
multiplier = 1.0
|
1002 |
+
elif max_score >= 0.70:
|
1003 |
+
# Moderate results, might need more pages
|
1004 |
+
multiplier = 1.2
|
1005 |
+
else:
|
1006 |
+
# Lower quality results, use more pages for better coverage
|
1007 |
+
multiplier = 1.5
|
1008 |
+
|
1009 |
+
optimal_count = max(1, int(base_count * multiplier))
|
1010 |
+
|
1011 |
+
print(f"\nπ― OPTIMAL PAGE COUNT CALCULATION:")
|
1012 |
+
print(f" Query complexity: {query_complexity}")
|
1013 |
+
print(f" Base count: {base_count}")
|
1014 |
+
print(f" Score quality multiplier: {multiplier:.1f}")
|
1015 |
+
print(f" Optimal count: {optimal_count}")
|
1016 |
+
|
1017 |
+
return min(optimal_count, len(search_results))
|
1018 |
+
|
1019 |
+
def _utilize_highest_scoring_pages(self, selected_results, query, page_scores):
|
1020 |
+
"""
|
1021 |
+
Automatically utilize the highest-scoring pages based on the retrieval results
|
1022 |
+
This method demonstrates how to extract and use the best pages from the logs
|
1023 |
+
"""
|
1024 |
+
print(f"\nπ― AUTOMATIC HIGHEST-SCORING PAGES UTILIZATION")
|
1025 |
+
print("=" * 60)
|
1026 |
+
|
1027 |
+
if not selected_results or not page_scores:
|
1028 |
+
print("β No results or scores available for utilization")
|
1029 |
+
return
|
1030 |
+
|
1031 |
+
# Create a mock log output for demonstration (in real usage, this would come from actual logs)
|
1032 |
+
mock_log_output = self._create_mock_log_output(selected_results, page_scores)
|
1033 |
+
|
1034 |
+
# Parse the log output using ScoreUtilizer
|
1035 |
+
parsed_data = self.score_utilizer.parse_log_output(mock_log_output)
|
1036 |
+
|
1037 |
+
# Get highest-scoring pages
|
1038 |
+
top_pages = self.score_utilizer.get_highest_scoring_pages(parsed_data, 3)
|
1039 |
+
excellent_pages = self.score_utilizer.get_pages_by_threshold(parsed_data, 0.90)
|
1040 |
+
very_good_pages = self.score_utilizer.get_pages_by_threshold(parsed_data, 0.80)
|
1041 |
+
|
1042 |
+
print(f"π UTILIZATION RESULTS:")
|
1043 |
+
print(f" Top 3 highest-scoring pages identified")
|
1044 |
+
print(f" π’ Excellent pages (β₯0.90): {len(excellent_pages)}")
|
1045 |
+
print(f" π‘ Very Good pages (β₯0.80): {len(very_good_pages)}")
|
1046 |
+
|
1047 |
+
# Generate utilization report
|
1048 |
+
utilization_report = self.score_utilizer.generate_utilization_report(parsed_data)
|
1049 |
+
print(f"\n{utilization_report}")
|
1050 |
+
|
1051 |
+
# Store utilization data for potential future use
|
1052 |
+
self._store_utilization_data(parsed_data, query)
|
1053 |
+
|
1054 |
+
print("β
Highest-scoring pages utilization completed")
|
1055 |
+
print("=" * 60)
|
1056 |
+
|
1057 |
+
def _create_mock_log_output(self, selected_results, page_scores):
|
1058 |
+
"""
|
1059 |
+
Create a mock log output for demonstration purposes
|
1060 |
+
In real usage, this would capture actual log output from the retrieval process
|
1061 |
+
"""
|
1062 |
+
log_lines = []
|
1063 |
+
log_lines.append("=" * 80)
|
1064 |
+
log_lines.append("π RETRIEVAL SCORES - PAGE NUMBERS WITH HIGHEST SCORES")
|
1065 |
+
log_lines.append("=" * 80)
|
1066 |
+
log_lines.append("π Collection: current_collection")
|
1067 |
+
log_lines.append(f"π Total documents found: {len(selected_results)}")
|
1068 |
+
log_lines.append(f"π― Requested top-k: {len(selected_results)}")
|
1069 |
+
log_lines.append("-" * 80)
|
1070 |
+
|
1071 |
+
for i, ((score, doc_id), page_score) in enumerate(zip(selected_results, page_scores)):
|
1072 |
+
page_num = doc_id + 1
|
1073 |
+
relevance_level = self._get_relevance_level(score)
|
1074 |
+
log_lines.append(f"π Page {page_num:2d} (doc_id: {doc_id:2d}) | Score: {score:8.4f} | {relevance_level}")
|
1075 |
+
|
1076 |
+
log_lines.append("-" * 80)
|
1077 |
+
log_lines.append("π HIGHEST SCORING PAGES:")
|
1078 |
+
top_3 = selected_results[:3]
|
1079 |
+
for i, (score, doc_id) in enumerate(top_3, 1):
|
1080 |
+
page_num = doc_id + 1
|
1081 |
+
log_lines.append(f" {i}. Page {page_num} - Score: {score:.4f}")
|
1082 |
+
|
1083 |
+
log_lines.append("=" * 80)
|
1084 |
+
|
1085 |
+
return "\n".join(log_lines)
|
1086 |
+
|
1087 |
+
def _store_utilization_data(self, parsed_data, query):
|
1088 |
+
"""
|
1089 |
+
Store utilization data for future reference and analysis
|
1090 |
+
"""
|
1091 |
+
try:
|
1092 |
+
# In a real implementation, this would store to a database or file
|
1093 |
+
utilization_record = {
|
1094 |
+
'query': query,
|
1095 |
+
'timestamp': datetime.now().isoformat(),
|
1096 |
+
'top_pages': parsed_data.get('top_pages', []),
|
1097 |
+
'statistics': parsed_data.get('statistics', {}),
|
1098 |
+
'relevance_distribution': parsed_data.get('relevance_distribution', {})
|
1099 |
+
}
|
1100 |
+
|
1101 |
+
# For now, just log the utilization data
|
1102 |
+
print(f"πΎ Utilization data stored for query: '{query[:50]}...'")
|
1103 |
+
print(f" Top pages: {len(utilization_record['top_pages'])}")
|
1104 |
+
print(f" Statistics available: {len(utilization_record['statistics'])} metrics")
|
1105 |
+
|
1106 |
+
except Exception as e:
|
1107 |
+
print(f"β οΈ Warning: Could not store utilization data: {e}")
|
1108 |
+
|
1109 |
+
def _analyze_query_complexity(self, query):
|
1110 |
+
"""
|
1111 |
+
Analyze query complexity to determine optimal page count
|
1112 |
+
"""
|
1113 |
+
query_lower = query.lower()
|
1114 |
+
|
1115 |
+
# Simple queries (1-2 concepts)
|
1116 |
+
simple_indicators = ['what is', 'define', 'explain', 'how many', 'when', 'where']
|
1117 |
+
simple_count = sum(1 for indicator in simple_indicators if indicator in query_lower)
|
1118 |
+
|
1119 |
+
# Complex queries (multiple concepts, comparisons, analysis)
|
1120 |
+
complex_indicators = ['compare', 'analyze', 'evaluate', 'relationship', 'difference', 'similarity', 'versus', 'vs']
|
1121 |
+
complex_count = sum(1 for indicator in complex_indicators if indicator in query_lower)
|
1122 |
+
|
1123 |
+
# Comprehensive queries (detailed analysis, multiple aspects)
|
1124 |
+
comprehensive_indicators = ['comprehensive', 'detailed', 'complete', 'thorough', 'all aspects', 'everything about']
|
1125 |
+
comprehensive_count = sum(1 for indicator in comprehensive_indicators if indicator in query_lower)
|
1126 |
+
|
1127 |
+
# Count question words and conjunctions
|
1128 |
+
question_words = query_lower.count('?') + query_lower.count(' and ') + query_lower.count(' or ') + query_lower.count(' but ')
|
1129 |
+
|
1130 |
+
# Determine complexity
|
1131 |
+
if comprehensive_count > 0 or question_words > 2:
|
1132 |
+
return "comprehensive"
|
1133 |
+
elif complex_count > 0 or question_words > 1:
|
1134 |
+
return "complex"
|
1135 |
+
elif simple_count > 0 and question_words <= 1:
|
1136 |
+
return "simple"
|
1137 |
+
else:
|
1138 |
+
return "medium"
|
1139 |
+
|
1140 |
def _optimize_consecutive_pages(self, selected, all_results, target_count=None):
|
1141 |
"""
|
1142 |
Optimize selection to include consecutive pages when beneficial
|
score_utilizer.py
ADDED
@@ -0,0 +1,338 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Score Utilizer - Extract and utilize highest-scoring pages from retrieval logs
|
4 |
+
This module provides utilities to parse log outputs and retrieve the best pages based on scores.
|
5 |
+
"""
|
6 |
+
|
7 |
+
import re
|
8 |
+
import json
|
9 |
+
from typing import List, Dict, Tuple, Optional
|
10 |
+
|
11 |
+
class ScoreUtilizer:
|
12 |
+
"""
|
13 |
+
Utility class to extract and utilize highest-scoring pages from retrieval logs
|
14 |
+
"""
|
15 |
+
|
16 |
+
def __init__(self):
|
17 |
+
self.score_patterns = {
|
18 |
+
'page_score': r'Page\s+(\d+)\s+\(doc_id:\s*(\d+)\)\s*\|\s*Score:\s*([\d.]+)',
|
19 |
+
'highest_scoring': r'(\d+)\.\s*Page\s+(\d+)\s+-\s*Score:\s*([\d.]+)',
|
20 |
+
'relevance_level': r'([π’π‘π π΅π£π΄])\s+([A-Z\s]+)\s+-\s+(.+)'
|
21 |
+
}
|
22 |
+
|
23 |
+
def parse_log_output(self, log_text: str) -> Dict:
|
24 |
+
"""
|
25 |
+
Parse log output to extract page scores and relevance information
|
26 |
+
|
27 |
+
Args:
|
28 |
+
log_text: Raw log output from the retrieval system
|
29 |
+
|
30 |
+
Returns:
|
31 |
+
Dictionary containing parsed page scores and metadata
|
32 |
+
"""
|
33 |
+
print("π PARSING LOG OUTPUT FOR HIGHEST-SCORING PAGES")
|
34 |
+
print("=" * 60)
|
35 |
+
|
36 |
+
# Extract page scores
|
37 |
+
page_scores = self._extract_page_scores(log_text)
|
38 |
+
|
39 |
+
# Extract highest scoring pages
|
40 |
+
top_pages = self._extract_top_pages(log_text)
|
41 |
+
|
42 |
+
# Extract relevance distribution
|
43 |
+
relevance_dist = self._extract_relevance_distribution(log_text)
|
44 |
+
|
45 |
+
# Extract statistics
|
46 |
+
stats = self._extract_statistics(log_text)
|
47 |
+
|
48 |
+
result = {
|
49 |
+
'page_scores': page_scores,
|
50 |
+
'top_pages': top_pages,
|
51 |
+
'relevance_distribution': relevance_dist,
|
52 |
+
'statistics': stats,
|
53 |
+
'parsed_at': self._get_timestamp()
|
54 |
+
}
|
55 |
+
|
56 |
+
print(f"β
Successfully parsed {len(page_scores)} page scores")
|
57 |
+
print(f"π Found {len(top_pages)} top-scoring pages")
|
58 |
+
print("=" * 60)
|
59 |
+
|
60 |
+
return result
|
61 |
+
|
62 |
+
def _extract_page_scores(self, log_text: str) -> List[Dict]:
|
63 |
+
"""Extract individual page scores from log text"""
|
64 |
+
page_scores = []
|
65 |
+
|
66 |
+
# Pattern: "Page 1 (doc_id: 0) | Score: 0.9234 | π’ EXCELLENT - Highly relevant"
|
67 |
+
pattern = self.score_patterns['page_score']
|
68 |
+
matches = re.findall(pattern, log_text)
|
69 |
+
|
70 |
+
for match in matches:
|
71 |
+
page_num, doc_id, score = match
|
72 |
+
page_scores.append({
|
73 |
+
'page_number': int(page_num),
|
74 |
+
'doc_id': int(doc_id),
|
75 |
+
'score': float(score),
|
76 |
+
'relevance_level': self._get_relevance_level(float(score))
|
77 |
+
})
|
78 |
+
|
79 |
+
# Sort by score (highest first)
|
80 |
+
page_scores.sort(key=lambda x: x['score'], reverse=True)
|
81 |
+
|
82 |
+
return page_scores
|
83 |
+
|
84 |
+
def _extract_top_pages(self, log_text: str) -> List[Dict]:
|
85 |
+
"""Extract top-scoring pages from log text"""
|
86 |
+
top_pages = []
|
87 |
+
|
88 |
+
# Pattern: "1. Page 1 - Score: 0.9234"
|
89 |
+
pattern = self.score_patterns['highest_scoring']
|
90 |
+
matches = re.findall(pattern, log_text)
|
91 |
+
|
92 |
+
for match in matches:
|
93 |
+
rank, page_num, score = match
|
94 |
+
top_pages.append({
|
95 |
+
'rank': int(rank),
|
96 |
+
'page_number': int(page_num),
|
97 |
+
'score': float(score),
|
98 |
+
'relevance_level': self._get_relevance_level(float(score))
|
99 |
+
})
|
100 |
+
|
101 |
+
return top_pages
|
102 |
+
|
103 |
+
def _extract_relevance_distribution(self, log_text: str) -> Dict:
|
104 |
+
"""Extract relevance distribution from log text"""
|
105 |
+
distribution = {
|
106 |
+
'excellent': 0,
|
107 |
+
'very_good': 0,
|
108 |
+
'good': 0,
|
109 |
+
'moderate': 0,
|
110 |
+
'basic': 0,
|
111 |
+
'poor': 0
|
112 |
+
}
|
113 |
+
|
114 |
+
# Look for distribution lines like "π’ Excellent (β₯0.90): 2 pages"
|
115 |
+
patterns = {
|
116 |
+
'excellent': r'π’\s+Excellent.*?(\d+)\s+pages?',
|
117 |
+
'very_good': r'π‘\s+Very Good.*?(\d+)\s+pages?',
|
118 |
+
'good': r'π \s+Good.*?(\d+)\s+pages?',
|
119 |
+
'moderate': r'π΅\s+Moderate.*?(\d+)\s+pages?',
|
120 |
+
'basic': r'π£\s+Basic.*?(\d+)\s+pages?',
|
121 |
+
'poor': r'π΄\s+Poor.*?(\d+)\s+pages?'
|
122 |
+
}
|
123 |
+
|
124 |
+
for level, pattern in patterns.items():
|
125 |
+
match = re.search(pattern, log_text)
|
126 |
+
if match:
|
127 |
+
distribution[level] = int(match.group(1))
|
128 |
+
|
129 |
+
return distribution
|
130 |
+
|
131 |
+
def _extract_statistics(self, log_text: str) -> Dict:
|
132 |
+
"""Extract statistical information from log text"""
|
133 |
+
stats = {}
|
134 |
+
|
135 |
+
# Extract average score
|
136 |
+
avg_match = re.search(r'Average.*?Score:\s*([\d.]+)', log_text)
|
137 |
+
if avg_match:
|
138 |
+
stats['average_score'] = float(avg_match.group(1))
|
139 |
+
|
140 |
+
# Extract highest score
|
141 |
+
high_match = re.search(r'Highest.*?Score:\s*([\d.]+)', log_text)
|
142 |
+
if high_match:
|
143 |
+
stats['highest_score'] = float(high_match.group(1))
|
144 |
+
|
145 |
+
# Extract lowest score
|
146 |
+
low_match = re.search(r'Lowest.*?Score:\s*([\d.]+)', log_text)
|
147 |
+
if low_match:
|
148 |
+
stats['lowest_score'] = float(low_match.group(1))
|
149 |
+
|
150 |
+
# Extract total pages
|
151 |
+
total_match = re.search(r'Total.*?(\d+).*?results?', log_text)
|
152 |
+
if total_match:
|
153 |
+
stats['total_pages'] = int(total_match.group(1))
|
154 |
+
|
155 |
+
return stats
|
156 |
+
|
157 |
+
def get_highest_scoring_pages(self, parsed_data: Dict, count: int = 5) -> List[Dict]:
|
158 |
+
"""
|
159 |
+
Get the highest-scoring pages from parsed data
|
160 |
+
|
161 |
+
Args:
|
162 |
+
parsed_data: Parsed log data from parse_log_output()
|
163 |
+
count: Number of top pages to return
|
164 |
+
|
165 |
+
Returns:
|
166 |
+
List of highest-scoring pages
|
167 |
+
"""
|
168 |
+
if 'page_scores' not in parsed_data:
|
169 |
+
return []
|
170 |
+
|
171 |
+
return parsed_data['page_scores'][:count]
|
172 |
+
|
173 |
+
def get_pages_by_threshold(self, parsed_data: Dict, threshold: float = 0.80) -> List[Dict]:
|
174 |
+
"""
|
175 |
+
Get pages that meet or exceed a score threshold
|
176 |
+
|
177 |
+
Args:
|
178 |
+
parsed_data: Parsed log data from parse_log_output()
|
179 |
+
threshold: Minimum score threshold
|
180 |
+
|
181 |
+
Returns:
|
182 |
+
List of pages meeting the threshold
|
183 |
+
"""
|
184 |
+
if 'page_scores' not in parsed_data:
|
185 |
+
return []
|
186 |
+
|
187 |
+
return [page for page in parsed_data['page_scores'] if page['score'] >= threshold]
|
188 |
+
|
189 |
+
def get_pages_by_relevance_level(self, parsed_data: Dict, level: str = 'excellent') -> List[Dict]:
|
190 |
+
"""
|
191 |
+
Get pages by specific relevance level
|
192 |
+
|
193 |
+
Args:
|
194 |
+
parsed_data: Parsed log data from parse_log_output()
|
195 |
+
level: Relevance level ('excellent', 'very_good', 'good', 'moderate', 'basic', 'poor')
|
196 |
+
|
197 |
+
Returns:
|
198 |
+
List of pages with the specified relevance level
|
199 |
+
"""
|
200 |
+
if 'page_scores' not in parsed_data:
|
201 |
+
return []
|
202 |
+
|
203 |
+
level_mapping = {
|
204 |
+
'excellent': 'π’ EXCELLENT',
|
205 |
+
'very_good': 'π‘ VERY GOOD',
|
206 |
+
'good': 'π GOOD',
|
207 |
+
'moderate': 'π΅ MODERATE',
|
208 |
+
'basic': 'π£ BASIC',
|
209 |
+
'poor': 'π΄ POOR'
|
210 |
+
}
|
211 |
+
|
212 |
+
target_level = level_mapping.get(level, 'π’ EXCELLENT')
|
213 |
+
return [page for page in parsed_data['page_scores'] if target_level in page['relevance_level']]
|
214 |
+
|
215 |
+
def generate_utilization_report(self, parsed_data: Dict) -> str:
|
216 |
+
"""
|
217 |
+
Generate a comprehensive report on how to utilize the highest-scoring pages
|
218 |
+
|
219 |
+
Args:
|
220 |
+
parsed_data: Parsed log data from parse_log_output()
|
221 |
+
|
222 |
+
Returns:
|
223 |
+
Formatted report string
|
224 |
+
"""
|
225 |
+
report = []
|
226 |
+
report.append("π HIGHEST-SCORING PAGES UTILIZATION REPORT")
|
227 |
+
report.append("=" * 60)
|
228 |
+
|
229 |
+
# Top pages summary
|
230 |
+
top_pages = self.get_highest_scoring_pages(parsed_data, 5)
|
231 |
+
report.append(f"\nπ TOP 5 HIGHEST-SCORING PAGES:")
|
232 |
+
for i, page in enumerate(top_pages, 1):
|
233 |
+
report.append(f" {i}. Page {page['page_number']} - Score: {page['score']:.4f} ({page['relevance_level']})")
|
234 |
+
|
235 |
+
# Threshold-based recommendations
|
236 |
+
excellent_pages = self.get_pages_by_threshold(parsed_data, 0.90)
|
237 |
+
very_good_pages = self.get_pages_by_threshold(parsed_data, 0.80)
|
238 |
+
|
239 |
+
report.append(f"\nπ― UTILIZATION RECOMMENDATIONS:")
|
240 |
+
report.append(f" π’ Excellent pages (β₯0.90): {len(excellent_pages)} pages - Use for primary context")
|
241 |
+
report.append(f" π‘ Very Good pages (β₯0.80): {len(very_good_pages)} pages - Use for comprehensive coverage")
|
242 |
+
|
243 |
+
# Statistics
|
244 |
+
if 'statistics' in parsed_data and parsed_data['statistics']:
|
245 |
+
stats = parsed_data['statistics']
|
246 |
+
report.append(f"\nπ QUALITY METRICS:")
|
247 |
+
if 'average_score' in stats:
|
248 |
+
report.append(f" Average Score: {stats['average_score']:.4f}")
|
249 |
+
if 'highest_score' in stats:
|
250 |
+
report.append(f" Highest Score: {stats['highest_score']:.4f}")
|
251 |
+
if 'total_pages' in stats:
|
252 |
+
report.append(f" Total Pages Analyzed: {stats['total_pages']}")
|
253 |
+
|
254 |
+
# Usage suggestions
|
255 |
+
report.append(f"\nπ‘ USAGE SUGGESTIONS:")
|
256 |
+
report.append(f" 1. Feed top 3 pages to language model for focused responses")
|
257 |
+
report.append(f" 2. Use excellent pages for critical information extraction")
|
258 |
+
report.append(f" 3. Include very good pages for comprehensive analysis")
|
259 |
+
report.append(f" 4. Consider page diversity for balanced coverage")
|
260 |
+
|
261 |
+
report.append("=" * 60)
|
262 |
+
|
263 |
+
return "\n".join(report)
|
264 |
+
|
265 |
+
def _get_relevance_level(self, score: float) -> str:
|
266 |
+
"""Get relevance level based on score"""
|
267 |
+
if score >= 0.90:
|
268 |
+
return "π’ EXCELLENT - Highly relevant"
|
269 |
+
elif score >= 0.80:
|
270 |
+
return "π‘ VERY GOOD - Very relevant"
|
271 |
+
elif score >= 0.70:
|
272 |
+
return "π GOOD - Relevant"
|
273 |
+
elif score >= 0.60:
|
274 |
+
return "π΅ MODERATE - Somewhat relevant"
|
275 |
+
elif score >= 0.50:
|
276 |
+
return "π£ BASIC - Minimally relevant"
|
277 |
+
else:
|
278 |
+
return "π΄ POOR - Not relevant"
|
279 |
+
|
280 |
+
def _get_timestamp(self) -> str:
|
281 |
+
"""Get current timestamp"""
|
282 |
+
from datetime import datetime
|
283 |
+
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
284 |
+
|
285 |
+
# Example usage function
|
286 |
+
def demonstrate_score_utilization():
|
287 |
+
"""
|
288 |
+
Demonstrate how to use the ScoreUtilizer to extract and utilize highest-scoring pages
|
289 |
+
"""
|
290 |
+
print("π§ͺ DEMONSTRATING SCORE UTILIZATION")
|
291 |
+
print("=" * 60)
|
292 |
+
|
293 |
+
# Example log output (this would come from your actual retrieval system)
|
294 |
+
example_log = """
|
295 |
+
================================================================================
|
296 |
+
π RETRIEVAL SCORES - PAGE NUMBERS WITH HIGHEST SCORES
|
297 |
+
================================================================================
|
298 |
+
π Collection: documents_20250101_120000
|
299 |
+
π Total documents found: 15
|
300 |
+
π― Requested top-k: 5
|
301 |
+
--------------------------------------------------------------------------------
|
302 |
+
π Page 1 (doc_id: 0) | Score: 0.9234 | π’ EXCELLENT - Highly relevant
|
303 |
+
π Page 3 (doc_id: 2) | Score: 0.8756 | π‘ VERY GOOD - Very relevant
|
304 |
+
π Page 7 (doc_id: 6) | Score: 0.8123 | π‘ VERY GOOD - Very relevant
|
305 |
+
π Page 2 (doc_id: 1) | Score: 0.7890 | π GOOD - Relevant
|
306 |
+
π Page 5 (doc_id: 4) | Score: 0.7456 | π GOOD - Relevant
|
307 |
+
--------------------------------------------------------------------------------
|
308 |
+
π HIGHEST SCORING PAGES:
|
309 |
+
1. Page 1 - Score: 0.9234
|
310 |
+
2. Page 3 - Score: 0.8756
|
311 |
+
3. Page 7 - Score: 0.8123
|
312 |
+
================================================================================
|
313 |
+
"""
|
314 |
+
|
315 |
+
# Initialize utilizer
|
316 |
+
utilizer = ScoreUtilizer()
|
317 |
+
|
318 |
+
# Parse the log output
|
319 |
+
parsed_data = utilizer.parse_log_output(example_log)
|
320 |
+
|
321 |
+
# Get highest-scoring pages
|
322 |
+
top_pages = utilizer.get_highest_scoring_pages(parsed_data, 3)
|
323 |
+
print(f"\nπ TOP 3 HIGHEST-SCORING PAGES:")
|
324 |
+
for page in top_pages:
|
325 |
+
print(f" Page {page['page_number']} - Score: {page['score']:.4f}")
|
326 |
+
|
327 |
+
# Get pages by threshold
|
328 |
+
excellent_pages = utilizer.get_pages_by_threshold(parsed_data, 0.90)
|
329 |
+
print(f"\nπ’ EXCELLENT PAGES (β₯0.90): {len(excellent_pages)} pages")
|
330 |
+
|
331 |
+
# Generate utilization report
|
332 |
+
report = utilizer.generate_utilization_report(parsed_data)
|
333 |
+
print(f"\n{report}")
|
334 |
+
|
335 |
+
print("\nβ
Score utilization demonstration completed!")
|
336 |
+
|
337 |
+
if __name__ == "__main__":
|
338 |
+
demonstrate_score_utilization()
|