Spaces:
Sleeping
Sleeping
import gradio as gr | |
import torch | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib import cm | |
import plotly.graph_objects as go | |
from plotly.subplots import make_subplots | |
import random | |
from typing import Tuple, List, Dict, Any, Optional | |
import time | |
import colorsys | |
import math | |
from PIL import Image, ImageDraw, ImageFilter | |
# Try importing Stable Diffusion dependencies | |
try: | |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler | |
STABLE_DIFFUSION_AVAILABLE = True | |
except ImportError: | |
print("Warning: diffusers package not available. Artistic visualization will be disabled.") | |
STABLE_DIFFUSION_AVAILABLE = False | |
# Try importing 3D visualization dependencies | |
try: | |
import plotly.express as px | |
PLOTLY_3D_AVAILABLE = True | |
except ImportError: | |
print("Warning: plotly.express not available. 3D visualization will be limited.") | |
PLOTLY_3D_AVAILABLE = False | |
# Initialize Stable Diffusion only if available | |
pipe = None | |
if STABLE_DIFFUSION_AVAILABLE: | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
model_repo_id = "tensorart/stable-diffusion-3.5-large-TurboX" | |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 | |
try: | |
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) | |
print(f"✅ Stable Diffusion initialized on {device}") | |
except Exception as e: | |
print(f"⚠️ Could not initialize Stable Diffusion: {e}") | |
STABLE_DIFFUSION_AVAILABLE = False | |
# Constants | |
MAX_SEED = np.iinfo(np.int32).max | |
DEFAULT_GRID_SIZE = 64 | |
WAVE_TYPES = ["sine", "cosine", "gaussian", "square"] | |
MEMORY_OPERATIONS = [ | |
"wave_memory", | |
"interference", | |
"resonance", | |
"hot_tub_mode", | |
"emotional_resonance", | |
"pattern_completion" | |
] | |
# Color palettes for different emotional states | |
COLOR_PALETTES = { | |
"positive": ["#FF5E5B", "#D8D8F6", "#E8AA14", "#32E875", "#3C91E6"], | |
"neutral": ["#FAFFFD", "#A1CDF4", "#7D83FF", "#3A3042", "#080708"], | |
"negative": ["#1B1B1E", "#373F51", "#58A4B0", "#A9BCD0", "#D8DBE2"] | |
} | |
class EmotionalContext: | |
"""Implements Mem|8's emotional context structure""" | |
def __init__(self, device="cuda" if torch.cuda.is_available() else "cpu"): | |
self.device = device | |
self.valence = torch.zeros(1).to(device) # -128 to 127: negative to positive | |
self.arousal = torch.zeros(1).to(device) # 0 to 255: intensity level | |
self.context = torch.zeros(1).to(device) # Contextual flags | |
self.safety = torch.ones(1).to(device) * 100 # Safety level (0-100) | |
# Memory blanket parameters | |
self.resonance_freq = torch.tensor(1.0).to(device) | |
self.filter_strength = torch.tensor(0.5).to(device) | |
# Hot tub mode parameters | |
self.hot_tub_active = False | |
self.hot_tub_temperature = torch.tensor(37.0).to(device) # Default comfortable temperature | |
self.hot_tub_participants = [] | |
def update(self, valence: float, arousal: Optional[float] = None): | |
"""Update emotional context based on valence and arousal""" | |
self.valence = torch.tensor([valence]).to(self.device) | |
# If arousal not provided, calculate it based on valence intensity | |
if arousal is None: | |
self.arousal = torch.abs(torch.tensor([valence * 2])).to(self.device) | |
else: | |
self.arousal = torch.tensor([arousal]).to(self.device) | |
# Update resonance frequency based on emotional state | |
self.resonance_freq = 1.0 + torch.sigmoid(self.valence/128) | |
# Update filter strength based on arousal | |
self.filter_strength = torch.sigmoid(self.arousal/128) | |
return self | |
def get_color_palette(self): | |
"""Get color palette based on emotional valence""" | |
if self.valence.item() > 20: | |
return COLOR_PALETTES["positive"] | |
elif self.valence.item() < -20: | |
return COLOR_PALETTES["negative"] | |
else: | |
return COLOR_PALETTES["neutral"] | |
def activate_hot_tub(self, temperature: float = 37.0): | |
"""Activate hot tub mode with specified temperature""" | |
self.hot_tub_active = True | |
self.hot_tub_temperature = torch.tensor(temperature).to(self.device) | |
return self | |
def deactivate_hot_tub(self): | |
"""Deactivate hot tub mode""" | |
self.hot_tub_active = False | |
self.hot_tub_participants = [] | |
return self | |
def add_hot_tub_participant(self, participant: str): | |
"""Add participant to hot tub session""" | |
if self.hot_tub_active and participant not in self.hot_tub_participants: | |
self.hot_tub_participants.append(participant) | |
return self | |
def get_state_dict(self) -> Dict[str, Any]: | |
"""Get emotional context as dictionary for display""" | |
return { | |
"valence": self.valence.item(), | |
"arousal": self.arousal.item(), | |
"resonance_frequency": self.resonance_freq.item(), | |
"filter_strength": self.filter_strength.item(), | |
"hot_tub_active": self.hot_tub_active, | |
"hot_tub_temperature": self.hot_tub_temperature.item() if self.hot_tub_active else None, | |
"hot_tub_participants": self.hot_tub_participants if self.hot_tub_active else [], | |
"safety_level": self.safety.item() | |
} | |
class WaveProcessor: | |
"""Processes wave-based memory patterns""" | |
def __init__(self, device="cuda" if torch.cuda.is_available() else "cpu"): | |
self.device = device | |
def create_wave_pattern(self, | |
size: int, | |
frequency: float, | |
amplitude: float, | |
wave_type: str = "sine") -> torch.Tensor: | |
"""Create a wave pattern as described in Mem|8 paper""" | |
t = torch.linspace(0, 2*np.pi, size).to(self.device) | |
x = torch.linspace(0, 2*np.pi, size).to(self.device) | |
T, X = torch.meshgrid(t, x, indexing='ij') | |
if wave_type == "sine": | |
return amplitude * torch.sin(frequency * T + X) | |
elif wave_type == "cosine": | |
return amplitude * torch.cos(frequency * T + X) | |
elif wave_type == "gaussian": | |
# Create a Gaussian wave pattern | |
sigma = size / (4 * frequency) | |
mu_t = size / 2 | |
mu_x = size / 2 | |
gauss_t = torch.exp(-((t - mu_t) ** 2) / (2 * sigma ** 2)) | |
gauss_x = torch.exp(-((x - mu_x) ** 2) / (2 * sigma ** 2)) | |
G_T, G_X = torch.meshgrid(gauss_t, gauss_x, indexing='ij') | |
return amplitude * G_T * G_X | |
elif wave_type == "square": | |
# Create a square wave pattern | |
square_t = torch.sign(torch.sin(frequency * t)) | |
square_x = torch.sign(torch.sin(frequency * x)) | |
S_T, S_X = torch.meshgrid(square_t, square_x, indexing='ij') | |
return amplitude * S_T * S_X | |
else: | |
# Default to sine wave | |
return amplitude * torch.sin(frequency * T + X) | |
def apply_emotional_modulation(self, | |
wave: torch.Tensor, | |
emotion: EmotionalContext) -> torch.Tensor: | |
"""Apply emotional modulation to wave pattern""" | |
# Modulate wave based on emotional valence | |
emotional_mod = torch.exp(emotion.valence/128 * wave) | |
return wave * emotional_mod | |
def create_interference_pattern(self, | |
wave1: torch.Tensor, | |
wave2: torch.Tensor, | |
emotion: EmotionalContext) -> torch.Tensor: | |
"""Create interference between two wave patterns""" | |
interference = wave1 + wave2 | |
# Weight by emotional valence | |
emotional_weight = torch.sigmoid(emotion.valence/128) * interference | |
return emotional_weight | |
def create_resonance_pattern(self, | |
base_wave: torch.Tensor, | |
emotion: EmotionalContext) -> torch.Tensor: | |
"""Create resonance pattern based on emotional state""" | |
resonant_wave = self.create_wave_pattern( | |
base_wave.shape[0], | |
emotion.resonance_freq.item(), | |
1.0 | |
) | |
resonance = base_wave * resonant_wave | |
return resonance | |
def apply_memory_blanket(self, | |
wave: torch.Tensor, | |
emotion: EmotionalContext) -> torch.Tensor: | |
"""Apply memory blanket filtering as described in the paper""" | |
# Create a filter based on wave amplitude and emotional state | |
wave_amplitude = torch.abs(wave) | |
importance_threshold = emotion.filter_strength * wave_amplitude.mean() | |
# Apply the filter - keep only significant waves | |
filtered_wave = wave * (wave_amplitude > importance_threshold).float() | |
return filtered_wave | |
def create_hot_tub_pattern(self, | |
size: int, | |
emotion: EmotionalContext) -> torch.Tensor: | |
"""Create a hot tub pattern for safe exploration""" | |
if not emotion.hot_tub_active: | |
return torch.zeros((size, size)).to(self.device) | |
# Create base wave pattern | |
base_wave = self.create_wave_pattern(size, 1.0, 1.0, "sine") | |
# Modulate based on hot tub temperature | |
temp_factor = emotion.hot_tub_temperature / 50.0 # Normalize to 0-1 range | |
temp_wave = self.create_wave_pattern(size, temp_factor.item(), 0.5, "gaussian") | |
# Add ripples for each participant | |
participant_count = len(emotion.hot_tub_participants) | |
if participant_count > 0: | |
ripple_wave = self.create_wave_pattern( | |
size, | |
2.0 + participant_count * 0.5, | |
0.3, | |
"gaussian" | |
) | |
hot_tub_pattern = base_wave + temp_wave + ripple_wave | |
else: | |
hot_tub_pattern = base_wave + temp_wave | |
# Apply safety modulation | |
safety_factor = emotion.safety / 100.0 | |
return hot_tub_pattern * safety_factor | |
def create_pattern_completion(self, | |
size: int, | |
emotion: EmotionalContext, | |
completion_ratio: float = 0.5) -> Tuple[torch.Tensor, torch.Tensor]: | |
"""Create a pattern completion demonstration""" | |
# Create original pattern | |
original = self.create_wave_pattern(size, 2.0, 1.0) | |
# Create mask for incomplete pattern (randomly remove portions) | |
mask = torch.rand(size, size).to(self.device) > completion_ratio | |
incomplete = original * mask | |
# Apply emotional context to reconstruction | |
emotional_weight = torch.sigmoid(emotion.valence/128) | |
# Simple reconstruction algorithm (in real system would be more sophisticated) | |
# Here we're just doing a simple interpolation | |
kernel_size = 3 | |
padding = kernel_size // 2 | |
# Create a kernel for interpolation | |
kernel = torch.ones(1, 1, kernel_size, kernel_size).to(self.device) / (kernel_size ** 2) | |
# Reshape for convolution | |
incomplete_reshaped = incomplete.reshape(1, 1, size, size) | |
# Apply convolution for interpolation | |
with torch.no_grad(): | |
reconstructed = torch.nn.functional.conv2d( | |
incomplete_reshaped, | |
kernel, | |
padding=padding | |
).reshape(size, size) | |
# Blend original where mask exists | |
reconstructed = torch.where(mask, reconstructed, original) | |
# Apply emotional modulation | |
reconstructed = reconstructed * (0.5 + emotional_weight * 0.5) | |
return incomplete, reconstructed | |
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, ", | |
"hot_tub_mode": "a safe space for exploring memories, like a warm therapeutic pool, ", | |
"emotional_resonance": "emotions as colorful waves interacting with memory patterns, ", | |
"pattern_completion": "fragmented memories being reconstructed into complete 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, " | |
# Add more specific styling based on operation | |
if operation == "hot_tub_mode": | |
style += "warm colors, therapeutic atmosphere, " | |
elif operation == "emotional_resonance": | |
style += "vibrant colors, emotional energy visualization, " | |
elif operation == "pattern_completion": | |
style += "fragmented to whole transition, reconstruction, " | |
prompt = f"{base_prompts[operation]}{emotion_desc}, {style} ethereal, dreamlike quality" | |
return prompt | |
def create_wave_visualization(wave_data: np.ndarray, emotion: EmotionalContext) -> go.Figure: | |
"""Create an interactive 3D visualization of wave data""" | |
# Get dimensions | |
n, m = wave_data.shape | |
# Create coordinate grids | |
x = np.linspace(0, 1, m) | |
y = np.linspace(0, 1, n) | |
X, Y = np.meshgrid(x, y) | |
# Get color palette based on emotional state | |
colors = emotion.get_color_palette() | |
colorscale = [[0, colors[0]], | |
[0.25, colors[1]], | |
[0.5, colors[2]], | |
[0.75, colors[3]], | |
[1, colors[4]]] | |
# Create 3D surface plot | |
fig = go.Figure(data=[go.Surface( | |
z=wave_data, | |
x=X, | |
y=Y, | |
colorscale=colorscale, | |
lighting=dict( | |
ambient=0.6, | |
diffuse=0.8, | |
fresnel=0.2, | |
roughness=0.5, | |
specular=1.0 | |
), | |
contours={ | |
"z": {"show": True, "start": -2, "end": 2, "size": 0.1, "color":"white"} | |
} | |
)]) | |
# Update layout | |
fig.update_layout( | |
title=dict( | |
text="Memory Wave Visualization", | |
font=dict(size=24, color="#333333") | |
), | |
scene=dict( | |
xaxis_title="Space", | |
yaxis_title="Time", | |
zaxis_title="Amplitude", | |
aspectratio=dict(x=1, y=1, z=0.8), | |
camera=dict( | |
eye=dict(x=1.5, y=1.5, z=1.2) | |
) | |
), | |
margin=dict(l=0, r=0, b=0, t=30), | |
template="plotly_white" | |
) | |
return fig | |
def create_2d_comparison(wave1: np.ndarray, wave2: np.ndarray, | |
title1: str, title2: str, | |
emotion: EmotionalContext) -> go.Figure: | |
"""Create a side-by-side comparison of two wave patterns""" | |
# Get color palette | |
colors = emotion.get_color_palette() | |
# Create subplots | |
fig = make_subplots( | |
rows=1, cols=2, | |
subplot_titles=(title1, title2), | |
specs=[[{"type": "heatmap"}, {"type": "heatmap"}]] | |
) | |
# Add heatmaps | |
fig.add_trace( | |
go.Heatmap( | |
z=wave1, | |
colorscale=[[0, colors[0]], [1, colors[-1]]], | |
showscale=False | |
), | |
row=1, col=1 | |
) | |
fig.add_trace( | |
go.Heatmap( | |
z=wave2, | |
colorscale=[[0, colors[0]], [1, colors[-1]]], | |
showscale=True | |
), | |
row=1, col=2 | |
) | |
# Update layout | |
fig.update_layout( | |
title_text="Memory Pattern Comparison", | |
height=500, | |
template="plotly_white" | |
) | |
return fig | |
def create_artistic_visualization(prompt: str, seed: int) -> Optional[Image.Image]: | |
"""Create artistic visualization using Stable Diffusion""" | |
if not STABLE_DIFFUSION_AVAILABLE or pipe is None: | |
return None | |
try: | |
generator = torch.Generator().manual_seed(seed) | |
image = 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] | |
return image | |
except Exception as e: | |
print(f"Error generating artistic visualization: {e}") | |
return None | |
def create_emotional_wave_animation(size: int, emotion: EmotionalContext) -> Image.Image: | |
"""Create an animated-like visualization of emotional waves""" | |
# Create a blank image | |
width, height = size * 10, size * 10 | |
image = Image.new('RGBA', (width, height), (255, 255, 255, 0)) | |
draw = ImageDraw.Draw(image) | |
# Get color palette | |
colors = emotion.get_color_palette() | |
# Calculate wave parameters based on emotional state | |
valence = emotion.valence.item() | |
arousal = emotion.arousal.item() | |
# Normalize to 0-1 range | |
valence_norm = (valence + 128) / 255 | |
arousal_norm = arousal / 255 | |
# Create multiple wave layers | |
for i in range(5): | |
# Calculate wave parameters | |
amplitude = 50 + i * 20 * arousal_norm | |
frequency = 0.01 + i * 0.005 * (1 + valence_norm) | |
phase = i * math.pi / 5 | |
# Select color | |
color = colors[i % len(colors)] | |
# Draw wave | |
points = [] | |
for x in range(width): | |
# Calculate y position with multiple sine waves | |
y = height/2 + amplitude * math.sin(frequency * x + phase) | |
y += amplitude/2 * math.sin(frequency * 2 * x + phase) | |
points.append((x, y)) | |
# Draw wave with varying thickness | |
for j in range(3): | |
thickness = 5 - j | |
draw.line(points, fill=color, width=thickness) | |
# Apply blur for smoother appearance | |
image = image.filter(ImageFilter.GaussianBlur(radius=3)) | |
return image | |
def quantum_memory_ops( | |
input_size: int, | |
operation: str, | |
emotion_valence: float, | |
emotion_arousal: float = None, | |
wave_type: str = "sine", | |
hot_tub_temp: float = 37.0, | |
hot_tub_participants: str = "", | |
generate_art: bool = True, | |
seed: int = 42 | |
) -> Tuple[str, go.Figure, go.Figure, Image.Image]: | |
"""Perform quantum-inspired memory operations using Mem|8 concepts.""" | |
# Initialize components | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
emotion = EmotionalContext(device) | |
emotion.update(emotion_valence, emotion_arousal) | |
wave_processor = WaveProcessor(device) | |
# Process hot tub participants if provided | |
if hot_tub_participants: | |
participants = [p.strip() for p in hot_tub_participants.split(',')] | |
emotion.activate_hot_tub(hot_tub_temp) | |
for participant in participants: | |
emotion.add_hot_tub_participant(participant) | |
results = [] | |
wave_viz = None | |
comparison_viz = None | |
art_viz = None | |
# Add header with emotional context | |
results.append(f"🌊 Mem|8 Wave Memory Analysis 🌊") | |
results.append(f"Operation: {operation}") | |
results.append(f"Wave Type: {wave_type}") | |
results.append(f"Grid Size: {input_size}x{input_size}") | |
results.append("") | |
if operation == "wave_memory": | |
# Create memory wave pattern (M = A·exp(iωt-kx)·D·E) | |
wave = wave_processor.create_wave_pattern(input_size, 2.0, 1.0, wave_type) | |