Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -125,19 +125,166 @@ DIRECTION_OPTIONS = {
|
|
125 |
|
126 |
@spaces.GPU
|
127 |
def infer(input_image, prompt, illumination_dropdown, direction_dropdown, seed=42, randomize_seed=False, guidance_scale=2.5, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
if randomize_seed:
|
130 |
seed = random.randint(0, MAX_SEED)
|
131 |
|
132 |
input_image = input_image.convert("RGB")
|
133 |
-
|
134 |
-
#
|
135 |
-
if
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
137 |
else:
|
138 |
-
prompt_prefix =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
-
prompt_with_template
|
141 |
|
142 |
image = pipe(
|
143 |
image=input_image,
|
@@ -191,13 +338,13 @@ with gr.Blocks(css=css) as demo:
|
|
191 |
scale=1
|
192 |
)
|
193 |
prompt = gr.Textbox(
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
)
|
202 |
|
203 |
|
@@ -256,4 +403,4 @@ with gr.Blocks(css=css) as demo:
|
|
256 |
)
|
257 |
|
258 |
|
259 |
-
demo.launch()
|
|
|
125 |
|
126 |
@spaces.GPU
|
127 |
def infer(input_image, prompt, illumination_dropdown, direction_dropdown, seed=42, randomize_seed=False, guidance_scale=2.5, progress=gr.Progress(track_tqdm=True)):
|
128 |
+
"""
|
129 |
+
Performs relighting on an input image using the FLUX.1-Kontext model.
|
130 |
|
131 |
+
This function constructs a detailed prompt based on user selections from dropdowns
|
132 |
+
or a custom text prompt. It then uses the diffusers pipeline to generate a new
|
133 |
+
image with the specified lighting, while aiming to preserve the original subject.
|
134 |
+
|
135 |
+
The logic prioritizes the custom text in the 'prompt' box. If the user types
|
136 |
+
their own prompt, it will be used directly, overriding the 'illumination_dropdown'
|
137 |
+
selection. The dropdowns serve as convenient presets to populate the prompt box.
|
138 |
+
|
139 |
+
Args:
|
140 |
+
input_image (PIL.Image.Image): The input image to be relighted.
|
141 |
+
prompt (str): The detailed text description of the desired lighting effect.
|
142 |
+
If this is manually filled, it overrides the illumination dropdown.
|
143 |
+
illumination_dropdown (str): A preset lighting style. See "Dropdown Options" below.
|
144 |
+
direction_dropdown (str): A preset for the light's direction. See "Dropdown Options" below.
|
145 |
+
seed (int): The seed for the random number generator for reproducibility.
|
146 |
+
randomize_seed (bool): If True, a random seed is used, overriding the 'seed' value.
|
147 |
+
guidance_scale (float): Controls how closely the model follows the prompt.
|
148 |
+
progress (gr.Progress): A Gradio progress tracker for the UI.
|
149 |
+
|
150 |
+
Returns:
|
151 |
+
tuple[PIL.Image.Image, int]: A tuple containing:
|
152 |
+
- The relighted output image.
|
153 |
+
- The seed used for the generation.
|
154 |
+
|
155 |
+
------------------------------------------------------------------------------------
|
156 |
+
Dropdown Options
|
157 |
+
------------------------------------------------------------------------------------
|
158 |
+
|
159 |
+
**illumination_dropdown Options:**
|
160 |
+
|
161 |
+
- "natural lighting"
|
162 |
+
- "sunshine from window"
|
163 |
+
- "golden time"
|
164 |
+
- "sunrise in the mountains"
|
165 |
+
- "afternoon light filtering through trees"
|
166 |
+
- "early morning rays, forest clearing"
|
167 |
+
- "golden sunlight streaming through trees"
|
168 |
+
- "sunset over sea"
|
169 |
+
- "golden hour in a meadow"
|
170 |
+
- "golden hour on a city skyline"
|
171 |
+
- "evening glow in the desert"
|
172 |
+
- "dusky evening on a beach"
|
173 |
+
- "mellow evening glow on a lake"
|
174 |
+
- "warm sunset in a rural village"
|
175 |
+
- "moonlight through curtains"
|
176 |
+
- "moonlight in a dark alley"
|
177 |
+
- "midnight in the forest"
|
178 |
+
- "midnight sky with bright starlight"
|
179 |
+
- "fireflies lighting up a summer night"
|
180 |
+
- "warm atmosphere, at home, bedroom"
|
181 |
+
- "home atmosphere, cozy bedroom illumination"
|
182 |
+
- "cozy candlelight"
|
183 |
+
- "candle-lit room, rustic vibe"
|
184 |
+
- "night, cozy warm light from fireplace"
|
185 |
+
- "campfire light"
|
186 |
+
- "neon night, city"
|
187 |
+
- "blue neon light, urban street"
|
188 |
+
- "neon, Wong Kar-wai, warm"
|
189 |
+
- "red and blue police lights in rain"
|
190 |
+
- "red glow, emergency lights"
|
191 |
+
- "sci-fi RGB glowing, cyberpunk"
|
192 |
+
- "rainbow reflections, neon"
|
193 |
+
- "magic lit"
|
194 |
+
- "mystical glow, enchanted forest"
|
195 |
+
- "ethereal glow, magical forest"
|
196 |
+
- "underwater glow, deep sea"
|
197 |
+
- "underwater luminescence"
|
198 |
+
- "aurora borealis glow, arctic landscape"
|
199 |
+
- "crystal reflections in a cave"
|
200 |
+
- "foggy forest at dawn"
|
201 |
+
- "foggy morning, muted light"
|
202 |
+
- "soft, diffused foggy glow"
|
203 |
+
- "stormy sky lighting"
|
204 |
+
- "lightning flash in storm"
|
205 |
+
- "rain-soaked reflections in city lights"
|
206 |
+
- "gentle snowfall at dusk"
|
207 |
+
- "hazy light of a winter morning"
|
208 |
+
- "mysterious twilight, heavy mist"
|
209 |
+
- "vibrant autumn lighting in a forest"
|
210 |
+
- "purple and pink hues at twilight"
|
211 |
+
- "desert sunset with mirage-like glow"
|
212 |
+
- "sunrise through foggy mountains"
|
213 |
+
- "soft studio lighting"
|
214 |
+
- "harsh, industrial lighting"
|
215 |
+
- "fluorescent office lighting"
|
216 |
+
- "harsh spotlight in dark room"
|
217 |
+
- "light and shadow"
|
218 |
+
- "shadow from window"
|
219 |
+
- "apocalyptic, smoky atmosphere"
|
220 |
+
- "evil, gothic, in a cave"
|
221 |
+
- "flickering light in a haunted house"
|
222 |
+
- "golden beams piercing through storm clouds"
|
223 |
+
- "dim candlelight in a gothic castle"
|
224 |
+
- "colorful lantern light at festival"
|
225 |
+
- "golden glow at a fairground"
|
226 |
+
- "soft glow through stained glass"
|
227 |
+
- "glowing embers from a forge"
|
228 |
+
- "custom"
|
229 |
+
|
230 |
+
**direction_dropdown Options:**
|
231 |
+
- "auto"
|
232 |
+
- "left side"
|
233 |
+
- "right side"
|
234 |
+
- "top"
|
235 |
+
- "top left"
|
236 |
+
- "top right"
|
237 |
+
- "bottom"
|
238 |
+
- "front"
|
239 |
+
- "back"
|
240 |
+
|
241 |
+
------------------------------------------------------------------------------------
|
242 |
+
How to Write Custom Prompts
|
243 |
+
------------------------------------------------------------------------------------
|
244 |
+
|
245 |
+
For full creative control, write your own prompt in the "Prompt" textbox. This will
|
246 |
+
override any selection from the "Choose Lighting Style" dropdown.
|
247 |
+
|
248 |
+
**1. Be Descriptive and Specific:**
|
249 |
+
- *Weak Prompt:* `candlelight`
|
250 |
+
- *Strong Prompt:* `Warm orange flickering light from a single candle, casting dramatic,
|
251 |
+
dancing shadows on the wall.`
|
252 |
+
|
253 |
+
**2. Focus on the Characteristics of Light:**
|
254 |
+
Build your prompt by considering these key elements:
|
255 |
+
- *Color Temperature:* Is the light warm, cool, golden, blue, orange-red, or neutral white?
|
256 |
+
- *Quality & Hardness:* Is it soft and diffused (gentle shadows) or hard and direct
|
257 |
+
(sharp, high-contrast shadows)?
|
258 |
+
- *Source:* Where is the light coming from? A window, a fireplace, a neon sign, the moon?
|
259 |
+
- *Atmospheric Effects:* Do you want `god rays`, `lens flare`, `volumetric fog`, `bokeh`,
|
260 |
+
`dust particles in light beams`, or `smoke`?
|
261 |
+
- *Shadows:* Describe them. Are they `long`, `short`, `deep`, `soft`, or `sharp-edged`?
|
262 |
+
"""
|
263 |
if randomize_seed:
|
264 |
seed = random.randint(0, MAX_SEED)
|
265 |
|
266 |
input_image = input_image.convert("RGB")
|
267 |
+
|
268 |
+
#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
|
269 |
+
if illumination_dropdown != "custom" and prompt == "":
|
270 |
+
prompt = ILLUMINATION_OPTIONS[illumination_dropdown]
|
271 |
+
|
272 |
+
#If the prompt matches the illumination options, prefix that
|
273 |
+
if prompt == ILLUMINATION_OPTIONS[illumination_dropdown]:
|
274 |
+
prompt_prefix = f", with {illumination_dropdown}"
|
275 |
+
#If the prompt was changed, the prefix is empty as the user prompt is predominant
|
276 |
else:
|
277 |
+
prompt_prefix = ""
|
278 |
+
|
279 |
+
# If direction isn't auto, add the direction suffix
|
280 |
+
if direction_dropdown != "auto" and prompt_prefix != "":
|
281 |
+
prompt_prefix = prompt_prefix + f"coming from the {direction_dropdown} of the image"
|
282 |
+
elif direction_dropdown != "auto" and prompt_prefix == "":
|
283 |
+
prompt_prefix = f", light coming from the {direction_dropdown} of the image"
|
284 |
+
|
285 |
+
prompt_with_template = f"Relight the image{prompt_prefix}. {prompt} Maintain the identity of the foreground subjects."
|
286 |
|
287 |
+
print(prompt_with_template)
|
288 |
|
289 |
image = pipe(
|
290 |
image=input_image,
|
|
|
338 |
scale=1
|
339 |
)
|
340 |
prompt = gr.Textbox(
|
341 |
+
label="Prompt",
|
342 |
+
info="Prompt is generated by the selected illumination style, but you can override it by writing your own",
|
343 |
+
show_label=True,
|
344 |
+
max_lines=3,
|
345 |
+
placeholder="select an illumination style above or type your custom description...",
|
346 |
+
value="Add directional sunlight from window source. Increase brightness on lit areas. Create hard shadows with sharp edges. Set warm white color temperature. Add visible light rays and dust particles in beams.",
|
347 |
+
container=True
|
348 |
)
|
349 |
|
350 |
|
|
|
403 |
)
|
404 |
|
405 |
|
406 |
+
demo.launch(mcp_server=True)
|