multimodalart HF staff commited on
Commit
1e93542
1 Parent(s): 259bd16

Update the template

Browse files
Files changed (1) hide show
  1. app.py +20 -24
app.py CHANGED
@@ -3,22 +3,24 @@ 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
20
 
21
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
 
22
 
23
  if randomize_seed:
24
  seed = random.randint(0, MAX_SEED)
@@ -35,7 +37,7 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
35
  generator = generator
36
  ).images[0]
37
 
38
- return image
39
 
40
  examples = [
41
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
@@ -46,21 +48,15 @@ examples = [
46
  css="""
47
  #col-container {
48
  margin: 0 auto;
49
- max-width: 520px;
50
  }
51
  """
52
 
53
- if torch.cuda.is_available():
54
- power_device = "GPU"
55
- else:
56
- power_device = "CPU"
57
-
58
  with gr.Blocks(css=css) as demo:
59
 
60
  with gr.Column(elem_id="col-container"):
61
  gr.Markdown(f"""
62
  # Text-to-Image Gradio Template
63
- Currently running on {power_device}.
64
  """)
65
 
66
  with gr.Row():
@@ -103,7 +99,7 @@ with gr.Blocks(css=css) as demo:
103
  minimum=256,
104
  maximum=MAX_IMAGE_SIZE,
105
  step=32,
106
- value=512,
107
  )
108
 
109
  height = gr.Slider(
@@ -111,7 +107,7 @@ with gr.Blocks(css=css) as demo:
111
  minimum=256,
112
  maximum=MAX_IMAGE_SIZE,
113
  step=32,
114
- value=512,
115
  )
116
 
117
  with gr.Row():
@@ -121,26 +117,26 @@ with gr.Blocks(css=css) as demo:
121
  minimum=0.0,
122
  maximum=10.0,
123
  step=0.1,
124
- value=0.0,
125
  )
126
 
127
  num_inference_steps = gr.Slider(
128
  label="Number of inference steps",
129
  minimum=1,
130
- maximum=12,
131
  step=1,
132
- value=2,
133
  )
134
 
135
  gr.Examples(
136
  examples = examples,
137
  inputs = [prompt]
138
  )
139
-
140
- run_button.click(
141
  fn = infer,
142
  inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
143
- outputs = [result]
144
  )
145
 
146
  demo.queue().launch()
 
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
6
+ #import spaces [uncomment to use ZeroGPU]
7
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
10
 
11
  if torch.cuda.is_available():
12
+ torch_dtype = torch.float16
13
+ else:
14
+ torch_dtype = torch.float32
15
+
16
+ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
+ pipe = pipe.to(device)
 
18
 
19
  MAX_SEED = np.iinfo(np.int32).max
20
  MAX_IMAGE_SIZE = 1024
21
 
22
+ #@spaces.GPU [uncomment to use ZeroGPU]
23
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
24
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
 
37
  generator = generator
38
  ).images[0]
39
 
40
+ return image, seed
41
 
42
  examples = [
43
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
 
48
  css="""
49
  #col-container {
50
  margin: 0 auto;
51
+ max-width: 640px;
52
  }
53
  """
54
 
 
 
 
 
 
55
  with gr.Blocks(css=css) as demo:
56
 
57
  with gr.Column(elem_id="col-container"):
58
  gr.Markdown(f"""
59
  # Text-to-Image Gradio Template
 
60
  """)
61
 
62
  with gr.Row():
 
99
  minimum=256,
100
  maximum=MAX_IMAGE_SIZE,
101
  step=32,
102
+ value=1024, #Replace with defaults that work for your model
103
  )
104
 
105
  height = gr.Slider(
 
107
  minimum=256,
108
  maximum=MAX_IMAGE_SIZE,
109
  step=32,
110
+ value=1024, #Replace with defaults that work for your model
111
  )
112
 
113
  with gr.Row():
 
117
  minimum=0.0,
118
  maximum=10.0,
119
  step=0.1,
120
+ value=0.0, #Replace with defaults that work for your model
121
  )
122
 
123
  num_inference_steps = gr.Slider(
124
  label="Number of inference steps",
125
  minimum=1,
126
+ maximum=50,
127
  step=1,
128
+ value=2, #Replace with defaults that work for your model
129
  )
130
 
131
  gr.Examples(
132
  examples = examples,
133
  inputs = [prompt]
134
  )
135
+ gr.on(
136
+ trigger=[run_button.click, prompt.submit],
137
  fn = infer,
138
  inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
139
+ outputs = [result, seed]
140
  )
141
 
142
  demo.queue().launch()