File size: 3,670 Bytes
b5ac208
79f99d4
 
b5ac208
76aea54
 
 
79f99d4
f4dd995
 
 
 
b5ac208
 
 
 
79f99d4
b5ac208
79f99d4
b5ac208
 
 
 
719095a
79f99d4
b5ac208
 
 
 
 
 
 
 
 
 
 
 
 
 
719095a
b5ac208
 
 
 
719095a
b5ac208
 
9a135cd
 
 
 
 
79f99d4
 
3119b7f
5306acf
3500e03
719095a
5306acf
 
 
f4dd995
 
 
 
b5ac208
 
 
 
 
 
 
f4dd995
5306acf
 
f351342
 
 
5306acf
b5ac208
 
5306acf
 
 
79f99d4
b5ac208
 
 
2d31d6d
 
719095a
b5ac208
79f99d4
b5ac208
3119b7f
2d31d6d
3119b7f
79f99d4
 
76aea54
3119b7f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import spaces
import os
import sys
import site
import gradio as gr

print(f"📦 [Environment] Gradio Version: {gr.__version__}")

if "--use-sage-attention" not in sys.argv:
    sys.argv.append("--use-sage-attention")
    print("🚀 [SageAttention] Injected '--use-sage-attention' into sys.argv.")

APP_DIR = os.path.dirname(os.path.abspath(__file__))
if APP_DIR not in sys.path:
    sys.path.insert(0, APP_DIR)
    print(f"✅ Added project root '{APP_DIR}' to sys.path.")

SAGE_PATCH_APPLIED = False

def apply_sage_attention_patch():
    global SAGE_PATCH_APPLIED
    if SAGE_PATCH_APPLIED:
        return "SageAttention patch already applied."

    try:
        from comfy import model_management
        import sageattention
        
        print("--- [Runtime Patch] sageattention package found. Applying patch... ---")
        model_management.sage_attention_enabled = lambda: True
        model_management.pytorch_attention_enabled = lambda: False
        
        SAGE_PATCH_APPLIED = True
        return "✅ Successfully enabled SageAttention."
    except ImportError:
        SAGE_PATCH_APPLIED = False
        msg = "--- [Runtime Patch] ⚠️ sageattention package not found. Continuing with default attention. ---"
        print(msg)
        return msg
    except Exception as e:
        SAGE_PATCH_APPLIED = False
        msg = f"--- [Runtime Patch] ❌ An error occurred while applying SageAttention patch: {e} ---"
        print(msg)
        return msg

@spaces.GPU
def dummy_gpu_for_startup():
    print("--- [GPU Startup] Dummy function for startup check initiated. ---")
    patch_result = apply_sage_attention_patch()
    print(f"--- [GPU Startup] {patch_result} ---")
    print("--- [GPU Startup] Startup check passed. ---")
    return "Startup check passed."


def create_app():
    from comfy_integration import setup as setup_comfyui
    from utils.app_utils import load_ipadapter_presets
    
    print("--- [Setup] Starting ComfyUI initialization ---")
    setup_comfyui.initialize_comfyui()
    
    print("--- [Setup] Applying SageAttention Runtime Patch ---")
    patch_result = apply_sage_attention_patch()
    print(f"--- [Setup] {patch_result} ---")

    print("--- [Setup] Reloading site-packages to detect newly installed packages... ---")
    try:
        site.main()
        print("--- [Setup] ✅ Site-packages reloaded. ---")
    except Exception as e:
        print(f"--- [Setup] ⚠️  Warning: Could not fully reload site-packages: {e} ---")

    print("--- Initiating GPU Startup Check & SageAttention Patch Verification ---")
    try:
        dummy_gpu_for_startup()
    except BaseException as e:
        err_msg = f"{type(e).__name__}: {str(e)}"
        print(f"--- [GPU Startup] ⚠️ Warning: Startup check failed: {err_msg} ---")

    print("--- Starting Application Setup ---")
    
    print("--- Loading IPAdapter presets ---")
    load_ipadapter_presets()
    print("--- ✅ IPAdapter setup complete. ---")

    print("--- Environment configured. Proceeding with module imports. ---")
    from ui.layout import build_ui
    from ui.events import attach_event_handlers
    import mcp_tools as mcp
    print(f"✅ Loaded MCP module with tools: {[fn.__name__ for fn in mcp.MCP_FUNCTIONS]}")

    print(f"✅ Working directory is stable: {os.getcwd()}")

    demo = build_ui(attach_event_handlers)
    return demo

demo = create_app()

if __name__ == "__main__":
    print(f"--- Launching Gradio Interface (v{gr.__version__}) ---")
    demo.queue().launch(mcp_server=True, footer_links=["api", "gradio", "settings"])