Spaces:
Sleeping
Sleeping
Bellok
commited on
Commit
Β·
a8a243e
1
Parent(s):
7ad1b0d
and again
Browse files
app.py
CHANGED
|
@@ -57,7 +57,7 @@ print("π Loading Warbler packs...")
|
|
| 57 |
pack_loader = PackLoader()
|
| 58 |
documents = pack_loader.discover_documents()
|
| 59 |
|
| 60 |
-
# If no packs found, try to download them
|
| 61 |
if len(documents) == 0:
|
| 62 |
print("β οΈ No packs found locally. Attempting to download from HuggingFace...")
|
| 63 |
try:
|
|
@@ -66,78 +66,80 @@ if len(documents) == 0:
|
|
| 66 |
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
|
| 67 |
|
| 68 |
from warbler_cda.utils.hf_warbler_ingest import HFWarblerIngestor
|
| 69 |
-
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
else:
|
| 106 |
-
print(
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
print(
|
| 113 |
-
# DEBUG - show what packs were actually loaded
|
| 114 |
-
pack_sources = {}
|
| 115 |
-
for doc in documents[:min(100, len(documents))]: # Sample first 100
|
| 116 |
-
doc_id = doc['id']
|
| 117 |
-
if '-' in doc_id and '/' in doc_id:
|
| 118 |
-
prefix = doc_id.split('/')[0]
|
| 119 |
-
if prefix not in pack_sources:
|
| 120 |
-
pack_sources[prefix] = 0
|
| 121 |
-
pack_sources[prefix] += 1
|
| 122 |
-
print(f"π Pack sources in first 100 docs: {pack_sources}")
|
| 123 |
-
else:
|
| 124 |
-
print("β Failed to download dataset, using sample documents...")
|
| 125 |
documents = []
|
|
|
|
|
|
|
| 126 |
except Exception as e:
|
| 127 |
print(f"β οΈ Could not download packs: {e}")
|
| 128 |
print("Using sample documents instead...")
|
| 129 |
documents = []
|
| 130 |
|
| 131 |
if len(documents) == 0:
|
| 132 |
-
# Fallback to sample documents
|
| 133 |
sample_docs = [
|
| 134 |
{"id": "sample1", "content": "FractalStat is an 8-dimensional addressing system for intelligent retrieval.", "metadata": {}},
|
| 135 |
{"id": "sample2", "content": "Semantic search finds documents by meaning, not just keywords.", "metadata": {}},
|
| 136 |
{"id": "sample3", "content": "Bob the Skeptic validates results to prevent bias and hallucinations.", "metadata": {}},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
]
|
| 138 |
for doc in sample_docs:
|
| 139 |
api.add_document(doc["id"], doc["content"], doc["metadata"])
|
| 140 |
-
print(f"β
Loaded {len(sample_docs)} sample documents")
|
| 141 |
else:
|
| 142 |
print(f"β
Found {len(documents)} documents")
|
| 143 |
|
|
@@ -199,14 +201,20 @@ def query_warbler(query_text: str, max_results: int = 5, use_hybrid: bool = True
|
|
| 199 |
semantic_query=query_text,
|
| 200 |
max_results=max_results,
|
| 201 |
fractalstat_hybrid=use_hybrid, # Full hybrid mode when user enables it
|
| 202 |
-
confidence_threshold=0.
|
| 203 |
)
|
| 204 |
|
|
|
|
|
|
|
|
|
|
| 205 |
# Execute query
|
| 206 |
assembly = api.retrieve_context(query)
|
| 207 |
-
|
| 208 |
elapsed_ms = (time.time() - start_time) * 1000
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
| 210 |
# Format results
|
| 211 |
output = f"## Query Results\n\n"
|
| 212 |
output += f"**Query:** {query_text}\n\n"
|
|
|
|
| 57 |
pack_loader = PackLoader()
|
| 58 |
documents = pack_loader.discover_documents()
|
| 59 |
|
| 60 |
+
# If no packs found, try to download them with timeout protection
|
| 61 |
if len(documents) == 0:
|
| 62 |
print("β οΈ No packs found locally. Attempting to download from HuggingFace...")
|
| 63 |
try:
|
|
|
|
| 66 |
os.environ["HF_HUB_DISABLE_PROGRESS_BARS"] = "1"
|
| 67 |
|
| 68 |
from warbler_cda.utils.hf_warbler_ingest import HFWarblerIngestor
|
| 69 |
+
|
| 70 |
+
# ADD TIMEOUT PROTECTION for HF Spaces
|
| 71 |
+
import signal
|
| 72 |
+
|
| 73 |
+
def timeout_handler(signum, frame):
|
| 74 |
+
raise TimeoutError("HF download timed out")
|
| 75 |
+
|
| 76 |
+
# Set 5-minute timeout for downloads (HF Spaces limit)
|
| 77 |
+
signal.signal(signal.SIGALRM, timeout_handler)
|
| 78 |
+
signal.alarm(300) # 5 minutes
|
| 79 |
+
|
| 80 |
+
try:
|
| 81 |
+
ingestor = HFWarblerIngestor(packs_dir=pack_loader.packs_dir, verbose=True)
|
| 82 |
+
|
| 83 |
+
# For HF Spaces demo, only download a minimal arxiv dataset to avoid timeout
|
| 84 |
+
datasets_to_download = [
|
| 85 |
+
# Skip heavy downloads that cause HF Spaces timeout
|
| 86 |
+
# "arxiv", # Temporarily disabled for HF Spaces reliability
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
total_docs = 0
|
| 90 |
+
successful_downloads = 0
|
| 91 |
+
|
| 92 |
+
for dataset in datasets_to_download:
|
| 93 |
+
print(f"π¦ Downloading {dataset}...")
|
| 94 |
+
# Very limited download to avoid timeout
|
| 95 |
+
arxiv_limit = 1000 if dataset == "arxiv" else None
|
| 96 |
+
success = ingestor.ingest_dataset(dataset, arxiv_limit=arxiv_limit)
|
| 97 |
+
if success:
|
| 98 |
+
successful_downloads += 1
|
| 99 |
+
# Count documents in this pack
|
| 100 |
+
pack_docs = pack_loader.discover_documents()
|
| 101 |
+
new_count = len(pack_docs) - total_docs
|
| 102 |
+
total_docs = len(pack_docs)
|
| 103 |
+
print(f"β
{dataset}: {new_count} documents")
|
| 104 |
+
else:
|
| 105 |
+
print(f"β Failed to download {dataset}")
|
| 106 |
+
|
| 107 |
+
print(f"π Total: {total_docs} documents from {successful_downloads}/{len(datasets_to_download)} packs")
|
| 108 |
+
if successful_downloads > 0:
|
| 109 |
+
# Reload after download
|
| 110 |
+
documents = pack_loader.discover_documents()
|
| 111 |
+
print(f"β
Downloaded {len(documents)} documents")
|
| 112 |
else:
|
| 113 |
+
print("β No HF datasets downloaded successfully, using sample documents...")
|
| 114 |
+
documents = []
|
| 115 |
+
|
| 116 |
+
signal.alarm(0) # Cancel timeout
|
| 117 |
+
|
| 118 |
+
except TimeoutError:
|
| 119 |
+
print("β° HF download timed out - proceeding with sample documents")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
documents = []
|
| 121 |
+
signal.alarm(0) # Cancel timeout
|
| 122 |
+
|
| 123 |
except Exception as e:
|
| 124 |
print(f"β οΈ Could not download packs: {e}")
|
| 125 |
print("Using sample documents instead...")
|
| 126 |
documents = []
|
| 127 |
|
| 128 |
if len(documents) == 0:
|
| 129 |
+
# Fallback to comprehensive sample documents that match common test queries
|
| 130 |
sample_docs = [
|
| 131 |
{"id": "sample1", "content": "FractalStat is an 8-dimensional addressing system for intelligent retrieval.", "metadata": {}},
|
| 132 |
{"id": "sample2", "content": "Semantic search finds documents by meaning, not just keywords.", "metadata": {}},
|
| 133 |
{"id": "sample3", "content": "Bob the Skeptic validates results to prevent bias and hallucinations.", "metadata": {}},
|
| 134 |
+
{"id": "sample4", "content": "Hello world! This is a sample document for testing search functionality.", "metadata": {}},
|
| 135 |
+
{"id": "sample5", "content": "This document contains information about rotation dynamics of Saturn's moons.", "metadata": {}},
|
| 136 |
+
{"id": "sample6", "content": "Machine learning and artificial intelligence are transforming technology.", "metadata": {}},
|
| 137 |
+
{"id": "sample7", "content": "Ancient library keepers preserved wisdom through generations.", "metadata": {}},
|
| 138 |
+
{"id": "sample8", "content": "Wisdom about courage comes from facing fears directly.", "metadata": {}},
|
| 139 |
]
|
| 140 |
for doc in sample_docs:
|
| 141 |
api.add_document(doc["id"], doc["content"], doc["metadata"])
|
| 142 |
+
print(f"β
Loaded {len(sample_docs)} comprehensive sample documents")
|
| 143 |
else:
|
| 144 |
print(f"β
Found {len(documents)} documents")
|
| 145 |
|
|
|
|
| 201 |
semantic_query=query_text,
|
| 202 |
max_results=max_results,
|
| 203 |
fractalstat_hybrid=use_hybrid, # Full hybrid mode when user enables it
|
| 204 |
+
confidence_threshold=0.1 # Lower threshold for better fallback search results
|
| 205 |
)
|
| 206 |
|
| 207 |
+
# DEBUG: Log query details
|
| 208 |
+
print(f"DEBUG: Executing query '{query_text}' with mode={query_mode}, max_results={max_results}, hybrid={use_hybrid}")
|
| 209 |
+
|
| 210 |
# Execute query
|
| 211 |
assembly = api.retrieve_context(query)
|
| 212 |
+
|
| 213 |
elapsed_ms = (time.time() - start_time) * 1000
|
| 214 |
+
|
| 215 |
+
# DEBUG: Log results summary
|
| 216 |
+
print(f"DEBUG: Query completed in {elapsed_ms:.0f}ms, found {len(assembly.results)} results")
|
| 217 |
+
|
| 218 |
# Format results
|
| 219 |
output = f"## Query Results\n\n"
|
| 220 |
output += f"**Query:** {query_text}\n\n"
|