Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,19 @@ import gradio as gr
|
|
6 |
import numpy as np
|
7 |
import torch
|
8 |
from PIL import Image
|
9 |
-
from diffusers import EDMEulerScheduler, StableDiffusionXLInstructPix2PixPipeline,
|
10 |
from huggingface_hub import hf_hub_download, InferenceClient
|
11 |
-
from diffusers import DiffusionPipeline
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
|
|
17 |
|
18 |
help_text = """
|
19 |
To optimize image results:
|
@@ -39,26 +44,18 @@ def set_timesteps_patched(self, num_inference_steps: int, device = None):
|
|
39 |
self.sigmas = self.sigmas.to("cpu")
|
40 |
|
41 |
# Image Editor
|
42 |
-
|
43 |
-
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
44 |
-
|
45 |
-
refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
46 |
-
refiner.to("cuda")
|
47 |
-
|
48 |
edit_file = hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl_edit.safetensors")
|
49 |
EDMEulerScheduler.set_timesteps = set_timesteps_patched
|
50 |
pipe_edit = StableDiffusionXLInstructPix2PixPipeline.from_single_file( edit_file, num_in_channels=8, is_cosxl_edit=True, vae=vae, torch_dtype=torch.float16 )
|
51 |
pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
|
52 |
pipe_edit.to("cuda")
|
53 |
|
54 |
-
|
55 |
-
|
56 |
client1 = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
57 |
-
system_instructions1 = "<|system|>\nAct as Image Prompt Generation expert, Your task is to modify prompt by USER to more better
|
58 |
|
59 |
def promptifier(prompt):
|
60 |
formatted_prompt = f"{system_instructions1}{prompt}\n<|assistant|>\n"
|
61 |
-
stream = client1.text_generation(formatted_prompt, max_new_tokens=
|
62 |
return stream
|
63 |
|
64 |
# Generator
|
@@ -74,6 +71,7 @@ def king(type ,
|
|
74 |
width: int = 1024,
|
75 |
height: int = 1024,
|
76 |
guidance_scale: float = 6,
|
|
|
77 |
progress=gr.Progress(track_tqdm=True)
|
78 |
):
|
79 |
if type=="Image Editing" :
|
@@ -104,15 +102,29 @@ def king(type ,
|
|
104 |
print(f"BEFORE: {instruction} ")
|
105 |
instruction = promptifier(instruction)
|
106 |
print(f"AFTER: {instruction} ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
|
109 |
-
prompt = instruction,
|
110 |
negative_prompt = negative_prompt,
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
guidance_scale=0.0 ).images[0]
|
116 |
return seed, refine
|
117 |
|
118 |
client = InferenceClient()
|
@@ -190,6 +202,7 @@ with gr.Blocks(css=css) as demo:
|
|
190 |
with gr.Row():
|
191 |
type = gr.Dropdown(["Image Generation","Image Editing"], label="Task", value="Image Generation",interactive=True)
|
192 |
enhance_prompt = gr.Checkbox(label="Enhance prompt", value=False, scale=0)
|
|
|
193 |
|
194 |
with gr.Row():
|
195 |
input_image = gr.Image(label="Image", type='filepath', interactive=True)
|
@@ -242,6 +255,7 @@ with gr.Blocks(css=css) as demo:
|
|
242 |
width,
|
243 |
height,
|
244 |
guidance_scale,
|
|
|
245 |
],
|
246 |
outputs=[seed, input_image],
|
247 |
api_name = "image_gen_pro",
|
|
|
6 |
import numpy as np
|
7 |
import torch
|
8 |
from PIL import Image
|
9 |
+
from diffusers import StableDiffusionXLImg2ImgPipeline, StableDiffusionXLPipeline, EDMEulerScheduler, StableDiffusionXLInstructPix2PixPipeline, AutoencoderKL, DPMSolverMultistepScheduler
|
10 |
from huggingface_hub import hf_hub_download, InferenceClient
|
|
|
11 |
|
12 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
13 |
+
pipe = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V4.0", torch_dtype=torch.float16, vae=vae)
|
14 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
|
15 |
+
pipe.to("cuda")
|
16 |
+
|
17 |
+
refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained("stabilityai/stable-diffusion-xl-refiner-1.0", vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
18 |
+
refiner.to("cuda")
|
19 |
|
20 |
+
pipe_fast = StableDiffusionXLPipeline.from_pretrained("SG161222/RealVisXL_V4.0_Lightning", torch_dtype=torch.float16, vae=vae, use_safetensors=True)
|
21 |
+
pipe_fast.to("cuda")
|
22 |
|
23 |
help_text = """
|
24 |
To optimize image results:
|
|
|
44 |
self.sigmas = self.sigmas.to("cpu")
|
45 |
|
46 |
# Image Editor
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
edit_file = hf_hub_download(repo_id="stabilityai/cosxl", filename="cosxl_edit.safetensors")
|
48 |
EDMEulerScheduler.set_timesteps = set_timesteps_patched
|
49 |
pipe_edit = StableDiffusionXLInstructPix2PixPipeline.from_single_file( edit_file, num_in_channels=8, is_cosxl_edit=True, vae=vae, torch_dtype=torch.float16 )
|
50 |
pipe_edit.scheduler = EDMEulerScheduler(sigma_min=0.002, sigma_max=120.0, sigma_data=1.0, prediction_type="v_prediction")
|
51 |
pipe_edit.to("cuda")
|
52 |
|
|
|
|
|
53 |
client1 = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
54 |
+
system_instructions1 = "<|system|>\nAct as Image Prompt Generation expert, Your task is to modify prompt by USER to more better prompt for Image Generation in Stable Diffusion XL. \n Modify the user's prompt to generate a high-quality image by incorporating essential keywords and styles according to prompt if none style is mentioned than assume realistic. The optimized prompt may include keywords according to prompt for resolution (4K, HD, 16:9 aspect ratio, , etc.), image quality (cute, masterpiece, high-quality, vivid colors, intricate details, etc.), and desired art styles (realistic, anime, 3D, logo, futuristic, fantasy, etc.). Ensure the prompt is concise, yet comprehensive and choose keywords wisely, to generate an exceptional image that meets the user's expectations. \n Your task is to reply with final optimized prompt only. If you get big prompt make it concise. and Apply all keyword at last of prompt. Reply with optimized prompt only.\n<|user|>\n"
|
55 |
|
56 |
def promptifier(prompt):
|
57 |
formatted_prompt = f"{system_instructions1}{prompt}\n<|assistant|>\n"
|
58 |
+
stream = client1.text_generation(formatted_prompt, max_new_tokens=100)
|
59 |
return stream
|
60 |
|
61 |
# Generator
|
|
|
71 |
width: int = 1024,
|
72 |
height: int = 1024,
|
73 |
guidance_scale: float = 6,
|
74 |
+
fast=True,
|
75 |
progress=gr.Progress(track_tqdm=True)
|
76 |
):
|
77 |
if type=="Image Editing" :
|
|
|
102 |
print(f"BEFORE: {instruction} ")
|
103 |
instruction = promptifier(instruction)
|
104 |
print(f"AFTER: {instruction} ")
|
105 |
+
guidance_scale2=(guidance_scale/2)
|
106 |
+
if fast:
|
107 |
+
refine = pipe_fast(prompt = instruction,
|
108 |
+
guidance_scale = guidance_scale2,
|
109 |
+
num_inference_steps = int(steps/2.5),
|
110 |
+
width = width, height = height,
|
111 |
+
generator = generator,
|
112 |
+
).images[0]
|
113 |
+
else:
|
114 |
+
image = pipe_fast( prompt = instruction,
|
115 |
+
negative_prompt=negative_prompt,
|
116 |
+
guidance_scale = guidance_scale,
|
117 |
+
num_inference_steps = steps,
|
118 |
+
width = width, height = height,
|
119 |
+
generator = generator, output_type="latent",
|
120 |
+
).images
|
121 |
|
122 |
+
refine = refiner( prompt=instruction,
|
|
|
123 |
negative_prompt = negative_prompt,
|
124 |
+
guidance_scale = 7.5,
|
125 |
+
num_inference_steps= steps,
|
126 |
+
image=image, generator=generator,
|
127 |
+
).images[0]
|
|
|
128 |
return seed, refine
|
129 |
|
130 |
client = InferenceClient()
|
|
|
202 |
with gr.Row():
|
203 |
type = gr.Dropdown(["Image Generation","Image Editing"], label="Task", value="Image Generation",interactive=True)
|
204 |
enhance_prompt = gr.Checkbox(label="Enhance prompt", value=False, scale=0)
|
205 |
+
fast = gr.Checkbox(label="FAST Generation", value=True, scale=0)
|
206 |
|
207 |
with gr.Row():
|
208 |
input_image = gr.Image(label="Image", type='filepath', interactive=True)
|
|
|
255 |
width,
|
256 |
height,
|
257 |
guidance_scale,
|
258 |
+
fast,
|
259 |
],
|
260 |
outputs=[seed, input_image],
|
261 |
api_name = "image_gen_pro",
|