Update app.py
Browse files
app.py
CHANGED
@@ -8,10 +8,15 @@ import numpy as np
|
|
8 |
import torch
|
9 |
from diffusers import FluxImg2ImgPipeline
|
10 |
|
|
|
11 |
dtype = torch.bfloat16
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
|
14 |
-
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def sanitize_prompt(prompt):
|
17 |
# Allow only alphanumeric characters, spaces, and basic punctuation
|
@@ -44,10 +49,10 @@ def process_images(image, prompt="a girl", strength=0.75, seed=0, inference_step
|
|
44 |
|
45 |
def process_img2img(image, prompt="a person", strength=0.75, seed=0, num_inference_steps=4):
|
46 |
if image is None:
|
47 |
-
print("
|
48 |
return None
|
49 |
|
50 |
-
# Ensure image is in RGB mode (
|
51 |
if image.mode != "RGB":
|
52 |
image = image.convert("RGB")
|
53 |
|
@@ -56,8 +61,17 @@ def process_images(image, prompt="a girl", strength=0.75, seed=0, inference_step
|
|
56 |
width, height = adjust_to_multiple_of_32(fit_width, fit_height)
|
57 |
image = image.resize((width, height), Image.LANCZOS)
|
58 |
|
59 |
-
output = pipe(
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
pil_image = output.images[0]
|
63 |
new_width, new_height = pil_image.size
|
@@ -66,7 +80,7 @@ def process_images(image, prompt="a girl", strength=0.75, seed=0, inference_step
|
|
66 |
resized_image = pil_image.resize((fit_width, fit_height), Image.LANCZOS)
|
67 |
return resized_image
|
68 |
return pil_image
|
69 |
-
|
70 |
output = process_img2img(image, prompt, strength, seed, inference_step)
|
71 |
return output
|
72 |
|
@@ -88,7 +102,7 @@ css = """
|
|
88 |
display: flex;
|
89 |
align-items: center;
|
90 |
justify-content: center;
|
91 |
-
gap:10px
|
92 |
}
|
93 |
.image {
|
94 |
width: 128px;
|
@@ -106,10 +120,22 @@ with gr.Blocks(css=css, elem_id="demo-container") as demo:
|
|
106 |
gr.HTML(read_file("demo_tools.html"))
|
107 |
with gr.Row():
|
108 |
with gr.Column():
|
109 |
-
image = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
with gr.Row(elem_id="prompt-container", equal_height=False):
|
111 |
with gr.Row():
|
112 |
-
prompt = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
113 |
btn = gr.Button("Img2Img", elem_id="run_button", variant="primary")
|
114 |
with gr.Accordion(label="Advanced Settings", open=False):
|
115 |
with gr.Row(equal_height=True):
|
|
|
8 |
import torch
|
9 |
from diffusers import FluxImg2ImgPipeline
|
10 |
|
11 |
+
# Set the torch data type and device
|
12 |
dtype = torch.bfloat16
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
|
15 |
+
# If needed, add use_auth_token="YOUR_TOKEN" in from_pretrained below.
|
16 |
+
pipe = FluxImg2ImgPipeline.from_pretrained(
|
17 |
+
"black-forest-labs/FLUX.1-schnell",
|
18 |
+
torch_dtype=torch.bfloat16,
|
19 |
+
).to(device)
|
20 |
|
21 |
def sanitize_prompt(prompt):
|
22 |
# Allow only alphanumeric characters, spaces, and basic punctuation
|
|
|
49 |
|
50 |
def process_img2img(image, prompt="a person", strength=0.75, seed=0, num_inference_steps=4):
|
51 |
if image is None:
|
52 |
+
print("Empty input image returned.")
|
53 |
return None
|
54 |
|
55 |
+
# Ensure the image is in RGB mode (handles formats like WebP and JFIF)
|
56 |
if image.mode != "RGB":
|
57 |
image = image.convert("RGB")
|
58 |
|
|
|
61 |
width, height = adjust_to_multiple_of_32(fit_width, fit_height)
|
62 |
image = image.resize((width, height), Image.LANCZOS)
|
63 |
|
64 |
+
output = pipe(
|
65 |
+
prompt=prompt,
|
66 |
+
image=image,
|
67 |
+
generator=generator,
|
68 |
+
strength=strength,
|
69 |
+
width=width,
|
70 |
+
height=height,
|
71 |
+
guidance_scale=0,
|
72 |
+
num_inference_steps=num_inference_steps,
|
73 |
+
max_sequence_length=256,
|
74 |
+
)
|
75 |
|
76 |
pil_image = output.images[0]
|
77 |
new_width, new_height = pil_image.size
|
|
|
80 |
resized_image = pil_image.resize((fit_width, fit_height), Image.LANCZOS)
|
81 |
return resized_image
|
82 |
return pil_image
|
83 |
+
|
84 |
output = process_img2img(image, prompt, strength, seed, inference_step)
|
85 |
return output
|
86 |
|
|
|
102 |
display: flex;
|
103 |
align-items: center;
|
104 |
justify-content: center;
|
105 |
+
gap: 10px;
|
106 |
}
|
107 |
.image {
|
108 |
width: 128px;
|
|
|
120 |
gr.HTML(read_file("demo_tools.html"))
|
121 |
with gr.Row():
|
122 |
with gr.Column():
|
123 |
+
image = gr.Image(
|
124 |
+
height=800,
|
125 |
+
sources=['upload', 'clipboard'],
|
126 |
+
image_mode='RGB',
|
127 |
+
elem_id="image_upload",
|
128 |
+
type="pil",
|
129 |
+
label="Upload"
|
130 |
+
)
|
131 |
with gr.Row(elem_id="prompt-container", equal_height=False):
|
132 |
with gr.Row():
|
133 |
+
prompt = gr.Textbox(
|
134 |
+
label="Prompt",
|
135 |
+
value="a women",
|
136 |
+
placeholder="Your prompt (what you want in place of what is erased)",
|
137 |
+
elem_id="prompt"
|
138 |
+
)
|
139 |
btn = gr.Button("Img2Img", elem_id="run_button", variant="primary")
|
140 |
with gr.Accordion(label="Advanced Settings", open=False):
|
141 |
with gr.Row(equal_height=True):
|