ImageGen / app.py
RioShiina's picture
Upload folder using huggingface_hub
e62785a verified
import spaces
import os
import sys
import site
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 main():
from utils.app_utils import print_welcome_message
from scripts import build_sage_attention
from comfy_integration import setup as setup_comfyui
print_welcome_message()
print("--- [Setup] Attempting to build and install SageAttention... ---")
try:
build_sage_attention.install_sage_attention()
print("--- [Setup] βœ… SageAttention installation process finished. ---")
except Exception as e:
print(f"--- [Setup] ❌ SageAttention installation failed: {e}. Continuing with default attention. ---")
print("--- [Setup] Starting ComfyUI initialization ---")
setup_comfyui.initialize_comfyui()
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 ---")
try:
dummy_gpu_for_startup()
except Exception as e:
print(f"--- [GPU Startup] ⚠️ Warning: Startup check failed: {e} ---")
from utils.app_utils import load_ipadapter_presets
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
print(f"βœ… Working directory is stable: {os.getcwd()}")
demo = build_ui(attach_event_handlers)
print("--- Launching Gradio Interface ---")
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
if __name__ == "__main__":
main()