UPB-chatbot-rag / diagnose_hf.py
rosvend's picture
Add Gradio UI with improved error handling for HuggingFace Spaces deployment
d767722
"""
Diagnostic script for HuggingFace Spaces deployment
Run this to check what's happening in the Space
"""
import os
import sys
from pathlib import Path
print("=" * 60)
print("πŸ” HuggingFace Space Diagnostic Report")
print("=" * 60)
# Check Python version
print(f"\nπŸ“Œ Python Version: {sys.version}")
# Check current directory
print(f"\nπŸ“ Current Directory: {os.getcwd()}")
print(f"πŸ“‚ Directory Contents:")
for item in sorted(os.listdir(".")):
print(f" - {item}")
# Check if data directory exists
data_path = Path("data")
if data_path.exists():
print(f"\nβœ… Data directory found")
print(f"πŸ“‚ Data directory contents:")
for item in sorted(data_path.rglob("*")):
if item.is_file():
print(f" - {item.relative_to(data_path)} ({item.stat().st_size} bytes)")
else:
print(f"\n❌ Data directory NOT found!")
# Check if src directory exists
src_path = Path("src")
if src_path.exists():
print(f"\nβœ… Src directory found")
print(f"πŸ“‚ Src directory structure:")
for item in sorted(src_path.rglob("*.py")):
print(f" - {item.relative_to(src_path)}")
else:
print(f"\n❌ Src directory NOT found!")
# Check environment variables
print(f"\nπŸ”‘ Environment Variables:")
required_vars = [
"AZURE_OPENAI_API_KEY",
"AZURE_OPENAI_ENDPOINT",
"AZURE_OPENAI_DEPLOYMENT_NAME",
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME",
"AZURE_OPENAI_API_VERSION",
]
for var in required_vars:
value = os.getenv(var)
if value:
# Mask sensitive values
if "KEY" in var:
display = f"{value[:10]}...{value[-5:]}" if len(value) > 15 else "***"
elif "ENDPOINT" in var:
display = f"{value[:40]}..." if len(value) > 40 else value
else:
display = value
print(f" βœ… {var}: {display}")
else:
print(f" ❌ {var}: NOT SET")
# Try importing dependencies
print(f"\nπŸ“¦ Checking Dependencies:")
dependencies = [
"gradio",
"langchain",
"langchain_openai",
"langchain_community",
"faiss",
"rank_bm25",
"dotenv",
]
for dep in dependencies:
try:
if dep == "faiss":
import faiss
elif dep == "dotenv":
import dotenv
else:
__import__(dep)
print(f" βœ… {dep}")
except ImportError as e:
print(f" ❌ {dep}: {e}")
# Try importing project modules
print(f"\nπŸ”§ Checking Project Modules:")
sys.path.insert(0, str(Path(__file__).parent / "src"))
try:
from setup_retrieval import setup_retrieval_system
print(f" βœ… setup_retrieval")
except Exception as e:
print(f" ❌ setup_retrieval: {e}")
try:
from rag.chain import UPBRAGChain
print(f" βœ… UPBRAGChain")
except Exception as e:
print(f" ❌ UPBRAGChain: {e}")
try:
from metadata.metadata_manager import MetadataManager
print(f" βœ… MetadataManager")
except Exception as e:
print(f" ❌ MetadataManager: {e}")
# Try minimal RAG initialization
print(f"\nπŸš€ Attempting RAG Initialization:")
try:
from setup_retrieval import setup_retrieval_system
print(" - Loading documents...")
retriever, vectorstore_manager, chunks = setup_retrieval_system()
print(f" βœ… Success! Loaded {len(chunks)} chunks")
except Exception as e:
import traceback
print(f" ❌ Failed:")
print(f"\n{traceback.format_exc()}")
print("\n" + "=" * 60)
print("βœ… Diagnostic Complete")
print("=" * 60)