Spaces:
Running
on
Zero
Running
on
Zero
prithivMLmods
commited on
Commit
•
f366c08
1
Parent(s):
caaf3eb
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
#patch 0.01 ()
|
3 |
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4 |
+
# of this software and associated documentation files (the "Software"), to deal
|
5 |
+
# in the Software without restriction, including without limitation the rights
|
6 |
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7 |
+
# copies of the Software, and to permit persons to whom the Software is
|
8 |
+
# furnished to do so, subject to the following conditions:
|
9 |
+
#
|
10 |
+
# ..
|
11 |
+
import os
|
12 |
+
import random
|
13 |
+
import uuid
|
14 |
+
|
15 |
+
import gradio as gr
|
16 |
+
import numpy as np
|
17 |
+
from PIL import Image
|
18 |
+
import spaces
|
19 |
+
import torch
|
20 |
+
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
21 |
+
|
22 |
+
DESCRIPTION = """
|
23 |
+
|
24 |
+
|
25 |
+
"""
|
26 |
+
|
27 |
+
def save_image(img):
|
28 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
29 |
+
img.save(unique_name)
|
30 |
+
return unique_name
|
31 |
+
|
32 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
33 |
+
if randomize_seed:
|
34 |
+
seed = random.randint(0, MAX_SEED)
|
35 |
+
return seed
|
36 |
+
|
37 |
+
MAX_SEED = np.iinfo(np.int32).max
|
38 |
+
|
39 |
+
if not torch.cuda.is_available():
|
40 |
+
DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
|
41 |
+
|
42 |
+
MAX_SEED = np.iinfo(np.int32).max
|
43 |
+
|
44 |
+
USE_TORCH_COMPILE = 0
|
45 |
+
ENABLE_CPU_OFFLOAD = 0
|
46 |
+
|
47 |
+
if torch.cuda.is_available():
|
48 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
49 |
+
"SG161222/RealVisXL_V4.0_Lightning",
|
50 |
+
torch_dtype=torch.float16,
|
51 |
+
use_safetensors=True,
|
52 |
+
)
|
53 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
54 |
+
pipe.load_lora_weights("prithivMLmods/Canopus-Realism-LoRA", weight_name="Canopus-Realism-LoRA.safetensors", adapter_name="rlms")
|
55 |
+
pipe.set_adapters("rlms")
|
56 |
+
pipe.to("cuda")
|
57 |
+
|
58 |
+
@spaces.GPU(duration=60, enable_queue=True)
|
59 |
+
def generate(
|
60 |
+
prompt: str,
|
61 |
+
negative_prompt: str = "",
|
62 |
+
use_negative_prompt: bool = False,
|
63 |
+
seed: int = 0,
|
64 |
+
width: int = 1024,
|
65 |
+
height: int = 1024,
|
66 |
+
guidance_scale: float = 3,
|
67 |
+
randomize_seed: bool = False,
|
68 |
+
progress=gr.Progress(track_tqdm=True),
|
69 |
+
):
|
70 |
+
|
71 |
+
|
72 |
+
seed = int(randomize_seed_fn(seed, randomize_seed))
|
73 |
+
|
74 |
+
if not use_negative_prompt:
|
75 |
+
negative_prompt = "" # type: ignore
|
76 |
+
|
77 |
+
images = pipe(
|
78 |
+
prompt=prompt,
|
79 |
+
negative_prompt=negative_prompt,
|
80 |
+
width=width,
|
81 |
+
height=height,
|
82 |
+
guidance_scale=guidance_scale,
|
83 |
+
num_inference_steps=10,
|
84 |
+
num_images_per_prompt=1,
|
85 |
+
cross_attention_kwargs={"scale": 0.65},
|
86 |
+
output_type="pil",
|
87 |
+
).images
|
88 |
+
image_paths = [save_image(img) for img in images]
|
89 |
+
print(image_paths)
|
90 |
+
return image_paths, seed
|
91 |
+
|
92 |
+
examples = [
|
93 |
+
"A man dressed in sunglasses and brown jacket, in the style of cypherpunk, timeless beauty, exacting precision, uhd image, aleksandr deyneka, matte background, leather/hide --ar 67:101 --v 5",
|
94 |
+
"A studio portrait of a brunette model wearing a dress in front of a natural background --v 6.0 --style raw",
|
95 |
+
"Man in the style of dark beige and brown, uhd image, youthful protagonists, nonrepresentational"
|
96 |
+
]
|
97 |
+
|
98 |
+
css = '''
|
99 |
+
.gradio-container{max-width: 545px !important}
|
100 |
+
h1{text-align:center}
|
101 |
+
footer {
|
102 |
+
visibility: hidden
|
103 |
+
}
|
104 |
+
'''
|
105 |
+
|
106 |
+
|
107 |
+
with gr.Blocks(css=css,theme="prithivMLmods/theme_brief") as demo:
|
108 |
+
|
109 |
+
with gr.Group():
|
110 |
+
with gr.Row():
|
111 |
+
prompt = gr.Text(
|
112 |
+
label="Prompt",
|
113 |
+
show_label=False,
|
114 |
+
max_lines=1,
|
115 |
+
placeholder="Enter your prompt",
|
116 |
+
container=False,
|
117 |
+
)
|
118 |
+
run_button = gr.Button("Run", scale=0)
|
119 |
+
result = gr.Gallery(label="Result", columns=1, preview=True, show_label=False)
|
120 |
+
with gr.Accordion("Advanced options", open=False):
|
121 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
122 |
+
negative_prompt = gr.Text(
|
123 |
+
label="Negative prompt",
|
124 |
+
lines=4,
|
125 |
+
max_lines=6,
|
126 |
+
value="(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation",
|
127 |
+
placeholder="Enter a negative prompt",
|
128 |
+
visible=True,
|
129 |
+
)
|
130 |
+
seed = gr.Slider(
|
131 |
+
label="Seed",
|
132 |
+
minimum=0,
|
133 |
+
maximum=MAX_SEED,
|
134 |
+
step=1,
|
135 |
+
value=0,
|
136 |
+
visible=True
|
137 |
+
)
|
138 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
139 |
+
with gr.Row(visible=True):
|
140 |
+
width = gr.Slider(
|
141 |
+
label="Width",
|
142 |
+
minimum=512,
|
143 |
+
maximum=2048,
|
144 |
+
step=8,
|
145 |
+
value=1024,
|
146 |
+
)
|
147 |
+
height = gr.Slider(
|
148 |
+
label="Height",
|
149 |
+
minimum=512,
|
150 |
+
maximum=2048,
|
151 |
+
step=8,
|
152 |
+
value=1024,
|
153 |
+
)
|
154 |
+
with gr.Row():
|
155 |
+
guidance_scale = gr.Slider(
|
156 |
+
label="Guidance Scale",
|
157 |
+
minimum=0.1,
|
158 |
+
maximum=20.0,
|
159 |
+
step=0.1,
|
160 |
+
value=3.0,
|
161 |
+
)
|
162 |
+
|
163 |
+
gr.Examples(
|
164 |
+
examples=examples,
|
165 |
+
inputs=prompt,
|
166 |
+
outputs=[result, seed],
|
167 |
+
fn=generate,
|
168 |
+
cache_examples=True,
|
169 |
+
)
|
170 |
+
|
171 |
+
use_negative_prompt.change(
|
172 |
+
fn=lambda x: gr.update(visible=x),
|
173 |
+
inputs=use_negative_prompt,
|
174 |
+
outputs=negative_prompt,
|
175 |
+
api_name=False,
|
176 |
+
)
|
177 |
+
|
178 |
+
|
179 |
+
gr.on(
|
180 |
+
triggers=[
|
181 |
+
prompt.submit,
|
182 |
+
negative_prompt.submit,
|
183 |
+
run_button.click,
|
184 |
+
],
|
185 |
+
fn=generate,
|
186 |
+
inputs=[
|
187 |
+
prompt,
|
188 |
+
negative_prompt,
|
189 |
+
use_negative_prompt,
|
190 |
+
seed,
|
191 |
+
width,
|
192 |
+
height,
|
193 |
+
guidance_scale,
|
194 |
+
randomize_seed,
|
195 |
+
],
|
196 |
+
outputs=[result, seed],
|
197 |
+
api_name="run",
|
198 |
+
)
|
199 |
+
|
200 |
+
if __name__ == "__main__":
|
201 |
+
demo.queue(max_size=40).launch()
|