Download app.py from MandyDeep/ImageGen: direct link, hf CLI and curl.
- Browser
- Download file 4.95 kB
-
https://huggingface.co/spaces/MandyDeep/ImageGen/resolve/f351342baa41229ef191663f43889b9f020fc9e6/app.py
- Command line
-
hf download hf://spaces/MandyDeep/ImageGen@f351342baa41229ef191663f43889b9f020fc9e6/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MandyDeep/ImageGen/resolve/f351342baa41229ef191663f43889b9f020fc9e6/app.py
4.95 kB
| import spaces | |
| import os | |
| import sys | |
| import site | |
| 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 | |
| def is_fatal_gpu_error(e: BaseException) -> bool: | |
| err_type = type(e).__name__ | |
| err_str = f"{err_type}: {str(e)} | {repr(e)}".lower() | |
| fatal_keywords = [ | |
| "ecc", | |
| "uncorrectable", | |
| "cudaerror", | |
| "acceleratorerror", | |
| "cuda error", | |
| "cuda_error", | |
| "device-side assertion", | |
| "cuda_launch_blocking", | |
| ] | |
| return any(kw in err_str for kw in fatal_keywords) | |
| def dummy_gpu_for_startup(): | |
| try: | |
| 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." | |
| except BaseException as e: | |
| if is_fatal_gpu_error(e): | |
| print("\n" + "="*80) | |
| print(f"🚨 [Fatal GPU Error] Captured fatal GPU error during startup check: {e}") | |
| print("🚨 Terminating process to trigger an automatic container restart...") | |
| print("="*80 + "\n") | |
| os._exit(1) | |
| raise e | |
| def main(): | |
| 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} ---") | |
| if is_fatal_gpu_error(e): | |
| print("\n" + "="*80) | |
| print(f"🚨 [Fatal GPU Error] Captured fatal GPU error during startup check: {err_msg}") | |
| print("🚨 Terminating process to trigger an automatic container restart...") | |
| print("="*80 + "\n") | |
| os._exit(1) | |
| 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 ---") | |
| try: | |
| demo.queue().launch(server_name="0.0.0.0", server_port=7860) | |
| except ValueError as e: | |
| if "localhost is not accessible" in str(e): | |
| print("\n" + "="*80) | |
| print(f"🚨 [Gradio Launch Error] Localhost accessibility check failed: {e}") | |
| print("🚨 Terminating process to trigger an automatic container restart...") | |
| print("="*80 + "\n") | |
| sys.exit(1) | |
| raise e | |
| if __name__ == "__main__": | |
| main() |