prithivMLmods commited on
Commit
e0da60b
·
verified ·
1 Parent(s): 6e59ef0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -51
app.py CHANGED
@@ -9,6 +9,7 @@ from diffusers import FluxKontextPipeline
9
  from diffusers.utils import load_image
10
  from huggingface_hub import hf_hub_download
11
  from aura_sr import AuraSR
 
12
 
13
  # --- Main Model Initialization ---
14
  MAX_SEED = np.iinfo(np.int32).max
@@ -21,29 +22,22 @@ pipe.load_lora_weights("prithivMLmods/Polaroid-Warm-i2i", weight_name="Polaroid-
21
  pipe.load_lora_weights("prithivMLmods/Monochrome-Pencil", weight_name="Monochrome-Pencil-i2i.safetensors", adapter_name="pencil")
22
 
23
  # --- Upscaler Model Initialization ---
24
- # FIX: Removed the .to("cuda") call. The library handles device placement automatically.
25
  aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
26
 
27
  @spaces.GPU
28
  def infer(input_image, prompt, lora_adapter, upscale_image, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
29
  """
30
- Perform image editing and optional upscaling.
31
-
32
- Args:
33
- input_image (PIL.Image.Image): The input image to be edited.
34
- prompt (str): Text description of the desired edit.
35
- lora_adapter (str): The name of the LoRA adapter to use.
36
- upscale_image (bool): If True, the final image will be upscaled 4x.
37
- seed (int, optional): Random seed for reproducible generation.
38
- randomize_seed (bool, optional): If True, generates a random seed.
39
- guidance_scale (float, optional): Controls adherence to the prompt.
40
- steps (int, optional): Number of diffusion steps.
41
- progress (gr.Progress, optional): Gradio progress tracker.
42
 
43
  Returns:
44
- tuple: A 3-tuple containing the generated/upscaled image, the seed used,
45
- and a Gradio update to make the reuse button visible.
 
 
46
  """
 
 
 
47
  if lora_adapter == "PhotoCleanser":
48
  pipe.set_adapters(["cleanser"], adapter_weights=[1.0])
49
  elif lora_adapter == "PhotoRestorer":
@@ -56,39 +50,31 @@ def infer(input_image, prompt, lora_adapter, upscale_image, seed=42, randomize_s
56
  if randomize_seed:
57
  seed = random.randint(0, MAX_SEED)
58
 
59
- if input_image:
60
- input_image = input_image.convert("RGB")
61
- image = pipe(
62
- image=input_image,
63
- prompt=prompt,
64
- guidance_scale=guidance_scale,
65
- width = input_image.size[0],
66
- height = input_image.size[1],
67
- num_inference_steps=steps,
68
- generator=torch.Generator().manual_seed(seed),
69
- ).images[0]
70
- else:
71
- image = pipe(
72
- prompt=prompt,
73
- guidance_scale=guidance_scale,
74
- num_inference_steps=steps,
75
- generator=torch.Generator().manual_seed(seed),
76
- ).images[0]
77
 
78
- # Conditionally upscale the generated image
79
  if upscale_image:
80
  progress(0.8, desc="Upscaling image...")
81
  image = aura_sr.upscale_4x(image)
82
 
83
- return image, seed, gr.Button(visible=True)
84
 
85
  @spaces.GPU
86
  def infer_example(input_image, prompt, lora_adapter):
87
  """
88
- Wrapper function for gr.Examples. Upscaling is disabled for examples for speed.
89
  """
90
- image, seed, _ = infer(input_image, prompt, lora_adapter, upscale_image=False)
91
- return image, seed
92
 
93
  css="""
94
  #col-container {
@@ -145,7 +131,8 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
145
  )
146
 
147
  with gr.Column():
148
- result = gr.Image(label="Result", show_label=False, interactive=False, format="png")
 
149
  reuse_button = gr.Button("Reuse this image", visible=False)
150
  with gr.Row():
151
  lora_adapter = gr.Dropdown(
@@ -156,17 +143,18 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
156
 
157
  gr.Examples(
158
  examples=[
159
- ["photocleanser/1.png", "[photo content], remove the embroidered pattern from the image while preserving the background and remaining elements, maintaining realism and original details.", "PhotoCleanser"],
160
- ["photocleanser/2.png", "[photo content], remove the cat from the image while preserving the background and remaining elements, maintaining realism and original details.", "PhotoCleanser"],
161
- ["photorestore/1.png", "[photo content], restore and enhance the image by repairing any damage, scratches, or fading. Colorize the photo naturally while preserving authentic textures and details, maintaining a realistic and historically accurate look.", "PhotoRestorer"],
162
- ["photorestore/2.png", "[photo content], restore and enhance the image by repairing any damage, scratches, or fading. Colorize the photo naturally while preserving authentic textures and details, maintaining a realistic and historically accurate look.", "PhotoRestorer"],
163
- ["polaroid/1.png", "[photo content], apply a warm, vintage Polaroid-style filter, enhancing the image with nostalgic tones, soft focus, and characteristic light leaks for an authentic, retro feel.", "PolaroidWarm"],
164
- ["polaroid/2.png", "[photo content], give the image a classic Polaroid look with warm, saturated colors, gentle fading, and a subtle vignette effect, evoking a sense of timeless memories.", "PolaroidWarm"],
165
- ["pencil/1.png", "[photo content], transform the image into a detailed monochrome pencil sketch, emphasizing sharp lines, textures, and shading for a classic hand-drawn look.", "MonochromePencil"],
166
- ["pencil/2.png", "[photo content], convert the photo into a realistic graphite pencil drawing, capturing the subject's form and depth with varied strokes and contrast.", "MonochromePencil"]
167
  ],
168
  inputs=[input_image, prompt, lora_adapter],
169
- outputs=[result, seed],
 
170
  fn=infer_example,
171
  cache_examples=False,
172
  label="Examples (Image | Prompt | Selected LoRA)"
@@ -176,11 +164,15 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
176
  triggers=[run_button.click, prompt.submit],
177
  fn=infer,
178
  inputs=[input_image, prompt, lora_adapter, upscale_checkbox, seed, randomize_seed, guidance_scale, steps],
179
- outputs=[result, seed, reuse_button]
 
180
  )
 
 
181
  reuse_button.click(
182
- fn=lambda image: image,
183
- inputs=[result],
 
184
  outputs=[input_image]
185
  )
186
 
 
9
  from diffusers.utils import load_image
10
  from huggingface_hub import hf_hub_download
11
  from aura_sr import AuraSR
12
+ from gradio_imageslider import ImageSlider
13
 
14
  # --- Main Model Initialization ---
15
  MAX_SEED = np.iinfo(np.int32).max
 
22
  pipe.load_lora_weights("prithivMLmods/Monochrome-Pencil", weight_name="Monochrome-Pencil-i2i.safetensors", adapter_name="pencil")
23
 
24
  # --- Upscaler Model Initialization ---
 
25
  aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
26
 
27
  @spaces.GPU
28
  def infer(input_image, prompt, lora_adapter, upscale_image, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
29
  """
30
+ Perform image editing and optional upscaling, returning a pair for the ImageSlider.
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  Returns:
33
+ tuple: A 3-tuple containing:
34
+ - (PIL.Image.Image, PIL.Image.Image): A tuple of the (original, generated) images for the slider.
35
+ - int: The seed used for generation.
36
+ - gr.update: A Gradio update to make the reuse button visible.
37
  """
38
+ if not input_image:
39
+ raise gr.Error("Please upload an image for editing.")
40
+
41
  if lora_adapter == "PhotoCleanser":
42
  pipe.set_adapters(["cleanser"], adapter_weights=[1.0])
43
  elif lora_adapter == "PhotoRestorer":
 
50
  if randomize_seed:
51
  seed = random.randint(0, MAX_SEED)
52
 
53
+ original_image = input_image.copy().convert("RGB")
54
+
55
+ image = pipe(
56
+ image=original_image,
57
+ prompt=prompt,
58
+ guidance_scale=guidance_scale,
59
+ width = original_image.size[0],
60
+ height = original_image.size[1],
61
+ num_inference_steps=steps,
62
+ generator=torch.Generator().manual_seed(seed),
63
+ ).images[0]
 
 
 
 
 
 
 
64
 
 
65
  if upscale_image:
66
  progress(0.8, desc="Upscaling image...")
67
  image = aura_sr.upscale_4x(image)
68
 
69
+ return (original_image, image), seed, gr.Button(visible=True)
70
 
71
  @spaces.GPU
72
  def infer_example(input_image, prompt, lora_adapter):
73
  """
74
+ Wrapper function for gr.Examples to call the main infer logic for the slider.
75
  """
76
+ (original_image, generated_image), seed, _ = infer(input_image, prompt, lora_adapter, upscale_image=False)
77
+ return (original_image, generated_image), seed
78
 
79
  css="""
80
  #col-container {
 
131
  )
132
 
133
  with gr.Column():
134
+ # Replace the single image result with the ImageSlider
135
+ output_slider = ImageSlider(label="Before / After", show_label=False, interactive=False)
136
  reuse_button = gr.Button("Reuse this image", visible=False)
137
  with gr.Row():
138
  lora_adapter = gr.Dropdown(
 
143
 
144
  gr.Examples(
145
  examples=[
146
+ ["photocleanser/1.png", "[photo content], remove the embroidered pattern from the image...", "PhotoCleanser"],
147
+ ["photocleanser/2.png", "[photo content], remove the cat from the image...", "PhotoCleanser"],
148
+ ["photorestore/1.png", "[photo content], restore and enhance the image by repairing any damage...", "PhotoRestorer"],
149
+ ["photorestore/2.png", "[photo content], restore and enhance the image by repairing any damage...", "PhotoRestorer"],
150
+ ["polaroid/1.png", "[photo content], apply a warm, vintage Polaroid-style filter...", "PolaroidWarm"],
151
+ ["polaroid/2.png", "[photo content], give the image a classic Polaroid look...", "PolaroidWarm"],
152
+ ["pencil/1.png", "[photo content], transform the image into a detailed monochrome pencil sketch...", "MonochromePencil"],
153
+ ["pencil/2.png", "[photo content], convert the photo into a realistic graphite pencil drawing...", "MonochromePencil"]
154
  ],
155
  inputs=[input_image, prompt, lora_adapter],
156
+ # The output now targets the ImageSlider component
157
+ outputs=[output_slider, seed],
158
  fn=infer_example,
159
  cache_examples=False,
160
  label="Examples (Image | Prompt | Selected LoRA)"
 
164
  triggers=[run_button.click, prompt.submit],
165
  fn=infer,
166
  inputs=[input_image, prompt, lora_adapter, upscale_checkbox, seed, randomize_seed, guidance_scale, steps],
167
+ # The output now targets the ImageSlider component
168
+ outputs=[output_slider, seed, reuse_button]
169
  )
170
+
171
+ # Update the reuse function to handle the ImageSlider output
172
  reuse_button.click(
173
+ # The slider outputs a tuple of images; we want the second one (the generated result)
174
+ fn=lambda images: images[1] if isinstance(images, (list, tuple)) and len(images) > 1 else images,
175
+ inputs=[output_slider],
176
  outputs=[input_image]
177
  )
178