xinglilu commited on
Commit
1a2b255
·
verified ·
1 Parent(s): 8151bd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -4,7 +4,6 @@ import random
4
  from diffusers import DiffusionPipeline
5
  import torch
6
 
7
- # Set device to GPU if available, else CPU
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
 
10
  if torch.cuda.is_available():
@@ -20,27 +19,28 @@ MAX_SEED = np.iinfo(np.int32).max
20
  MAX_IMAGE_SIZE = 1024
21
 
22
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
 
23
  if randomize_seed:
24
  seed = random.randint(0, MAX_SEED)
25
 
26
- generator = torch.Generator(device).manual_seed(seed)
27
 
28
  image = pipe(
29
- prompt=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
  return image
39
 
40
  examples = [
41
- ["Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"],
42
- ["An astronaut riding a green horse"],
43
- ["A delicious ceviche cheesecake slice"],
44
  ]
45
 
46
  css="""
@@ -50,9 +50,13 @@ css="""
50
  }
51
  """
52
 
53
- power_device = "GPU" if torch.cuda.is_available() else "CPU"
 
 
 
54
 
55
  with gr.Blocks(css=css) as demo:
 
56
  with gr.Column(elem_id="col-container"):
57
  gr.Markdown(f"""
58
  # Text-to-Image Gradio Template
@@ -60,23 +64,26 @@ with gr.Blocks(css=css) as demo:
60
  """)
61
 
62
  with gr.Row():
63
- prompt = gr.Textbox(
 
64
  label="Prompt",
65
  show_label=False,
66
  max_lines=1,
67
  placeholder="Enter your prompt",
68
  container=False,
69
  )
 
70
  run_button = gr.Button("Run", scale=0)
71
 
72
  result = gr.Image(label="Result", show_label=False)
73
 
74
  with gr.Accordion("Advanced Settings", open=False):
75
- negative_prompt = gr.Textbox(
 
76
  label="Negative prompt",
77
  max_lines=1,
78
  placeholder="Enter a negative prompt",
79
- visible=True,
80
  )
81
 
82
  seed = gr.Slider(
@@ -90,6 +97,7 @@ with gr.Blocks(css=css) as demo:
90
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
91
 
92
  with gr.Row():
 
93
  width = gr.Slider(
94
  label="Width",
95
  minimum=256,
@@ -107,33 +115,32 @@ with gr.Blocks(css=css) as demo:
107
  )
108
 
109
  with gr.Row():
 
110
  guidance_scale = gr.Slider(
111
  label="Guidance scale",
112
  minimum=0.0,
113
  maximum=10.0,
114
  step=0.1,
115
- value=7.5,
116
  )
117
 
118
  num_inference_steps = gr.Slider(
119
  label="Number of inference steps",
120
  minimum=1,
121
- maximum=50,
122
  step=1,
123
- value=25,
124
  )
125
 
126
  gr.Examples(
127
- examples=examples,
128
- inputs=[prompt],
129
- outputs=[result],
130
- fn=infer
131
  )
132
 
133
  run_button.click(
134
- fn=infer,
135
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
136
- outputs=[result]
137
  )
138
 
139
- demo.queue().launch(share=True)
 
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():
 
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)
25
 
26
+ generator = torch.Generator().manual_seed(seed)
27
 
28
  image = pipe(
29
+ prompt = 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
  return image
39
 
40
  examples = [
41
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
42
+ "An astronaut riding a green horse",
43
+ "A delicious ceviche cheesecake slice",
44
  ]
45
 
46
  css="""
 
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
 
64
  """)
65
 
66
  with gr.Row():
67
+
68
+ prompt = gr.Text(
69
  label="Prompt",
70
  show_label=False,
71
  max_lines=1,
72
  placeholder="Enter your prompt",
73
  container=False,
74
  )
75
+
76
  run_button = gr.Button("Run", scale=0)
77
 
78
  result = gr.Image(label="Result", show_label=False)
79
 
80
  with gr.Accordion("Advanced Settings", open=False):
81
+
82
+ negative_prompt = gr.Text(
83
  label="Negative prompt",
84
  max_lines=1,
85
  placeholder="Enter a negative prompt",
86
+ visible=False,
87
  )
88
 
89
  seed = gr.Slider(
 
97
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
98
 
99
  with gr.Row():
100
+
101
  width = gr.Slider(
102
  label="Width",
103
  minimum=256,
 
115
  )
116
 
117
  with gr.Row():
118
+
119
  guidance_scale = gr.Slider(
120
  label="Guidance scale",
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()