gaur3009 commited on
Commit
1f48b27
·
verified ·
1 Parent(s): 4d169f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -3,17 +3,20 @@ import numpy as np
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
 
6
 
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
8
 
9
- if torch.cuda.is_available():
10
- torch.cuda.max_memory_allocated(device=device)
11
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
12
- pipe.enable_xformers_memory_efficient_attention()
13
- pipe = pipe.to(device)
14
- else:
15
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
16
- pipe = pipe.to(device)
 
17
 
18
  MAX_SEED = np.iinfo(np.int32).max
19
  MAX_IMAGE_SIZE = 1024
@@ -22,26 +25,28 @@ def infer(prompt_part1, color, dress_type, front_design, back_design, prompt_par
22
  if randomize_seed:
23
  seed = random.randint(0, MAX_SEED)
24
 
25
- generator = torch.Generator().manual_seed(seed)
26
 
 
27
  front_prompt = f"front view of {prompt_part1} {color} colored plain {dress_type} with {front_design} design, {prompt_part5}"
28
  front_image = pipe(
29
- prompt=front_prompt,
30
  negative_prompt=negative_prompt,
31
- guidance_scale=guidance_scale,
32
- num_inference_steps=num_inference_steps,
33
- width=width,
34
  height=height,
35
  generator=generator
36
  ).images[0]
37
 
 
38
  back_prompt = f"back view of {prompt_part1} {color} colored plain {dress_type} with {back_design} design, {prompt_part5}"
39
  back_image = pipe(
40
- prompt=back_prompt,
41
  negative_prompt=negative_prompt,
42
- guidance_scale=guidance_scale,
43
- num_inference_steps=num_inference_steps,
44
- width=width,
45
  height=height,
46
  generator=generator
47
  ).images[0]
@@ -61,17 +66,12 @@ css = """
61
  }
62
  """
63
 
64
- if torch.cuda.is_available():
65
- power_device = "GPU"
66
- else:
67
- power_device = "CPU"
68
-
69
  with gr.Blocks(css=css) as demo:
70
 
71
  with gr.Column(elem_id="col-container"):
72
  gr.Markdown(f"""
73
  # Text-to-Image Gradio Template
74
- Currently running on {power_device}.
75
  """)
76
 
77
  with gr.Row():
@@ -179,15 +179,15 @@ with gr.Blocks(css=css) as demo:
179
  minimum=0.0,
180
  maximum=10.0,
181
  step=0.1,
182
- value=0.0,
183
  )
184
 
185
  num_inference_steps = gr.Slider(
186
  label="Number of inference steps",
187
  minimum=1,
188
- maximum=12,
189
  step=1,
190
- value=2,
191
  )
192
 
193
  gr.Examples(
 
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
6
+ from optimum.intel import ipex
7
 
8
+ # Use Intel Extension for PyTorch for CPU optimization
9
+ device = "cpu"
10
 
11
+ # Load the pipeline with optimizations for CPU inference
12
+ pipe = DiffusionPipeline.from_pretrained(
13
+ "stabilityai/sdxl-turbo",
14
+ use_safetensors=True
15
+ )
16
+ pipe = pipe.to(device)
17
+
18
+ # Optimize the pipeline using Intel Extension for PyTorch
19
+ ipex.optimize(pipe.unet, dtype=torch.float32) # Optimized for Intel CPUs
20
 
21
  MAX_SEED = np.iinfo(np.int32).max
22
  MAX_IMAGE_SIZE = 1024
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
 
28
+ generator = torch.Generator(device=device).manual_seed(seed)
29
 
30
+ # Front view prompt generation and inference
31
  front_prompt = f"front view of {prompt_part1} {color} colored plain {dress_type} with {front_design} design, {prompt_part5}"
32
  front_image = pipe(
33
+ prompt=front_prompt,
34
  negative_prompt=negative_prompt,
35
+ guidance_scale=guidance_scale,
36
+ num_inference_steps=num_inference_steps,
37
+ width=width,
38
  height=height,
39
  generator=generator
40
  ).images[0]
41
 
42
+ # Back view prompt generation and inference
43
  back_prompt = f"back view of {prompt_part1} {color} colored plain {dress_type} with {back_design} design, {prompt_part5}"
44
  back_image = pipe(
45
+ prompt=back_prompt,
46
  negative_prompt=negative_prompt,
47
+ guidance_scale=guidance_scale,
48
+ num_inference_steps=num_inference_steps,
49
+ width=width,
50
  height=height,
51
  generator=generator
52
  ).images[0]
 
66
  }
67
  """
68
 
 
 
 
 
 
69
  with gr.Blocks(css=css) as demo:
70
 
71
  with gr.Column(elem_id="col-container"):
72
  gr.Markdown(f"""
73
  # Text-to-Image Gradio Template
74
+ Currently running on CPU (Optimized).
75
  """)
76
 
77
  with gr.Row():
 
179
  minimum=0.0,
180
  maximum=10.0,
181
  step=0.1,
182
+ value=7.5, # Default value optimized for accuracy and speed
183
  )
184
 
185
  num_inference_steps = gr.Slider(
186
  label="Number of inference steps",
187
  minimum=1,
188
+ maximum=12, # Reduced steps for faster execution
189
  step=1,
190
+ value=8, # Balanced between speed and quality
191
  )
192
 
193
  gr.Examples(