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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
def run_command(command):
|
@@ -19,4 +332,5 @@ iface = gr.Interface(
|
|
19 |
["echo 'Hello, Gradio!'"],
|
20 |
["python --version"]]
|
21 |
)
|
22 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
1 |
+
run_api = False
|
2 |
+
SSD_1B = False
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Use GPU
|
6 |
+
gpu_info = os.popen("nvidia-smi").read()
|
7 |
+
if "failed" in gpu_info:
|
8 |
+
print("Not connected to a GPU")
|
9 |
+
is_gpu = False
|
10 |
+
else:
|
11 |
+
print(gpu_info)
|
12 |
+
is_gpu = True
|
13 |
+
print(is_gpu)
|
14 |
+
|
15 |
+
|
16 |
+
from IPython.display import clear_output
|
17 |
+
|
18 |
+
|
19 |
+
def check_enviroment():
|
20 |
+
try:
|
21 |
+
import torch
|
22 |
+
|
23 |
+
print("Enviroment is already installed.")
|
24 |
+
except ImportError:
|
25 |
+
print("Enviroment not found. Installing...")
|
26 |
+
# Install requirements from requirements.txt
|
27 |
+
os.system("pip install -r requirements.txt")
|
28 |
+
# Install gradio version 3.48.0
|
29 |
+
os.system("pip install gradio==3.39.0")
|
30 |
+
# Install python-dotenv
|
31 |
+
os.system("pip install python-dotenv")
|
32 |
+
# Clear the output
|
33 |
+
clear_output()
|
34 |
+
|
35 |
+
print("Enviroment installed successfully.")
|
36 |
+
|
37 |
+
|
38 |
+
# Call the function to check and install Packages if necessary
|
39 |
+
check_enviroment()
|
40 |
+
|
41 |
+
|
42 |
+
from IPython.display import clear_output
|
43 |
+
import os
|
44 |
+
import gradio as gr
|
45 |
+
import numpy as np
|
46 |
+
import PIL
|
47 |
+
import base64
|
48 |
+
import io
|
49 |
+
import torch
|
50 |
+
from diffusers import UNet2DConditionModel, DiffusionPipeline, LCMScheduler
|
51 |
+
|
52 |
+
# SDXL
|
53 |
+
from diffusers import UNet2DConditionModel, DiffusionPipeline, LCMScheduler
|
54 |
+
|
55 |
+
# Get the current directory
|
56 |
+
current_dir = os.getcwd()
|
57 |
+
model_path = os.path.join(current_dir)
|
58 |
+
# Set the cache path
|
59 |
+
cache_path = os.path.join(current_dir, "cache")
|
60 |
+
MAX_SEED = np.iinfo(np.int32).max
|
61 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1024"))
|
62 |
+
SECRET_TOKEN = os.getenv("SECRET_TOKEN", "default_secret")
|
63 |
+
|
64 |
+
# Uncomment the following line if you are using PyTorch 1.10 or later
|
65 |
+
# os.environ["TORCH_USE_CUDA_DSA"] = "1"
|
66 |
+
|
67 |
+
if is_gpu:
|
68 |
+
# Uncomment the following line if you want to enable CUDA launch blocking
|
69 |
+
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
|
70 |
+
else:
|
71 |
+
# Uncomment the following line if you want to use CPU instead of GPU
|
72 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
73 |
+
|
74 |
+
|
75 |
+
# Get the current directory
|
76 |
+
current_dir = os.getcwd()
|
77 |
+
model_path = os.path.join(current_dir)
|
78 |
+
|
79 |
+
# Set the cache path
|
80 |
+
cache_path = os.path.join(current_dir, "cache")
|
81 |
+
|
82 |
+
if not SSD_1B:
|
83 |
+
|
84 |
+
unet = UNet2DConditionModel.from_pretrained(
|
85 |
+
"latent-consistency/lcm-sdxl",
|
86 |
+
torch_dtype=torch.float16,
|
87 |
+
variant="fp16",
|
88 |
+
cache_dir=cache_path,
|
89 |
+
)
|
90 |
+
pipe = DiffusionPipeline.from_pretrained(
|
91 |
+
# "stabilityai/stable-diffusion-xl-base-1.0",
|
92 |
+
"stabilityai/sdxl-turbo",
|
93 |
+
unet=unet,
|
94 |
+
torch_dtype=torch.float16,
|
95 |
+
variant="fp16",
|
96 |
+
cache_dir=cache_path,
|
97 |
+
)
|
98 |
+
|
99 |
+
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
100 |
+
if torch.cuda.is_available():
|
101 |
+
pipe.to("cuda")
|
102 |
+
else:
|
103 |
+
# SSD-1B
|
104 |
+
from diffusers import LCMScheduler, AutoPipelineForText2Image
|
105 |
+
|
106 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
107 |
+
"segmind/SSD-1B",
|
108 |
+
torch_dtype=torch.float16,
|
109 |
+
variant="fp16",
|
110 |
+
cache_dir=cache_path,
|
111 |
+
)
|
112 |
+
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
113 |
+
if torch.cuda.is_available():
|
114 |
+
pipe.to("cuda")
|
115 |
+
|
116 |
+
# load and fuse
|
117 |
+
pipe.load_lora_weights("latent-consistency/lcm-lora-ssd-1b")
|
118 |
+
pipe.fuse_lora()
|
119 |
+
|
120 |
+
|
121 |
+
def generate(
|
122 |
+
prompt: str,
|
123 |
+
negative_prompt: str = "",
|
124 |
+
seed: int = 0,
|
125 |
+
width: int = 1024,
|
126 |
+
height: int = 1024,
|
127 |
+
guidance_scale: float = 0.0,
|
128 |
+
num_inference_steps: int = 4,
|
129 |
+
secret_token: str = "",
|
130 |
+
) -> PIL.Image.Image:
|
131 |
+
if secret_token != SECRET_TOKEN:
|
132 |
+
raise gr.Error(
|
133 |
+
f"Invalid secret token. Please fork the original space if you want to use it for yourself."
|
134 |
+
)
|
135 |
+
|
136 |
+
generator = torch.Generator().manual_seed(seed)
|
137 |
+
|
138 |
+
image = pipe(
|
139 |
+
prompt=prompt,
|
140 |
+
negative_prompt=negative_prompt,
|
141 |
+
width=width,
|
142 |
+
height=height,
|
143 |
+
guidance_scale=guidance_scale,
|
144 |
+
num_inference_steps=num_inference_steps,
|
145 |
+
generator=generator,
|
146 |
+
output_type="pil",
|
147 |
+
).images[0]
|
148 |
+
return image
|
149 |
+
|
150 |
+
|
151 |
+
clear_output()
|
152 |
+
|
153 |
+
from IPython.display import display
|
154 |
+
|
155 |
+
|
156 |
+
def generate_image(prompt="A beautiful and sexy girl"):
|
157 |
+
# Generate the image using the prompt
|
158 |
+
generated_image = generate(
|
159 |
+
prompt=prompt,
|
160 |
+
negative_prompt="",
|
161 |
+
seed=0,
|
162 |
+
width=1024,
|
163 |
+
height=1024,
|
164 |
+
guidance_scale=0.0,
|
165 |
+
num_inference_steps=4,
|
166 |
+
secret_token="default_secret", # Replace with your secret token
|
167 |
+
)
|
168 |
+
# Display the image in the Jupyter Notebook
|
169 |
+
display(generated_image)
|
170 |
+
|
171 |
+
|
172 |
+
if not run_api:
|
173 |
+
secret_token = gr.Text(
|
174 |
+
label="Secret Token",
|
175 |
+
max_lines=1,
|
176 |
+
placeholder="Enter your secret token",
|
177 |
+
)
|
178 |
+
prompt = gr.Text(
|
179 |
+
label="Prompt",
|
180 |
+
show_label=False,
|
181 |
+
max_lines=1,
|
182 |
+
placeholder="Enter your prompt",
|
183 |
+
container=False,
|
184 |
+
)
|
185 |
+
result = gr.Image(label="Result", show_label=False)
|
186 |
+
negative_prompt = gr.Text(
|
187 |
+
label="Negative prompt",
|
188 |
+
max_lines=1,
|
189 |
+
placeholder="Enter a negative prompt",
|
190 |
+
visible=True,
|
191 |
+
)
|
192 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
193 |
+
|
194 |
+
width = gr.Slider(
|
195 |
+
label="Width",
|
196 |
+
minimum=256,
|
197 |
+
maximum=MAX_IMAGE_SIZE,
|
198 |
+
step=32,
|
199 |
+
value=1024,
|
200 |
+
)
|
201 |
+
height = gr.Slider(
|
202 |
+
label="Height",
|
203 |
+
minimum=256,
|
204 |
+
maximum=MAX_IMAGE_SIZE,
|
205 |
+
step=32,
|
206 |
+
value=1024,
|
207 |
+
)
|
208 |
+
guidance_scale = gr.Slider(
|
209 |
+
label="Guidance scale", minimum=0, maximum=2, step=0.1, value=0.0
|
210 |
+
)
|
211 |
+
num_inference_steps = gr.Slider(
|
212 |
+
label="Number of inference steps", minimum=1, maximum=8, step=1, value=4
|
213 |
+
)
|
214 |
+
inputs = [
|
215 |
+
prompt,
|
216 |
+
negative_prompt,
|
217 |
+
seed,
|
218 |
+
width,
|
219 |
+
height,
|
220 |
+
guidance_scale,
|
221 |
+
num_inference_steps,
|
222 |
+
secret_token,
|
223 |
+
]
|
224 |
+
iface = gr.Interface(
|
225 |
+
fn=generate,
|
226 |
+
inputs=inputs,
|
227 |
+
outputs=result,
|
228 |
+
title="Image Generator",
|
229 |
+
description="Generate images based on prompts.",
|
230 |
+
)
|
231 |
+
|
232 |
+
iface.launch()
|
233 |
+
|
234 |
+
|
235 |
+
if run_api:
|
236 |
+
with gr.Blocks() as demo:
|
237 |
+
gr.HTML(
|
238 |
+
"""
|
239 |
+
<div style="z-index: 100; position: fixed; top: 0px; right: 0px; left: 0px; bottom: 0px; width: 100%; height: 100%; background: white; display: flex; align-items: center; justify-content: center; color: black;">
|
240 |
+
<div style="text-align: center; color: black;">
|
241 |
+
<p style="color: black;">This space is a REST API to programmatically generate images using LCM LoRA SSD-1B.</p>
|
242 |
+
<p style="color: black;">It is not meant to be directly used through a user interface, but using code and an access key.</p>
|
243 |
+
</div>
|
244 |
+
</div>"""
|
245 |
+
)
|
246 |
+
secret_token = gr.Text(
|
247 |
+
label="Secret Token",
|
248 |
+
max_lines=1,
|
249 |
+
placeholder="Enter your secret token",
|
250 |
+
)
|
251 |
+
prompt = gr.Text(
|
252 |
+
label="Prompt",
|
253 |
+
show_label=False,
|
254 |
+
max_lines=1,
|
255 |
+
placeholder="Enter your prompt",
|
256 |
+
container=False,
|
257 |
+
)
|
258 |
+
result = gr.Image(label="Result", show_label=False)
|
259 |
+
negative_prompt = gr.Text(
|
260 |
+
label="Negative prompt",
|
261 |
+
max_lines=1,
|
262 |
+
placeholder="Enter a negative prompt",
|
263 |
+
visible=True,
|
264 |
+
)
|
265 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
266 |
+
|
267 |
+
width = gr.Slider(
|
268 |
+
label="Width",
|
269 |
+
minimum=256,
|
270 |
+
maximum=MAX_IMAGE_SIZE,
|
271 |
+
step=32,
|
272 |
+
value=1024,
|
273 |
+
)
|
274 |
+
height = gr.Slider(
|
275 |
+
label="Height",
|
276 |
+
minimum=256,
|
277 |
+
maximum=MAX_IMAGE_SIZE,
|
278 |
+
step=32,
|
279 |
+
value=1024,
|
280 |
+
)
|
281 |
+
guidance_scale = gr.Slider(
|
282 |
+
label="Guidance scale", minimum=0, maximum=2, step=0.1, value=0.0
|
283 |
+
)
|
284 |
+
num_inference_steps = gr.Slider(
|
285 |
+
label="Number of inference steps", minimum=1, maximum=8, step=1, value=4
|
286 |
+
)
|
287 |
+
|
288 |
+
inputs = [
|
289 |
+
prompt,
|
290 |
+
negative_prompt,
|
291 |
+
seed,
|
292 |
+
width,
|
293 |
+
height,
|
294 |
+
guidance_scale,
|
295 |
+
num_inference_steps,
|
296 |
+
secret_token,
|
297 |
+
]
|
298 |
+
prompt.submit(
|
299 |
+
fn=generate,
|
300 |
+
inputs=inputs,
|
301 |
+
outputs=result,
|
302 |
+
api_name="run",
|
303 |
+
)
|
304 |
+
|
305 |
+
# demo.queue(max_size=32).launch()
|
306 |
+
# Launch the Gradio app with multiple workers and debug mode enabled
|
307 |
+
# demo.queue(max_size=32).launch(debug=True)# For Standard
|
308 |
+
demo.queue(max_size=32).launch(server_name="0.0.0.0", server_port=7860) # Docker
|
309 |
+
|
310 |
+
|
311 |
+
'''
|
312 |
+
|
313 |
+
|
314 |
import gradio as gr
|
315 |
import subprocess
|
316 |
def run_command(command):
|
|
|
332 |
["echo 'Hello, Gradio!'"],
|
333 |
["python --version"]]
|
334 |
)
|
335 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
336 |
+
'''
|