File size: 2,470 Bytes
5fd4bb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Hugging Face Spaces Entry Point
This file is the main entry point for running the RAG system on HF Spaces
"""
import os
import sys
import logging

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)


def validate_hf_token():
    """
    Validate that HF_TOKEN is set
    
    Returns:
        True if token is valid, False otherwise
    """
    token = os.getenv("HF_TOKEN", "")
    
    if not token:
        logger.error(
            "❌ HF_TOKEN not found! Please set it in Space Settings → Repository secrets"
        )
        return False
    
    if not token.startswith("hf_"):
        logger.error(
            "❌ Invalid HF_TOKEN format. Token should start with 'hf_'"
        )
        return False
    
    logger.info("✅ HF_TOKEN validated successfully")
    return True


def main():
    """
    Main entry point for Hugging Face Spaces
    """
    logger.info("🚀 Starting RAG System on Hugging Face Spaces...")
    
    # Validate HF token
    if not validate_hf_token():
        logger.error(
            "\n" + "="*60 + "\n"
            "SETUP REQUIRED:\n"
            "1. Go to your Space Settings\n"
            "2. Navigate to 'Repository secrets'\n"
            "3. Add a new secret:\n"
            "   - Name: HF_TOKEN\n"
            "   - Value: Your Hugging Face token from https://huggingface.co/settings/tokens\n"
            "4. Restart the Space\n"
            + "="*60
        )
        sys.exit(1)
    
    # Import main application after token validation
    from main import rag_system, create_gradio_interface
    
    # Setup the pipeline
    logger.info("Setting up RAG pipeline...")
    success = rag_system.setup_pipeline(force_rebuild=False)
    
    if not success:
        logger.error("Failed to setup RAG pipeline. Please check the logs.")
        sys.exit(1)
    
    # Create and launch Gradio interface
    logger.info("Creating Gradio interface...")
    interface = create_gradio_interface()
    
    logger.info("Launching web interface on Hugging Face Spaces...")
    
    # Launch without share (not needed on HF Spaces)
    # HF Spaces automatically provides the public URL
    interface.queue().launch(
        share=False,  # No need for share on HF Spaces
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True,
    )


if __name__ == "__main__":
    main()