Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,8 @@ from src.lora_helper import set_single_lora, set_multi_lora, unset_lora
|
|
| 16 |
base_path = "black-forest-labs/FLUX.1-dev"
|
| 17 |
lora_base_path = "./models"
|
| 18 |
|
|
|
|
|
|
|
| 19 |
|
| 20 |
pipe = FluxPipeline.from_pretrained(base_path, torch_dtype=torch.bfloat16)
|
| 21 |
transformer = FluxTransformer2DModel.from_pretrained(base_path, subfolder="transformer", torch_dtype=torch.bfloat16)
|
|
@@ -28,16 +30,18 @@ def clear_cache(transformer):
|
|
| 28 |
|
| 29 |
# Define the Gradio interface
|
| 30 |
@spaces.GPU()
|
| 31 |
-
def single_condition_generate_image(
|
| 32 |
-
#
|
| 33 |
-
if
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
set_single_lora(pipe.transformer, lora_path, lora_weights=[1], cond_size=512)
|
| 36 |
|
| 37 |
# Process the image
|
| 38 |
spatial_imgs = [spatial_img] if spatial_img else []
|
| 39 |
image = pipe(
|
| 40 |
-
|
| 41 |
height=int(height),
|
| 42 |
width=int(width),
|
| 43 |
guidance_scale=3.5,
|
|
@@ -51,54 +55,283 @@ def single_condition_generate_image(prompt, spatial_img, height, width, seed, co
|
|
| 51 |
clear_cache(pipe.transformer)
|
| 52 |
return image
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-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gr.Column():
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
gr.Examples(
|
| 89 |
-
examples=
|
| 90 |
-
inputs=[
|
| 91 |
-
outputs=
|
| 92 |
fn=single_condition_generate_image,
|
| 93 |
-
cache_examples=False,
|
| 94 |
-
|
| 95 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
# Link the
|
| 98 |
-
|
| 99 |
single_condition_generate_image,
|
| 100 |
-
inputs=[
|
| 101 |
-
outputs=
|
| 102 |
)
|
| 103 |
|
| 104 |
# Launch the Gradio app
|
|
|
|
| 16 |
base_path = "black-forest-labs/FLUX.1-dev"
|
| 17 |
lora_base_path = "./models"
|
| 18 |
|
| 19 |
+
# System prompt that will be hidden from users but automatically added to their input
|
| 20 |
+
SYSTEM_PROMPT = "Ghibli Studio style, Charming hand-drawn anime-style illustration"
|
| 21 |
|
| 22 |
pipe = FluxPipeline.from_pretrained(base_path, torch_dtype=torch.bfloat16)
|
| 23 |
transformer = FluxTransformer2DModel.from_pretrained(base_path, subfolder="transformer", torch_dtype=torch.bfloat16)
|
|
|
|
| 30 |
|
| 31 |
# Define the Gradio interface
|
| 32 |
@spaces.GPU()
|
| 33 |
+
def single_condition_generate_image(user_prompt, spatial_img, height, width, seed):
|
| 34 |
+
# Combine the system prompt with user prompt
|
| 35 |
+
full_prompt = f"{SYSTEM_PROMPT}, {user_prompt}" if user_prompt else SYSTEM_PROMPT
|
| 36 |
+
|
| 37 |
+
# Set the Ghibli LoRA
|
| 38 |
+
lora_path = os.path.join(lora_base_path, "Ghibli.safetensors")
|
| 39 |
set_single_lora(pipe.transformer, lora_path, lora_weights=[1], cond_size=512)
|
| 40 |
|
| 41 |
# Process the image
|
| 42 |
spatial_imgs = [spatial_img] if spatial_img else []
|
| 43 |
image = pipe(
|
| 44 |
+
full_prompt,
|
| 45 |
height=int(height),
|
| 46 |
width=int(width),
|
| 47 |
guidance_scale=3.5,
|
|
|
|
| 55 |
clear_cache(pipe.transformer)
|
| 56 |
return image
|
| 57 |
|
| 58 |
+
# Load example images
|
| 59 |
+
def load_examples():
|
| 60 |
+
examples = []
|
| 61 |
+
test_img_dir = "./test_imgs"
|
| 62 |
+
example_prompts = [
|
| 63 |
+
"a cat sitting by the window",
|
| 64 |
+
"a peaceful mountain village",
|
| 65 |
+
"a young girl with flowers in her hair",
|
| 66 |
+
"a magical forest with spirits",
|
| 67 |
+
"a flying castle in the clouds",
|
| 68 |
+
"a serene river with boats",
|
| 69 |
+
"a cozy cottage in the countryside",
|
| 70 |
+
"a bustling market in a small town"
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
for i, filename in enumerate(["00.png", "02.png", "03.png", "04.png", "06.png", "07.png", "08.png", "09.png"]):
|
| 74 |
+
img_path = os.path.join(test_img_dir, filename)
|
| 75 |
+
if os.path.exists(img_path):
|
| 76 |
+
# Use dimensions from original code for each specific example
|
| 77 |
+
if filename == "00.png":
|
| 78 |
+
height, width = 680, 1024
|
| 79 |
+
elif filename == "02.png":
|
| 80 |
+
height, width = 560, 1024
|
| 81 |
+
elif filename == "03.png":
|
| 82 |
+
height, width = 568, 1024
|
| 83 |
+
elif filename == "04.png":
|
| 84 |
+
height, width = 768, 672
|
| 85 |
+
elif filename == "06.png":
|
| 86 |
+
height, width = 896, 1024
|
| 87 |
+
elif filename == "07.png":
|
| 88 |
+
height, width = 528, 800
|
| 89 |
+
elif filename == "08.png":
|
| 90 |
+
height, width = 696, 1024
|
| 91 |
+
elif filename == "09.png":
|
| 92 |
+
height, width = 896, 1024
|
| 93 |
+
else:
|
| 94 |
+
height, width = 768, 768
|
| 95 |
+
|
| 96 |
+
examples.append([
|
| 97 |
+
example_prompts[i % len(example_prompts)], # User prompt (without system prompt)
|
| 98 |
+
Image.open(img_path), # Reference image
|
| 99 |
+
height, # Height
|
| 100 |
+
width, # Width
|
| 101 |
+
i + 1 # Seed
|
| 102 |
+
])
|
| 103 |
+
|
| 104 |
+
return examples
|
| 105 |
|
| 106 |
+
# CSS for improved UI
|
| 107 |
+
css = """
|
| 108 |
+
:root {
|
| 109 |
+
--primary-color: #4a6670;
|
| 110 |
+
--accent-color: #ff8a65;
|
| 111 |
+
--background-color: #f5f5f5;
|
| 112 |
+
--card-background: #ffffff;
|
| 113 |
+
--text-color: #333333;
|
| 114 |
+
--border-radius: 10px;
|
| 115 |
+
--shadow: 0 4px 6px rgba(0,0,0,0.1);
|
| 116 |
+
}
|
| 117 |
|
| 118 |
+
body {
|
| 119 |
+
background-color: var(--background-color);
|
| 120 |
+
color: var(--text-color);
|
| 121 |
+
font-family: 'Helvetica Neue', Arial, sans-serif;
|
| 122 |
+
}
|
| 123 |
|
| 124 |
+
.container {
|
| 125 |
+
max-width: 1200px;
|
| 126 |
+
margin: 0 auto;
|
| 127 |
+
padding: 20px;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
.gr-header {
|
| 131 |
+
background: linear-gradient(135deg, #668796 0%, #4a6670 100%);
|
| 132 |
+
padding: 24px;
|
| 133 |
+
border-radius: var(--border-radius);
|
| 134 |
+
margin-bottom: 24px;
|
| 135 |
+
box-shadow: var(--shadow);
|
| 136 |
+
text-align: center;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
.gr-header h1 {
|
| 140 |
+
color: white;
|
| 141 |
+
font-size: 2.5rem;
|
| 142 |
+
margin: 0;
|
| 143 |
+
font-weight: 700;
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
.gr-header p {
|
| 147 |
+
color: rgba(255, 255, 255, 0.9);
|
| 148 |
+
font-size: 1.1rem;
|
| 149 |
+
margin-top: 8px;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
.gr-panel {
|
| 153 |
+
background-color: var(--card-background);
|
| 154 |
+
border-radius: var(--border-radius);
|
| 155 |
+
padding: 16px;
|
| 156 |
+
box-shadow: var(--shadow);
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
.gr-button {
|
| 160 |
+
background-color: var(--accent-color);
|
| 161 |
+
border: none;
|
| 162 |
+
color: white;
|
| 163 |
+
padding: 10px 20px;
|
| 164 |
+
border-radius: 5px;
|
| 165 |
+
font-size: 16px;
|
| 166 |
+
font-weight: bold;
|
| 167 |
+
cursor: pointer;
|
| 168 |
+
transition: transform 0.1s, background-color 0.3s;
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
.gr-button:hover {
|
| 172 |
+
background-color: #ff7043;
|
| 173 |
+
transform: translateY(-2px);
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
.gr-input, .gr-select {
|
| 177 |
+
border-radius: 5px;
|
| 178 |
+
border: 1px solid #ddd;
|
| 179 |
+
padding: 10px;
|
| 180 |
+
width: 100%;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
.gr-form {
|
| 184 |
+
display: grid;
|
| 185 |
+
gap: 16px;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
.gr-box {
|
| 189 |
+
background-color: var(--card-background);
|
| 190 |
+
border-radius: var(--border-radius);
|
| 191 |
+
padding: 20px;
|
| 192 |
+
box-shadow: var(--shadow);
|
| 193 |
+
margin-bottom: 20px;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
.gr-gallery {
|
| 197 |
+
display: grid;
|
| 198 |
+
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
| 199 |
+
gap: 16px;
|
| 200 |
+
}
|
| 201 |
|
| 202 |
+
.gr-gallery-item {
|
| 203 |
+
overflow: hidden;
|
| 204 |
+
border-radius: var(--border-radius);
|
| 205 |
+
box-shadow: var(--shadow);
|
| 206 |
+
transition: transform 0.3s;
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
.gr-gallery-item:hover {
|
| 210 |
+
transform: scale(1.02);
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
.gr-image {
|
| 214 |
+
width: 100%;
|
| 215 |
+
height: auto;
|
| 216 |
+
object-fit: cover;
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
.gr-footer {
|
| 220 |
+
text-align: center;
|
| 221 |
+
margin-top: 40px;
|
| 222 |
+
padding: 20px;
|
| 223 |
+
color: #666;
|
| 224 |
+
font-size: 14px;
|
| 225 |
+
}
|
| 226 |
+
|
| 227 |
+
.gr-examples-gallery {
|
| 228 |
+
margin-top: 20px;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
/* Responsive adjustments */
|
| 232 |
+
@media (max-width: 768px) {
|
| 233 |
+
.gr-header h1 {
|
| 234 |
+
font-size: 1.8rem;
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
.gr-panel {
|
| 238 |
+
padding: 12px;
|
| 239 |
+
}
|
| 240 |
+
}
|
| 241 |
+
|
| 242 |
+
/* Ghibli-inspired accent colors */
|
| 243 |
+
.gr-accent-1 {
|
| 244 |
+
background-color: #95ccd9;
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
.gr-accent-2 {
|
| 248 |
+
background-color: #74ad8c;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
.gr-accent-3 {
|
| 252 |
+
background-color: #f9c06b;
|
| 253 |
+
}
|
| 254 |
+
"""
|
| 255 |
+
|
| 256 |
+
# Create the Gradio Blocks interface
|
| 257 |
+
with gr.Blocks(css=css) as demo:
|
| 258 |
+
gr.HTML("""
|
| 259 |
+
<div class="gr-header">
|
| 260 |
+
<h1>✨ Ghibli Art Generator ✨</h1>
|
| 261 |
+
<p>Transform your ideas into magical Ghibli-inspired artwork</p>
|
| 262 |
+
</div>
|
| 263 |
+
""")
|
| 264 |
+
|
| 265 |
+
with gr.Tab("Create Ghibli Art"):
|
| 266 |
with gr.Row():
|
| 267 |
+
with gr.Column(scale=1):
|
| 268 |
+
gr.HTML("""
|
| 269 |
+
<div class="gr-box">
|
| 270 |
+
<h3>🎨 Your Creative Input</h3>
|
| 271 |
+
<p>Describe what you want to see in your Ghibli-inspired image</p>
|
| 272 |
+
</div>
|
| 273 |
+
""")
|
| 274 |
+
|
| 275 |
+
user_prompt = gr.Textbox(
|
| 276 |
+
label="Your description",
|
| 277 |
+
placeholder="Describe what you want to see (e.g., a cat sitting by the window)",
|
| 278 |
+
lines=2
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
+
spatial_img = gr.Image(
|
| 282 |
+
label="Reference Image (Optional)",
|
| 283 |
+
type="pil",
|
| 284 |
+
elem_classes="gr-image-upload"
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
with gr.Group():
|
| 288 |
+
with gr.Row():
|
| 289 |
+
height = gr.Slider(minimum=256, maximum=1024, step=64, label="Height", value=768)
|
| 290 |
+
width = gr.Slider(minimum=256, maximum=1024, step=64, label="Width", value=768)
|
| 291 |
+
|
| 292 |
+
seed = gr.Slider(minimum=1, maximum=9999, step=1, label="Seed", value=42,
|
| 293 |
+
info="Change for different variations")
|
| 294 |
+
|
| 295 |
+
generate_btn = gr.Button("✨ Generate Ghibli Art", elem_classes="gr-button")
|
| 296 |
+
|
| 297 |
+
with gr.Column(scale=1):
|
| 298 |
+
gr.HTML("""
|
| 299 |
+
<div class="gr-box">
|
| 300 |
+
<h3>✨ Your Magical Creation</h3>
|
| 301 |
+
<p>Your Ghibli-inspired artwork will appear here</p>
|
| 302 |
+
</div>
|
| 303 |
+
""")
|
| 304 |
+
output_image = gr.Image(label="Generated Image", elem_classes="gr-output-image")
|
| 305 |
+
|
| 306 |
+
gr.HTML("""
|
| 307 |
+
<div class="gr-box gr-examples-gallery">
|
| 308 |
+
<h3>✨ Inspiration Gallery</h3>
|
| 309 |
+
<p>Click on any example to try it out</p>
|
| 310 |
+
</div>
|
| 311 |
+
""")
|
| 312 |
+
|
| 313 |
+
# Add examples
|
| 314 |
+
examples = load_examples()
|
| 315 |
gr.Examples(
|
| 316 |
+
examples=examples,
|
| 317 |
+
inputs=[user_prompt, spatial_img, height, width, seed],
|
| 318 |
+
outputs=output_image,
|
| 319 |
fn=single_condition_generate_image,
|
| 320 |
+
cache_examples=False,
|
| 321 |
+
examples_per_page=4
|
| 322 |
)
|
| 323 |
+
|
| 324 |
+
gr.HTML("""
|
| 325 |
+
<div class="gr-footer">
|
| 326 |
+
<p>Powered by FLUX.1 and Ghibli LoRA • Created with ❤️</p>
|
| 327 |
+
</div>
|
| 328 |
+
""")
|
| 329 |
|
| 330 |
+
# Link the button to the function
|
| 331 |
+
generate_btn.click(
|
| 332 |
single_condition_generate_image,
|
| 333 |
+
inputs=[user_prompt, spatial_img, height, width, seed],
|
| 334 |
+
outputs=output_image
|
| 335 |
)
|
| 336 |
|
| 337 |
# Launch the Gradio app
|