File size: 8,567 Bytes
f4f6007
 
 
124cef3
 
b4e28c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4f6007
124cef3
 
 
 
 
 
f4f6007
124cef3
 
 
 
 
 
 
b4e28c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124cef3
 
 
 
 
 
 
b4e28c8
 
124cef3
 
 
 
 
 
 
 
 
 
 
b4e28c8
124cef3
 
 
 
 
 
 
 
 
 
 
b4e28c8
124cef3
 
 
 
 
 
 
 
 
 
 
b4e28c8
124cef3
 
 
 
 
 
b4e28c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124cef3
 
 
 
 
 
b4e28c8
124cef3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e28c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124cef3
 
 
 
b4e28c8
 
 
124cef3
 
 
b4e28c8
 
124cef3
 
 
 
 
 
 
 
 
 
b4e28c8
 
124cef3
 
 
f4f6007
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import gradio as gr
import spaces
import torch
import numpy as np
from typing import Tuple, List
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
import random

# Initialize Stable Diffusion
device = "cuda" if torch.cuda.is_available() else "cpu"
model_repo_id = "tensorart/stable-diffusion-3.5-large-TurboX"

if torch.cuda.is_available():
    torch_dtype = torch.float16
else:
    torch_dtype = torch.float32

pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_pretrained(model_repo_id, subfolder="scheduler", shift=5)
pipe = pipe.to(device)

MAX_SEED = np.iinfo(np.int32).max

class EmotionalContext:
    """Implements Mem|8's emotional context structure"""
    def __init__(self):
        self.valence = torch.zeros(1).cuda()  # -128 to 127: negative to positive
        self.arousal = torch.zeros(1).cuda()  # 0 to 255: intensity level
        self.context = torch.zeros(1).cuda()  # Contextual flags

def create_wave_pattern(size: int, frequency: float, amplitude: float) -> torch.Tensor:
    """Create a wave pattern as described in Mem|8 paper"""
    t = torch.linspace(0, 2*np.pi, size).cuda()
    x = torch.linspace(0, 2*np.pi, size).cuda()
    T, X = torch.meshgrid(t, x, indexing='ij')
    return amplitude * torch.sin(frequency * T + X)

def generate_memory_prompt(operation: str, emotion_valence: float) -> str:
    """Generate artistic prompts based on memory operation and emotional state"""
    base_prompts = {
        "wave_memory": "memories flowing like waves in an infinite ocean, ",
        "interference": "two waves of memory intersecting and creating patterns, ",
        "resonance": "resonating waves of consciousness forming harmonious patterns, "
    }
    
    emotion_desc = "serene and peaceful" if -20 <= emotion_valence <= 20 else \
                  "joyful and vibrant" if emotion_valence > 20 else \
                  "dark and introspective"
    
    style = "digital art, abstract, flowing, wave patterns, "
    prompt = f"{base_prompts[operation]}{emotion_desc}, {style} ethereal, dreamlike quality"
    return prompt

