Spaces:
Running
on
Zero
Running
on
Zero
Alexander Bagus
commited on
Commit
·
5af6243
1
Parent(s):
16bea72
22
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import torch, random, json, spaces, time
|
|
|
|
| 4 |
from diffsynth.pipelines.qwen_image import (
|
| 5 |
QwenImagePipeline, ModelConfig,
|
| 6 |
QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode
|
|
@@ -14,6 +15,7 @@ from utils import repo_utils, image_utils, prompt_utils
|
|
| 14 |
# repo_utils.clone_repo_if_not_exists("git clone https://huggingface.co/DiffSynth-Studio/General-Image-Encoders", "app/repos")
|
| 15 |
# repo_utils.clone_repo_if_not_exists("https://huggingface.co/apple/starflow", "app/models")
|
| 16 |
|
|
|
|
| 17 |
DTYPE = torch.bfloat16
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
| 19 |
|
|
@@ -57,35 +59,14 @@ pipe = QwenImagePipeline.from_pretrained(
|
|
| 57 |
)
|
| 58 |
|
| 59 |
|
| 60 |
-
|
| 61 |
-
# pipe = ZImageControlPipeline(
|
| 62 |
-
# vae=vae,
|
| 63 |
-
# tokenizer=tokenizer,
|
| 64 |
-
# text_encoder=text_encoder,
|
| 65 |
-
# transformer=transformer,
|
| 66 |
-
# scheduler=scheduler,
|
| 67 |
-
# )
|
| 68 |
-
# pipe.to("cuda", DTYPE)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# def prepare(prompt, is_polish_prompt):
|
| 72 |
-
# if not is_polish_prompt: return prompt, False
|
| 73 |
-
# polished_prompt = prompt_utils.polish_prompt(prompt)
|
| 74 |
-
# return polished_prompt, True
|
| 75 |
-
|
| 76 |
-
|
| 77 |
@spaces.GPU
|
| 78 |
-
def
|
| 79 |
-
|
| 80 |
-
negative_prompt,
|
| 81 |
-
seed=42,
|
| 82 |
-
randomize_seed=True,
|
| 83 |
-
guidance_scale=1.5,
|
| 84 |
-
num_inference_steps=8,
|
| 85 |
progress=gr.Progress(track_tqdm=True),
|
| 86 |
):
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
# Load images
|
| 91 |
images = [
|
|
@@ -102,40 +83,25 @@ def inference(
|
|
| 102 |
embs = QwenImageUnit_Image2LoRAEncode().process(pipe, image2lora_images=images)
|
| 103 |
lora = QwenImageUnit_Image2LoRADecode().process(pipe, **embs)["lora"]
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
return True
|
| 108 |
|
| 109 |
-
|
| 110 |
-
# print("DEBUG: process image")
|
| 111 |
-
# if input_image is None:
|
| 112 |
-
# print("Error: input_image is empty.")
|
| 113 |
-
# return None
|
| 114 |
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
|
| 122 |
-
# if randomize_seed: seed = random.randint(0, MAX_SEED)
|
| 123 |
-
# generator = torch.Generator().manual_seed(seed)
|
| 124 |
-
|
| 125 |
-
# output_image = pipe(
|
| 126 |
-
# prompt=prompt,
|
| 127 |
-
# negative_prompt = negative_prompt,
|
| 128 |
-
# width=width,
|
| 129 |
-
# height=height,
|
| 130 |
-
# generator=generator,
|
| 131 |
-
# guidance_scale=guidance_scale,
|
| 132 |
-
# control_image=control_image_torch,
|
| 133 |
-
# num_inference_steps=num_inference_steps,
|
| 134 |
-
# control_context_scale=control_context_scale,
|
| 135 |
-
# ).images[0]
|
| 136 |
-
|
| 137 |
-
# output_image = output_image.resize((orig_width * image_scale, orig_height * image_scale))
|
| 138 |
-
# return output_image, seed
|
| 139 |
|
| 140 |
|
| 141 |
def read_file(path: str) -> str:
|
|
@@ -151,15 +117,17 @@ css = """
|
|
| 151 |
}
|
| 152 |
"""
|
| 153 |
|
| 154 |
-
with open('examples/0_examples.json', 'r') as file: examples = json.load(file)
|
| 155 |
|
|
|
|
|
|
|
| 156 |
with gr.Blocks() as demo:
|
|
|
|
| 157 |
with gr.Column(elem_id="col-container"):
|
| 158 |
with gr.Column():
|
| 159 |
gr.HTML(read_file("static/header.html"))
|
| 160 |
with gr.Row():
|
| 161 |
with gr.Column():
|
| 162 |
-
|
| 163 |
label="Generated images",
|
| 164 |
show_label=False,
|
| 165 |
elem_id="gallery",
|
|
@@ -167,6 +135,15 @@ with gr.Blocks() as demo:
|
|
| 167 |
object_fit="cover",
|
| 168 |
height=300)
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
prompt = gr.Textbox(
|
| 171 |
label="Prompt",
|
| 172 |
show_label=False,
|
|
@@ -175,13 +152,10 @@ with gr.Blocks() as demo:
|
|
| 175 |
value="a man in a fishing boat. high quality, detailed"
|
| 176 |
# container=False,
|
| 177 |
)
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
# label="Control Mode"
|
| 183 |
-
# )
|
| 184 |
-
run_button = gr.Button("Generate", variant="primary")
|
| 185 |
with gr.Accordion("Advanced Settings", open=False):
|
| 186 |
|
| 187 |
negative_prompt = gr.Textbox(
|
|
@@ -227,26 +201,16 @@ with gr.Blocks() as demo:
|
|
| 227 |
|
| 228 |
with gr.Column():
|
| 229 |
output_image = gr.Image(label="Generated image", show_label=False)
|
| 230 |
-
# polished_prompt = gr.Textbox(label="Polished prompt", interactive=False)
|
| 231 |
-
|
| 232 |
-
# with gr.Accordion("Preprocessor output", open=False):
|
| 233 |
-
# control_image = gr.Image(label="Control image", show_label=False)
|
| 234 |
-
|
| 235 |
|
| 236 |
# gr.Examples(examples=examples, inputs=[input_image])
|
| 237 |
gr.Markdown(read_file("static/footer.md"))
|
| 238 |
|
| 239 |
-
|
| 240 |
-
fn=
|
| 241 |
inputs=[
|
| 242 |
-
|
| 243 |
-
negative_prompt,
|
| 244 |
-
seed,
|
| 245 |
-
randomize_seed,
|
| 246 |
-
guidance_scale,
|
| 247 |
-
num_inference_steps,
|
| 248 |
],
|
| 249 |
-
outputs=[
|
| 250 |
)
|
| 251 |
|
| 252 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import torch, random, json, spaces, time
|
| 4 |
+
from ulid import ULID
|
| 5 |
from diffsynth.pipelines.qwen_image import (
|
| 6 |
QwenImagePipeline, ModelConfig,
|
| 7 |
QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode
|
|
|
|
| 15 |
# repo_utils.clone_repo_if_not_exists("git clone https://huggingface.co/DiffSynth-Studio/General-Image-Encoders", "app/repos")
|
| 16 |
# repo_utils.clone_repo_if_not_exists("https://huggingface.co/apple/starflow", "app/models")
|
| 17 |
|
| 18 |
+
URL_PUBLIC = "https://huggingface.co/spaces/AiSudo/Qwen-Image-to-LoRA/blob/main"
|
| 19 |
DTYPE = torch.bfloat16
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
@spaces.GPU
|
| 63 |
+
def generate_lora(
|
| 64 |
+
images,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
progress=gr.Progress(track_tqdm=True),
|
| 66 |
):
|
| 67 |
+
|
| 68 |
+
ulid = str(ULID()).lower()
|
| 69 |
+
print(f"ulid: {ulid}")
|
| 70 |
|
| 71 |
# Load images
|
| 72 |
images = [
|
|
|
|
| 83 |
embs = QwenImageUnit_Image2LoRAEncode().process(pipe, image2lora_images=images)
|
| 84 |
lora = QwenImageUnit_Image2LoRADecode().process(pipe, **embs)["lora"]
|
| 85 |
|
| 86 |
+
lora_path = f"loras/{ulid}.safetensors"
|
| 87 |
+
lora_url = f"{URL_PUBLIC}/{lora_path}"
|
|
|
|
| 88 |
|
| 89 |
+
save_file(lora, lora_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
return True
|
| 92 |
|
| 93 |
+
@spaces.GPU
|
| 94 |
+
def generate_image(
|
| 95 |
+
prompt,
|
| 96 |
+
negative_prompt,
|
| 97 |
+
seed=42,
|
| 98 |
+
randomize_seed=True,
|
| 99 |
+
guidance_scale=1.5,
|
| 100 |
+
num_inference_steps=8,
|
| 101 |
+
progress=gr.Progress(track_tqdm=True),
|
| 102 |
+
):
|
| 103 |
|
| 104 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
def read_file(path: str) -> str:
|
|
|
|
| 117 |
}
|
| 118 |
"""
|
| 119 |
|
|
|
|
| 120 |
|
| 121 |
+
|
| 122 |
+
with open('examples/0_examples.json', 'r') as file: examples = json.load(file)
|
| 123 |
with gr.Blocks() as demo:
|
| 124 |
+
has_lora = True
|
| 125 |
with gr.Column(elem_id="col-container"):
|
| 126 |
with gr.Column():
|
| 127 |
gr.HTML(read_file("static/header.html"))
|
| 128 |
with gr.Row():
|
| 129 |
with gr.Column():
|
| 130 |
+
input_images = gr.Gallery(
|
| 131 |
label="Generated images",
|
| 132 |
show_label=False,
|
| 133 |
elem_id="gallery",
|
|
|
|
| 135 |
object_fit="cover",
|
| 136 |
height=300)
|
| 137 |
|
| 138 |
+
lora_button = gr.Button("Generate LoRA", variant="primary")
|
| 139 |
+
|
| 140 |
+
with gr.Column():
|
| 141 |
+
lora_path = gr.Textbox(label="Generated LoRA path",lines=2, interactive=False)
|
| 142 |
+
lora_download = gr.DownloadButton(label=f"Download LoRA", visible=has_lora)
|
| 143 |
+
|
| 144 |
+
with gr.Row(visible=has_lora):
|
| 145 |
+
gr.Markdown("Your LoRA is ready! Now, try generating some images.")
|
| 146 |
+
with gr.Column():
|
| 147 |
prompt = gr.Textbox(
|
| 148 |
label="Prompt",
|
| 149 |
show_label=False,
|
|
|
|
| 152 |
value="a man in a fishing boat. high quality, detailed"
|
| 153 |
# container=False,
|
| 154 |
)
|
| 155 |
+
|
| 156 |
+
imagen_button = gr.Button("Generate Image", variant="primary")
|
| 157 |
+
|
| 158 |
+
|
|
|
|
|
|
|
|
|
|
| 159 |
with gr.Accordion("Advanced Settings", open=False):
|
| 160 |
|
| 161 |
negative_prompt = gr.Textbox(
|
|
|
|
| 201 |
|
| 202 |
with gr.Column():
|
| 203 |
output_image = gr.Image(label="Generated image", show_label=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
# gr.Examples(examples=examples, inputs=[input_image])
|
| 206 |
gr.Markdown(read_file("static/footer.md"))
|
| 207 |
|
| 208 |
+
lora_button.click(
|
| 209 |
+
fn=generate_lora,
|
| 210 |
inputs=[
|
| 211 |
+
input_images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
],
|
| 213 |
+
outputs=[lora_path, lora_download, has_lora],
|
| 214 |
)
|
| 215 |
|
| 216 |
|