Spaces:
Configuration error
Configuration error
Update src/app.py
Browse files24 unmodified lines2526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848524 unmodified linesprint(f"β οΈ WARNING: Pickle file not found at: {CHUNKS_FILE}")else:print(f"β SUCCESS: Pickle file found at: {CHUNKS_FILE}")# --- SEARCH ENGINE (Search Quotes Only) ---def search_archives(query):status_log = []results = []# PHASE 1: LOCAL TEXT SCAN (Vacuum Mode)if os.path.exists(CHUNKS_FILE):try:with open(CHUNKS_FILE, "rb") as f:chunks = pickle.load(f)status_log.append(f"π Loaded {len(chunks)} local chunks.")query_lower = query.lower().strip()count = 0for doc in chunks:if query_lower in doc.page_content.lower():results.append(doc)count += 1# Grab up to 30 matchesif count >= 30: breakif results:status_log.append(f"β FOUND {len(results)} EXACT MATCHES LOCALLY.")except Exception as e:status_log.append(f"β Local Load Error: {e}")else:status_log.append("β Pickle file missing. Skipping local search.")# PHASE 2: PINECONE FALLBACKstatus_log.append("βοΈ Attempting Vector Search (Pinecone)...")try:pinecone_key = os.environ.get("PINECONE_API_KEY") or st.secrets.get("PINECONE_API_KEY")google_key = os.environ.get("GOOGLE_API_KEY") or st.secrets.get("GOOGLE_API_KEY")if not pinecone_key or not google_key:return results, status_log + ["β Missing API Keys"]embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")vector_store = PineconeVectorStore(index_name=INDEX_NAME, embedding=embeddings)# Grab top 20 Semantic Matchesretriever = vector_store.as_retriever(search_kwargs={"k": 20})cloud_docs = retriever.invoke(query)for doc in cloud_docs:results.append(doc)status_log.append(f"β Added {len(cloud_docs)} cloud matches.")return results, status_logexcept Exception as e:status_log.append(f"β Cloud Error: {e}")return results, status_log# --- RAG CHAIN (The Chat Tool - POWERED BY GEMINI) ---def get_rag_chain():24 unmodified lines2526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697024 unmodified linesprint(f"β οΈ WARNING: Pickle file not found at: {CHUNKS_FILE}")else:print(f"β SUCCESS: Pickle file found at: {CHUNKS_FILE}")def search_archives(query):"""This function strictly scans the local file.It does NOT use Pinecone.It returns ALL matches found."""status_log = []results = []if os.path.exists(CHUNKS_FILE):try:# 1. Load the Datawith open(CHUNKS_FILE, "rb") as f:chunks = pickle.load(f)status_log.append(f"π Scanning {len(chunks)} local paragraphs...")query_lower = query.lower().strip()# 2. Find ALL Matches (No Limit)# We check every single chunk.results = [doc for doc in chunks if query_lower in doc.page_content.lower()]# 3. Safety Check# If we find > 1000 results, we show the first 1000 to keep the browser from freezing.total_found = len(results)if total_found > 1000:results = results[:1000]status_log.append(f"β οΈ Found {total_found} matches! Showing first 1000 to prevent crash.")else:status_log.append(f"β Found {total_found} exact matches.")return results, status_logexcept Exception as e:status_log.append(f"β Local Load Error: {e}")return [], status_logelse:status_log.append("β Pickle file missing. Cannot search.")return [], status_log# --- RAG CHAIN (The Chat Tool - POWERED BY GEMINI) ---def get_rag_chain():
src/app.py+25-40