gaur3009 commited on
Commit
39758aa
1 Parent(s): 26bfdaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -33
app.py CHANGED
@@ -3,13 +3,12 @@ import numpy as np
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
6
- from PIL import Image
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
- # Function to apply FFT and return an image
24
- def apply_fft(image: Image.Image):
25
- # Convert the image to grayscale for FFT (can be extended for color images too)
26
- image_gray = image.convert("L")
27
-
28
- # Convert the image to numpy array
29
- image_array = np.array(image_gray)
30
-
31
- # Apply 2D FFT
32
- fft_image = np.fft.fft2(image_array)
33
- fft_shifted = np.fft.fftshift(fft_image) # Shift the zero frequency to the center
34
-
35
- # Magnitude spectrum for visualization
36
- magnitude_spectrum = 20 * np.log(np.abs(fft_shifted))
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
- # Generate the image using the diffusion pipeline
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 FFT post-processing to the generated image
66
- fft_image = apply_fft(image)
67
-
68
- return fft_image
 
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 with FFT Post-Processing
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