prithivMLmods commited on
Commit
f097250
·
verified ·
1 Parent(s): 7ac44e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -62
app.py CHANGED
@@ -11,16 +11,19 @@ from huggingface_hub import hf_hub_download
11
 
12
  MAX_SEED = np.iinfo(np.int32).max
13
 
 
14
  pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
15
 
16
- # Add the adapter to the app
17
- pipe.load_lora_weights("prithivMLmods/PhotoCleanser-i2i", weight_name="PhotoCleanser-i2i.safetensors", adapter_name="lora")
18
- pipe.set_adapters(["lora"], adapter_weights=[1.0])
 
 
19
 
20
  @spaces.GPU
21
- def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
22
  """
23
- Perform image editing using the FLUX.1 Kontext pipeline.
24
 
25
  This function takes an input image and a text prompt to generate a modified version
26
  of the image based on the provided instructions. It uses the FLUX.1 Kontext model
@@ -30,36 +33,27 @@ def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5
30
  input_image (PIL.Image.Image): The input image to be edited. Will be converted
31
  to RGB format if not already in that format.
32
  prompt (str): Text description of the desired edit to apply to the image.
33
- Examples: "Remove glasses", "Add a hat", "Change background to beach".
34
  seed (int, optional): Random seed for reproducible generation. Defaults to 42.
35
- Must be between 0 and MAX_SEED (2^31 - 1).
36
- randomize_seed (bool, optional): If True, generates a random seed instead of
37
- using the provided seed value. Defaults to False.
38
- guidance_scale (float, optional): Controls how closely the model follows the
39
- prompt. Higher values mean stronger adherence to the prompt but may reduce
40
- image quality. Range: 1.0-10.0. Defaults to 2.5.
41
- steps (int, optional): Controls how many steps to run the diffusion model for.
42
- Range: 1-30. Defaults to 28.
43
- progress (gr.Progress, optional): Gradio progress tracker for monitoring
44
- generation progress. Defaults to gr.Progress(track_tqdm=True).
45
 
46
  Returns:
47
- tuple: A 3-tuple containing:
48
- - PIL.Image.Image: The generated/edited image
49
- - int: The seed value used for generation (useful when randomize_seed=True)
50
- - gr.update: Gradio update object to make the reuse button visible
51
-
52
- Example:
53
- >>> edited_image, used_seed, button_update = infer(
54
- ... input_image=my_image,
55
- ... prompt="Add sunglasses",
56
- ... seed=123,
57
- ... randomize_seed=False,
58
- ... guidance_scale=2.5
59
- ... )
60
  """
61
  if randomize_seed:
62
  seed = random.randint(0, MAX_SEED)
 
 
 
 
 
 
 
 
 
63
 
64
  if input_image:
65
  input_image = input_image.convert("RGB")
@@ -79,13 +73,30 @@ def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5
79
  num_inference_steps=steps,
80
  generator=torch.Generator().manual_seed(seed),
81
  ).images[0]
 
82
  return image, seed, gr.Button(visible=True)
83
 
 
84
  @spaces.GPU
85
- def infer_example(input_image, prompt):
86
- image, seed, _ = infer(input_image, prompt)
87
  return image, seed
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  css="""
90
  #col-container {
91
  margin: 0 auto;
@@ -98,6 +109,13 @@ with gr.Blocks(css=css, theme="YTheme/Minecraft") as demo:
98
  with gr.Column(elem_id="col-container"):
99
  gr.Markdown(f"""# **[Photo-Mate-i2i](https://huggingface.co/collections/prithivMLmods/i2i-kontext-exp-68ce573b5c0623476b636ec7)**
