Spaces:
Running
on
Zero
Running
on
Zero
prithivMLmods
commited on
Commit
•
2ce9dce
1
Parent(s):
56d96a3
Delete file
Browse files- file/1.png +0 -0
- file/2.png +0 -0
- file/3.png +0 -0
- file/4.png +0 -0
- file/demo.txt +0 -235
file/1.png
DELETED
Binary file (279 kB)
|
|
file/2.png
DELETED
Binary file (222 kB)
|
|
file/3.png
DELETED
Binary file (241 kB)
|
|
file/4.png
DELETED
Binary file (253 kB)
|
|
file/demo.txt
DELETED
@@ -1,235 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import random
|
3 |
-
import uuid
|
4 |
-
import json
|
5 |
-
import gradio as gr
|
6 |
-
import numpy as np
|
7 |
-
from PIL import Image
|
8 |
-
import spaces
|
9 |
-
import torch
|
10 |
-
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
11 |
-
|
12 |
-
#Load the HTML content
|
13 |
-
#html_file_url = "https://prithivmlmods-hamster-static.static.hf.space/index.html"
|
14 |
-
#html_content = f'<iframe src="{html_file_url}" style="width:100%; height:180px; border:none;"></iframe>'
|
15 |
-
#html_file_url = "https://prithivmlmods-static-loading-theme.static.hf.space/index.html"
|
16 |
-
|
17 |
-
#html_file_url = "https://prithivhamster.vercel.app/"
|
18 |
-
#html_content = f'<iframe src="{html_file_url}" style="width:100%; height:400px; border:none"></iframe>'
|
19 |
-
|
20 |
-
DESCRIPTIONx = """## STABLE HAMSTER
|
21 |
-
"""
|
22 |
-
|
23 |
-
css = '''
|
24 |
-
.gradio-container{max-width: 560px !important}
|
25 |
-
h1{text-align:center}
|
26 |
-
footer {
|
27 |
-
visibility: hidden
|
28 |
-
}
|
29 |
-
'''
|
30 |
-
|
31 |
-
examples = [
|
32 |
-
"3d image, cute girl, in the style of Pixar --ar 1:2 --stylize 750, 4K resolution highlights, Sharp focus, octane render, ray tracing, Ultra-High-Definition, 8k, UHD, HDR, (Masterpiece:1.5), (best quality:1.5)",
|
33 |
-
"Cold coffee in a cup bokeh --ar 85:128 --v 6.0 --style raw5, 4K",
|
34 |
-
"Vector illustration of a horse, vector graphic design with flat colors on an brown background in the style of vector art, using simple shapes and graphics with simple details, professionally designed as a tshirt logo ready for print on a white background. --ar 89:82 --v 6.0 --style raw",
|
35 |
-
"Man in brown leather jacket posing for camera, in the style of sleek and stylized, clockpunk, subtle shades, exacting precision, ferrania p30 --ar 67:101 --v 5",
|
36 |
-
"Commercial photography, giant burger, white lighting, studio light, 8k octane rendering, high resolution photography, insanely detailed, fine details, on white isolated plain, 8k, commercial photography, stock photo, professional color grading, --v 4 --ar 9:16 "
|
37 |
-
|
38 |
-
|
39 |
-
]
|
40 |
-
|
41 |
-
#Set an os.Getenv variable
|
42 |
-
#set VAR_NAME=”VALUE”
|
43 |
-
#Fetch an environment variable
|
44 |
-
#echo %VAR_NAME%
|
45 |
-
|
46 |
-
MODEL_ID = os.getenv("MODEL_REPO")
|
47 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
|
48 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
49 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
50 |
-
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "1")) # Allow generating multiple images at once
|
51 |
-
|
52 |
-
#Load model outside of function
|
53 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
54 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
55 |
-
MODEL_ID,
|
56 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
57 |
-
use_safetensors=True,
|
58 |
-
add_watermarker=False,
|
59 |
-
).to(device)
|
60 |
-
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
61 |
-
|
62 |
-
# <compile speedup >
|
63 |
-
if USE_TORCH_COMPILE:
|
64 |
-
pipe.compile()
|
65 |
-
|
66 |
-
# Offloading capacity (RAM)
|
67 |
-
if ENABLE_CPU_OFFLOAD:
|
68 |
-
pipe.enable_model_cpu_offload()
|
69 |
-
|
70 |
-
MAX_SEED = np.iinfo(np.int32).max
|
71 |
-
|
72 |
-
def save_image(img):
|
73 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
74 |
-
img.save(unique_name)
|
75 |
-
return unique_name
|
76 |
-
|
77 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
78 |
-
if randomize_seed:
|
79 |
-
seed = random.randint(0, MAX_SEED)
|
80 |
-
return seed
|
81 |
-
|
82 |
-
@spaces.GPU(duration=60, enable_queue=True)
|
83 |
-
def generate(
|
84 |
-
prompt: str,
|
85 |
-
negative_prompt: str = "",
|
86 |
-
use_negative_prompt: bool = False,
|
87 |
-
seed: int = 1,
|
88 |
-
width: int = 1024,
|
89 |
-
height: int = 1024,
|
90 |
-
guidance_scale: float = 3,
|
91 |
-
num_inference_steps: int = 25,
|
92 |
-
randomize_seed: bool = False,
|
93 |
-
use_resolution_binning: bool = True,
|
94 |
-
num_images: int = 1, # Number of images to generate
|
95 |
-
progress=gr.Progress(track_tqdm=True),
|
96 |
-
):
|
97 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
98 |
-
generator = torch.Generator(device=device).manual_seed(seed)
|
99 |
-
|
100 |
-
#Options
|
101 |
-
options = {
|
102 |
-
"prompt": [prompt] * num_images,
|
103 |
-
"negative_prompt": [negative_prompt] * num_images if use_negative_prompt else None,
|
104 |
-
"width": width,
|
105 |
-
"height": height,
|
106 |
-
"guidance_scale": guidance_scale,
|
107 |
-
"num_inference_steps": num_inference_steps,
|
108 |
-
"generator": generator,
|
109 |
-
"output_type": "pil",
|
110 |
-
}
|
111 |
-
|
112 |
-
#VRAM usage Lesser
|
113 |
-
if use_resolution_binning:
|
114 |
-
options["use_resolution_binning"] = True
|
115 |
-
|
116 |
-
#Images potential batches
|
117 |
-
images = []
|
118 |
-
for i in range(0, num_images, BATCH_SIZE):
|
119 |
-
batch_options = options.copy()
|
120 |
-
batch_options["prompt"] = options["prompt"][i:i+BATCH_SIZE]
|
121 |
-
if "negative_prompt" in batch_options:
|
122 |
-
batch_options["negative_prompt"] = options["negative_prompt"][i:i+BATCH_SIZE]
|
123 |
-
images.extend(pipe(**batch_options).images)
|
124 |
-
|
125 |
-
image_paths = [save_image(img) for img in images]
|
126 |
-
return image_paths, seed
|
127 |
-
#Main gr.Block
|
128 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
129 |
-
gr.Markdown(DESCRIPTIONx)
|
130 |
-
with gr.Group():
|
131 |
-
with gr.Row():
|
132 |
-
prompt = gr.Text(
|
133 |
-
label="Prompt",
|
134 |
-
show_label=False,
|
135 |
-
max_lines=1,
|
136 |
-
placeholder="Enter your prompt",
|
137 |
-
container=False,
|
138 |
-
)
|
139 |
-
run_button = gr.Button("Run", scale=0)
|
140 |
-
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
141 |
-
with gr.Accordion("Advanced options", open=False, visible=False):
|
142 |
-
num_images = gr.Slider(
|
143 |
-
label="Number of Images",
|
144 |
-
minimum=1,
|
145 |
-
maximum=4,
|
146 |
-
step=1,
|
147 |
-
value=1,
|
148 |
-
)
|
149 |
-
with gr.Row():
|
150 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True)
|
151 |
-
negative_prompt = gr.Text(
|
152 |
-
label="Negative prompt",
|
153 |
-
max_lines=5,
|
154 |
-
lines=4,
|
155 |
-
placeholder="Enter a negative prompt",
|
156 |
-
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",
|
157 |
-
visible=True,
|
158 |
-
)
|
159 |
-
seed = gr.Slider(
|
160 |
-
label="Seed",
|
161 |
-
minimum=0,
|
162 |
-
maximum=MAX_SEED,
|
163 |
-
step=1,
|
164 |
-
value=0,
|
165 |
-
)
|
166 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
167 |
-
with gr.Row(visible=True):
|
168 |
-
width = gr.Slider(
|
169 |
-
label="Width",
|
170 |
-
minimum=512,
|
171 |
-
maximum=MAX_IMAGE_SIZE,
|
172 |
-
step=64,
|
173 |
-
value=1024,
|
174 |
-
)
|
175 |
-
height = gr.Slider(
|
176 |
-
label="Height",
|
177 |
-
minimum=512,
|
178 |
-
maximum=MAX_IMAGE_SIZE,
|
179 |
-
step=64,
|
180 |
-
value=1024,
|
181 |
-
)
|
182 |
-
with gr.Row():
|
183 |
-
guidance_scale = gr.Slider(
|
184 |
-
label="Guidance Scale",
|
185 |
-
minimum=0.1,
|
186 |
-
maximum=6,
|
187 |
-
step=0.1,
|
188 |
-
value=3.0,
|
189 |
-
)
|
190 |
-
num_inference_steps = gr.Slider(
|
191 |
-
label="Number of inference steps",
|
192 |
-
minimum=1,
|
193 |
-
maximum=25,
|
194 |
-
step=1,
|
195 |
-
value=23,
|
196 |
-
)
|
197 |
-
|
198 |
-
gr.Examples(
|
199 |
-
examples=examples,
|
200 |
-
inputs=prompt,
|
201 |
-
cache_examples=False
|
202 |
-
)
|
203 |
-
|
204 |
-
use_negative_prompt.change(
|
205 |
-
fn=lambda x: gr.update(visible=x),
|
206 |
-
inputs=use_negative_prompt,
|
207 |
-
outputs=negative_prompt,
|
208 |
-
api_name=False,
|
209 |
-
)
|
210 |
-
|
211 |
-
gr.on(
|
212 |
-
triggers=[
|
213 |
-
prompt.submit,
|
214 |
-
negative_prompt.submit,
|
215 |
-
run_button.click,
|
216 |
-
],
|
217 |
-
fn=generate,
|
218 |
-
inputs=[
|
219 |
-
prompt,
|
220 |
-
negative_prompt,
|
221 |
-
use_negative_prompt,
|
222 |
-
seed,
|
223 |
-
width,
|
224 |
-
height,
|
225 |
-
guidance_scale,
|
226 |
-
num_inference_steps,
|
227 |
-
randomize_seed,
|
228 |
-
num_images
|
229 |
-
],
|
230 |
-
outputs=[result, seed],
|
231 |
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