@spaces.GPU(duration=65)
def quantum_memory_ops(
    input_size: int, 
    operation: str, 
    emotion_valence: float,
    generate_art: bool = True,
    seed: int = 42
) -> Tuple[str, np.ndarray, np.ndarray]:
    """Perform quantum-inspired memory operations using Mem|8 concepts."""
    # Initialize emotional context
    emotion = EmotionalContext()
    emotion.valence = torch.tensor([emotion_valence]).cuda()
    emotion.arousal = torch.abs(torch.tensor([emotion_valence * 2])).cuda()
    
    results = []
    wave_viz = None
    art_viz = None
    
    if operation == "wave_memory":
        # Create memory wave pattern (M = A·exp(iωt-kx)·D·E)
        wave = create_wave_pattern(input_size, 2.0, 1.0)
        emotional_mod = torch.exp(emotion.valence/128 * wave)
        memory_state = wave * emotional_mod
        
        results.append(f"Wave Memory Pattern Created:")
        results.append(f"Shape: {memory_state.shape}")
        results.append(f"Emotional Modulation: {emotional_mod.mean().item():.4f}")
        results.append(f"Memory Coherence: {torch.linalg.norm(memory_state).item():.4f}")
        wave_viz = memory_state.cpu().numpy()
        
    elif operation == "interference":
        # Create interference between two memory waves
        wave1 = create_wave_pattern(input_size, 2.0, 1.0)
        wave2 = create_wave_pattern(input_size, 3.0, 0.5)
        interference = wave1 + wave2
        emotional_weight = torch.sigmoid(emotion.valence/128) * interference
        
        results.append(f"Memory Interference Pattern:")
        results.append(f"Pattern Strength: {torch.max(emotional_weight).item():.4f}")
        results.append(f"Emotional Weight: {emotion.valence.item()/128:.4f}")
        wave_viz = emotional_weight.cpu().numpy()
        
    elif operation == "resonance":
        # Demonstrate emotional resonance patterns
        base_wave = create_wave_pattern(input_size, 2.0, 1.0)
        resonance_freq = 1.0 + torch.sigmoid(emotion.valence/128)
        resonant_wave = create_wave_pattern(input_size, resonance_freq.item(), 1.0)
        resonance = base_wave * resonant_wave
        
        results.append(f"Emotional Resonance Pattern:")
        results.append(f"Resonance Frequency: {resonance_freq.item():.4f}")
        results.append(f"Pattern Energy: {torch.sum(resonance**2).item():.4f}")
        wave_viz = resonance.cpu().numpy()
    
    results.append(f"\nEmotional Context:")
    results.append(f"Valence: {emotion.valence.item():.2f}")
    results.append(f"Arousal: {emotion.arousal.item():.2f}")
    results.append(f"Device: {wave.device}")
    
    # Generate artistic visualization if requested
    if generate_art:
        prompt = generate_memory_prompt(operation, emotion_valence)
        generator = torch.Generator().manual_seed(seed)
        art_viz = pipe(
            prompt=prompt,
            negative_prompt="text, watermark, signature, blurry, distorted",
            guidance_scale=1.5,
            num_inference_steps=8,
            width=768,
            height=768,
            generator=generator,
        ).images[0]
        
        results.append(f"\nArtistic Visualization:")
        results.append(f"Prompt: {prompt}")
        results.append(f"Seed: {seed}")
    
    return "\n".join(results), wave_viz, art_viz

# Create a beautiful interface inspired by Mem|8's wave concepts
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="blue")) as demo:
    gr.Markdown("""
    # 🌊 Mem|8 Wave Memory Explorer
    
    Welcome to 8b.is's memory ocean demonstration! This showcase implements concepts from our Mem|8 
    wave-based memory architecture paper, visualizing how memories propagate and interact like waves 
    in an ocean of consciousness.
    
    > "Memory is not a storage unit, but a living ocean of waves" - Mem|8 Paper
    """)
    
    with gr.Row():
        with gr.Column():
            size_input = gr.Slider(
                minimum=16, 
                maximum=128, 
                value=32, 
                step=16, 
                label="Memory Grid Size"
            )
            operation_input = gr.Radio(
                ["wave_memory", "interference", "resonance"],
                label="Memory Wave Operation",
                value="wave_memory",
                info="Select the type of wave-based memory operation to visualize"
            )
            emotion_input = gr.Slider(
                minimum=-128,
                maximum=127,
                value=0,
                step=1,
                label="Emotional Valence",
                info="Emotional context from negative to positive (-128 to 127)"
            )
            
            with gr.Accordion("Advanced Settings", open=False):
                generate_art = gr.Checkbox(
                    label="Generate Artistic Visualization", 
                    value=True,
                    info="Use Stable Diffusion to create artistic representations"
                )
                seed = gr.Slider(
                    label="Art Generation Seed",
                    minimum=0,
                    maximum=MAX_SEED,
                    step=1,
                    value=42
                )
            
            run_btn = gr.Button("Generate Memory Wave", variant="primary")
        
        with gr.Column():
            output_text = gr.Textbox(label="Wave Analysis", lines=8)
            with gr.Row():
                wave_plot = gr.Plot(label="Wave Pattern")
                art_output = gr.Image(label="Artistic Visualization")
            
    run_btn.click(
        quantum_memory_ops,
        inputs=[size_input, operation_input, emotion_input, generate_art, seed],
        outputs=[output_text, wave_plot, art_output]
    )
    
    gr.Markdown("""
    ### 🧠 Understanding Wave Memory
    
    This demo visualizes three key concepts from our Mem|8 paper:
    1. **Wave Memory**: Memories as propagating waves with emotional modulation
    2. **Interference**: How different memories interact and combine
    3. **Resonance**: Emotional resonance patterns in memory formation
    
    The visualization shows both the mathematical wave patterns and artistic interpretations,
    demonstrating how emotional context affects memory formation and recall.
    
    All computations are accelerated using Hugging Face's Zero GPU technology! 
    """)

demo.launch()