Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -132,7 +132,6 @@ Return only the rewritten instruction text directly, without JSON formatting or
|
|
| 132 |
|
| 133 |
return polish_prompt_hf(full_prompt, SYSTEM_PROMPT)
|
| 134 |
|
| 135 |
-
|
| 136 |
# --- Model Loading ---
|
| 137 |
dtype = torch.bfloat16
|
| 138 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -180,11 +179,119 @@ except Exception as e:
|
|
| 180 |
# --- UI Constants and Helpers ---
|
| 181 |
MAX_SEED = np.iinfo(np.int32).max
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
# --- Main Inference Function ---
|
| 184 |
@spaces.GPU(duration=60)
|
| 185 |
def infer(
|
| 186 |
image,
|
| 187 |
prompt,
|
|
|
|
| 188 |
seed=42,
|
| 189 |
randomize_seed=False,
|
| 190 |
true_guidance_scale=1.0,
|
|
@@ -208,15 +315,30 @@ def infer(
|
|
| 208 |
print(f"Negative Prompt: '{negative_prompt}'")
|
| 209 |
print(f"Seed: {seed}, Steps: {num_inference_steps}, Guidance: {true_guidance_scale}")
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
if rewrite_prompt:
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
# Generate the edited image - always generate just 1 image
|
| 216 |
try:
|
| 217 |
images = pipe(
|
| 218 |
image,
|
| 219 |
-
prompt=
|
| 220 |
negative_prompt=negative_prompt,
|
| 221 |
num_inference_steps=num_inference_steps,
|
| 222 |
generator=generator,
|
|
@@ -231,6 +353,13 @@ def infer(
|
|
| 231 |
print(f"Error during inference: {e}")
|
| 232 |
raise e
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
# --- Examples and UI Layout ---
|
| 235 |
examples = [
|
| 236 |
# You can add example pairs of [image_path, prompt] here
|
|
@@ -273,6 +402,21 @@ with gr.Blocks(css=css) as demo:
|
|
| 273 |
show_label=True,
|
| 274 |
type="pil"
|
| 275 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
# Changed from Gallery to Image
|
| 277 |
result = gr.Image(
|
| 278 |
label="Result",
|
|
@@ -324,13 +468,20 @@ with gr.Blocks(css=css) as demo:
|
|
| 324 |
)
|
| 325 |
|
| 326 |
# gr.Examples(examples=examples, inputs=[input_image, prompt], outputs=[result, seed], fn=infer, cache_examples=False)
|
| 327 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
gr.on(
|
| 329 |
triggers=[run_button.click, prompt.submit],
|
| 330 |
fn=infer,
|
| 331 |
inputs=[
|
| 332 |
input_image,
|
| 333 |
prompt,
|
|
|
|
| 334 |
seed,
|
| 335 |
randomize_seed,
|
| 336 |
true_guidance_scale,
|
|
|
|
| 132 |
|
| 133 |
return polish_prompt_hf(full_prompt, SYSTEM_PROMPT)
|
| 134 |
|
|
|
|
| 135 |
# --- Model Loading ---
|
| 136 |
dtype = torch.bfloat16
|
| 137 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 179 |
# --- UI Constants and Helpers ---
|
| 180 |
MAX_SEED = np.iinfo(np.int32).max
|
| 181 |
|
| 182 |
+
# Illumination options mapping
|
| 183 |
+
ILLUMINATION_OPTIONS = {
|
| 184 |
+
# Natural Daylight
|
| 185 |
+
"natural lighting": "Neutral white color temperature with balanced exposure and soft shadows",
|
| 186 |
+
"sunshine from window": "Bright directional sunlight with hard shadows and visible light rays",
|
| 187 |
+
"golden time": "Warm golden hour lighting with enhanced warm colors and soft shadows",
|
| 188 |
+
"sunrise in the mountains": "Warm backlighting with atmospheric haze and lens flare",
|
| 189 |
+
"afternoon light filtering through trees": "Dappled sunlight patterns with green color cast from foliage",
|
| 190 |
+
"early morning rays, forest clearing": "God rays through trees with warm color temperature",
|
| 191 |
+
"golden sunlight streaming through trees": "Golden god rays with atmospheric particles in light beams",
|
| 192 |
+
|
| 193 |
+
# Sunset & Evening
|
| 194 |
+
"sunset over sea": "Warm sunset light with soft diffused lighting and gentle gradients",
|
| 195 |
+
"golden hour in a meadow": "Golden backlighting with lens flare and rim lighting",
|
| 196 |
+
"golden hour on a city skyline": "Golden lighting on buildings with silhouette effects",
|
| 197 |
+
"evening glow in the desert": "Warm directional lighting with long shadows",
|
| 198 |
+
"dusky evening on a beach": "Cool backlighting with horizon silhouettes",
|
| 199 |
+
"mellow evening glow on a lake": "Warm lighting with water reflections",
|
| 200 |
+
"warm sunset in a rural village": "Golden hour lighting with peaceful warm tones",
|
| 201 |
+
|
| 202 |
+
# Night & Moonlight
|
| 203 |
+
"moonlight through curtains": "Cool blue lighting with curtain shadow patterns",
|
| 204 |
+
"moonlight in a dark alley": "Cool blue lighting with deep urban shadows",
|
| 205 |
+
"midnight in the forest": "Very low brightness with minimal ambient lighting",
|
| 206 |
+
"midnight sky with bright starlight": "Cool blue lighting with star point sources",
|
| 207 |
+
"fireflies lighting up a summer night": "Small glowing points with warm ambient lighting",
|
| 208 |
+
|
| 209 |
+
# Indoor & Cozy
|
| 210 |
+
"warm atmosphere, at home, bedroom": "Very warm lighting with soft diffused glow",
|
| 211 |
+
"home atmosphere, cozy bedroom illumination": "Warm table lamp lighting with pools of light",
|
| 212 |
+
"cozy candlelight": "Warm orange flickering light with dramatic shadows",
|
| 213 |
+
"candle-lit room, rustic vibe": "Multiple warm candlelight sources with atmospheric shadows",
|
| 214 |
+
"night, cozy warm light from fireplace": "Warm orange-red firelight with flickering effects",
|
| 215 |
+
"campfire light": "Warm orange flickering light from below with dancing shadows",
|
| 216 |
+
|
| 217 |
+
# Urban & Neon
|
| 218 |
+
"neon night, city": "Vibrant blue, magenta, and green neon lights with reflections",
|
| 219 |
+
"blue neon light, urban street": "Blue neon lighting with urban glow effects",
|
| 220 |
+
"neon, Wong Kar-wai, warm": "Warm amber and red neon with moody selective lighting",
|
| 221 |
+
"red and blue police lights in rain": "Alternating red and blue strobing with wet reflections",
|
| 222 |
+
"red glow, emergency lights": "Red emergency lighting with harsh shadows and high contrast",
|
| 223 |
+
|
| 224 |
+
# Sci-Fi & Fantasy
|
| 225 |
+
"sci-fi RGB glowing, cyberpunk": "Electric blue, pink, and green RGB lighting with glowing effects",
|
| 226 |
+
"rainbow reflections, neon": "Chromatic rainbow patterns with prismatic reflections",
|
| 227 |
+
"magic lit": "Colored rim lighting in purple and blue with soft ethereal glow",
|
| 228 |
+
"mystical glow, enchanted forest": "Supernatural green and blue glowing with floating particles",
|
| 229 |
+
"ethereal glow, magical forest": "Supernatural lighting with blue-green rim lighting",
|
| 230 |
+
"underwater glow, deep sea": "Blue-green lighting with caustic patterns and particles",
|
| 231 |
+
"underwater luminescence": "Blue-green bioluminescent glow with caustic light patterns",
|
| 232 |
+
"aurora borealis glow, arctic landscape": "Green and purple dancing sky lighting",
|
| 233 |
+
"crystal reflections in a cave": "Sparkle effects with prismatic light dispersion",
|
| 234 |
+
|
| 235 |
+
# Weather & Atmosphere
|
| 236 |
+
"foggy forest at dawn": "Volumetric fog with cool god rays through trees",
|
| 237 |
+
"foggy morning, muted light": "Soft fog effects with reduced contrast throughout",
|
| 238 |
+
"soft, diffused foggy glow": "Heavy fog with soft lighting and no harsh shadows",
|
| 239 |
+
"stormy sky lighting": "Dramatic lighting with high contrast and rim lighting",
|
| 240 |
+
"lightning flash in storm": "Brief intense white light with stark shadows",
|
| 241 |
+
"rain-soaked reflections in city lights": "Wet surface reflections with streaking light effects",
|
| 242 |
+
"gentle snowfall at dusk": "Cool blue lighting with snowflake particle effects",
|
| 243 |
+
"hazy light of a winter morning": "Neutral lighting with atmospheric haze",
|
| 244 |
+
"mysterious twilight, heavy mist": "Heavy fog with cool lighting and atmospheric depth",
|
| 245 |
+
|
| 246 |
+
# Seasonal & Nature
|
| 247 |
+
"vibrant autumn lighting in a forest": "Enhanced warm autumn colors with dappled sunlight",
|
| 248 |
+
"purple and pink hues at twilight": "Warm lighting with soft purple and pink color grading",
|
| 249 |
+
"desert sunset with mirage-like glow": "Warm orange lighting with heat distortion effects",
|
| 250 |
+
"sunrise through foggy mountains": "Warm lighting through mist with atmospheric perspective",
|
| 251 |
+
|
| 252 |
+
# Professional & Studio
|
| 253 |
+
"soft studio lighting": "Multiple diffused sources with even illumination and minimal shadows",
|
| 254 |
+
"harsh, industrial lighting": "Bright fluorescent lighting with hard shadows",
|
| 255 |
+
"fluorescent office lighting": "Cool white overhead lighting with slight green tint",
|
| 256 |
+
"harsh spotlight in dark room": "Single intense directional light with dramatic shadows",
|
| 257 |
+
|
| 258 |
+
# Special Effects & Drama
|
| 259 |
+
"light and shadow": "Maximum contrast with sharp shadow boundaries",
|
| 260 |
+
"shadow from window": "Window frame shadow patterns with geometric shapes",
|
| 261 |
+
"apocalyptic, smoky atmosphere": "Orange-red fire tint with smoke effects",
|
| 262 |
+
"evil, gothic, in a cave": "Low brightness with cool lighting and deep shadows",
|
| 263 |
+
"flickering light in a haunted house": "Unstable flickering with cool and warm mixed lighting",
|
| 264 |
+
"golden beams piercing through storm clouds": "Dramatic god rays with high contrast",
|
| 265 |
+
"dim candlelight in a gothic castle": "Warm orange candlelight with stone texture enhancement",
|
| 266 |
+
|
| 267 |
+
# Festival & Celebration
|
| 268 |
+
"colorful lantern light at festival": "Multiple colored lantern sources with bokeh effects",
|
| 269 |
+
"golden glow at a fairground": "Warm carnival lighting with colorful bulb effects",
|
| 270 |
+
"soft glow through stained glass": "Colored light filtering with rainbow surface patterns",
|
| 271 |
+
"glowing embers from a forge": "Orange-red glowing particles with intense heat effects"
|
| 272 |
+
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
# Lighting direction options
|
| 276 |
+
DIRECTION_OPTIONS = {
|
| 277 |
+
"auto": "",
|
| 278 |
+
"left side": "Position the light source from the left side of the frame, creating shadows falling to the right.",
|
| 279 |
+
"right side": "Position the light source from the right side of the frame, creating shadows falling to the left.",
|
| 280 |
+
"top": "Position the light source from directly above, creating downward shadows.",
|
| 281 |
+
"top left": "Position the light source from the top left corner, creating diagonal shadows falling down and to the right.",
|
| 282 |
+
"top right": "Position the light source from the top right corner, creating diagonal shadows falling down and to the left.",
|
| 283 |
+
"bottom": "Position the light source from below, creating upward shadows and dramatic under-lighting.",
|
| 284 |
+
"front": "Position the light source from the front, minimizing shadows and creating even illumination.",
|
| 285 |
+
"back": "Position the light source from behind the subject, creating silhouette effects and rim lighting."
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
|
| 289 |
# --- Main Inference Function ---
|
| 290 |
@spaces.GPU(duration=60)
|
| 291 |
def infer(
|
| 292 |
image,
|
| 293 |
prompt,
|
| 294 |
+
illumination_dropdown, direction_dropdown,
|
| 295 |
seed=42,
|
| 296 |
randomize_seed=False,
|
| 297 |
true_guidance_scale=1.0,
|
|
|
|
| 315 |
print(f"Negative Prompt: '{negative_prompt}'")
|
| 316 |
print(f"Seed: {seed}, Steps: {num_inference_steps}, Guidance: {true_guidance_scale}")
|
| 317 |
|
| 318 |
+
#If the dropdown isn't custom, and the user didn't specify a prompt, fill the prompt with the correct one from the illumination options
|
| 319 |
+
if illumination_dropdown != "custom" and (prompt == "" or prompt == ILLUMINATION_OPTIONS[illumination_dropdown]):
|
| 320 |
+
prompt = f"change the lighting. add {ILLUMINATION_OPTIONS[illumination_dropdown]}"
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
# If direction isn't auto, add the direction suffix
|
| 324 |
+
if direction_dropdown != "auto":
|
| 325 |
+
prompt_with_template = prompt+ f" coming from the {direction_dropdown}"
|
| 326 |
+
else:
|
| 327 |
+
prompt_with_template= prompt
|
| 328 |
+
|
| 329 |
if rewrite_prompt:
|
| 330 |
+
final_prompt = polish_prompt(prompt_with_template, input_image)
|
| 331 |
+
else:
|
| 332 |
+
final_prompt = prompt_with_template
|
| 333 |
+
|
| 334 |
+
print(f"Calling pipeline with prompt: '{final_prompt}'")
|
| 335 |
+
|
| 336 |
|
| 337 |
# Generate the edited image - always generate just 1 image
|
| 338 |
try:
|
| 339 |
images = pipe(
|
| 340 |
image,
|
| 341 |
+
prompt=final_prompt,
|
| 342 |
negative_prompt=negative_prompt,
|
| 343 |
num_inference_steps=num_inference_steps,
|
| 344 |
generator=generator,
|
|
|
|
| 353 |
print(f"Error during inference: {e}")
|
| 354 |
raise e
|
| 355 |
|
| 356 |
+
def update_prompt_from_dropdown(illumination_option):
|
| 357 |
+
"""Update the prompt textbox based on dropdown selection"""
|
| 358 |
+
if illumination_option == "custom":
|
| 359 |
+
return "" # Clear the prompt for custom input
|
| 360 |
+
else:
|
| 361 |
+
return ILLUMINATION_OPTIONS[illumination_option]
|
| 362 |
+
|
| 363 |
# --- Examples and UI Layout ---
|
| 364 |
examples = [
|
| 365 |
# You can add example pairs of [image_path, prompt] here
|
|
|
|
| 402 |
show_label=True,
|
| 403 |
type="pil"
|
| 404 |
)
|
| 405 |
+
with gr.Row():
|
| 406 |
+
|
| 407 |
+
illumination_dropdown = gr.Dropdown(
|
| 408 |
+
choices=["custom"] + list(ILLUMINATION_OPTIONS.keys()),
|
| 409 |
+
value="sunshine from window",
|
| 410 |
+
label="Choose Lighting Style",
|
| 411 |
+
scale=2
|
| 412 |
+
)
|
| 413 |
+
|
| 414 |
+
direction_dropdown = gr.Dropdown(
|
| 415 |
+
choices=list(DIRECTION_OPTIONS.keys()),
|
| 416 |
+
value="auto",
|
| 417 |
+
label="Light Direction",
|
| 418 |
+
scale=1
|
| 419 |
+
)
|
| 420 |
# Changed from Gallery to Image
|
| 421 |
result = gr.Image(
|
| 422 |
label="Result",
|
|
|
|
| 468 |
)
|
| 469 |
|
| 470 |
# gr.Examples(examples=examples, inputs=[input_image, prompt], outputs=[result, seed], fn=infer, cache_examples=False)
|
| 471 |
+
# update prompt when dropdown changes
|
| 472 |
+
illumination_dropdown.change(
|
| 473 |
+
fn=update_prompt_from_dropdown,
|
| 474 |
+
inputs=[illumination_dropdown],
|
| 475 |
+
outputs=[prompt]
|
| 476 |
+
)
|
| 477 |
+
|
| 478 |
gr.on(
|
| 479 |
triggers=[run_button.click, prompt.submit],
|
| 480 |
fn=infer,
|
| 481 |
inputs=[
|
| 482 |
input_image,
|
| 483 |
prompt,
|
| 484 |
+
illumination_dropdown, direction_dropdown,
|
| 485 |
seed,
|
| 486 |
randomize_seed,
|
| 487 |
true_guidance_scale,
|