Update app.py
Browse files
app.py
CHANGED
|
@@ -3,26 +3,24 @@ 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 #
|
| 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=
|
| 23 |
"""
|
| 24 |
Creates a dataset of mathematical/spiritual patterns (Mandalas/Energy Orbs)
|
| 25 |
-
instead of using boring
|
| 26 |
"""
|
| 27 |
print(f"Generating {num_images} unique Veda patterns...")
|
| 28 |
data = []
|
|
@@ -46,7 +44,7 @@ def generate_veda_patterns(num_images=1000):
|
|
| 46 |
# Colorize (RGB)
|
| 47 |
img = np.zeros((IMG_SIZE, IMG_SIZE, 3))
|
| 48 |
|
| 49 |
-
# Random
|
| 50 |
r_boost = np.random.rand()
|
| 51 |
g_boost = np.random.rand()
|
| 52 |
b_boost = np.random.rand()
|
|
@@ -62,6 +60,7 @@ def generate_veda_patterns(num_images=1000):
|
|
| 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
|
|
@@ -89,7 +88,6 @@ def build_unet():
|
|
| 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)
|
|
@@ -102,29 +100,32 @@ def build_unet():
|
|
| 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
|
| 110 |
if os.path.exists("veda_art_model.keras"):
|
| 111 |
try:
|
| 112 |
model.load_weights("veda_art_model.keras")
|
| 113 |
-
print("
|
| 114 |
except:
|
| 115 |
-
|
| 116 |
|
| 117 |
# =========================================
|
| 118 |
# 5. TRAINING FUNCTION
|
| 119 |
# =========================================
|
| 120 |
-
def train_model(
|
| 121 |
-
# 1. Generate fresh data
|
| 122 |
-
|
|
|
|
| 123 |
dataset = tf.data.Dataset.from_tensor_slices(x_train).batch(BATCH_SIZE).shuffle(500)
|
| 124 |
|
| 125 |
-
yield "Dataset
|
| 126 |
|
| 127 |
-
|
|
|
|
| 128 |
total_loss = 0
|
| 129 |
steps = 0
|
| 130 |
for batch in dataset:
|
|
@@ -141,10 +142,11 @@ def train_model(epochs):
|
|
| 141 |
total_loss += loss
|
| 142 |
steps += 1
|
| 143 |
|
| 144 |
-
|
| 145 |
model.save_weights("veda_art_model.keras")
|
|
|
|
| 146 |
|
| 147 |
-
yield "Training Done! Go to 'Dream' tab."
|
| 148 |
|
| 149 |
# =========================================
|
| 150 |
# 6. GENERATION FUNCTION
|
|
@@ -184,9 +186,9 @@ def generate_art():
|
|
| 184 |
# =========================================
|
| 185 |
# 7. UI
|
| 186 |
# =========================================
|
| 187 |
-
with gr.Blocks(title="
|
| 188 |
-
gr.Markdown("#
|
| 189 |
-
gr.Markdown("
|
| 190 |
|
| 191 |
with gr.Tab("Dream"):
|
| 192 |
gen_btn = gr.Button("Generate Sacred Pattern", variant="primary")
|
|
@@ -194,9 +196,11 @@ with gr.Blocks(title="Veda Art Engine") as demo:
|
|
| 194 |
gen_btn.click(generate_art, outputs=out_img)
|
| 195 |
|
| 196 |
with gr.Tab("Train"):
|
| 197 |
-
gr.Markdown("Click to generate
|
| 198 |
train_btn = gr.Button("Train AI (5 Epochs)")
|
| 199 |
log = gr.Textbox(label="Status")
|
| 200 |
-
|
|
|
|
|
|
|
| 201 |
|
| 202 |
demo.launch()
|
|
|
|
| 3 |
from tensorflow import keras
|
| 4 |
from tensorflow.keras import layers
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
import os
|
| 7 |
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
# =========================================
|
| 10 |
# 1. SETTINGS
|
| 11 |
# =========================================
|
| 12 |
+
IMG_SIZE = 48 # Small size for CPU speed
|
| 13 |
CHANNELS = 3
|
| 14 |
TIMESTEPS = 200
|
| 15 |
BATCH_SIZE = 32
|
| 16 |
|
| 17 |
# =========================================
|
| 18 |
+
# 2. GENERATE CUSTOM "VEDA" DATASET (MATH ART)
|
| 19 |
# =========================================
|
| 20 |
+
def generate_veda_patterns(num_images=500):
|
| 21 |
"""
|
| 22 |
Creates a dataset of mathematical/spiritual patterns (Mandalas/Energy Orbs)
|
| 23 |
+
instead of using boring photos.
|
| 24 |
"""
|
| 25 |
print(f"Generating {num_images} unique Veda patterns...")
|
| 26 |
data = []
|
|
|
|
| 44 |
# Colorize (RGB)
|
| 45 |
img = np.zeros((IMG_SIZE, IMG_SIZE, 3))
|
| 46 |
|
| 47 |
+
# Random color boosters
|
| 48 |
r_boost = np.random.rand()
|
| 49 |
g_boost = np.random.rand()
|
| 50 |
b_boost = np.random.rand()
|
|
|
|
| 60 |
# =========================================
|
| 61 |
# 3. DIFFUSION MATH
|
| 62 |
# =========================================
|
| 63 |
+
# Cast to float32 immediately to avoid type errors
|
| 64 |
beta_np = np.linspace(0.0001, 0.02, TIMESTEPS)
|
| 65 |
beta = tf.cast(beta_np, dtype=tf.float32)
|
| 66 |
alpha = 1.0 - beta
|
|
|
|
| 88 |
x = layers.Conv2D(32, 3, padding="same", activation="relu")(image_input)
|
| 89 |
|
| 90 |
# Simple addition of time info
|
|
|
|
| 91 |
x = layers.Conv2D(64, 3, strides=2, padding="same", activation="relu")(x) # 24x24
|
| 92 |
x = layers.Conv2D(128, 3, strides=2, padding="same", activation="relu")(x) # 12x12
|
| 93 |
x = layers.Conv2D(256, 3, padding="same", activation="relu")(x)
|
|
|
|
| 100 |
outputs = layers.Conv2D(CHANNELS, 1, padding="same")(x)
|
| 101 |
return keras.Model([image_input, time_input], outputs)
|
| 102 |
|
| 103 |
+
# Initialize Model
|
| 104 |
model = build_unet()
|
| 105 |
optimizer = keras.optimizers.Adam(learning_rate=1e-4)
|
| 106 |
loss_fn = keras.losses.MeanSquaredError()
|
| 107 |
|
| 108 |
+
# Load weights if they exist (Persistence)
|
| 109 |
if os.path.exists("veda_art_model.keras"):
|
| 110 |
try:
|
| 111 |
model.load_weights("veda_art_model.keras")
|
| 112 |
+
print("Previous brain loaded successfully!")
|
| 113 |
except:
|
| 114 |
+
print("Starting fresh brain.")
|
| 115 |
|
| 116 |
# =========================================
|
| 117 |
# 5. TRAINING FUNCTION
|
| 118 |
# =========================================
|
| 119 |
+
def train_model():
|
| 120 |
+
# 1. Generate fresh data on the fly
|
| 121 |
+
yield "Generating Sacred Geometry Data..."
|
| 122 |
+
x_train = generate_veda_patterns(num_images=500)
|
| 123 |
dataset = tf.data.Dataset.from_tensor_slices(x_train).batch(BATCH_SIZE).shuffle(500)
|
| 124 |
|
| 125 |
+
yield "Dataset Ready. Starting Training Loop (5 Epochs)..."
|
| 126 |
|
| 127 |
+
epochs = 5
|
| 128 |
+
for epoch in range(epochs):
|
| 129 |
total_loss = 0
|
| 130 |
steps = 0
|
| 131 |
for batch in dataset:
|
|
|
|
| 142 |
total_loss += loss
|
| 143 |
steps += 1
|
| 144 |
|
| 145 |
+
# Save progress
|
| 146 |
model.save_weights("veda_art_model.keras")
|
| 147 |
+
yield f"Epoch {epoch+1}/{epochs} Complete - Loss: {total_loss/steps:.4f}"
|
| 148 |
|
| 149 |
+
yield "Training Done! Go to 'Dream' tab to generate images."
|
| 150 |
|
| 151 |
# =========================================
|
| 152 |
# 6. GENERATION FUNCTION
|
|
|
|
| 186 |
# =========================================
|
| 187 |
# 7. UI
|
| 188 |
# =========================================
|
| 189 |
+
with gr.Blocks(title="Akasha Art Engine") as demo:
|
| 190 |
+
gr.Markdown("# Akasha Art Engine")
|
| 191 |
+
gr.Markdown("A custom Diffusion model that learns sacred geometry from pure math.")
|
| 192 |
|
| 193 |
with gr.Tab("Dream"):
|
| 194 |
gen_btn = gr.Button("Generate Sacred Pattern", variant="primary")
|
|
|
|
| 196 |
gen_btn.click(generate_art, outputs=out_img)
|
| 197 |
|
| 198 |
with gr.Tab("Train"):
|
| 199 |
+
gr.Markdown("Click below to generate data and train the model.")
|
| 200 |
train_btn = gr.Button("Train AI (5 Epochs)")
|
| 201 |
log = gr.Textbox(label="Status")
|
| 202 |
+
|
| 203 |
+
# This connects the stream properly
|
| 204 |
+
train_btn.click(train_model, outputs=log)
|
| 205 |
|
| 206 |
demo.launch()
|