Spaces:
Sleeping
Sleeping
| """ | |
| 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() | |