Spaces:
Running
Running
| # -*- coding: utf-8 -*- | |
| # [Imperial HF Wrapper v2.8.0 - Sync Blueprint Registration] | |
| # Flask 3.x PROHIBITS register_blueprint after first request. | |
| # ALL route/blueprint registration MUST happen synchronously before gunicorn serves. | |
| # Only heavy AI engine loading is deferred to background thread. | |
| import os | |
| import sys | |
| import threading | |
| # ββ Path Resolution βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| # Add the root (dist_hf/) so 'shadow_brain_core' package is importable | |
| if current_dir not in sys.path: | |
| sys.path.insert(0, current_dir) | |
| # Add shadow_brain_core itself so inner modules are importable by short name | |
| core_path = os.path.join(current_dir, "shadow_brain_core") | |
| if os.path.exists(core_path) and core_path not in sys.path: | |
| sys.path.insert(0, core_path) | |
| # Add scripts/tools so cloud_tool, gdrive_sync, quota_manager etc. resolve | |
| tools_path = os.path.join(current_dir, "scripts", "tools") | |
| if os.path.exists(tools_path) and tools_path not in sys.path: | |
| sys.path.insert(0, tools_path) | |
| # ββ Minimal Flask Bootstrap βββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| from flask import Flask, jsonify, request | |
| from flask_cors import CORS | |
| print(f"[SYSTEM] [HF] Gunicorn worker starting... v2.8.0 Sync-Blueprint.", flush=True) | |
| print(f"[SYSTEM] [HF] Environment PORT: {os.environ.get('PORT', 'Not Set (Default 7860)')}", flush=True) | |
| app = Flask(__name__) | |
| # [π±] CORS is handled dynamically in web_server.py via after_request to support credentials. | |
| # We initialize it here with minimal settings to avoid conflicts. | |
| CORS(app, supports_credentials=True) | |
| app.status = "BOOTING" | |
| app.boot_error = None | |
| def log_request_info(): | |
| # [π± Imperial] Mute periodic polling logs to keep terminal clean | |
| poll_paths = ["/api/logs/shadow-brain", "/api/system/monitor/r2", "/health", "/api/health", "/api/status"] | |
| if any(p in request.path for p in poll_paths): | |
| return | |
| print(f"[DEBUG] [REQUEST] Path: {request.path}, Method: {request.method}", flush=True) | |
| def hf_root(): | |
| return jsonify({ | |
| "status": app.status, | |
| "message": "Imperial Shadow Brain (v2.8.0) β Sync Blueprint Architecture", | |
| "boot_error": str(app.boot_error) if app.boot_error else None, | |
| "engine": "LinuxCloudEngine", | |
| "assigned_port": os.environ.get('PORT', '7860') | |
| }) | |
| def hf_health(): | |
| return jsonify({ | |
| "status": app.status, | |
| "engine_ready": (app.status in ["RUNNING", "IGNITING"]), | |
| "ok": True, | |
| "version": "2.8.0", | |
| }) | |
| # ββ [π± CRITICAL FIX v2.8.0] Register all blueprints SYNCHRONOUSLY ββββββββββ | |
| # Flask 3.x requires ALL blueprints registered BEFORE first request. | |
| # The previous background-thread approach caused FATAL errors: blueprints | |
| # could not be registered after gunicorn served the first /health request. | |
| print("[SYSTEM] [HF] Registering blueprints synchronously (Flask 3.x compliance)...", flush=True) | |
| try: | |
| from shadow_brain_core.web_server import create_web_server | |
| class LinuxCloudEngine: | |
| def set_title(self, title): pass | |
| def get_status(self): return "Active (Cloud)" | |
| # create_web_server registers ALL blueprints onto app synchronously. | |
| # It also spawns its own background threads for heavy AI engine loading. | |
| create_web_server(current_dir, LinuxCloudEngine(), app_instance=app) | |
| app.status = "IGNITING" | |
| print("[SYSTEM] [HF] All blueprints registered. Engine igniting in background...", flush=True) | |
| # [π± Imperial Telegram] ν λ κ·Έλ¨ λ΄μ λ‘컬 μ μ© μ μ± μ΄λ―λ‘ HF(ν΄λΌμ°λ)μμλ μ ννμ§ μλλ€. | |
| # (λ‘컬μ web_server.run_server()μμ telegram_bot.start()λ‘ κΈ°λλ¨) | |
| print("[SYSTEM] [HF] βοΈ ν λ κ·Έλ¨ λ΄ λ―Έμ ν β λ‘컬 μ μ© μ μ± .", flush=True) | |
| # [π± Imperial Discord] HF(ν΄λΌμ°λ)λ discord.com:443 μμλ°μ΄λκ° μ°¨λ¨λμ΄ κ²μ΄νΈμ¨μ΄ | |
| # μ°κ²°μ΄ λΆκ°νλ€(Cannot connect to host discord.com:443). λμ€μ½λ λ΄μ λ‘컬 μ μ© μ μ± μ΄λ―λ‘ | |
| # HFμμλ μ ννμ§ μλλ€. | |
| print("[SYSTEM] [HF] βοΈ λμ€μ½λ λ΄ λ―Έμ ν β λ‘컬 μ μ© μ μ± (ν΄λΌμ°λ discord.com μμλ°μ΄λ μ°¨λ¨).", flush=True) | |
| except Exception as e: | |
| app.status = "ERROR" | |
| app.boot_error = e | |
| print(f"[FATAL] [HF] Synchronous blueprint registration failed: {e}", flush=True) | |
| import traceback | |
| traceback.print_exc() | |
| # ββ WSGI entry point βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Gunicorn: `gunicorn app:app` | |
| if __name__ == "__main__": | |
| port = int(os.environ.get("PORT", 7860)) | |
| app.run(host="0.0.0.0", port=port, debug=False, use_reloader=False) | |