Download scripts/verify_knowledge.py from Surajnitp/flowmarg: direct link, hf CLI and curl.
- Browser
- Download file 4.7 kB
-
https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/scripts/verify_knowledge.py
- Command line
-
hf download hf://spaces/Surajnitp/flowmarg/scripts/verify_knowledge.py
-
curl -L -o verify_knowledge.py https://huggingface.co/spaces/Surajnitp/flowmarg/resolve/main/scripts/verify_knowledge.py
4.7 kB
| """ | |
| KNOWLEDGE BASE VERIFICATION | |
| Check if chunks are correctly created and retrievable | |
| """ | |
| import os | |
| import sys | |
| import json | |
| from pathlib import Path | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| def verify_knowledge_base(): | |
| print("\n" + "=" * 60) | |
| print(" 🔍 KNOWLEDGE BASE VERIFICATION") | |
| print("=" * 60 + "\n") | |
| # 1. Check ChromaDB | |
| print("1️⃣ Checking ChromaDB...") | |
| try: | |
| import chromadb | |
| client = chromadb.PersistentClient(path="knowledge/chroma_db") | |
| collection = client.get_collection("track_design_knowledge") | |
| count = collection.count() | |
| print(f" ✅ Collection exists with {count} documents") | |
| except Exception as e: | |
| print(f" ❌ Error: {e}") | |
| return False | |
| # 2. Sample query - Network Rail standards | |
| print("\n2️⃣ Testing search - 'Network Rail ballast depth'...") | |
| try: | |
| from sentence_transformers import SentenceTransformer | |
| model = SentenceTransformer('all-MiniLM-L6-v2') | |
| query = "Network Rail ballast depth requirement" | |
| embedding = model.encode([query]) | |
| results = collection.query( | |
| query_embeddings=embedding.tolist(), | |
| n_results=3 | |
| ) | |
| if results and results.get('documents'): | |
| print(f" ✅ Found {len(results['documents'][0])} results") | |
| for i, doc in enumerate(results['documents'][0][:3], 1): | |
| print(f"\n Result {i}:") | |
| print(f" Source: {results['metadatas'][0][i-1].get('source', 'Unknown')}") | |
| print(f" Preview: {doc[:200]}...") | |
| else: | |
| print(" ⚠️ No results found") | |
| except Exception as e: | |
| print(f" ❌ Error: {e}") | |
| # 3. Test S&C query | |
| print("\n3️⃣ Testing search - 'S&C switch crossing NR56'...") | |
| try: | |
| query = "NR56 switch and crossing details" | |
| embedding = model.encode([query]) | |
| results = collection.query( | |
| query_embeddings=embedding.tolist(), | |
| n_results=3 | |
| ) | |
| if results and results.get('documents'): | |
| print(f" ✅ Found {len(results['documents'][0])} results") | |
| for i, doc in enumerate(results['documents'][0][:3], 1): | |
| print(f"\n Result {i}:") | |
| print(f" Source: {results['metadatas'][0][i-1].get('source', 'Unknown')}") | |
| print(f" Preview: {doc[:200]}...") | |
| else: | |
| print(" ⚠️ No results found") | |
| except Exception as e: | |
| print(f" ❌ Error: {e}") | |
| # 4. Test SOP query | |
| print("\n4️⃣ Testing search - 'SOP track design procedure'...") | |
| try: | |
| query = "track design SOP procedure" | |
| embedding = model.encode([query]) | |
| results = collection.query( | |
| query_embeddings=embedding.tolist(), | |
| n_results=3 | |
| ) | |
| if results and results.get('documents'): | |
| print(f" ✅ Found {len(results['documents'][0])} results") | |
| for i, doc in enumerate(results['documents'][0][:3], 1): | |
| print(f"\n Result {i}:") | |
| print(f" Source: {results['metadatas'][0][i-1].get('source', 'Unknown')}") | |
| print(f" Preview: {doc[:200]}...") | |
| else: | |
| print(" ⚠️ No results found") | |
| except Exception as e: | |
| print(f" ❌ Error: {e}") | |
| # 5. Check chunk distribution | |
| print("\n5️⃣ Checking chunk distribution...") | |
| try: | |
| # Get all documents | |
| all_results = collection.get() | |
| if all_results and all_results.get('metadatas'): | |
| sources = {} | |
| for meta in all_results['metadatas']: | |
| source = meta.get('source', 'Unknown') | |
| # Clean source name | |
| if 'REPW' in source: | |
| source = source[:15] # Show first 15 chars for REPW files | |
| sources[source] = sources.get(source, 0) + 1 | |
| print(f" Total chunks: {len(all_results['metadatas'])}") | |
| print(f" Unique sources: {len(sources)}") | |
| # Show top sources | |
| print("\n Top sources:") | |
| sorted_sources = sorted(sources.items(), key=lambda x: x[1], reverse=True) | |
| for source, count in sorted_sources[:10]: | |
| print(f" - {source}: {count} chunks") | |
| except Exception as e: | |
| print(f" ❌ Error: {e}") | |
| print("\n" + "=" * 60) | |
| print(" ✅ VERIFICATION COMPLETE") | |
| print("=" * 60) | |
| return True | |
| if __name__ == "__main__": | |
| verify_knowledge_base() | |