| import os |
| import shutil |
| import subprocess |
| import sys |
| import importlib.util |
| import gradio as gr |
|
|
| REPO_DIR = "/app/private_repo" |
|
|
| def load_private_code(): |
| try: |
| print("Loading private code from GitHub...") |
| github_url = os.getenv("GITHUB_REPO", "").strip() |
| if not github_url: |
| print("GITHUB_REPO secret not found or empty") |
| return False |
| if os.path.exists(REPO_DIR): |
| shutil.rmtree(REPO_DIR) |
| print("Cloning private repository...") |
| result = subprocess.run(["git", "clone", github_url, REPO_DIR], capture_output=True, text=True, timeout=120) |
| if result.returncode != 0: |
| print(f"Git clone failed: {result.stderr}") |
| return False |
| requirements_path = os.path.join(REPO_DIR, "requirements.txt") |
| if os.path.exists(requirements_path): |
| print("Installing dependencies...") |
| subprocess.run([sys.executable, "-m", "pip", "install", "-r", requirements_path], check=True) |
| sys.path.insert(0, REPO_DIR) |
| print("Private code loaded successfully!") |
| return True |
| except Exception as e: |
| print(f"Error loading private code: {e}") |
| return False |
|
|
| if load_private_code(): |
| try: |
| private_app_path = os.path.join(REPO_DIR, "app.py") |
| if not os.path.exists(private_app_path): |
| raise FileNotFoundError("app.py not found in private repo") |
| spec = importlib.util.spec_from_file_location("private_app", private_app_path) |
| private_module = importlib.util.module_from_spec(spec) |
| spec.loader.exec_module(private_module) |
| print("Using private GitHub version") |
| if hasattr(private_module, "main_app"): |
| demo = private_module.main_app() |
| else: |
| print("No main_app() found, running default empty UI") |
| demo = gr.Blocks() |
| with demo: |
| gr.Markdown("# main_app() not found in private repo") |
| except Exception as e: |
| print(f"Import failed: {e}") |
| demo = gr.Blocks() |
| with demo: |
| gr.Markdown(f"# Error Loading Private Code\n\n{e}") |
| else: |
| demo = gr.Blocks() |
| with demo: |
| gr.Markdown("# Private Code Not Loaded") |
|
|
| if __name__ == "__main__": |
| demo.launch() |