Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,320 +0,0 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
|
3 |
-
import os
|
4 |
-
import random
|
5 |
-
import uuid
|
6 |
-
import json
|
7 |
-
|
8 |
-
import gradio as gr
|
9 |
-
import numpy as np
|
10 |
-
from PIL import Image
|
11 |
-
import spaces
|
12 |
-
import torch
|
13 |
-
from diffusers import DiffusionPipeline
|
14 |
-
from typing import Tuple
|
15 |
-
|
16 |
-
#Check for the Model Base..//
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
bad_words = json.loads(os.getenv('BAD_WORDS', "[]"))
|
21 |
-
bad_words_negative = json.loads(os.getenv('BAD_WORDS_NEGATIVE', "[]"))
|
22 |
-
default_negative = os.getenv("default_negative","")
|
23 |
-
|
24 |
-
def check_text(prompt, negative=""):
|
25 |
-
for i in bad_words:
|
26 |
-
if i in prompt:
|
27 |
-
return True
|
28 |
-
for i in bad_words_negative:
|
29 |
-
if i in negative:
|
30 |
-
return True
|
31 |
-
return False
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
style_list = [
|
36 |
-
|
37 |
-
{
|
38 |
-
"name": "2560 x 1440",
|
39 |
-
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
40 |
-
"negative_prompt": "cartoonish, low resolution, blurry, simplistic, abstract, deformed, ugly",
|
41 |
-
},
|
42 |
-
|
43 |
-
{
|
44 |
-
"name": "Photo",
|
45 |
-
"prompt": "cinematic photo {prompt}. 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
46 |
-
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
47 |
-
},
|
48 |
-
|
49 |
-
{
|
50 |
-
"name": "Cinematic",
|
51 |
-
"prompt": "cinematic still {prompt}. emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
52 |
-
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
53 |
-
},
|
54 |
-
|
55 |
-
{
|
56 |
-
"name": "Anime",
|
57 |
-
"prompt": "anime artwork {prompt}. anime style, key visual, vibrant, studio anime, highly detailed",
|
58 |
-
"negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
|
59 |
-
},
|
60 |
-
{
|
61 |
-
"name": "3D Model",
|
62 |
-
"prompt": "professional 3d model {prompt}. octane render, highly detailed, volumetric, dramatic lighting",
|
63 |
-
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
64 |
-
},
|
65 |
-
{
|
66 |
-
"name": "(No style)",
|
67 |
-
"prompt": "{prompt}",
|
68 |
-
"negative_prompt": "",
|
69 |
-
},
|
70 |
-
]
|
71 |
-
|
72 |
-
styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
|
73 |
-
STYLE_NAMES = list(styles.keys())
|
74 |
-
DEFAULT_STYLE_NAME = "2560 x 1440"
|
75 |
-
|
76 |
-
def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
|
77 |
-
p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
|
78 |
-
if not negative:
|
79 |
-
negative = ""
|
80 |
-
return p.replace("{prompt}", positive), n + negative
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
DESCRIPTION = """## MidJourney-V6
|
87 |
-
|
88 |
-
hyper realistic
|
89 |
-
"""
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
if not torch.cuda.is_available():
|
94 |
-
DESCRIPTION += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
|
95 |
-
|
96 |
-
MAX_SEED = np.iinfo(np.int32).max
|
97 |
-
CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "0") == "1"
|
98 |
-
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "2048"))
|
99 |
-
USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
|
100 |
-
ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
|
101 |
-
|
102 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
103 |
-
|
104 |
-
NUM_IMAGES_PER_PROMPT = 1
|
105 |
-
|
106 |
-
if torch.cuda.is_available():
|
107 |
-
pipe = DiffusionPipeline.from_pretrained(
|
108 |
-
"SG161222/RealVisXL_V3.0_Turbo",
|
109 |
-
torch_dtype=torch.float16,
|
110 |
-
use_safetensors=True,
|
111 |
-
add_watermarker=False,
|
112 |
-
variant="fp16"
|
113 |
-
)
|
114 |
-
pipe2 = DiffusionPipeline.from_pretrained(
|
115 |
-
"SG161222/RealVisXL_V2.02_Turbo",
|
116 |
-
torch_dtype=torch.float16,
|
117 |
-
use_safetensors=True,
|
118 |
-
add_watermarker=False,
|
119 |
-
variant="fp16"
|
120 |
-
)
|
121 |
-
if ENABLE_CPU_OFFLOAD:
|
122 |
-
pipe.enable_model_cpu_offload()
|
123 |
-
pipe2.enable_model_cpu_offload()
|
124 |
-
else:
|
125 |
-
pipe.to(device)
|
126 |
-
pipe2.to(device)
|
127 |
-
print("Loaded on Device!")
|
128 |
-
|
129 |
-
if USE_TORCH_COMPILE:
|
130 |
-
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
131 |
-
pipe2.unet = torch.compile(pipe2.unet, mode="reduce-overhead", fullgraph=True)
|
132 |
-
print("Model Compiled!")
|
133 |
-
|
134 |
-
def save_image(img):
|
135 |
-
unique_name = str(uuid.uuid4()) + ".png"
|
136 |
-
img.save(unique_name)
|
137 |
-
return unique_name
|
138 |
-
|
139 |
-
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
140 |
-
if randomize_seed:
|
141 |
-
seed = random.randint(0, MAX_SEED)
|
142 |
-
return seed
|
143 |
-
|
144 |
-
@spaces.GPU(enable_queue=True)
|
145 |
-
def generate(
|
146 |
-
prompt: str,
|
147 |
-
negative_prompt: str = "",
|
148 |
-
use_negative_prompt: bool = False,
|
149 |
-
style: str = DEFAULT_STYLE_NAME,
|
150 |
-
seed: int = 0,
|
151 |
-
width: int = 1024,
|
152 |
-
height: int = 1024,
|
153 |
-
guidance_scale: float = 3,
|
154 |
-
randomize_seed: bool = False,
|
155 |
-
use_resolution_binning: bool = True,
|
156 |
-
progress=gr.Progress(track_tqdm=True),
|
157 |
-
):
|
158 |
-
if check_text(prompt, negative_prompt):
|
159 |
-
raise ValueError("Prompt contains restricted words.")
|
160 |
-
|
161 |
-
prompt, negative_prompt = apply_style(style, prompt, negative_prompt)
|
162 |
-
seed = int(randomize_seed_fn(seed, randomize_seed))
|
163 |
-
generator = torch.Generator().manual_seed(seed)
|
164 |
-
|
165 |
-
if not use_negative_prompt:
|
166 |
-
negative_prompt = "" # type: ignore
|
167 |
-
negative_prompt += default_negative
|
168 |
-
|
169 |
-
options = {
|
170 |
-
"prompt": prompt,
|
171 |
-
"negative_prompt": negative_prompt,
|
172 |
-
"width": width,
|
173 |
-
"height": height,
|
174 |
-
"guidance_scale": guidance_scale,
|
175 |
-
"num_inference_steps": 25,
|
176 |
-
"generator": generator,
|
177 |
-
"num_images_per_prompt": NUM_IMAGES_PER_PROMPT,
|
178 |
-
"use_resolution_binning": use_resolution_binning,
|
179 |
-
"output_type": "pil",
|
180 |
-
}
|
181 |
-
|
182 |
-
images = pipe(**options).images + pipe2(**options).images
|
183 |
-
|
184 |
-
image_paths = [save_image(img) for img in images]
|
185 |
-
return image_paths, seed
|
186 |
-
|
187 |
-
examples = [
|
188 |
-
"Annie Leibovitz tarzında ve Wes Anderson tarzında, rustik bir kabinde, sığ bir alan derinliğine sahip, vintage film grenli, yakın çekim bir kedinin, bir pencerenin yakın çekimi. --ar 85:128 --v 6.0 --stil ham",
|
189 |
-
"Daria Morgendorffer animasyon serisinin ana karakteri Daria, ciddi ifadesi, çok heyecan verici şehvetli görünümü, çok ateşli bir kız, güzel karizmatik bir kız, çok ateşli bir atış, gözlük takan bir kadın, muhteşem bir figür, ilginç şekiller, gerçek boyutlu figürler",
|
190 |
-
"Koyu yeşil büyük antoryum yaprakları, yakın çekim, fotoğraf, havadan görünüm, unsplash tarzında, hasselblad h6d400c --ar 85:128 --v 6.0 --style raw",
|
191 |
-
"Sarışın kadının yakın çekimi alan derinliği, bokeh, sığ odak, minimalizm, Canon EF lensli Fujifilm xh2s, sinematik --ar 85:128 --v 6.0 --style raw"
|
192 |
-
]
|
193 |
-
|
194 |
-
css = '''
|
195 |
-
.gradio-container{max-width: 700px !important}
|
196 |
-
h1{text-align:center}
|
197 |
-
'''
|
198 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
199 |
-
gr.Markdown(DESCRIPTION)
|
200 |
-
gr.DuplicateButton(
|
201 |
-
value="Duplicate Space for private use",
|
202 |
-
elem_id="duplicate-button",
|
203 |
-
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
|
204 |
-
)
|
205 |
-
with gr.Group():
|
206 |
-
with gr.Row():
|
207 |
-
prompt = gr.Text(
|
208 |
-
label="Prompt",
|
209 |
-
show_label=False,
|
210 |
-
max_lines=1,
|
211 |
-
placeholder="Enter your prompt",
|
212 |
-
container=False,
|
213 |
-
)
|
214 |
-
run_button = gr.Button("Run")
|
215 |
-
result = gr.Gallery(label="Result", columns=1, preview=True)
|
216 |
-
with gr.Accordion("Advanced options", open=False):
|
217 |
-
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=True, visible=True)
|
218 |
-
negative_prompt = gr.Text(
|
219 |
-
label="Negative prompt",
|
220 |
-
max_lines=1,
|
221 |
-
placeholder="Enter a negative prompt",
|
222 |
-
value="(deformed iris, deformed pupils, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck",
|
223 |
-
visible=True,
|
224 |
-
)
|
225 |
-
with gr.Row():
|
226 |
-
num_inference_steps = gr.Slider(
|
227 |
-
label="Steps",
|
228 |
-
minimum=10,
|
229 |
-
maximum=60,
|
230 |
-
step=1,
|
231 |
-
value=30,
|
232 |
-
)
|
233 |
-
with gr.Row():
|
234 |
-
num_images_per_prompt = gr.Slider(
|
235 |
-
label="Images",
|
236 |
-
minimum=1,
|
237 |
-
maximum=5,
|
238 |
-
step=1,
|
239 |
-
value=2,
|
240 |
-
)
|
241 |
-
seed = gr.Slider(
|
242 |
-
label="Seed",
|
243 |
-
minimum=0,
|
244 |
-
maximum=MAX_SEED,
|
245 |
-
step=1,
|
246 |
-
value=0,
|
247 |
-
visible=True
|
248 |
-
)
|
249 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
250 |
-
with gr.Row(visible=True):
|
251 |
-
width = gr.Slider(
|
252 |
-
label="Width",
|
253 |
-
minimum=512,
|
254 |
-
maximum=2048,
|
255 |
-
step=8,
|
256 |
-
value=1024,
|
257 |
-
)
|
258 |
-
height = gr.Slider(
|
259 |
-
label="Height",
|
260 |
-
minimum=512,
|
261 |
-
maximum=2048,
|
262 |
-
step=8,
|
263 |
-
value=1024,
|
264 |
-
)
|
265 |
-
with gr.Row():
|
266 |
-
guidance_scale = gr.Slider(
|
267 |
-
label="Guidance Scale",
|
268 |
-
minimum=0.1,
|
269 |
-
maximum=20.0,
|
270 |
-
step=0.1,
|
271 |
-
value=6,
|
272 |
-
)
|
273 |
-
with gr.Row(visible=True):
|
274 |
-
style_selection = gr.Radio(
|
275 |
-
show_label=True,
|
276 |
-
container=True,
|
277 |
-
interactive=True,
|
278 |
-
choices=STYLE_NAMES,
|
279 |
-
value=DEFAULT_STYLE_NAME,
|
280 |
-
label="Image Style",
|
281 |
-
)
|
282 |
-
gr.Examples(
|
283 |
-
examples=examples,
|
284 |
-
inputs=prompt,
|
285 |
-
outputs=[result, seed],
|
286 |
-
fn=generate,
|
287 |
-
cache_examples=CACHE_EXAMPLES,
|
288 |
-
)
|
289 |
-
|
290 |
-
use_negative_prompt.change(
|
291 |
-
fn=lambda x: gr.update(visible=x),
|
292 |
-
inputs=use_negative_prompt,
|
293 |
-
outputs=negative_prompt,
|
294 |
-
api_name=False,
|
295 |
-
)
|
296 |
-
|
297 |
-
gr.on(
|
298 |
-
triggers=[
|
299 |
-
prompt.submit,
|
300 |
-
negative_prompt.submit,
|
301 |
-
run_button.click,
|
302 |
-
],
|
303 |
-
fn=generate,
|
304 |
-
inputs=[
|
305 |
-
prompt,
|
306 |
-
negative_prompt,
|
307 |
-
use_negative_prompt,
|
308 |
-
style_selection,
|
309 |
-
seed,
|
310 |
-
width,
|
311 |
-
height,
|
312 |
-
guidance_scale,
|
313 |
-
randomize_seed,
|
314 |
-
],
|
315 |
-
outputs=[result, seed],
|
316 |
-
api_name="run",
|
317 |
-
)
|
318 |
-
|
319 |
-
if __name__ == "__main__":
|
320 |
-
demo.queue(max_size=20).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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