Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,202 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from tensorflow import keras
|
| 4 |
+
from tensorflow.keras import layers
|
| 5 |
import numpy as np
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import os
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import math
|
| 10 |
+
|
| 11 |
+
# =========================================
|
| 12 |
+
# 1. SETTINGS
|
| 13 |
+
# =========================================
|
| 14 |
+
IMG_SIZE = 48 # Slightly larger than CIFAR for better detail
|
| 15 |
+
CHANNELS = 3
|
| 16 |
+
TIMESTEPS = 200
|
| 17 |
+
BATCH_SIZE = 32
|
| 18 |
+
|
| 19 |
+
# =========================================
|
| 20 |
+
# 2. GENERATE CUSTOM "VEDA" DATASET
|
| 21 |
+
# =========================================
|
| 22 |
+
def generate_veda_patterns(num_images=1000):
|
| 23 |
+
"""
|
| 24 |
+
Creates a dataset of mathematical/spiritual patterns (Mandalas/Energy Orbs)
|
| 25 |
+
instead of using boring CIFAR-10 images.
|
| 26 |
+
"""
|
| 27 |
+
print(f"Generating {num_images} unique Veda patterns...")
|
| 28 |
+
data = []
|
| 29 |
+
|
| 30 |
+
for _ in range(num_images):
|
| 31 |
+
# Create a grid
|
| 32 |
+
x = np.linspace(-1, 1, IMG_SIZE)
|
| 33 |
+
y = np.linspace(-1, 1, IMG_SIZE)
|
| 34 |
+
X, Y = np.meshgrid(x, y)
|
| 35 |
+
|
| 36 |
+
# Random parameters for the pattern
|
| 37 |
+
freq = np.random.uniform(2, 10)
|
| 38 |
+
phase = np.random.uniform(0, np.pi)
|
| 39 |
+
center_x = np.random.uniform(-0.5, 0.5)
|
| 40 |
+
center_y = np.random.uniform(-0.5, 0.5)
|
| 41 |
+
|
| 42 |
+
# Math: Radial Gradient + Sine waves (Ripple effect)
|
| 43 |
+
R = np.sqrt((X - center_x)**2 + (Y - center_y)**2)
|
| 44 |
+
pattern = np.sin(freq * R - phase)
|
| 45 |
+
|
| 46 |
+
# Colorize (RGB)
|
| 47 |
+
img = np.zeros((IMG_SIZE, IMG_SIZE, 3))
|
| 48 |
+
|
| 49 |
+
# Random colors
|
| 50 |
+
r_boost = np.random.rand()
|
| 51 |
+
g_boost = np.random.rand()
|
| 52 |
+
b_boost = np.random.rand()
|
| 53 |
+
|
| 54 |
+
img[:, :, 0] = (pattern + 1) / 2 * r_boost # Red
|
| 55 |
+
img[:, :, 1] = (pattern + 1) / 2 * g_boost # Green
|
| 56 |
+
img[:, :, 2] = (pattern + 1) / 2 * b_boost # Blue
|
| 57 |
+
|
| 58 |
+
data.append(img)
|
| 59 |
+
|
| 60 |
+
return np.array(data).astype("float32")
|
| 61 |
+
|
| 62 |
+
# =========================================
|
| 63 |
+
# 3. DIFFUSION MATH
|
| 64 |
+
# =========================================
|
| 65 |
+
beta_np = np.linspace(0.0001, 0.02, TIMESTEPS)
|
| 66 |
+
beta = tf.cast(beta_np, dtype=tf.float32)
|
| 67 |
+
alpha = 1.0 - beta
|
| 68 |
+
alpha_bar = tf.math.cumprod(alpha, axis=0)
|
| 69 |
+
|
| 70 |
+
def forward_noise(x_0, t):
|
| 71 |
+
n = tf.shape(x_0)[0]
|
| 72 |
+
a_bar = tf.gather(alpha_bar, t)
|
| 73 |
+
a_bar = tf.reshape(a_bar, [n, 1, 1, 1])
|
| 74 |
+
noise = tf.random.normal(shape=tf.shape(x_0), dtype=tf.float32)
|
| 75 |
+
return (tf.sqrt(a_bar) * x_0) + (tf.sqrt(1.0 - a_bar) * noise), noise
|
| 76 |
+
|
| 77 |
+
# =========================================
|
| 78 |
+
# 4. THE BRAIN (U-NET)
|
| 79 |
+
# =========================================
|
| 80 |
+
def build_unet():
|
| 81 |
+
image_input = layers.Input(shape=(IMG_SIZE, IMG_SIZE, CHANNELS))
|
| 82 |
+
time_input = layers.Input(shape=(1,))
|
| 83 |
+
|
| 84 |
+
# Embed Time
|
| 85 |
+
t = layers.Dense(IMG_SIZE, activation="relu")(time_input)
|
| 86 |
+
t = layers.Reshape((1, 1, IMG_SIZE))(t)
|
| 87 |
+
|
| 88 |
+
# In
|
| 89 |
+
x = layers.Conv2D(32, 3, padding="same", activation="relu")(image_input)
|
| 90 |
+
|
| 91 |
+
# Simple addition of time info
|
| 92 |
+
# Note: We keep architecture simple for CPU speed
|
| 93 |
+
x = layers.Conv2D(64, 3, strides=2, padding="same", activation="relu")(x) # 24x24
|
| 94 |
+
x = layers.Conv2D(128, 3, strides=2, padding="same", activation="relu")(x) # 12x12
|
| 95 |
+
x = layers.Conv2D(256, 3, padding="same", activation="relu")(x)
|
| 96 |
+
|
| 97 |
+
# Decode
|
| 98 |
+
x = layers.Conv2DTranspose(128, 3, strides=2, padding="same", activation="relu")(x)
|
| 99 |
+
x = layers.Conv2DTranspose(64, 3, strides=2, padding="same", activation="relu")(x)
|
| 100 |
+
x = layers.Conv2D(32, 3, padding="same", activation="relu")(x)
|
| 101 |
+
|
| 102 |
+
outputs = layers.Conv2D(CHANNELS, 1, padding="same")(x)
|
| 103 |
+
return keras.Model([image_input, time_input], outputs)
|
| 104 |
+
|
| 105 |
+
model = build_unet()
|
| 106 |
+
optimizer = keras.optimizers.Adam(learning_rate=1e-4)
|
| 107 |
+
loss_fn = keras.losses.MeanSquaredError()
|
| 108 |
+
|
| 109 |
+
# Load weights if trained
|
| 110 |
+
if os.path.exists("veda_art_model.keras"):
|
| 111 |
+
try:
|
| 112 |
+
model.load_weights("veda_art_model.keras")
|
| 113 |
+
print("Brain Loaded!")
|
| 114 |
+
except:
|
| 115 |
+
pass
|
| 116 |
+
|
| 117 |
+
# =========================================
|
| 118 |
+
# 5. TRAINING FUNCTION
|
| 119 |
+
# =========================================
|
| 120 |
+
def train_model(epochs):
|
| 121 |
+
# 1. Generate fresh data
|
| 122 |
+
x_train = generate_veda_patterns(num_images=500) # 500 images is enough for patterns
|
| 123 |
+
dataset = tf.data.Dataset.from_tensor_slices(x_train).batch(BATCH_SIZE).shuffle(500)
|
| 124 |
+
|
| 125 |
+
yield "Dataset Generated (Mathematical Patterns). Starting Training..."
|
| 126 |
+
|
| 127 |
+
for epoch in range(int(epochs)):
|
| 128 |
+
total_loss = 0
|
| 129 |
+
steps = 0
|
| 130 |
+
for batch in dataset:
|
| 131 |
+
batch_size = tf.shape(batch)[0]
|
| 132 |
+
t = tf.random.uniform([batch_size], minval=0, maxval=TIMESTEPS, dtype=tf.int32)
|
| 133 |
+
noisy_images, noise = forward_noise(batch, t)
|
| 134 |
+
|
| 135 |
+
with tf.GradientTape() as tape:
|
| 136 |
+
pred_noise = model([noisy_images, t], training=True)
|
| 137 |
+
loss = loss_fn(noise, pred_noise)
|
| 138 |
+
|
| 139 |
+
grads = tape.gradient(loss, model.trainable_variables)
|
| 140 |
+
optimizer.apply_gradients(zip(grads, model.trainable_variables))
|
| 141 |
+
total_loss += loss
|
| 142 |
+
steps += 1
|
| 143 |
+
|
| 144 |
+
yield f"Epoch {epoch+1}/{epochs} - Loss: {total_loss/steps:.4f}"
|
| 145 |
+
model.save_weights("veda_art_model.keras")
|
| 146 |
+
|
| 147 |
+
yield "Training Done! Go to 'Dream' tab."
|
| 148 |
+
|
| 149 |
+
# =========================================
|
| 150 |
+
# 6. GENERATION FUNCTION
|
| 151 |
+
# =========================================
|
| 152 |
+
def generate_art():
|
| 153 |
+
# Start with static
|
| 154 |
+
img = tf.random.normal((1, IMG_SIZE, IMG_SIZE, CHANNELS), dtype=tf.float32)
|
| 155 |
+
|
| 156 |
+
# Reverse Diffusion
|
| 157 |
+
for i in range(TIMESTEPS - 1, 0, -1):
|
| 158 |
+
t = tf.fill([1], i)
|
| 159 |
+
pred_noise = model([img, t], training=False)
|
| 160 |
+
|
| 161 |
+
alpha_t = tf.gather(alpha, i)
|
| 162 |
+
alpha_bar_t = tf.gather(alpha_bar, i)
|
| 163 |
+
beta_t = tf.gather(beta, i)
|
| 164 |
+
|
| 165 |
+
term1 = 1.0 / tf.sqrt(alpha_t)
|
| 166 |
+
term2 = (1.0 - alpha_t) / tf.sqrt(1.0 - alpha_bar_t)
|
| 167 |
+
|
| 168 |
+
img = term1 * (img - term2 * pred_noise)
|
| 169 |
+
|
| 170 |
+
if i > 1:
|
| 171 |
+
z = tf.random.normal((1, IMG_SIZE, IMG_SIZE, CHANNELS), dtype=tf.float32)
|
| 172 |
+
img = img + (tf.sqrt(beta_t) * z)
|
| 173 |
+
|
| 174 |
+
# Normalize for display
|
| 175 |
+
img = tf.clip_by_value(img, 0.0, 1.0)
|
| 176 |
+
img = img[0].numpy()
|
| 177 |
+
img = (img * 255).astype(np.uint8)
|
| 178 |
+
|
| 179 |
+
# Resize to be big and pretty
|
| 180 |
+
pil_img = Image.fromarray(img)
|
| 181 |
+
pil_img = pil_img.resize((300, 300), Image.BILINEAR)
|
| 182 |
+
return pil_img
|
| 183 |
+
|
| 184 |
+
# =========================================
|
| 185 |
+
# 7. UI
|
| 186 |
+
# =========================================
|
| 187 |
+
with gr.Blocks(title="Veda Art Engine") as demo:
|
| 188 |
+
gr.Markdown("# Veda Art Engine")
|
| 189 |
+
gr.Markdown("This AI generates its own training data using math, then learns to dream up new patterns.")
|
| 190 |
+
|
| 191 |
+
with gr.Tab("Dream"):
|
| 192 |
+
gen_btn = gr.Button("Generate Sacred Pattern", variant="primary")
|
| 193 |
+
out_img = gr.Image(label="Veda Dream")
|
| 194 |
+
gen_btn.click(generate_art, outputs=out_img)
|
| 195 |
+
|
| 196 |
+
with gr.Tab("Train"):
|
| 197 |
+
gr.Markdown("Click to generate a dataset of mathematical mandalas and train the AI on them.")
|
| 198 |
+
train_btn = gr.Button("Train AI (5 Epochs)")
|
| 199 |
+
log = gr.Textbox(label="Status")
|
| 200 |
+
train_btn.click(lambda: train_model(5), outputs=log)
|
| 201 |
+
|
| 202 |
+
demo.launch()
|