prithivMLmods commited on
Commit
3af12da
·
verified ·
1 Parent(s): cc5210e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -33
app.py CHANGED
@@ -5,12 +5,80 @@ import spaces
5
  import torch
6
  import random
7
  from PIL import Image
 
8
 
9
  from diffusers import FluxKontextPipeline
10
  from diffusers.utils import load_image
11
  from huggingface_hub import hf_hub_download
12
  from aura_sr import AuraSR
13
  from gradio_imageslider import ImageSlider
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
 
@@ -35,7 +103,6 @@ pipe.load_lora_weights("prithivMLmods/PhotoCleanser-i2i", weight_name="PhotoClea
35
  pipe.load_lora_weights("prithivMLmods/Photo-Restore-i2i", weight_name="Photo-Restore-i2i.safetensors", adapter_name="restorer")
36
  pipe.load_lora_weights("prithivMLmods/Polaroid-Warm-i2i", weight_name="Polaroid-Warm-i2i.safetensors", adapter_name="polaroid")
37
  pipe.load_lora_weights("prithivMLmods/Monochrome-Pencil", weight_name="Monochrome-Pencil-i2i.safetensors", adapter_name="pencil")
38
- # Add the new LZO adapter
39
  pipe.load_lora_weights("prithivMLmods/LZO-1-Preview", weight_name="LZO-1-Preview.safetensors", adapter_name="lzo")
40
 
41
 
@@ -46,12 +113,6 @@ aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
46
  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)):
47
  """
48
  Perform image editing and optional upscaling, returning a pair for the ImageSlider.
49
-
50
- Returns:
51
- tuple: A 3-tuple containing:
52
- - (PIL.Image.Image, PIL.Image.Image): A tuple of the (original, generated) images for the slider.
53
- - int: The seed used for generation.
54
- - gr.update: A Gradio update to make the reuse button visible.
55
  """
56
  if not input_image:
57
  raise gr.Error("Please upload an image for editing.")
@@ -64,11 +125,9 @@ def infer(input_image, prompt, lora_adapter, upscale_image, seed=42, randomize_s
64
  pipe.set_adapters(["polaroid"], adapter_weights=[1.0])
65
  elif lora_adapter == "MonochromePencil":
66
  pipe.set_adapters(["pencil"], adapter_weights=[1.0])
67
- # Add the new LZO adapter condition
68
  elif lora_adapter == "LZO-Zoom":
69
  pipe.set_adapters(["lzo"], adapter_weights=[1.0])
70
 
71
-
72
  if randomize_seed:
73
  seed = random.randint(0, MAX_SEED)
74
 
@@ -103,20 +162,15 @@ css="""
103
  margin: 0 auto;
104
  max-width: 960px;
105
  }
106
- .submit-btn {
107
- background-color: #2980b9 !important;
108
- color: white !important;
109
- }
110
- .submit-btn:hover {
111
- background-color: #3498db !important;
112
- }
113
  """
114
 
115
- with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
116
 
117
  with gr.Column(elem_id="col-container"):
118
- gr.Markdown(f"""# **[Photo-Mate-i2i](https://huggingface.co/collections/prithivMLmods/i2i-kontext-exp-68ce573b5c0623476b636ec7)**
119
- Image manipulation with FLUX.1 Kontext adapters. [How to Use](https://huggingface.co/spaces/prithivMLmods/Photo-Mate-i2i/discussions/2)""")
 
120
  with gr.Row():
121
  with gr.Column():
122
  input_image = gr.Image(label="Image [PIL]", type="pil", height="300")
@@ -125,10 +179,10 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
125
  label="Edit Prompt",
126
  show_label=False,
127
  max_lines=1,
128
- placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
129
  container=False,
130
  )
131
- run_button = gr.Button("Run", elem_classes="submit-btn", scale=0)
132
  with gr.Accordion("Advanced Settings", open=False):
133
 
134
  seed = gr.Slider(
@@ -158,18 +212,16 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
158
  )
159
 
160
  with gr.Column():
161
- # Replace the single image result with the ImageSlider
162
  output_slider = ImageSlider(label="Before / After", show_label=False, interactive=False)
163
  reuse_button = gr.Button("Reuse this image", visible=False)
164
 
165
  with gr.Row():
166
  lora_adapter = gr.Dropdown(
167
- label="Chosen LoRA",
168
- choices=["PhotoCleanser", "PhotoRestorer", "PolaroidWarm", "MonochromePencil", "LZO-Zoom"],
169
- value="PhotoCleanser"
170
- )
171
 
172
- #AuraSR Upscale
173
  with gr.Row():
174
  upscale_checkbox = gr.Checkbox(label="Upscale the final image", value=False)
175
 
@@ -178,15 +230,12 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
178
  ["photocleanser/2.png", "[photo content], remove the cat from the image while preserving the background and remaining elements, maintaining realism and original details.", "PhotoCleanser"],
