Spaces:
Sleeping
Sleeping
Commit
·
b4e28c8
1
Parent(s):
124cef3
[Update]: Integrated Stable Diffusion for artistic visualizations 🎨
Browse files- Added: Initialization of Stable Diffusion model for generating artistic representations based on memory operations.
- Implemented: New function to generate artistic prompts based on emotional context and memory operations.
- Updated: Quantum memory operations to include options for generating art and visualizations.
- Enhanced: Gradio interface to display both wave patterns and artistic visualizations.
- Pro Tip of the Commit: Art and science make a beautiful wave together! 🌊✨
Aye, Aye! 🚢
- app.py +88 -13
- requirements.txt +4 -1
app.py
CHANGED
@@ -3,6 +3,23 @@ import spaces
|
|
3 |
import torch
|
4 |
import numpy as np
|
5 |
from typing import Tuple, List
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
class EmotionalContext:
|
8 |
"""Implements Mem|8's emotional context structure"""
|
@@ -18,8 +35,30 @@ def create_wave_pattern(size: int, frequency: float, amplitude: float) -> torch.
|
|
18 |
T, X = torch.meshgrid(t, x, indexing='ij')
|
19 |
return amplitude * torch.sin(frequency * T + X)
|
20 |
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
"""Perform quantum-inspired memory operations using Mem|8 concepts."""
|
24 |
# Initialize emotional context
|
25 |
emotion = EmotionalContext()
|
@@ -27,7 +66,8 @@ def quantum_memory_ops(input_size: int, operation: str, emotion_valence: float)
|
|
27 |
emotion.arousal = torch.abs(torch.tensor([emotion_valence * 2])).cuda()
|
28 |
|
29 |
results = []
|
30 |
-
|
|
|
31 |
|
32 |
if operation == "wave_memory":
|
33 |
# Create memory wave pattern (M = A·exp(iωt-kx)·D·E)
|
@@ -39,7 +79,7 @@ def quantum_memory_ops(input_size: int, operation: str, emotion_valence: float)
|
|
39 |
results.append(f"Shape: {memory_state.shape}")
|
40 |
results.append(f"Emotional Modulation: {emotional_mod.mean().item():.4f}")
|
41 |
results.append(f"Memory Coherence: {torch.linalg.norm(memory_state).item():.4f}")
|
42 |
-
|
43 |
|
44 |
elif operation == "interference":
|
45 |
# Create interference between two memory waves
|
@@ -51,7 +91,7 @@ def quantum_memory_ops(input_size: int, operation: str, emotion_valence: float)
|
|
51 |
results.append(f"Memory Interference Pattern:")
|
52 |
results.append(f"Pattern Strength: {torch.max(emotional_weight).item():.4f}")
|
53 |
results.append(f"Emotional Weight: {emotion.valence.item()/128:.4f}")
|
54 |
-
|
55 |
|
56 |
elif operation == "resonance":
|
57 |
# Demonstrate emotional resonance patterns
|
@@ -63,21 +103,39 @@ def quantum_memory_ops(input_size: int, operation: str, emotion_valence: float)
|
|
63 |
results.append(f"Emotional Resonance Pattern:")
|
64 |
results.append(f"Resonance Frequency: {resonance_freq.item():.4f}")
|
65 |
results.append(f"Pattern Energy: {torch.sum(resonance**2).item():.4f}")
|
66 |
-
|
67 |
|
68 |
results.append(f"\nEmotional Context:")
|
69 |
results.append(f"Valence: {emotion.valence.item():.2f}")
|
70 |
results.append(f"Arousal: {emotion.arousal.item():.2f}")
|
71 |
results.append(f"Device: {wave.device}")
|
72 |
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# Create a beautiful interface inspired by Mem|8's wave concepts
|
76 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue")) as demo:
|
77 |
gr.Markdown("""
|
78 |
# 🌊 Mem|8 Wave Memory Explorer
|
79 |
|
80 |
-
Welcome to 8b.is's
|
81 |
wave-based memory architecture paper, visualizing how memories propagate and interact like waves
|
82 |
in an ocean of consciousness.
|
83 |
|
@@ -107,16 +165,33 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue"))
|
|
107 |
label="Emotional Valence",
|
108 |
info="Emotional context from negative to positive (-128 to 127)"
|
109 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
run_btn = gr.Button("Generate Memory Wave", variant="primary")
|
111 |
|
112 |
with gr.Column():
|
113 |
output_text = gr.Textbox(label="Wave Analysis", lines=8)
|
114 |
-
|
|
|
|
|
115 |
|
116 |
run_btn.click(
|
117 |
quantum_memory_ops,
|
118 |
-
inputs=[size_input, operation_input, emotion_input],
|
119 |
-
outputs=[output_text,
|
120 |
)
|
121 |
|
122 |
gr.Markdown("""
|
@@ -127,8 +202,8 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue"))
|
|
127 |
2. **Interference**: How different memories interact and combine
|
128 |
3. **Resonance**: Emotional resonance patterns in memory formation
|
129 |
|
130 |
-
The visualization shows
|
131 |
-
demonstrating
|
132 |
|
133 |
All computations are accelerated using Hugging Face's Zero GPU technology!
|
134 |
""")
|
|
|
3 |
import torch
|
4 |
import numpy as np
|
5 |
from typing import Tuple, List
|
6 |
+
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
|
7 |
+
import random
|
8 |
+
|
9 |
+
# Initialize Stable Diffusion
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
11 |
+
model_repo_id = "tensorart/stable-diffusion-3.5-large-TurboX"
|
12 |
+
|
13 |
+
if torch.cuda.is_available():
|
14 |
+
torch_dtype = torch.float16
|
15 |
+
else:
|
16 |
+
torch_dtype = torch.float32
|
17 |
+
|
18 |
+
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
19 |
+
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(model_repo_id, subfolder="scheduler", shift=5)
|
20 |
+
pipe = pipe.to(device)
|
21 |
+
|
22 |
+
MAX_SEED = np.iinfo(np.int32).max
|
23 |
|
24 |
class EmotionalContext:
|
25 |
"""Implements Mem|8's emotional context structure"""
|
|
|
35 |
T, X = torch.meshgrid(t, x, indexing='ij')
|
36 |
return amplitude * torch.sin(frequency * T + X)
|
37 |
|
38 |
+
def generate_memory_prompt(operation: str, emotion_valence: float) -> str:
|
39 |
+
"""Generate artistic prompts based on memory operation and emotional state"""
|
40 |
+
base_prompts = {
|
41 |
+
"wave_memory": "memories flowing like waves in an infinite ocean, ",
|
42 |
+
"interference": "two waves of memory intersecting and creating patterns, ",
|
43 |
+
"resonance": "resonating waves of consciousness forming harmonious patterns, "
|
44 |
+
}
|
45 |
+
|
46 |
+
emotion_desc = "serene and peaceful" if -20 <= emotion_valence <= 20 else \
|
47 |
+
"joyful and vibrant" if emotion_valence > 20 else \
|
48 |
+
"dark and introspective"
|
49 |
+
|
50 |
+
style = "digital art, abstract, flowing, wave patterns, "
|
51 |
+
prompt = f"{base_prompts[operation]}{emotion_desc}, {style} ethereal, dreamlike quality"
|
52 |
+
return prompt
|
53 |
+
|
54 |
+
@spaces.GPU(duration=65)
|
55 |
+
def quantum_memory_ops(
|
56 |
+
input_size: int,
|
57 |
+
operation: str,
|
58 |
+
emotion_valence: float,
|
59 |
+
generate_art: bool = True,
|
60 |
+
seed: int = 42
|
61 |
+
) -> Tuple[str, np.ndarray, np.ndarray]:
|
62 |
"""Perform quantum-inspired memory operations using Mem|8 concepts."""
|
63 |
# Initialize emotional context
|
64 |
emotion = EmotionalContext()
|
|
|
66 |
emotion.arousal = torch.abs(torch.tensor([emotion_valence * 2])).cuda()
|
67 |
|
68 |
results = []
|
69 |
+
wave_viz = None
|
70 |
+
art_viz = None
|
71 |
|
72 |
if operation == "wave_memory":
|
73 |
# Create memory wave pattern (M = A·exp(iωt-kx)·D·E)
|
|
|
79 |
results.append(f"Shape: {memory_state.shape}")
|
80 |
results.append(f"Emotional Modulation: {emotional_mod.mean().item():.4f}")
|
81 |
results.append(f"Memory Coherence: {torch.linalg.norm(memory_state).item():.4f}")
|
82 |
+
wave_viz = memory_state.cpu().numpy()
|
83 |
|
84 |
elif operation == "interference":
|
85 |
# Create interference between two memory waves
|
|
|
91 |
results.append(f"Memory Interference Pattern:")
|
92 |
results.append(f"Pattern Strength: {torch.max(emotional_weight).item():.4f}")
|
93 |
results.append(f"Emotional Weight: {emotion.valence.item()/128:.4f}")
|
94 |
+
wave_viz = emotional_weight.cpu().numpy()
|
95 |
|
96 |
elif operation == "resonance":
|
97 |
# Demonstrate emotional resonance patterns
|
|
|
103 |
results.append(f"Emotional Resonance Pattern:")
|
104 |
results.append(f"Resonance Frequency: {resonance_freq.item():.4f}")
|
105 |
results.append(f"Pattern Energy: {torch.sum(resonance**2).item():.4f}")
|
106 |
+
wave_viz = resonance.cpu().numpy()
|
107 |
|
108 |
results.append(f"\nEmotional Context:")
|
109 |
results.append(f"Valence: {emotion.valence.item():.2f}")
|
110 |
results.append(f"Arousal: {emotion.arousal.item():.2f}")
|
111 |
results.append(f"Device: {wave.device}")
|
112 |
|
113 |
+
# Generate artistic visualization if requested
|
114 |
+
if generate_art:
|
115 |
+
prompt = generate_memory_prompt(operation, emotion_valence)
|
116 |
+
generator = torch.Generator().manual_seed(seed)
|
117 |
+
art_viz = pipe(
|
118 |
+
prompt=prompt,
|
119 |
+
negative_prompt="text, watermark, signature, blurry, distorted",
|
120 |
+
guidance_scale=1.5,
|
121 |
+
num_inference_steps=8,
|
122 |
+
width=768,
|
123 |
+
height=768,
|
124 |
+
generator=generator,
|
125 |
+
).images[0]
|
126 |
+
|
127 |
+
results.append(f"\nArtistic Visualization:")
|
128 |
+
results.append(f"Prompt: {prompt}")
|
129 |
+
results.append(f"Seed: {seed}")
|
130 |
+
|
131 |
+
return "\n".join(results), wave_viz, art_viz
|
132 |
|
133 |
# Create a beautiful interface inspired by Mem|8's wave concepts
|
134 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue")) as demo:
|
135 |
gr.Markdown("""
|
136 |
# 🌊 Mem|8 Wave Memory Explorer
|
137 |
|
138 |
+
Welcome to 8b.is's memory ocean demonstration! This showcase implements concepts from our Mem|8
|
139 |
wave-based memory architecture paper, visualizing how memories propagate and interact like waves
|
140 |
in an ocean of consciousness.
|
141 |
|
|
|
165 |
label="Emotional Valence",
|
166 |
info="Emotional context from negative to positive (-128 to 127)"
|
167 |
)
|
168 |
+
|
169 |
+
with gr.Accordion("Advanced Settings", open=False):
|
170 |
+
generate_art = gr.Checkbox(
|
171 |
+
label="Generate Artistic Visualization",
|
172 |
+
value=True,
|
173 |
+
info="Use Stable Diffusion to create artistic representations"
|
174 |
+
)
|
175 |
+
seed = gr.Slider(
|
176 |
+
label="Art Generation Seed",
|
177 |
+
minimum=0,
|
178 |
+
maximum=MAX_SEED,
|
179 |
+
step=1,
|
180 |
+
value=42
|
181 |
+
)
|
182 |
+
|
183 |
run_btn = gr.Button("Generate Memory Wave", variant="primary")
|
184 |
|
185 |
with gr.Column():
|
186 |
output_text = gr.Textbox(label="Wave Analysis", lines=8)
|
187 |
+
with gr.Row():
|
188 |
+
wave_plot = gr.Plot(label="Wave Pattern")
|
189 |
+
art_output = gr.Image(label="Artistic Visualization")
|
190 |
|
191 |
run_btn.click(
|
192 |
quantum_memory_ops,
|
193 |
+
inputs=[size_input, operation_input, emotion_input, generate_art, seed],
|
194 |
+
outputs=[output_text, wave_plot, art_output]
|
195 |
)
|
196 |
|
197 |
gr.Markdown("""
|
|
|
202 |
2. **Interference**: How different memories interact and combine
|
203 |
3. **Resonance**: Emotional resonance patterns in memory formation
|
204 |
|
205 |
+
The visualization shows both the mathematical wave patterns and artistic interpretations,
|
206 |
+
demonstrating how emotional context affects memory formation and recall.
|
207 |
|
208 |
All computations are accelerated using Hugging Face's Zero GPU technology!
|
209 |
""")
|
requirements.txt
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
gradio>=4.19.2
|
2 |
torch>=2.2.0
|
3 |
numpy>=1.24.0
|
4 |
-
spaces>=0.19.4
|
|
|
|
|
|
|
|
1 |
gradio>=4.19.2
|
2 |
torch>=2.2.0
|
3 |
numpy>=1.24.0
|
4 |
+
spaces>=0.19.4
|
5 |
+
diffusers>=0.25.0
|
6 |
+
transformers>=4.37.0
|
7 |
+
accelerate>=0.27.0
|