100
  Image manipulation with Kontext adapters""")
 
 
 
 
 
 
 
101
  with gr.Row():
102
  with gr.Column():
103
  input_image = gr.Image(label="Upload the image for editing", type="pil", height="300")
@@ -106,60 +124,69 @@ with gr.Blocks(css=css, theme="YTheme/Minecraft") as demo:
106
  label="Prompt",
107
  show_label=False,
108
  max_lines=1,
109
- placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
110
  container=False,
111
  )
112
  run_button = gr.Button("Run", scale=0)
113
  with gr.Accordion("Advanced Settings", open=False):
114
 
115
  seed = gr.Slider(
116
- label="Seed",
117
- minimum=0,
118
- maximum=MAX_SEED,
119
- step=1,
120
- value=0,
121
  )
122
-
123
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
124
-
125
  guidance_scale = gr.Slider(
126
- label="Guidance Scale",
127
- minimum=1,
128
- maximum=10,
129
- step=0.1,
130
- value=2.5,
131
  )
132
-
133
  steps = gr.Slider(
134
- label="Steps",
135
- minimum=1,
136
- maximum=30,
137
- value=28,
138
- step=1
139
  )
140
 
141
  with gr.Column():
142
  result = gr.Image(label="Result", show_label=False, interactive=False, format="png")
143
  reuse_button = gr.Button("Reuse this image", visible=False)
144
 
 
 
 
 
 
 
 
 
 
 
 
 
145
 
146
- examples = gr.Examples(
147
- examples=[
148
- ["photocleanser/1.png", "[photo content], remove the embroidered pattern from the image while preserving the background and remaining elements, maintaining realism and original details."],
149
- ["photocleanser/2.png", "[photo content], remove the cat from the image while preserving the background and remaining elements, maintaining realism and original details."]
150
- ],
151
- inputs=[input_image, prompt],
152
- outputs=[result, seed],
153
- fn=infer_example,
154
- cache_examples=False
155
- )
156
-
 
 
 
157
  gr.on(
158
  triggers=[run_button.click, prompt.submit],
159
  fn = infer,
160
- inputs = [input_image, prompt, seed, randomize_seed, guidance_scale, steps],
161
  outputs = [result, seed, reuse_button]
162
  )
 
 
 
 
 
 
 
 
 
163
  reuse_button.click(
164
  fn = lambda image: image,
165
  inputs = [result],
 
11
 
12
  MAX_SEED = np.iinfo(np.int32).max
13
 
14
+ # Load the base pipeline
15
  pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
16
 
17
+ # Load the PhotoCleanser adapter
18
+ pipe.load_lora_weights("prithivMLmods/PhotoCleanser-i2i", weight_name="PhotoCleanser-i2i.safetensors", adapter_name="cleanser")
19
+ # Load the Photo-Restore adapter
20
+ pipe.load_lora_weights("prithivMLmods/Photo-Restore-i2i", weight_name="Photo-Restore-i2i.safetensors", adapter_name="restorer")
21
+
22
 
23
  @spaces.GPU
24
+ def infer(input_image, prompt, lora_selection, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
25
  """
26
+ Perform image editing using the FLUX.1 Kontext pipeline with selectable LoRA adapters.
27
 
28
  This function takes an input image and a text prompt to generate a modified version
29
  of the image based on the provided instructions. It uses the FLUX.1 Kontext model
 
33
  input_image (PIL.Image.Image): The input image to be edited. Will be converted
34
  to RGB format if not already in that format.
35
  prompt (str): Text description of the desired edit to apply to the image.
36
+ lora_selection (str): The name of the LoRA adapter to use ("PhotoCleanser" or "PhotoRestore").
37
  seed (int, optional): Random seed for reproducible generation. Defaults to 42.
38
+ randomize_seed (bool, optional): If True, generates a random seed. Defaults to False.
39
+ guidance_scale (float, optional): Controls how closely the model follows the prompt. Defaults to 2.5.
40
+ steps (int, optional): Number of diffusion steps. Defaults to 28.
41
+ progress (gr.Progress, optional): Gradio progress tracker.
 
 
 
 
 
 
42
 
43
  Returns:
44
+ tuple: A 3-tuple containing the generated image, the seed used, and a Gradio update object.
 
 
 
 
 
 
 
 
 
 
 
 
45
  """
46
  if randomize_seed:
47
  seed = random.randint(0, MAX_SEED)
48
+
49
+ # Set the adapter based on the user's selection
50
+ if lora_selection == "PhotoCleanser":
51
+ pipe.set_adapters(["cleanser"], adapter_weights=[1.0])
52
+ elif lora_selection == "PhotoRestore":
53
+ pipe.set_adapters(["restorer"], adapter_weights=[1.0])
54
+ else: # If "None" or any other value, disable LoRA
55
+ pipe.disable_lora()
56
+
57
 
58
  if input_image:
59
  input_image = input_image.convert("RGB")
 
73
  num_inference_steps=steps,
74
  generator=torch.Generator().manual_seed(seed),
75
  ).images[0]
76
+
77
  return image, seed, gr.Button(visible=True)
78
 
79
+ # Wrapper function for PhotoCleanser examples
80
  @spaces.GPU
81
+ def infer_example_cleanser(input_image, prompt):
82
+ image, seed, _ = infer(input_image, prompt, lora_selection="PhotoCleanser")
83
  return image, seed
84
 
85
+ # Wrapper function for PhotoRestore examples
86
+ @spaces.GPU
87
+ def infer_example_restorer(input_image, prompt):
88
+ image, seed, _ = infer(input_image, prompt, lora_selection="PhotoRestore")
89
+ return image, seed
90
+
91
+ # Function to switch visibility of example sets based on dropdown selection
92
+ def switch_examples(lora_choice):
93
+ if lora_choice == "PhotoCleanser":
94
+ return gr.update(visible=True), gr.update(visible=False)
95
+ elif lora_choice == "PhotoRestore":
96
+ return gr.update(visible=False), gr.update(visible=True)
97
+ return gr.update(visible=False), gr.update(visible=False)
98
+
99
+
100
  css="""
