tmblack commited on
Commit
c03b4f2
·
verified ·
1 Parent(s): 31f88ad

Upload app.py

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