Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,286 +1,125 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
import spaces
|
4 |
import torch
|
5 |
-
import
|
6 |
-
import
|
7 |
-
import os
|
8 |
-
from PIL import Image
|
9 |
-
from diffusers import FluxKontextPipeline
|
10 |
-
from diffusers.utils import load_image
|
11 |
-
from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard
|
12 |
-
from safetensors.torch import load_file
|
13 |
-
import requests
|
14 |
-
import re
|
15 |
-
|
16 |
-
# Load Kontext model from your local path
|
17 |
-
MAX_SEED = np.iinfo(np.int32).max
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
"black-forest-labs/FLUX.1-Kontext-dev",
|
22 |
-
torch_dtype=torch.bfloat16
|
23 |
-
).to("cuda")
|
24 |
-
|
25 |
-
# Load LoRA data from our custom JSON file
|
26 |
-
with open("kontext_loras.json", "r") as file:
|
27 |
-
data = json.load(file)
|
28 |
-
# Add default values for keys that might be missing, to prevent errors
|
29 |
-
flux_loras_raw = [
|
30 |
-
{
|
31 |
-
"image": item["image"],
|
32 |
-
"title": item["title"],
|
33 |
-
"repo": item["repo"],
|
34 |
-
"weights": item.get("weights", "pytorch_lora_weights.safetensors"),
|
35 |
-
"prompt": item.get("prompt", f"Turn this image into {item['title']} style."),
|
36 |
-
# The following keys are kept for compatibility with the original demo structure,
|
37 |
-
# but our simplified logic doesn't heavily rely on them.
|
38 |
-
"lora_type": item.get("lora_type", "flux"),
|
39 |
-
"lora_scale_config": item.get("lora_scale", 1.0), # Default scale set to 1.0
|
40 |
-
"prompt_placeholder": item.get("prompt_placeholder", "You can edit the prompt here..."),
|
41 |
-
}
|
42 |
-
for item in data
|
43 |
-
]
|
44 |
-
print(f"Loaded {len(flux_loras_raw)} LoRAs from kontext_loras.json")
|
45 |
-
|
46 |
-
def update_selection(selected_state: gr.SelectData, flux_loras):
|
47 |
-
"""Update UI when a LoRA is selected"""
|
48 |
-
if selected_state.index >= len(flux_loras):
|
49 |
-
return "### No LoRA selected", gr.update(), None, gr.update()
|
50 |
-
|
51 |
-
selected_lora = flux_loras[selected_state.index]
|
52 |
-
lora_repo = selected_lora["repo"]
|
53 |
-
default_prompt = selected_lora.get("prompt")
|
54 |
-
|
55 |
-
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo})"
|
56 |
|
57 |
-
|
58 |
-
print("Selected Style: ", selected_lora['title'])
|
59 |
-
print("Optimal Scale: ", optimal_scale)
|
60 |
-
return updated_text, gr.update(value=default_prompt), selected_state.index, optimal_scale
|
61 |
-
|
62 |
-
# This wrapper is kept for compatibility with the Gradio event triggers
|
63 |
-
def infer_with_lora_wrapper(input_image, prompt, selected_index, lora_state, custom_lora, seed=0, guidance_scale=2.5, num_inference_steps=28, lora_scale=1.0, flux_loras=None, progress=gr.Progress(track_tqdm=True)):
|
64 |
-
"""Wrapper function to handle state serialization"""
|
65 |
-
# The 'custom_lora' and 'lora_state' arguments are no longer used but kept in the signature
|
66 |
-
return infer_with_lora(input_image, prompt, selected_index, seed, guidance_scale, num_inference_steps, lora_scale, flux_loras, progress)
|
67 |
-
|
68 |
-
@spaces.GPU # This decorator is only for Hugging Face Spaces hardware, not needed for local execution
|
69 |
-
def infer_with_lora(input_image, prompt, selected_index, seed=0, guidance_scale=2.5, num_inference_steps=28, lora_scale=1.0, flux_loras=None, progress=gr.Progress(track_tqdm=True)):
|
70 |
-
"""Generate image with selected LoRA"""
|
71 |
-
global pipe
|
72 |
-
|
73 |
-
# The seed is now always taken directly from the input. Randomization has been removed.
|
74 |
-
|
75 |
-
# Unload any previous LoRA to ensure a clean state
|
76 |
-
if "selected_lora" in pipe.get_active_adapters():
|
77 |
-
pipe.unload_lora_weights()
|
78 |
-
|
79 |
-
# Determine which LoRA to use from our gallery
|
80 |
-
lora_to_use = None
|
81 |
-
if selected_index is not None and flux_loras and selected_index < len(flux_loras):
|
82 |
-
lora_to_use = flux_loras[selected_index]
|
83 |
-
|
84 |
-
if lora_to_use:
|
85 |
-
print(f"Applying LoRA: {lora_to_use['title']}")
|
86 |
-
try:
|
87 |
-
# Load LoRA directly from the Hugging Face Hub
|
88 |
-
pipe.load_lora_weights(
|
89 |
-
lora_to_use["repo"],
|
90 |
-
weight_name=lora_to_use["weights"],
|
91 |
-
adapter_name="selected_lora"
|
92 |
-
)
|
93 |
-
pipe.set_adapters(["selected_lora"], adapter_weights=[lora_scale])
|
94 |
-
print(f"Loaded {lora_to_use['repo']} with scale {lora_scale}")
|
95 |
-
|
96 |
-
except Exception as e:
|
97 |
-
print(f"Error loading LoRA: {e}")
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
print(f"Using prompt: {final_prompt}")
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
#main_app {
|
129 |
-
display: flex;
|
130 |
-
gap: 20px;
|
131 |
-
}
|
132 |
-
#box_column {
|
133 |
-
min-width: 400px;
|
134 |
-
}
|
135 |
-
#title{text-align: center}
|
136 |
-
#title h1{font-size: 3em; display:inline-flex; align-items:center}
|
137 |
-
#title img{width: 100px; margin-right: 0.5em}
|
138 |
-
#selected_lora {
|
139 |
-
color: #2563eb;
|
140 |
-
font-weight: bold;
|
141 |
-
}
|
142 |
-
#prompt {
|
143 |
-
flex-grow: 1;
|
144 |
-
}
|
145 |
-
#run_button {
|
146 |
-
background: linear-gradient(45deg, #2563eb, #3b82f6);
|
147 |
-
color: white;
|
148 |
-
border: none;
|
149 |
-
padding: 8px 16px;
|
150 |
-
border-radius: 6px;
|
151 |
-
font-weight: bold;
|
152 |
-
}
|
153 |
-
.custom_lora_card {
|
154 |
-
background: #f8fafc;
|
155 |
-
border: 1px solid #e2e8f0;
|
156 |
-
border-radius: 8px;
|
157 |
-
padding: 12px;
|
158 |
-
margin: 8px 0;
|
159 |
-
}
|
160 |
-
#gallery{
|
161 |
-
overflow: scroll !important
|
162 |
-
}
|
163 |
-
/* Custom CSS to ensure the input image is fully visible */
|
164 |
-
#input_image_display div[data-testid="image"] img {
|
165 |
-
object-fit: contain !important;
|
166 |
}
|
167 |
"""
|
168 |
|
169 |
-
|
170 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
171 |
-
gr_flux_loras = gr.State(value=flux_loras_raw)
|
172 |
|
173 |
-
|
174 |
-
"""<h1>FLUX Kontext Super LoRAs🖖</h1>""",
|
175 |
-
elem_id="title",
|
176 |
-
)
|
177 |
-
selected_state = gr.State(value=None)
|
178 |
-
# The following states are no longer used by the simplified logic but kept for component structure
|
179 |
-
custom_loaded_lora = gr.State(value=None)
|
180 |
-
lora_state = gr.State(value=1.0)
|
181 |
-
|
182 |
-
with gr.Row(elem_id="main_app"):
|
183 |
-
with gr.Column(scale=4, elem_id="box_column"):
|
184 |
-
with gr.Group(elem_id="gallery_box"):
|
185 |
-
input_image = gr.Image(
|
186 |
-
label="Upload a picture of yourself",
|
187 |
-
type="pil",
|
188 |
-
height=300,
|
189 |
-
elem_id="input_image_display"
|
190 |
-
)
|
191 |
-
gallery = gr.Gallery(
|
192 |
-
label="Pick a LoRA",
|
193 |
-
allow_preview=False,
|
194 |
-
columns=4,
|
195 |
-
elem_id="gallery",
|
196 |
-
show_share_button=False,
|
197 |
-
height=300,
|
198 |
-
object_fit="contain"
|
199 |
-
)
|
200 |
-
|
201 |
-
custom_model = gr.Textbox(
|
202 |
-
label="Or enter a custom HuggingFace FLUX LoRA",
|
203 |
-
placeholder="e.g., username/lora-name",
|
204 |
-
visible=False
|
205 |
-
)
|
206 |
-
custom_model_card = gr.HTML(visible=False)
|
207 |
-
custom_model_button = gr.Button("Remove custom LoRA", visible=False)
|
208 |
|
209 |
-
with gr.
|
210 |
-
with gr.Row():
|
211 |
-
prompt = gr.Textbox(
|
212 |
-
label="Editing Prompt",
|
213 |
-
show_label=False,
|
214 |
-
lines=1,
|
215 |
-
max_lines=1,
|
216 |
-
placeholder="opt - describe the person/subject, e.g. 'a man with glasses and a beard'",
|
217 |
-
elem_id="prompt"
|
218 |
-
)
|
219 |
-
run_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
|
220 |
|
221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
)
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
|
|
238 |
)
|
|
|
|
|
|
|
239 |
guidance_scale = gr.Slider(
|
240 |
label="Guidance Scale",
|
241 |
minimum=1,
|
242 |
-
maximum=
|
243 |
step=0.1,
|
244 |
-
value=
|
245 |
)
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
prompt_title = gr.Markdown(
|
256 |
-
value="### Click on a LoRA in the gallery to select it",
|
257 |
-
visible=True,
|
258 |
-
elem_id="selected_lora",
|
259 |
-
)
|
260 |
|
261 |
-
# Event handlers
|
262 |
-
# The custom model inputs are no longer needed as we've hidden them.
|
263 |
-
|
264 |
-
gallery.select(
|
265 |
-
fn=update_selection,
|
266 |
-
inputs=[gr_flux_loras],
|
267 |
-
outputs=[prompt_title, prompt, selected_state, lora_scale],
|
268 |
-
show_progress=False
|
269 |
-
)
|
270 |
-
|
271 |
gr.on(
|
272 |
triggers=[run_button.click, prompt.submit],
|
273 |
-
fn=
|
274 |
-
inputs=[
|
275 |
-
outputs=[result,
|
276 |
-
)
|
277 |
-
|
278 |
-
# Initialize gallery
|
279 |
-
demo.load(
|
280 |
-
fn=lambda loras: ([(item["image"], item["title"]) for item in loras], loras),
|
281 |
-
inputs=[gr_flux_loras],
|
282 |
-
outputs=[gallery, gr_flux_loras]
|
283 |
)
|
284 |
|
285 |
-
demo.queue(default_concurrency_limit=None)
|
286 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
import random
|
4 |
import spaces
|
5 |
import torch
|
6 |
+
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, FluxTransformer2DModel
|
7 |
+
from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
dtype = torch.bfloat16
|
10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
pipe = DiffusionPipeline.from_pretrained("prithivMLmods/Flux.1-krea-Merge-Transformer", torch_dtype=dtype).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
MAX_SEED = np.iinfo(np.int32).max
|
15 |
+
MAX_IMAGE_SIZE = 2048
|
|
|
16 |
|
17 |
+
@spaces.GPU()
|
18 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=8, progress=gr.Progress(track_tqdm=True)):
|
19 |
+
if randomize_seed:
|
20 |
+
seed = random.randint(0, MAX_SEED)
|
21 |
+
generator = torch.Generator().manual_seed(seed)
|
22 |
+
image = pipe(
|
23 |
+
prompt = prompt,
|
24 |
+
width = width,
|
25 |
+
height = height,
|
26 |
+
num_inference_steps = num_inference_steps,
|
27 |
+
generator = generator,
|
28 |
+
guidance_scale=guidance_scale
|
29 |
+
).images[0]
|
30 |
+
return image, seed
|
31 |
+
|
32 |
+
examples = [
|
33 |
+
"a tiny astronaut hatching from an egg on the moon",
|
34 |
+
"a cat holding a sign that says hello world",
|
35 |
+
"an anime illustration of a wiener schnitzel",
|
36 |
+
]
|
37 |
|
38 |
+
css="""
|
39 |
+
#col-container {
|
40 |
+
margin: 0 auto;
|
41 |
+
max-width: 520px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
}
|
43 |
"""
|
44 |
|
45 |
+
with gr.Blocks(css=css) as demo:
|
|
|
|
|
46 |
|
47 |
+
with gr.Column(elem_id="col-container"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
prompt = gr.Text(
|
52 |
+
label="Prompt",
|
53 |
+
show_label=False,
|
54 |
+
max_lines=1,
|
55 |
+
placeholder="Enter your prompt",
|
56 |
+
container=False,
|
57 |
+
)
|
58 |
+
run_button = gr.Button("Run", scale=0)
|
59 |
+
|
60 |
+
num_inference_steps = gr.Slider(
|
61 |
+
label="Number of inference steps",
|
62 |
+
minimum=1,
|
63 |
+
maximum=50,
|
64 |
+
step=1,
|
65 |
+
value=8,
|
66 |
+
)
|
67 |
+
|
68 |
+
result = gr.Image(label="Result", show_label=False)
|
69 |
+
|
70 |
+
with gr.Accordion("Advanced Settings", open=False):
|
71 |
|
72 |
+
seed = gr.Slider(
|
73 |
+
label="Seed",
|
74 |
+
minimum=0,
|
75 |
+
maximum=MAX_SEED,
|
76 |
+
step=1,
|
77 |
+
value=0,
|
78 |
+
)
|
79 |
+
|
80 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
|
84 |
+
width = gr.Slider(
|
85 |
+
label="Width",
|
86 |
+
minimum=256,
|
87 |
+
maximum=MAX_IMAGE_SIZE,
|
88 |
+
step=32,
|
89 |
+
value=1024,
|
90 |
)
|
91 |
+
|
92 |
+
height = gr.Slider(
|
93 |
+
label="Height",
|
94 |
+
minimum=256,
|
95 |
+
maximum=MAX_IMAGE_SIZE,
|
96 |
+
step=32,
|
97 |
+
value=1024,
|
98 |
)
|
99 |
+
|
100 |
+
with gr.Row():
|
101 |
+
|
102 |
guidance_scale = gr.Slider(
|
103 |
label="Guidance Scale",
|
104 |
minimum=1,
|
105 |
+
maximum=15,
|
106 |
step=0.1,
|
107 |
+
value=3.5,
|
108 |
)
|
109 |
+
|
110 |
+
gr.Examples(
|
111 |
+
examples = examples,
|
112 |
+
fn = infer,
|
113 |
+
inputs = [prompt],
|
114 |
+
outputs = [result, seed],
|
115 |
+
cache_examples="lazy"
|
116 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
gr.on(
|
119 |
triggers=[run_button.click, prompt.submit],
|
120 |
+
fn = infer,
|
121 |
+
inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
122 |
+
outputs = [result, seed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
)
|
124 |
|
|
|
125 |
demo.launch()
|