ThreadAbort commited on
Commit
614ca0d
Β·
1 Parent(s): 459b003

[Update]: Enhanced app.py for optimized GPU usage and improved Gradio interface 🌟

Browse files

- Updated: Replaced `import space` with `import os` for better compatibility.
- Added: Device configuration to utilize GPU if available, enhancing performance for memory operations.
- Implemented: Memory optimizations for the Stable Diffusion pipeline when using GPU.
- Updated: Gradio interface to include queuing and server configuration for better resource management.
- Added: New dependency `spaces` to requirements.txt for GPU functionality.
- Pro Tip of the Commit: When it comes to performance, let’s ride the GPU wave! πŸŒŠπŸš€
Aye, Aye! 🚒

Files changed (2) hide show
  1. app.py +37 -11
  2. requirements.txt +2 -1
app.py CHANGED
@@ -10,7 +10,7 @@ and interact like waves in an ocean of consciousness.
10
 
11
  Created by: Aye & Hue (with Trisha from Accounting keeping the numbers flowing)
12
  """
13
- import space
14
  import gradio as gr
15
  import torch
16
  import numpy as np
@@ -19,7 +19,6 @@ from matplotlib import cm
19
  import random
20
  import time
21
  from typing import Tuple, List, Dict, Optional, Union
22
- import os
23
  import json
24
  from datetime import datetime
25
  import plotly.graph_objects as go
@@ -36,8 +35,21 @@ random.seed(RANDOM_SEED)
36
  # Constants
37
  DEFAULT_GRID_SIZE = 64
38
  EMOTION_RANGE = (-5, 5) # Range for emotional valence
 
39
  MAX_SEED = 999999999 # Maximum seed value for art generation
40
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # Try to import Stable Diffusion components
42
  STABLE_DIFFUSION_AVAILABLE = False
43
  pipe = None
@@ -45,18 +57,24 @@ try:
45
  from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
46
  STABLE_DIFFUSION_AVAILABLE = True
47
 
48
- # Initialize Stable Diffusion pipeline
49
  try:
50
  pipe = DiffusionPipeline.from_pretrained(
51
  "stabilityai/stable-diffusion-xl-base-1.0",
52
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
53
  use_safetensors=True,
54
- variant="fp16" if torch.cuda.is_available() else None
55
  )
56
  pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config)
57
- pipe.to("cuda" if torch.cuda.is_available() else "cpu")
58
- pipe.enable_model_cpu_offload()
59
- pipe.enable_vae_slicing()
 
 
 
 
 
 
60
  except Exception as e:
61
  print(f"Warning: Failed to initialize Stable Diffusion: {e}")
62
  pipe = None
@@ -143,7 +161,7 @@ class MemoryWave:
143
  """
144
  def __init__(self,
145
  size: int = DEFAULT_GRID_SIZE,
146
- device: str = "cuda" if torch.cuda.is_available() else "cpu"):
147
  """
148
  Initialize a memory wave system.
149
 
@@ -748,7 +766,8 @@ def generate_memory_prompt(operation: str, emotion_valence: float) -> str:
748
  @spaces.GPU(duration=30)
749
  def create_interface():
750
  """Create the Gradio interface for the Mem|8 Wave Memory Explorer."""
751
- memory_wave = MemoryWave()
 
752
 
753
  def process_memory_operation(
754
  operation: str,
@@ -942,5 +961,12 @@ def create_interface():
942
  return demo
943
 
944
  if __name__ == "__main__":
 
945
  demo = create_interface()
946
- demo.launch()
 
 
 
 
 
 
 
10
 
11
  Created by: Aye & Hue (with Trisha from Accounting keeping the numbers flowing)
12
  """
13
+ import os
14
  import gradio as gr
15
  import torch
16
  import numpy as np
 
19
  import random
20
  import time
21
  from typing import Tuple, List, Dict, Optional, Union
 
22
  import json
23
  from datetime import datetime
24
  import plotly.graph_objects as go
 
35
  # Constants
36
  DEFAULT_GRID_SIZE = 64
37
  EMOTION_RANGE = (-5, 5) # Range for emotional valence
38
+ AROUSAL_RANGE = (0, 255) # Range for arousal
39
  MAX_SEED = 999999999 # Maximum seed value for art generation
40
 
41
+ # Configure device - Use Zero GPU if available
42
+ if torch.cuda.is_available():
43
+ print("πŸš€ Zero GPU detected! Enabling hardware acceleration...")
44
+ device = "cuda"
45
+ # Enable Zero GPU optimizations
46
+ torch.backends.cuda.matmul.allow_tf32 = True
47
+ torch.backends.cudnn.allow_tf32 = True
48
+ torch.backends.cudnn.benchmark = True
49
+ else:
50
+ print("⚑ Running on CPU")
51
+ device = "cpu"
52
+
53
  # Try to import Stable Diffusion components
54
  STABLE_DIFFUSION_AVAILABLE = False
55
  pipe = None
 
57
  from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
58
  STABLE_DIFFUSION_AVAILABLE = True
59
 
60
+ # Initialize Stable Diffusion pipeline with Zero GPU optimizations
61
  try:
62
  pipe = DiffusionPipeline.from_pretrained(
63
  "stabilityai/stable-diffusion-xl-base-1.0",
64
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
65
  use_safetensors=True,
66
+ variant="fp16" if device == "cuda" else None
67
  )
68
  pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config)
69
+ pipe.to(device)
70
+
71
+ # Enable memory optimizations
72
+ if device == "cuda":
73
+ pipe.enable_model_cpu_offload()
74
+ pipe.enable_vae_slicing()
75
+ pipe.enable_vae_tiling()
76
+ # Enable attention slicing for lower memory usage
77
+ pipe.enable_attention_slicing(slice_size="max")
78
  except Exception as e:
79
  print(f"Warning: Failed to initialize Stable Diffusion: {e}")
80
  pipe = None
 
161
  """
162
  def __init__(self,
163
  size: int = DEFAULT_GRID_SIZE,
164
+ device: str = device): # Use global device
165
  """
166
  Initialize a memory wave system.
167
 
 
766
  @spaces.GPU(duration=30)
767
  def create_interface():
768
  """Create the Gradio interface for the Mem|8 Wave Memory Explorer."""
769
+ # Initialize with Zero GPU device
770
+ memory_wave = MemoryWave(device=device)
771
 
772
  def process_memory_operation(
773
  operation: str,
 
961
  return demo
962
 
963
  if __name__ == "__main__":
964
+ # Add Hugging Face Space-specific configuration
965
  demo = create_interface()
966
+ demo.queue(max_size=10) # Enable queuing for better resource management
967
+ demo.launch(
968
+ share=False, # Don't create a public link
969
+ server_name="0.0.0.0", # Listen on all interfaces
970
+ server_port=7860, # Default Spaces port
971
+ show_api=False, # Hide API docs
972
+ )
requirements.txt CHANGED
@@ -6,4 +6,5 @@ matplotlib>=3.8.0
6
  diffusers>=0.25.0
7
  transformers>=4.37.0
8
  accelerate>=0.27.0
9
- scipy>=1.11.0
 
 
6
  diffusers>=0.25.0
7
  transformers>=4.37.0
8
  accelerate>=0.27.0
9
+ scipy>=1.11.0
10
+ spaces>=0.1.0