Spaces:
Sleeping
Sleeping
Upload diagnostic_script.py
Browse files- diagnostic_script.py +144 -0
diagnostic_script.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Diagnostic script for HF Space deployment issues"""
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
def check_files():
|
| 9 |
+
"""Check if all required files exist"""
|
| 10 |
+
print("=== File Check ===")
|
| 11 |
+
|
| 12 |
+
required_files = [
|
| 13 |
+
"config/settings.py",
|
| 14 |
+
"src/retrieval/semantic_retriever.py",
|
| 15 |
+
"src/generation/response_generator.py",
|
| 16 |
+
"data/vector_store/embeddings.npy",
|
| 17 |
+
"data/vector_store/faiss_index.bin",
|
| 18 |
+
"data/vector_store/chunks.pkl",
|
| 19 |
+
"data/vector_store/bm25.pkl",
|
| 20 |
+
"data/vector_store/relationships.pkl"
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
for file_path in required_files:
|
| 24 |
+
if os.path.exists(file_path):
|
| 25 |
+
print(f"β
{file_path}")
|
| 26 |
+
else:
|
| 27 |
+
print(f"β {file_path} - MISSING")
|
| 28 |
+
|
| 29 |
+
def check_imports():
|
| 30 |
+
"""Check if imports work"""
|
| 31 |
+
print("\n=== Import Check ===")
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
from config.settings import config
|
| 35 |
+
print("β
Config imported")
|
| 36 |
+
print(f" - Embedding model: {config.EMBEDDING_MODEL}")
|
| 37 |
+
print(f" - Vector store path: {config.VECTOR_STORE_PATH}")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"β Config import failed: {e}")
|
| 40 |
+
return
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
from src.retrieval.semantic_retriever import SemanticRetriever
|
| 44 |
+
print("β
SemanticRetriever imported")
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"β SemanticRetriever import failed: {e}")
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
from src.generation.response_generator import ResponseGenerator
|
| 50 |
+
print("β
ResponseGenerator imported")
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"β ResponseGenerator import failed: {e}")
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
from sentence_transformers import SentenceTransformer
|
| 56 |
+
print("β
SentenceTransformers imported")
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"β SentenceTransformers import failed: {e}")
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
import faiss
|
| 62 |
+
print("β
FAISS imported")
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"β FAISS import failed: {e}")
|
| 65 |
+
|
| 66 |
+
def check_vector_store():
|
| 67 |
+
"""Check vector store files"""
|
| 68 |
+
print("\n=== Vector Store Check ===")
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
from config.settings import config
|
| 72 |
+
|
| 73 |
+
# Check embeddings
|
| 74 |
+
import numpy as np
|
| 75 |
+
embeddings = np.load(os.path.join(config.VECTOR_STORE_PATH, "embeddings.npy"))
|
| 76 |
+
print(f"β
Embeddings loaded: {embeddings.shape}")
|
| 77 |
+
|
| 78 |
+
# Check FAISS
|
| 79 |
+
import faiss
|
| 80 |
+
faiss_index = faiss.read_index(os.path.join(config.VECTOR_STORE_PATH, "faiss_index.bin"))
|
| 81 |
+
print(f"β
FAISS index loaded: {faiss_index.ntotal} vectors")
|
| 82 |
+
|
| 83 |
+
# Check chunks
|
| 84 |
+
import pickle
|
| 85 |
+
with open(os.path.join(config.VECTOR_STORE_PATH, "chunks.pkl"), "rb") as f:
|
| 86 |
+
chunks = pickle.load(f)
|
| 87 |
+
print(f"β
Chunks loaded: {len(chunks)} chunks")
|
| 88 |
+
|
| 89 |
+
# Check BM25
|
| 90 |
+
with open(os.path.join(config.VECTOR_STORE_PATH, "bm25.pkl"), "rb") as f:
|
| 91 |
+
bm25 = pickle.load(f)
|
| 92 |
+
print(f"β
BM25 loaded")
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
print(f"β Vector store check failed: {e}")
|
| 96 |
+
|
| 97 |
+
def check_model_loading():
|
| 98 |
+
"""Check if embedding model loads"""
|
| 99 |
+
print("\n=== Model Loading Check ===")
|
| 100 |
+
|
| 101 |
+
try:
|
| 102 |
+
from config.settings import config
|
| 103 |
+
from sentence_transformers import SentenceTransformer
|
| 104 |
+
|
| 105 |
+
print("Loading embedding model...")
|
| 106 |
+
model = SentenceTransformer(config.EMBEDDING_MODEL, device='cpu')
|
| 107 |
+
print("β
Embedding model loaded")
|
| 108 |
+
|
| 109 |
+
# Test encoding
|
| 110 |
+
test_embedding = model.encode(["test"], show_progress_bar=False)
|
| 111 |
+
print(f"β
Test encoding successful: shape {test_embedding.shape}")
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"β Model loading failed: {e}")
|
| 115 |
+
|
| 116 |
+
def check_api_keys():
|
| 117 |
+
"""Check API keys"""
|
| 118 |
+
print("\n=== API Keys Check ===")
|
| 119 |
+
|
| 120 |
+
openrouter_key = os.getenv("OPENROUTER_API_KEY")
|
| 121 |
+
tavily_key = os.getenv("TAVILY_API_KEY")
|
| 122 |
+
|
| 123 |
+
if openrouter_key:
|
| 124 |
+
print("β
OPENROUTER_API_KEY found")
|
| 125 |
+
else:
|
| 126 |
+
print("β OPENROUTER_API_KEY missing")
|
| 127 |
+
|
| 128 |
+
if tavily_key:
|
| 129 |
+
print("β
TAVILY_API_KEY found")
|
| 130 |
+
else:
|
| 131 |
+
print("β οΈ TAVILY_API_KEY missing (optional)")
|
| 132 |
+
|
| 133 |
+
if __name__ == "__main__":
|
| 134 |
+
print("π MANIT RAG Chatbot Diagnostic")
|
| 135 |
+
print("=" * 50)
|
| 136 |
+
|
| 137 |
+
check_files()
|
| 138 |
+
check_imports()
|
| 139 |
+
check_vector_store()
|
| 140 |
+
check_model_loading()
|
| 141 |
+
check_api_keys()
|
| 142 |
+
|
| 143 |
+
print("\n" + "=" * 50)
|
| 144 |
+
print("Diagnostic complete!")
|