179
  ["photocleanser/1.png", "[photo content], remove the football from the image while preserving the background and remaining elements, maintaining realism and original details.", "PhotoCleanser"],
180
  ["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"],
181
- # Add the new LZO example
182
  ["lzo/1.jpg", "[photo content], zoom in on the specified [face close-up], enhancing resolution and detail while preserving sharpness, realism, and original context. Maintain natural proportions and background continuity around the zoomed area.", "LZO-Zoom"],
183
  ["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"],
184
  ["polaroid/1.png", "[photo content], in the style of a vintage Polaroid, with warm, faded tones, and a white border.", "PolaroidWarm"],
185
  ["pencil/1.png", "[photo content], replicate the image as a pencil illustration, black and white, with sketch-like detailing.", "MonochromePencil"],
186
-
187
  ],
188
  inputs=[input_image, prompt, lora_adapter],
189
- # The output now targets the ImageSlider component
190
  outputs=[output_slider, seed],
191
  fn=infer_example,
192
  cache_examples="lazy",
@@ -197,13 +246,10 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
197
  triggers=[run_button.click, prompt.submit],
198
  fn=infer,
199
  inputs=[input_image, prompt, lora_adapter, upscale_checkbox, seed, randomize_seed, guidance_scale, steps],
200
- # The output now targets the ImageSlider component
201
  outputs=[output_slider, seed, reuse_button]
202
  )
203
 
204
- # Update the reuse function to handle the ImageSlider output
205
  reuse_button.click(
206
- # The slider outputs a tuple of images; we want the second one (the generated result)
207
  fn=lambda images: images[1] if isinstance(images, (list, tuple)) and len(images) > 1 else images,
208
  inputs=[output_slider],
209
  outputs=[input_image]
 
5
  import torch
6
  import random
7
  from PIL import Image
8
+ from typing import Iterable
9
 
10
  from diffusers import FluxKontextPipeline
11
  from diffusers.utils import load_image
12
  from huggingface_hub import hf_hub_download
13
  from aura_sr import AuraSR
14
  from gradio_imageslider import ImageSlider
15
+ from gradio.themes import Soft
16
+ from gradio.themes.utils import colors, fonts, sizes
17
+
18
+ # --- Theme and CSS Definition ---
19
+
20
+ # Define the new HotPink color palette
21
+ colors.hot_pink = colors.Color(
22
+ name="hot_pink",
23
+ c50="#FFF0F7",
24
+ c100="#FFE0EF",
25
+ c200="#FFC2E0",
26
+ c300="#FFA4D1",
27
+ c400="#FF87C3",
28
+ c500="#FF69B4", # HotPink base color
29
+ c600="#E65FA2",
30
+ c700="#CC5490",
31
+ c800="#B34A7E",
32
+ c900="#993F6C",
33
+ c950="#80355A",
34
+ )
35
+
36
+ class HotPinkTheme(Soft):
37
+ def __init__(
38
+ self,
39
+ *,
40
+ primary_hue: colors.Color | str = colors.gray,
41
+ secondary_hue: colors.Color | str = colors.hot_pink, # Use the new color
42
+ neutral_hue: colors.Color | str = colors.slate,
43
+ text_size: sizes.Size | str = sizes.text_lg,
44
+ font: fonts.Font | str | Iterable[fonts.Font | str] = (
45
+ fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
46
+ ),
47
+ font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
48
+ fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
49
+ ),
50
+ ):
51
+ super().__init__(
52
+ primary_hue=primary_hue,
53
+ secondary_hue=secondary_hue,
54
+ neutral_hue=neutral_hue,
55
+ text_size=text_size,
56
+ font=font,
57
+ font_mono=font_mono,
58
+ )
59
+ super().set(
60
+ background_fill_primary="*primary_50",
61
+ background_fill_primary_dark="*primary_900",
62
+ body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
63
+ body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
64
+ button_primary_text_color="white",
65
+ button_primary_text_color_hover="white",
66
+ button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
67
+ button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
68
+ button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_700)",
69
+ button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_600)",
70
+ slider_color="*secondary_500",
71
+ slider_color_dark="*secondary_600",
72
+ block_title_text_weight="600",
73
+ block_border_width="2px",
74
+ block_shadow="*shadow_drop_lg",
75
+ button_primary_shadow="*shadow_drop_lg",
76
+ button_large_padding="12px",
77
+ block_label_background_fill="*primary_200",
78
+ )
79
+
80
+ # Instantiate the new theme
81
+ hot_pink_theme = HotPinkTheme()
82
 
83
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
84
 
 
103
  pipe.load_lora_weights("prithivMLmods/Photo-Restore-i2i", weight_name="Photo-Restore-i2i.safetensors", adapter_name="restorer")
