esen3110 commited on
Commit
dbc5b70
·
verified ·
1 Parent(s): 099afe7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +188 -187
app.py CHANGED
@@ -1,187 +1,188 @@
1
- import gradio as gr
2
- import numpy as np
3
- import random
4
- #from diffusers import DiffusionPipeline
5
- from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
6
- #from diffusers.utils import load_image
7
- import torch
8
- from PIL import Image
9
-
10
-
11
- modelPath = "stabilityai/sdxl-turbo"
12
-
13
- if torch.cuda.is_available():
14
- device = "cuda"
15
- torch.cuda.max_memory_allocated(device=device)
16
- #pipe = DiffusionPipeline.from_pretrained(modelPath, torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
17
- pipeTex2Image = AutoPipelineForText2Image.from_pretrained(modelPath, torch_dtype=torch.float16, variant="fp16")
18
- pipeImage2Image = AutoPipelineForImage2Image.from_pretrained(modelPath, torch_dtype=torch.float16, variant="fp16")
19
- #pipe.enable_xformers_memory_efficient_attention()
20
- pipeTex2Image.enable_xformers_memory_efficient_attention()
21
- pipeImage2Image.enable_xformers_memory_efficient_attention()
22
- #pipe = pipe.to(device)
23
- else:
24
- device = "cpu"
25
- #pipe = DiffusionPipeline.from_pretrained(modelPath, use_safetensors=True)
26
- pipeTex2Image = AutoPipelineForText2Image.from_pretrained(modelPath, use_safetensors=True)
27
- pipeImage2Image = AutoPipelineForImage2Image.from_pretrained(modelPath, use_safetensors=True)
28
- #pipe = pipe.to(device)
29
-
30
- #pipe = pipe.to(device)
31
- pipeTex2Image.to(device)
32
- pipeImage2Image.to(device)
33
-
34
- MAX_SEED = np.iinfo(np.int32).max
35
- MAX_IMAGE_SIZE = 1024
36
-
37
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, use_as_input, strength, image):
38
-
39
- if randomize_seed:
40
- seed = random.randint(0, MAX_SEED)
41
-
42
- generator = torch.Generator().manual_seed(seed)
43
-
44
- if use_as_input:
45
- print("Image to Image:")
46
- pipe = pipeImage2Image
47
- init_image = Image.fromarray(np.uint8(image)).resize((width, height)).convert("RGB")
48
- init_image.save("input.png", format="PNG")
49
- print(type(init_image), init_image.size)
50
- image = pipe(
51
- prompt = prompt,
52
- negative_prompt = negative_prompt,
53
- guidance_scale = guidance_scale,
54
- num_inference_steps = num_inference_steps,
55
- width = width,
56
- height = height,
57
- generator = generator,
58
- strength=strength,
59
- image=init_image
60
- ).images[0]
61
- else:
62
- print("Text to Image:")
63
- pipe = pipeTex2Image
64
- image = pipe(
65
- prompt = prompt,
66
- negative_prompt = negative_prompt,
67
- guidance_scale = guidance_scale,
68
- num_inference_steps = num_inference_steps,
69
- width = width,
70
- height = height,
71
- generator = generator
72
- ).images[0]
73
-
74
- return image
75
-
76
- examples = [
77
- "Face of a modern woman of Balkan descent 25 years old",
78
- "Blue car sandero stepway on dirt road",
79
- "Cow in the skin of a dog of dalmatian breed",
80
- ]
81
-
82
- css="""
83
- #col-container {
84
- margin: 0 auto;
85
- max-width: auto;
86
- }
87
- """
88
-
89
-
90
- with gr.Blocks(css=css) as app:
91
-
92
- with gr.Column(elem_id="col-container"):
93
- gr.Markdown(f"""
94
- # Text-to-Image, Image-to-Image by Slavko Novak
95
- Currently running on {device}.
96
- """)
97
-
98
- with gr.Row():
99
-
100
- prompt = gr.Text(
101
- label="Prompt",
102
- show_label=False,
103
- max_lines=1,
104
- placeholder="Enter your prompt",
105
- container=False,
106
- )
107
-
108
- run_button = gr.Button("Generate", scale=0)
109
-
110
- result = gr.Image(label="Result", show_label=False)
111
- use_as_input = gr.Checkbox(label="Use image as input", value=False)
112
-
113
- with gr.Accordion("Advanced Settings", open=False):
114
-
115
- negative_prompt = gr.Text(
116
- label="Negative prompt",
117
- max_lines=1,
118
- placeholder="Enter a negative prompt",
119
- visible=True,
120
- )
121
-
122
- seed = gr.Slider(
123
- label="Seed",
124
- minimum=0,
125
- maximum=MAX_SEED,
126
- step=1,
127
- value=0,
128
- )
129
-
130
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
131
-
132
- with gr.Row():
133
-
134
- width = gr.Slider(
135
- label="Width",
136
- minimum=256,
137
- maximum=MAX_IMAGE_SIZE,
138
- step=32,
139
- value=512,
140
- )
141
-
142
- height = gr.Slider(
143
- label="Height",
144
- minimum=256,
145
- maximum=MAX_IMAGE_SIZE,
146
- step=32,
147
- value=512,
148
- )
149
-
150
- with gr.Row():
151
-
152
- guidance_scale = gr.Slider(
153
- label="Guidance scale",
154
- minimum=0.0,
155
- maximum=10.0,
156
- step=0.1,
157
- value=0.0,
158
- )
159
-
160
- strength = gr.Slider(
161
- label="Strength scale",
162
- minimum=0.0,
163
- maximum=1.0,
164
- step=0.1,
165
- value=0.5,
166
- )
167
-
168
- num_inference_steps = gr.Slider(
169
- label="Number of inference steps",
170
- minimum=1,
171
- maximum=12,
172
- step=1,
173
- value=2,
174
- )
175
-
176
- gr.Examples(
177
- examples = examples,
178
- inputs = [prompt]
179
- )
180
-
181
- run_button.click(
182
- fn = infer,
183
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, use_as_input, strength, result],
184
- outputs = [result]
185
- )
186
-
187
- app.queue().launch(server_name="0.0.0.0", server_port=8080, share=True)
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ #from diffusers import DiffusionPipeline
5
+ from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
6
+ #from diffusers.utils import load_image
7
+ import torch
8
+ from PIL import Image
9
+
10
+
11
+ modelPath = "stabilityai/sdxl-turbo"
12
+
13
+ if torch.cuda.is_available():
14
+ device = "cuda"
15
+ torch.cuda.max_memory_allocated(device=device)
16
+ #pipe = DiffusionPipeline.from_pretrained(modelPath, torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
17
+ pipeTex2Image = AutoPipelineForText2Image.from_pretrained(modelPath, torch_dtype=torch.float16, variant="fp16")
18
+ pipeImage2Image = AutoPipelineForImage2Image.from_pretrained(modelPath, torch_dtype=torch.float16, variant="fp16")
19
+ #pipe.enable_xformers_memory_efficient_attention()
20
+ pipeTex2Image.enable_xformers_memory_efficient_attention()
21
+ pipeImage2Image.enable_xformers_memory_efficient_attention()
22
+ #pipe = pipe.to(device)
23
+ else:
24
+ device = "cpu"
25
+ #pipe = DiffusionPipeline.from_pretrained(modelPath, use_safetensors=True)
26
+ pipeTex2Image = AutoPipelineForText2Image.from_pretrained(modelPath, use_safetensors=True)
27
+ pipeImage2Image = AutoPipelineForImage2Image.from_pretrained(modelPath, use_safetensors=True)
28
+ #pipe = pipe.to(device)
29
+
30
+ #pipe = pipe.to(device)
31
+ pipeTex2Image.to(device)
32
+ pipeImage2Image.to(device)
33
+
34
+ MAX_SEED = np.iinfo(np.int32).max
35
+ MAX_IMAGE_SIZE = 1024
36
+
37
+ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, use_as_input, strength, image):
38
+
39
+ if randomize_seed:
40
+ seed = random.randint(0, MAX_SEED)
41
+
42
+ generator = torch.Generator().manual_seed(seed)
43
+
44
+ if use_as_input:
45
+ print("Image to Image:")
46
+ pipe = pipeImage2Image
47
+ init_image = Image.fromarray(np.uint8(image)).resize((width, height)).convert("RGB")
48
+ init_image.save("input.png", format="PNG")
49
+ print(type(init_image), init_image.size)
50
+ image = pipe(
51
+ prompt = prompt,
52
+ negative_prompt = negative_prompt,
53
+ guidance_scale = guidance_scale,
54
+ num_inference_steps = num_inference_steps,
55
+ width = width,
56
+ height = height,
57
+ generator = generator,
58
+ strength=strength,
59
+ image=init_image
60
+ ).images[0]
61
+ else:
62
+ print("Text to Image:")
63
+ pipe = pipeTex2Image
64
+ image = pipe(
65
+ prompt = prompt,
66
+ negative_prompt = negative_prompt,
67
+ guidance_scale = guidance_scale,
68
+ num_inference_steps = num_inference_steps,
69
+ width = width,
70
+ height = height,
71
+ generator = generator
72
+ ).images[0]
73
+
74
+ return image
75
+
76
+ examples = [
77
+ "Face of a modern woman of Balkan descent 25 years old",
78
+ "Blue car sandero stepway on dirt road",
79
+ "Cow in the skin of a dog of dalmatian breed",
80
+ ]
81
+
82
+ css="""
83
+ #col-container {
84
+ margin: 0 auto;
85
+ max-width: auto;
86
+ }
87
+ """
88
+
89
+
90
+ with gr.Blocks(css=css) as app:
91
+
92
+ with gr.Column(elem_id="col-container"):
93
+ gr.Markdown(f"""
94
+ # Text-to-Image, Image-to-Image by Slavko Novak
95
+ Currently running on {device}.
96
+ """)
97
+
98
+ with gr.Row():
99
+
100
+ prompt = gr.Text(
101
+ label="Prompt",
102
+ show_label=False,
103
+ max_lines=1,
104
+ placeholder="Enter your prompt",
105
+ container=False,
106
+ )
107
+
108
+ run_button = gr.Button("Generate", scale=0)
109
+
110
+ result = gr.Image(label="Result", show_label=False)
111
+ use_as_input = gr.Checkbox(label="Use image as input", value=False)
112
+
113
+ with gr.Accordion("Advanced Settings", open=False):
114
+
115
+ negative_prompt = gr.Text(
116
+ label="Negative prompt",
117
+ max_lines=1,
118
+ placeholder="Enter a negative prompt",
119
+ visible=True,
120
+ )
121
+
122
+ seed = gr.Slider(
123
+ label="Seed",
124
+ minimum=0,
125
+ maximum=MAX_SEED,
126
+ step=1,
127
+ value=0,
128
+ )
129
+
130
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
131
+
132
+ with gr.Row():
133
+
134
+ width = gr.Slider(
135
+ label="Width",
136
+ minimum=256,
137
+ maximum=MAX_IMAGE_SIZE,
138
+ step=32,
139
+ value=512,
140
+ )
141
+
142
+ height = gr.Slider(
143
+ label="Height",
144
+ minimum=256,
145
+ maximum=MAX_IMAGE_SIZE,
146
+ step=32,
147
+ value=512,
148
+ )
149
+
150
+ with gr.Row():
151
+
152
+ guidance_scale = gr.Slider(
153
+ label="Guidance scale",
154
+ minimum=0.0,
155
+ maximum=10.0,
156
+ step=0.1,
157
+ value=0.0,
158
+ )
159
+
160
+ strength = gr.Slider(
161
+ label="Strength scale",
162
+ minimum=0.0,
163
+ maximum=1.0,
164
+ step=0.1,
165
+ value=0.5,
166
+ )
167
+
168
+ num_inference_steps = gr.Slider(
169
+ label="Number of inference steps",
170
+ minimum=1,
171
+ maximum=12,
172
+ step=1,
173
+ value=2,
174
+ )
175
+
176
+ gr.Examples(
177
+ examples = examples,
178
+ inputs = [prompt]
179
+ )
180
+
181
+ run_button.click(
182
+ fn = infer,
183
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, use_as_input, strength, result],
184
+ outputs = [result]
185
+ )
186
+
187
+ #app.queue().launch(server_name="0.0.0.0", server_port=8080, share=True)
188
+ app.queue().launch()