Spaces:
Sleeping
Sleeping
Download app.py from ml-stuff01/daily_paper_reader: direct link, hf CLI and curl.
- Browser
- Download file 6.07 kB
-
https://huggingface.co/spaces/ml-stuff01/daily_paper_reader/resolve/main/app.py
- Command line
-
hf download hf://spaces/ml-stuff01/daily_paper_reader/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ml-stuff01/daily_paper_reader/resolve/main/app.py
6.07 kB
| """ | |
| Public-facing loader app for Daily Paper Reader. | |
| 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 | |
| # Configuration - UPDATE THIS WITH YOUR PRIVATE SPACE REPO ID | |
| PRIVATE_SPACE_REPO = "ml-stuff01/daily_paper_reader_code" # e.g., "ml-stuff01/daily-paper-reader-code" | |
| 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(f" 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.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("π Daily Paper Reader") | |
| 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 | |