101
  #col-container {
102
  margin: 0 auto;
 
109
  with gr.Column(elem_id="col-container"):
110
  gr.Markdown(f"""# **[Photo-Mate-i2i](https://huggingface.co/collections/prithivMLmods/i2i-kontext-exp-68ce573b5c0623476b636ec7)**
111
  Image manipulation with Kontext adapters""")
112
+
113
+ lora_dropdown = gr.Dropdown(
114
+ label="Select Adapter (LoRA)",
115
+ choices=["PhotoCleanser", "PhotoRestore"],
116
+ value="PhotoCleanser"
117
+ )
118
+
119
  with gr.Row():
120
  with gr.Column():
121
  input_image = gr.Image(label="Upload the image for editing", type="pil", height="300")
 
124
  label="Prompt",
125
  show_label=False,
126
  max_lines=1,
127
+ placeholder="Enter your prompt for editing (e.g., 'Remove the cat')",
128
  container=False,
129
  )
130
  run_button = gr.Button("Run", scale=0)
131
  with gr.Accordion("Advanced Settings", open=False):
132
 
133
  seed = gr.Slider(
134
+ label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0,
 
 
 
 
135
  )
 
136
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
 
137
  guidance_scale = gr.Slider(
138
+ label="Guidance Scale", minimum=1, maximum=10, step=0.1, value=2.5,
 
 
 
 
139
  )
 
140
  steps = gr.Slider(
141
+ label="Steps", minimum=1, maximum=30, value=28, step=1
 
 
 
 
142
  )
143
 
144
  with gr.Column():
145
  result = gr.Image(label="Result", show_label=False, interactive=False, format="png")
146
  reuse_button = gr.Button("Reuse this image", visible=False)
147
 
148
+ with gr.Group():
149
+ cleanser_examples = gr.Examples(
150
+ examples=[
151
+ ["photocleanser/1.png", "[photo content], remove the embroidered pattern from the image while preserving the background and remaining elements, maintaining realism and original details."],
152
+ ["photocleanser/2.png", "[photo content], remove the cat from the image while preserving the background and remaining elements, maintaining realism and original details."]
153
+ ],
154
+ inputs=[input_image, prompt],
155
+ outputs=[result, seed],
156
+ fn=infer_example_cleanser,
157
+ cache_examples=False,
158
+ label="PhotoCleanser Examples"
159
+ )
160
 
161
+ restorer_examples = gr.Examples(
162
+ examples=[
163
+ ["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."],
164
+ ["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."]
165
+ ],
166
+ inputs=[input_image, prompt],
167
+ outputs=[result, seed],
168
+ fn=infer_example_restorer,
169
+ cache_examples=False,
170
+ visible=False,
171
+ label="PhotoRestore Examples"
172
+ )
173
+
174
+ # Event listener for the main run button
175
  gr.on(
176
  triggers=[run_button.click, prompt.submit],
177
  fn = infer,
178
+ inputs = [input_image, prompt, lora_dropdown, seed, randomize_seed, guidance_scale, steps],
179
  outputs = [result, seed, reuse_button]
180
  )
181
+
182
+ # Event listener to switch example sets when the dropdown changes
183
+ lora_dropdown.change(
184
+ fn=switch_examples,
185
+ inputs=lora_dropdown,
186
+ outputs=[cleanser_examples, restorer_examples]
187
+ )
188
+
189
+ # Event listener for the reuse button
190
  reuse_button.click(
191
  fn = lambda image: image,
192
  inputs = [result],