""" Public-facing loader app for AI Resume Builder. This app downloads and executes the main application from a private HF Space. """ import os import sys import importlib.util from pathlib import Path from huggingface_hub import snapshot_download, hf_hub_download # Configuration - UPDATE THESE WITH YOUR VALUES PRIVATE_SPACE_REPO = "ml-stuff01/resume-builder-code1" # Your private space repo ID CACHE_DIR = Path("/tmp/private_space_cache") def setup_cache_directory(): """Setup and return cache directory for private space files.""" CACHE_DIR.mkdir(exist_ok=True) print(f"āœ“ Cache directory created: {CACHE_DIR}") return CACHE_DIR def download_private_space(): """Download the entire private space repository.""" try: # Check for HF token hf_token = os.getenv("HF_TOKEN") if not hf_token: print("āŒ HF_TOKEN environment variable is not set!") print("To fix this:") print(" 1. Go to https://huggingface.co/settings/tokens") print(" 2. Create a token with 'read' access") print(" 3. Add it as a secret named 'HF_TOKEN' in your Space settings") print(" 4. Or set it locally: export HF_TOKEN='your_token_here'") return False print(f"šŸ“„ Downloading private space from {PRIVATE_SPACE_REPO}...") print(f"šŸ”‘ Using authentication token (length: {len(hf_token)} chars)") snapshot_download( repo_id=PRIVATE_SPACE_REPO, repo_type="space", local_dir=CACHE_DIR, token=hf_token ) print("āœ“ Private space downloaded successfully!") return True except Exception as e: print(f"āŒ Error downloading private space: {str(e)}") print("\nTroubleshooting:") print(f" 1. Verify the space '{PRIVATE_SPACE_REPO}' exists and is spelled correctly") print(" 2. Ensure your HF token has access to this private space") print(" 3. Check that the token has 'read' permissions") print(" 4. Try accessing the space directly: https://huggingface.co/spaces/{PRIVATE_SPACE_REPO}") return False def install_requirements(): """Install requirements from the private space if requirements.txt exists.""" requirements_path = CACHE_DIR / "requirements.txt" if not requirements_path.exists(): print("ā„¹ļø No requirements.txt found in private space, skipping installation") return True try: print(f"šŸ“¦ Installing dependencies from {requirements_path}...") import subprocess result = subprocess.run( [sys.executable, "-m", "pip", "install", "-r", str(requirements_path)], capture_output=True, text=True, timeout=300 # 5 minute timeout ) if result.returncode == 0: print("āœ“ Dependencies installed successfully!") return True else: print(f"āš ļø Warning during dependency installation:") print(result.stderr) print("Continuing anyway - some dependencies might be missing...") return True # Continue even if some packages fail except subprocess.TimeoutExpired: print("āš ļø Dependency installation timed out. Continuing anyway...") return True except Exception as e: print(f"āš ļø Error installing dependencies: {str(e)}") print("Continuing anyway - some dependencies might be missing...") return True # Continue even on error def load_and_run_app(): """Load the main application from the private space and run it.""" try: # Path to the main app file in the private space cache app_path = CACHE_DIR / "app_new.py" if not app_path.exists(): print(f"āŒ App file not found at {app_path}") return False print(f"šŸš€ Loading application from {app_path}...") # Add the cache directory to Python path so imports work sys.path.insert(0, str(CACHE_DIR)) # Change to the cache directory so relative paths work os.chdir(str(CACHE_DIR)) # Load the module dynamically spec = importlib.util.spec_from_file_location("app_main", app_path) app_module = importlib.util.module_from_spec(spec) # Execute the module - this will start the Flask server # The exec_module will block if the app runs a server spec.loader.exec_module(app_module) # If we get here, the module loaded but didn't start a blocking server # Try to find and run the Flask app if hasattr(app_module, 'app'): print("āœ“ Flask app found, starting server...") port = int(os.getenv('PORT', 7860)) app_module.app.run(host='0.0.0.0', port=port, debug=False) else: print("āœ“ Application module loaded successfully!") return True except Exception as e: print(f"āŒ Error loading application: {str(e)}") import traceback traceback.print_exc() return False if __name__ == "__main__": print("=" * 60) print("šŸš€ AI Resume Builder") print("=" * 60) # Step 1: Setup cache directory setup_cache_directory() # Step 2: Download private space if not download_private_space(): print("\nāŒ Failed to download private space. Exiting.") sys.exit(1) # Step 3: Install requirements from private space if not install_requirements(): print("\nāŒ Failed to install requirements. Exiting.") sys.exit(1) # Step 4: Load and run the app (this should block if it starts a server) if not load_and_run_app(): print("\nāŒ Failed to load application. Exiting.") sys.exit(1) # If we reach here, the app finished without error # This shouldn't normally happen if the app runs a server