Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,12 @@ import numpy as np
|
|
3 |
import random
|
4 |
from diffusers import DiffusionPipeline
|
5 |
import torch
|
6 |
-
from
|
7 |
-
import matplotlib.pyplot as plt
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
|
|
11 |
if torch.cuda.is_available():
|
12 |
-
torch.cuda.max_memory_allocated(device=device)
|
13 |
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
14 |
pipe.enable_xformers_memory_efficient_attention()
|
15 |
pipe = pipe.to(device)
|
@@ -20,38 +19,33 @@ else:
|
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
MAX_IMAGE_SIZE = 1024
|
22 |
|
23 |
-
#
|
24 |
-
def
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
# Normalize magnitude spectrum to 0-255 for visualization
|
39 |
-
magnitude_spectrum = np.interp(magnitude_spectrum, (magnitude_spectrum.min(), magnitude_spectrum.max()), (0, 255))
|
40 |
-
|
41 |
-
# Convert back to image
|
42 |
-
fft_image_pil = Image.fromarray(magnitude_spectrum.astype(np.uint8))
|
43 |
-
|
44 |
-
return fft_image_pil
|
45 |
|
46 |
def infer(prompt_part1, color, dress_type, design, prompt_part5, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
47 |
prompt = f"{prompt_part1} {color} colored plain {dress_type} with {design} design, {prompt_part5}"
|
48 |
|
49 |
if randomize_seed:
|
50 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
|
51 |
|
52 |
generator = torch.Generator().manual_seed(seed)
|
53 |
|
54 |
-
#
|
55 |
image = pipe(
|
56 |
prompt=prompt,
|
57 |
negative_prompt=negative_prompt,
|
@@ -60,12 +54,13 @@ def infer(prompt_part1, color, dress_type, design, prompt_part5, negative_prompt
|
|
60 |
width=width,
|
61 |
height=height,
|
62 |
generator=generator
|
63 |
-
).images[0]
|
64 |
-
|
65 |
-
# Apply
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
69 |
|
70 |
examples = [
|
71 |
"red, t-shirt, yellow stripes",
|
@@ -89,7 +84,7 @@ with gr.Blocks(css=css) as demo:
|
|
89 |
|
90 |
with gr.Column(elem_id="col-container"):
|
91 |
gr.Markdown(f"""
|
92 |
-
# Text-to-Image Gradio Template
|
93 |
Currently running on {power_device}.
|
94 |
""")
|
95 |
|
|
|
3 |
import random
|
4 |
from diffusers import DiffusionPipeline
|
5 |
import torch
|
6 |
+
from torch.fft import fftn, ifftn # For Fourier transforms
|
|
|
7 |
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
|
10 |
+
# Load the model
|
11 |
if torch.cuda.is_available():
|
|
|
12 |
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
13 |
pipe.enable_xformers_memory_efficient_attention()
|
14 |
pipe = pipe.to(device)
|
|
|
19 |
MAX_SEED = np.iinfo(np.int32).max
|
20 |
MAX_IMAGE_SIZE = 1024
|
21 |
|
22 |
+
# Group-theory-based seed reduction (example: cyclic group on mod N)
|
23 |
+
def reduce_seeds(seed):
|
24 |
+
group_size = 100 # Cyclic group of size 100
|
25 |
+
return seed % group_size
|
26 |
+
|
27 |
+
# Fourier-based optimization
|
28 |
+
def fft_convolution(image):
|
29 |
+
# Convert to frequency domain using FFT
|
30 |
+
freq_image = fftn(image)
|
31 |
+
# Apply some transformation in the frequency domain (e.g., filtering, smoothing)
|
32 |
+
# This is a placeholder; you can implement any frequency domain operation here
|
33 |
+
transformed_freq_image = freq_image * np.exp(-np.abs(freq_image)) # Example operation
|
34 |
+
# Convert back to spatial domain using inverse FFT
|
35 |
+
transformed_image = ifftn(transformed_freq_image).real
|
36 |
+
return transformed_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
def infer(prompt_part1, color, dress_type, design, prompt_part5, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
39 |
prompt = f"{prompt_part1} {color} colored plain {dress_type} with {design} design, {prompt_part5}"
|
40 |
|
41 |
if randomize_seed:
|
42 |
seed = random.randint(0, MAX_SEED)
|
43 |
+
else:
|
44 |
+
seed = reduce_seeds(seed) # Apply symmetry-based seed reduction
|
45 |
|
46 |
generator = torch.Generator().manual_seed(seed)
|
47 |
|
48 |
+
# Run the diffusion model
|
49 |
image = pipe(
|
50 |
prompt=prompt,
|
51 |
negative_prompt=negative_prompt,
|
|
|
54 |
width=width,
|
55 |
height=height,
|
56 |
generator=generator
|
57 |
+
).images[0]
|
58 |
+
|
59 |
+
# Apply Fourier-based convolution optimization
|
60 |
+
image = np.array(image) # Convert to numpy array
|
61 |
+
optimized_image = fft_convolution(image) # Apply Fourier convolution
|
62 |
+
|
63 |
+
return optimized_image
|
64 |
|
65 |
examples = [
|
66 |
"red, t-shirt, yellow stripes",
|
|
|
84 |
|
85 |
with gr.Column(elem_id="col-container"):
|
86 |
gr.Markdown(f"""
|
87 |
+
# Text-to-Image Gradio Template
|
88 |
Currently running on {power_device}.
|
89 |
""")
|
90 |
|