fffiloni commited on
Commit
9b61f14
1 Parent(s): fe012f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -8
app.py CHANGED
@@ -1,30 +1,65 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline
 
3
  import torch
4
 
5
- model_id = "runwayml/stable-diffusion-v1-5"
6
- pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32, safety_checker=None)
7
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- def infer(prompt):
 
10
 
11
- prompt = "a photo of an astronaut riding a horse on mars"
12
- image = pipe(prompt=prompt, guidance_scale=10.0, num_inference_steps=25, width=256, height=256).images[0]
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  return image
15
 
 
 
 
 
 
 
16
  css="""
17
  #col-container {
18
  margin: 0 auto;
19
- max-width: 720px;
20
  }
21
  """
22
 
23
  with gr.Blocks(css=css) as demo:
24
 
25
  with gr.Column(elem_id="col-container"):
 
 
 
26
 
27
  with gr.Row():
 
28
  prompt = gr.Text(
29
  label="Prompt",
30
  show_label=False,
@@ -32,13 +67,74 @@ with gr.Blocks(css=css) as demo:
32
  placeholder="Enter your prompt",
33
  container=False,
34
  )
 
35
  run_button = gr.Button("Run", scale=0)
36
 
37
  result = gr.Image(label="Result", show_label=False)
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  run_button.click(
40
  fn = infer,
41
- inputs = [prompt],
42
  outputs = [result]
43
  )
44
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ from diffusers import DiffusionPipeline
4
  import torch
5
 
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
7
 
8
+ if torch.cuda.is_available():
9
+ torch.cuda.max_memory_allocated(device=device)
10
+ #torch.cuda.empty_cache()
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
+ #torch.cuda.empty_cache()
15
+ else:
16
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
17
+ pipe = pipe.to(device)
18
 
19
+ 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
 
24
+ if randomize_seed:
25
+ seed = random.randint(0, MAX_SEED)
26
+
27
+ generator = torch.Generator().manual_seed(seed)
28
+
29
+ image = pipe(
30
+ prompt = prompt,
31
+ negative_prompt = negative_prompt,
32
+ guidance_scale = guidance_scale,
33
+ num_inference_steps = num_inference_steps,
34
+ width = width,
35
+ height = height,
36
+ generator = generator
37
+ ).images[0]
38
+
39
  return image
40
 
41
+ examples = [
42
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
43
+ "An astronaut riding a green horse",
44
+ "A delicious ceviche cheesecake slice",
45
+ ]
46
+
47
  css="""
48
  #col-container {
49
  margin: 0 auto;
50
+ max-width: 520px;
51
  }
52
  """
53
 
54
  with gr.Blocks(css=css) as demo:
55
 
56
  with gr.Column(elem_id="col-container"):
57
+ gr.Markdown("""
58
+ # Text-to-Image Gradio Template
59
+ """)
60
 
61
  with gr.Row():
62
+
63
  prompt = gr.Text(
64
  label="Prompt",
65
  show_label=False,
 
67
  placeholder="Enter your prompt",
68
  container=False,
69
  )
70
+
71
  run_button = gr.Button("Run", scale=0)
72
 
73
  result = gr.Image(label="Result", show_label=False)
74
 
75
+ with gr.Accordion("Advanced Settings", open=False):
76
+
77
+ negative_prompt = gr.Text(
78
+ label="Negative prompt",
79
+ max_lines=1,
80
+ placeholder="Enter a negative prompt",
81
+ visible=False,
82
+ )
83
+
84
+ seed = gr.Slider(
85
+ label="Seed",
86
+ minimum=0,
87
+ maximum=MAX_SEED,
88
+ step=1,
89
+ value=0,
90
+ )
91
+
92
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
93
+
94
+ with gr.Row():
95
+
96
+ width = gr.Slider(
97
+ label="Width",
98
+ minimum=256,
99
+ maximum=MAX_IMAGE_SIZE,
100
+ step=32,
101
+ value=256,
102
+ )
103
+
104
+ height = gr.Slider(
105
+ label="Height",
106
+ minimum=256,
107
+ maximum=MAX_IMAGE_SIZE,
108
+ step=32,
109
+ value=256,
110
+ )
111
+
112
+ with gr.Row():
113
+
114
+ guidance_scale = gr.Slider(
115
+ label="Guidance scale",
116
+ minimum=1,
117
+ maximum=20,
118
+ step=0.1,
119
+ value=5.0,
120
+ )
121
+
122
+ num_inference_steps = gr.Slider(
123
+ label="Number of inference steps",
124
+ minimum=1,
125
+ maximum=12,
126
+ step=1,
127
+ value=2,
128
+ )
129
+
130
+ gr.Examples(
131
+ examples = examples,
132
+ inputs = [prompt]
133
+ )
134
+
135
  run_button.click(
136
  fn = infer,
137
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
138
  outputs = [result]
139
  )
140