shadowbrain / app.py
taemin1980's picture
πŸ”± Imperial Deployment: Shadow Brain Core ignition
d50a68d verified
Raw
History Blame Contribute Delete
5.22 kB
# -*- 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
@app.before_request
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)
@app.route("/", strict_slashes=False)
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')
})
@app.route("/health", strict_slashes=False)
@app.route("/api/health", strict_slashes=False)
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)