Spaces:
Runtime error
Runtime error
| """ | |
| Public Space Wrapper - Hugging Face | |
| This wrapper downloads and executes the real app.py from a private space. | |
| Only app.py is hidden; all other files remain public. | |
| Last updated: 2025-02-25 - Two-step pattern for button disable fix | |
| """ | |
| import os | |
| import importlib.util | |
| import streamlit as st | |
| from pathlib import Path | |
| from huggingface_hub import hf_hub_download, HfApi | |
| # ============================================================================== | |
| # CONFIGURATION | |
| # ============================================================================== | |
| PRIVATE_SPACE_ID = "kaiucsd/deep-wenokn-int" # Private space ID | |
| HF_TOKEN_ENV_VAR = "HF_TOKEN" # Environment variable name for token | |
| # ============================================================================== | |
| def get_hf_token(): | |
| """Get Hugging Face token from environment or secrets.""" | |
| token = os.environ.get(HF_TOKEN_ENV_VAR) | |
| if not token: | |
| st.error(f""" | |
| ### π Authentication Required | |
| Please set your Hugging Face token as a secret in this Space: | |
| 1. Go to your Space Settings β Secrets | |
| 2. Add a new secret: `{HF_TOKEN_ENV_VAR}` | |
| 3. Paste your Hugging Face token (with read access) | |
| [Get your token here](https://huggingface.co/settings/tokens) | |
| """) | |
| st.stop() | |
| return token | |
| def verify_token(token): | |
| """Verify the token has access to the private space.""" | |
| try: | |
| api = HfApi(token=token) | |
| repo_info = api.repo_info(repo_id=PRIVATE_SPACE_ID, repo_type="space") | |
| return True | |
| except Exception as e: | |
| st.error(f""" | |
| ### β Cannot Access Private Space | |
| Error: {str(e)} | |
| Please verify: | |
| 1. Your token is correct | |
| 2. The private space ID is correct: `{PRIVATE_SPACE_ID}` | |
| 3. Your token has access to the private space | |
| """) | |
| st.stop() | |
| def download_private_app(): | |
| """Download app.py from private space to a temp location.""" | |
| token = get_hf_token() | |
| verify_token(token) | |
| try: | |
| with st.spinner("π Loading secure application..."): | |
| # Download app.py from private space | |
| app_path = hf_hub_download( | |
| repo_id=PRIVATE_SPACE_ID, | |
| filename="app.py", | |
| repo_type="space", | |
| token=token | |
| ) | |
| return app_path | |
| except Exception as e: | |
| st.error(f""" | |
| ### β Failed to Load Application | |
| Error: {str(e)} | |
| Please contact the administrator. | |
| """) | |
| st.stop() | |
| def execute_private_app(app_path): | |
| """Dynamically load and execute the private app.py.""" | |
| try: | |
| # Load the module | |
| spec = importlib.util.spec_from_file_location("private_app", app_path) | |
| private_app = importlib.util.module_from_spec(spec) | |
| # Execute the module (this runs the Streamlit app) | |
| spec.loader.exec_module(private_app) | |
| except Exception as e: | |
| st.error(f""" | |
| ### β Application Error | |
| The application failed to start: {str(e)} | |
| """) | |
| raise | |
| # ============================================================================== | |
| # MAIN EXECUTION | |
| # ============================================================================== | |
| # Download and execute the private app | |
| private_app_path = download_private_app() | |
| execute_private_app(private_app_path) | |