Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from PIL import Image
|
|
5 |
import io
|
6 |
import base64
|
7 |
import os
|
|
|
8 |
import numpy as np
|
9 |
import torch
|
10 |
from diffusers import FluxImg2ImgPipeline
|
@@ -73,7 +74,7 @@ def sanitize_prompt(prompt):
|
|
73 |
sanitized_prompt = allowed_chars.sub("", prompt)
|
74 |
return sanitized_prompt
|
75 |
|
76 |
-
def convert_to_fit_size(original_width_and_height, maximum_size
|
77 |
width, height = original_width_and_height
|
78 |
if width <= maximum_size and height <= maximum_size:
|
79 |
return width, height
|
@@ -93,7 +94,8 @@ def adjust_to_multiple_of_32(width: int, height: int):
|
|
93 |
return width, height
|
94 |
|
95 |
@spaces.GPU(duration=120)
|
96 |
-
def process_images(image, prompt="a girl", strength=0.75, seed=0, inference_step=4,
|
|
|
97 |
progress(0, desc="Starting")
|
98 |
|
99 |
def process_img2img(image, prompt="a person", strength=0.75, seed=0, num_inference_steps=4):
|
@@ -105,8 +107,17 @@ def process_images(image, prompt="a girl", strength=0.75, seed=0, inference_step
|
|
105 |
width, height = adjust_to_multiple_of_32(fit_width, fit_height)
|
106 |
image = image.resize((width, height), Image.LANCZOS)
|
107 |
|
108 |
-
output = pipe(
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
pil_image = output.images[0]
|
112 |
new_width, new_height = pil_image.size
|
@@ -140,7 +151,7 @@ def read_file(path: str) -> str:
|
|
140 |
content = f.read()
|
141 |
return content
|
142 |
|
143 |
-
css="""
|
144 |
#col-left {
|
145 |
margin: 0 auto;
|
146 |
max-width: 640px;
|
@@ -184,25 +195,57 @@ with gr.Blocks(css=css, elem_id="demo-container") as demo:
|
|
184 |
gr.HTML(read_file("demo_tools.html"))
|
185 |
with gr.Row():
|
186 |
with gr.Column():
|
187 |
-
image = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
with gr.Row(elem_id="prompt-container", equal_height=False):
|
189 |
with gr.Row():
|
190 |
-
prompt = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
191 |
|
192 |
btn = gr.Button("Img2Img", elem_id="run_button", variant="primary")
|
193 |
|
194 |
with gr.Accordion(label="Advanced Settings", open=False):
|
195 |
with gr.Row(equal_height=True):
|
196 |
-
strength = gr.Number(
|
197 |
-
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
id_input = gr.Text(label="Name", visible=False)
|
201 |
|
202 |
with gr.Column():
|
203 |
# Display placeholder image
|
204 |
-
image_out = gr.Image(
|
205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
save_btn = gr.Button("Save Encrypted Image")
|
207 |
save_result = gr.Text(label="Save Result")
|
208 |
|
@@ -225,26 +268,29 @@ with gr.Blocks(css=css, elem_id="demo-container") as demo:
|
|
225 |
if result:
|
226 |
return result["display_image"], result["encrypted_data"]
|
227 |
return None, None
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
fn=handle_image_generation,
|
232 |
inputs=[image, prompt, strength, seed, inference_step, encrypt_password],
|
233 |
-
outputs=[image_out, encrypted_output_state]
|
|
|
234 |
)
|
235 |
-
|
236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
def handle_save_encrypted(encrypted_data):
|
238 |
if encrypted_data:
|
239 |
-
import json
|
240 |
import tempfile
|
241 |
-
import os
|
242 |
-
|
243 |
-
# Create a temporary file with the encrypted data
|
244 |
fd, path = tempfile.mkstemp(suffix='.encimg')
|
245 |
with os.fdopen(fd, 'w') as f:
|
246 |
json.dump(encrypted_data, f)
|
247 |
-
|
248 |
return f"Encrypted image saved to {path}"
|
249 |
return "No encrypted image to save"
|
250 |
|
@@ -255,4 +301,4 @@ with gr.Blocks(css=css, elem_id="demo-container") as demo:
|
|
255 |
)
|
256 |
|
257 |
if __name__ == "__main__":
|
258 |
-
demo.launch(share=True, show_error=True)
|
|
|
5 |
import io
|
6 |
import base64
|
7 |
import os
|
8 |
+
import json
|
9 |
import numpy as np
|
10 |
import torch
|
11 |
from diffusers import FluxImg2ImgPipeline
|
|
|
74 |
sanitized_prompt = allowed_chars.sub("", prompt)
|
75 |
return sanitized_prompt
|
76 |
|
77 |
+
def convert_to_fit_size(original_width_and_height, maximum_size=2048):
|
78 |
width, height = original_width_and_height
|
79 |
if width <= maximum_size and height <= maximum_size:
|
80 |
return width, height
|
|
|
94 |
return width, height
|
95 |
|
96 |
@spaces.GPU(duration=120)
|
97 |
+
def process_images(image, prompt="a girl", strength=0.75, seed=0, inference_step=4,
|
98 |
+
encrypt_password="default_password", progress=gr.Progress(track_tqdm=True)):
|
99 |
progress(0, desc="Starting")
|
100 |
|
101 |
def process_img2img(image, prompt="a person", strength=0.75, seed=0, num_inference_steps=4):
|
|
|
107 |
width, height = adjust_to_multiple_of_32(fit_width, fit_height)
|
108 |
image = image.resize((width, height), Image.LANCZOS)
|
109 |
|
110 |
+
output = pipe(
|
111 |
+
prompt=prompt,
|
112 |
+
image=image,
|
113 |
+
generator=generator,
|
114 |
+
strength=strength,
|
115 |
+
width=width,
|
116 |
+
height=height,
|
117 |
+
guidance_scale=0,
|
118 |
+
num_inference_steps=num_inference_steps,
|
119 |
+
max_sequence_length=256
|
120 |
+
)
|
121 |
|
122 |
pil_image = output.images[0]
|
123 |
new_width, new_height = pil_image.size
|
|
|
151 |
content = f.read()
|
152 |
return content
|
153 |
|
154 |
+
css = """
|
155 |
#col-left {
|
156 |
margin: 0 auto;
|
157 |
max-width: 640px;
|
|
|
195 |
gr.HTML(read_file("demo_tools.html"))
|
196 |
with gr.Row():
|
197 |
with gr.Column():
|
198 |
+
image = gr.Image(
|
199 |
+
height=800,
|
200 |
+
sources=['upload', 'clipboard'],
|
201 |
+
image_mode='RGB',
|
202 |
+
elem_id="image_upload",
|
203 |
+
type="pil",
|
204 |
+
label="Upload"
|
205 |
+
)
|
206 |
with gr.Row(elem_id="prompt-container", equal_height=False):
|
207 |
with gr.Row():
|
208 |
+
prompt = gr.Textbox(
|
209 |
+
label="Prompt",
|
210 |
+
value="a women",
|
211 |
+
placeholder="Your prompt (what you want in place of what is erased)",
|
212 |
+
elem_id="prompt"
|
213 |
+
)
|
214 |
|
215 |
btn = gr.Button("Img2Img", elem_id="run_button", variant="primary")
|
216 |
|
217 |
with gr.Accordion(label="Advanced Settings", open=False):
|
218 |
with gr.Row(equal_height=True):
|
219 |
+
strength = gr.Number(
|
220 |
+
value=0.75, minimum=0, maximum=0.75, step=0.01, label="Strength"
|
221 |
+
)
|
222 |
+
seed = gr.Number(
|
223 |
+
value=100, minimum=0, step=1, label="Seed"
|
224 |
+
)
|
225 |
+
inference_step = gr.Number(
|
226 |
+
value=4, minimum=1, step=4, label="Inference Steps"
|
227 |
+
)
|
228 |
+
encrypt_password = gr.Textbox(
|
229 |
+
label="Encryption Password",
|
230 |
+
value="default_password",
|
231 |
+
type="password"
|
232 |
+
)
|
233 |
id_input = gr.Text(label="Name", visible=False)
|
234 |
|
235 |
with gr.Column():
|
236 |
# Display placeholder image
|
237 |
+
image_out = gr.Image(
|
238 |
+
height=800,
|
239 |
+
sources=[],
|
240 |
+
label="Output (Encrypted)",
|
241 |
+
elem_id="output-img",
|
242 |
+
format="jpg"
|
243 |
+
)
|
244 |
+
encryption_notice = gr.HTML(
|
245 |
+
'<div class="encryption-notice">'
|
246 |
+
'The output image is encrypted. Use the Save button to download the encrypted file.'
|
247 |
+
'</div>'
|
248 |
+
)
|
249 |
save_btn = gr.Button("Save Encrypted Image")
|
250 |
save_result = gr.Text(label="Save Result")
|
251 |
|
|
|
268 |
if result:
|
269 |
return result["display_image"], result["encrypted_data"]
|
270 |
return None, None
|
271 |
+
|
272 |
+
# >>>> CHANGED: Use .click() and .submit() with api_name
|
273 |
+
btn.click(
|
274 |
fn=handle_image_generation,
|
275 |
inputs=[image, prompt, strength, seed, inference_step, encrypt_password],
|
276 |
+
outputs=[image_out, encrypted_output_state],
|
277 |
+
api_name="/process_images" # Exposes handle_image_generation as /process_images
|
278 |
)
|
279 |
+
|
280 |
+
prompt.submit(
|
281 |
+
fn=handle_image_generation,
|
282 |
+
inputs=[image, prompt, strength, seed, inference_step, encrypt_password],
|
283 |
+
outputs=[image_out, encrypted_output_state],
|
284 |
+
api_name="/process_images" # Same endpoint
|
285 |
+
)
|
286 |
+
# <<<< END CHANGE
|
287 |
+
|
288 |
def handle_save_encrypted(encrypted_data):
|
289 |
if encrypted_data:
|
|
|
290 |
import tempfile
|
|
|
|
|
|
|
291 |
fd, path = tempfile.mkstemp(suffix='.encimg')
|
292 |
with os.fdopen(fd, 'w') as f:
|
293 |
json.dump(encrypted_data, f)
|
|
|
294 |
return f"Encrypted image saved to {path}"
|
295 |
return "No encrypted image to save"
|
296 |
|
|
|
301 |
)
|
302 |
|
303 |
if __name__ == "__main__":
|
304 |
+
demo.launch(share=True, show_error=True)
|