vedaco commited on
Commit
e7cdc87
·
verified ·
1 Parent(s): bc7c756

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +200 -152
app.py CHANGED
@@ -1,154 +1,202 @@
1
  import gradio as gr
 
 
 
2
  import numpy as np
3
- import random
4
-
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
- import torch
8
-
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
-
20
- MAX_SEED = np.iinfo(np.int32).max
21
- MAX_IMAGE_SIZE = 1024
22
-
23
-
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
- def infer(
26
- prompt,
27
- negative_prompt,
28
- seed,
29
- randomize_seed,
30
- width,
31
- height,
32
- guidance_scale,
33
- num_inference_steps,
34
- progress=gr.Progress(track_tqdm=True),
35
- ):
36
- if randomize_seed:
37
- seed = random.randint(0, MAX_SEED)
38
-
39
- generator = torch.Generator().manual_seed(seed)
40
-
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
-
51
- return image, seed
52
-
53
-
54
- examples = [
55
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
- "An astronaut riding a green horse",
57
- "A delicious ceviche cheesecake slice",
58
- ]
59
-
60
- css = """
61
- #col-container {
62
- margin: 0 auto;
63
- max-width: 640px;
64
- }
65
- """
66
-
67
- with gr.Blocks(css=css) as demo:
68
- with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
70
-
71
- with gr.Row():
72
- prompt = gr.Text(
73
- label="Prompt",
74
- show_label=False,
75
- max_lines=1,
76
- placeholder="Enter your prompt",
77
- container=False,
78
- )
79
-
80
- run_button = gr.Button("Run", scale=0, variant="primary")
81
-
82
- result = gr.Image(label="Result", show_label=False)
83
-
84
- with gr.Accordion("Advanced Settings", open=False):
85
- negative_prompt = gr.Text(
86
- label="Negative prompt",
87
- max_lines=1,
88
- placeholder="Enter a negative prompt",
89
- visible=False,
90
- )
91
-
92
- seed = gr.Slider(
93
- label="Seed",
94
- minimum=0,
95
- maximum=MAX_SEED,
96
- step=1,
97
- value=0,
98
- )
99
-
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
-
102
- with gr.Row():
103
- width = gr.Slider(
104
- label="Width",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=1024, # Replace with defaults that work for your model
109
- )
110
-
111
- height = gr.Slider(
112
- label="Height",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=1024, # Replace with defaults that work for your model
117
- )
118
-
119
- with gr.Row():
120
- guidance_scale = gr.Slider(
121
- label="Guidance scale",
122
- minimum=0.0,
123
- maximum=10.0,
124
- step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
- )
127
-
128
- num_inference_steps = gr.Slider(
129
- label="Number of inference steps",
130
- minimum=1,
131
- maximum=50,
132
- step=1,
133
- value=2, # Replace with defaults that work for your model
134
- )
135
-
136
- gr.Examples(examples=examples, inputs=[prompt])
137
- gr.on(
138
- triggers=[run_button.click, prompt.submit],
139
- fn=infer,
140
- inputs=[
141
- prompt,
142
- negative_prompt,
143
- seed,
144
- randomize_seed,
145
- width,
146
- height,
147
- guidance_scale,
148
- num_inference_steps,
149
- ],
150
- outputs=[result, seed],
151
- )
152
-
153
- if __name__ == "__main__":
154
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()