104
  pipe.load_lora_weights("prithivMLmods/Polaroid-Warm-i2i", weight_name="Polaroid-Warm-i2i.safetensors", adapter_name="polaroid")
105
  pipe.load_lora_weights("prithivMLmods/Monochrome-Pencil", weight_name="Monochrome-Pencil-i2i.safetensors", adapter_name="pencil")
 
106
  pipe.load_lora_weights("prithivMLmods/LZO-1-Preview", weight_name="LZO-1-Preview.safetensors", adapter_name="lzo")
107
 
108
 
 
113
  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)):
114
  """
115
  Perform image editing and optional upscaling, returning a pair for the ImageSlider.
 
 
 
 
 
 
116
  """
117
  if not input_image:
118
  raise gr.Error("Please upload an image for editing.")
 
125
  pipe.set_adapters(["polaroid"], adapter_weights=[1.0])
126
  elif lora_adapter == "MonochromePencil":
127
  pipe.set_adapters(["pencil"], adapter_weights=[1.0])
 
128
  elif lora_adapter == "LZO-Zoom":
129
  pipe.set_adapters(["lzo"], adapter_weights=[1.0])
130
 
 
131
  if randomize_seed:
132
  seed = random.randint(0, MAX_SEED)
133
 
 
162
  margin: 0 auto;
163
  max-width: 960px;
164
  }
165
+ #main-title h1 {font-size: 2.1em !important;}
 
 
 
 
 
 
166
  """
167
 
168
+ with gr.Blocks(css=css, theme=hot_pink_theme) as demo:
169
 
170
  with gr.Column(elem_id="col-container"):
171
+ gr.Markdown("# **Photo-Mate-i2i**", elem_id="main-title")
172
+ gr.Markdown("Image manipulation with FLUX.1 Kontext adapters. [How to Use](https://huggingface.co/spaces/prithivMLmods/Photo-Mate-i2i/discussions/2)")
173
+
174
  with gr.Row():
175
  with gr.Column():
176
  input_image = gr.Image(label="Image [PIL]", type="pil", height="300")
 
179
  label="Edit Prompt",
180
  show_label=False,
181
  max_lines=1,
182
+ placeholder="Enter your prompt for editing (e.g., 'Remove glasses')",
183
  container=False,
184
  )
185
+ run_button = gr.Button("Run", variant="primary", scale=0)
186
  with gr.Accordion("Advanced Settings", open=False):
187
 
188
  seed = gr.Slider(
 
212
  )
213
 
214
  with gr.Column():
 
215
  output_slider = ImageSlider(label="Before / After", show_label=False, interactive=False)
216
  reuse_button = gr.Button("Reuse this image", visible=False)
217
 
218
  with gr.Row():
219
  lora_adapter = gr.Dropdown(
220
+ label="Chosen LoRA",
221
+ choices=["PhotoCleanser", "PhotoRestorer", "PolaroidWarm", "MonochromePencil", "LZO-Zoom"],
222
+ value="PhotoCleanser"
223
+ )
224
 
 
225
  with gr.Row():
226
  upscale_checkbox = gr.Checkbox(label="Upscale the final image", value=False)
227
 
 
230
  ["photocleanser/2.png", "[photo content], remove the cat from the image while preserving the background and remaining elements, maintaining realism and original details.", "PhotoCleanser"],
231
  ["photocleanser/1.png", "[photo content], remove the football from the image while preserving the background and remaining elements, maintaining realism and original details.", "PhotoCleanser"],
232
  ["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"],
 
233
  ["lzo/1.jpg", "[photo content], zoom in on the specified [face close-up], enhancing resolution and detail while preserving sharpness, realism, and original context. Maintain natural proportions and background continuity around the zoomed area.", "LZO-Zoom"],
234
  ["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"],
235
  ["polaroid/1.png", "[photo content], in the style of a vintage Polaroid, with warm, faded tones, and a white border.", "PolaroidWarm"],
236
  ["pencil/1.png", "[photo content], replicate the image as a pencil illustration, black and white, with sketch-like detailing.", "MonochromePencil"],
 
237
  ],
238
  inputs=[input_image, prompt, lora_adapter],
 
239
  outputs=[output_slider, seed],
240
  fn=infer_example,
241
  cache_examples="lazy",
 
246
  triggers=[run_button.click, prompt.submit],
247
  fn=infer,
248
  inputs=[input_image, prompt, lora_adapter, upscale_checkbox, seed, randomize_seed, guidance_scale, steps],
 
249
  outputs=[output_slider, seed, reuse_button]
250
  )
251
 
 
252
  reuse_button.click(
 
253
  fn=lambda images: images[1] if isinstance(images, (list, tuple)) and len(images) > 1 else images,
254
  inputs=[output_slider],
255
  outputs=[input_image]