Spaces:
Running
on
Zero
Running
on
Zero
Update app_v5.py
Browse files
app_v5.py
CHANGED
@@ -1,17 +1,15 @@
|
|
1 |
-
#
|
2 |
import gradio as gr
|
3 |
-
import spaces
|
4 |
-
import logging
|
5 |
import torch
|
6 |
from gradio_client import Client, handle_file
|
|
|
7 |
import os
|
8 |
import datetime
|
9 |
import io
|
|
|
10 |
import moondream as md
|
11 |
from transformers import T5EncoderModel
|
12 |
-
from diffusers import FluxControlNetPipeline,
|
13 |
-
from diffusers.quantizers import PipelineQuantizationConfig
|
14 |
-
|
15 |
from diffusers.utils import load_image
|
16 |
from PIL import Image
|
17 |
from threading import Thread
|
@@ -19,12 +17,40 @@ from typing import Generator
|
|
19 |
from huggingface_hub import CommitScheduler, HfApi
|
20 |
from debug import log_params, scheduler, save_image
|
21 |
from huggingface_hub.utils._runtime import dump_environment_info
|
|
|
22 |
|
|
|
|
|
|
|
23 |
|
24 |
-
logging.basicConfig(level=logging.
|
25 |
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
# Ensure device is set
|
28 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
29 |
MAX_SEED = 1000000
|
30 |
|
@@ -39,66 +65,52 @@ try:
|
|
39 |
except Exception as e:
|
40 |
print(f"Error setting memory usage: {e}")
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
46 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
# text_encoder_2_8b = T5EncoderModel.from_pretrained(
|
50 |
-
# "LPX55/FLUX.1-merged_lightning_v2",
|
51 |
-
# subfolder="text_encoder_2",
|
52 |
-
# quantization_config=quant_config_5_t5,
|
53 |
-
# torch_dtype=torch.float16,
|
54 |
-
# )
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
# )
|
63 |
-
pipe = FluxControlNetPipeline.from_pretrained(
|
64 |
-
"LPX55/FLUX.1M-8step_upscaler-cnet",
|
65 |
-
quantization_config=pipeline_quant_config,
|
66 |
-
torch_dtype=torch.float16
|
67 |
)
|
68 |
pipe.to("cuda")
|
|
|
|
|
69 |
|
70 |
try:
|
71 |
dump_environment_info()
|
72 |
except Exception as e:
|
73 |
print(f"Failed to dump env info: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
@spaces.GPU(duration=6, progress=gr.Progress(track_tqdm=True))
|
76 |
-
@torch.no_grad()
|
77 |
-
def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale, seed, guidance_end):
|
78 |
-
generator = torch.Generator().manual_seed(seed)
|
79 |
-
# Load control image
|
80 |
-
control_image = load_image(control_image)
|
81 |
-
w, h = control_image.size
|
82 |
-
w = w - w % 32
|
83 |
-
h = h - h % 32
|
84 |
-
control_image = control_image.resize((int(w * scale), int(h * scale)), resample=2) # Resample.BILINEAR
|
85 |
-
print("Size to: " + str(control_image.size[0]) + ", " + str(control_image.size[1]))
|
86 |
-
print(f"PromptLog: {repr(prompt)}")
|
87 |
-
with torch.inference_mode():
|
88 |
-
image = pipe(
|
89 |
-
generator=generator,
|
90 |
-
prompt=prompt,
|
91 |
-
control_image=control_image,
|
92 |
-
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
93 |
-
num_inference_steps=steps,
|
94 |
-
guidance_scale=guidance_scale,
|
95 |
-
height=control_image.size[1],
|
96 |
-
width=control_image.size[0],
|
97 |
-
control_guidance_start=0.0,
|
98 |
-
control_guidance_end=guidance_end,
|
99 |
-
).images[0]
|
100 |
-
# print("Type: " + str(type(image)))
|
101 |
-
return image
|
102 |
|
103 |
def combine_caption_focus(caption, focus):
|
104 |
try:
|
@@ -110,55 +122,138 @@ def combine_caption_focus(caption, focus):
|
|
110 |
except Exception as e:
|
111 |
print(f"Error combining caption and focus: {e}")
|
112 |
return "highly detailed photo, raw photography."
|
113 |
-
|
114 |
def generate_caption(control_image):
|
115 |
try:
|
116 |
if control_image is None:
|
117 |
-
return "Waiting for control image..."
|
|
|
|
|
|
|
118 |
|
|
|
|
|
|
|
119 |
# Generate a detailed caption
|
120 |
mcaption = model.caption(control_image, length="short")
|
121 |
detailed_caption = mcaption["caption"]
|
122 |
print(f"Detailed caption: {detailed_caption}")
|
123 |
|
124 |
-
return detailed_caption
|
125 |
except Exception as e:
|
126 |
print(f"Error generating caption: {e}")
|
127 |
-
return "A detailed photograph"
|
128 |
|
129 |
def generate_focus(control_image, focus_list):
|
130 |
try:
|
131 |
if control_image is None:
|
132 |
-
return None
|
133 |
if focus_list is None:
|
134 |
-
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
# Generate a detailed caption
|
136 |
focus_query = model.query(control_image, "Please provide a concise but illustrative description of the following area(s) of focus: " + focus_list)
|
137 |
focus_description = focus_query["answer"]
|
138 |
print(f"Areas of focus: {focus_description}")
|
139 |
-
|
140 |
-
return focus_description
|
141 |
except Exception as e:
|
142 |
print(f"Error generating focus: {e}")
|
143 |
-
return "highly detailed photo, raw photography."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
def process_image(control_image, user_prompt, system_prompt, scale, steps,
|
146 |
controlnet_conditioning_scale, guidance_scale, seed,
|
147 |
-
guidance_end, temperature,
|
148 |
# Initialize with empty caption
|
149 |
final_prompt = user_prompt.strip()
|
150 |
# If no user prompt provided, generate a caption first
|
151 |
if not final_prompt:
|
152 |
# Generate a detailed caption
|
153 |
-
|
154 |
mcaption = model.caption(control_image, length="normal")
|
155 |
detailed_caption = mcaption["caption"]
|
156 |
final_prompt = detailed_caption
|
157 |
yield f"Using caption: {final_prompt}", None, final_prompt
|
158 |
-
|
159 |
-
# Show the final prompt being used
|
160 |
yield f"Generating with: {final_prompt}", None, final_prompt
|
161 |
-
|
162 |
# Generate the image
|
163 |
try:
|
164 |
image = generate_image(
|
@@ -171,10 +266,12 @@ def process_image(control_image, user_prompt, system_prompt, scale, steps,
|
|
171 |
seed=seed,
|
172 |
guidance_end=guidance_end
|
173 |
)
|
174 |
-
|
175 |
try:
|
176 |
-
|
177 |
-
|
|
|
|
|
|
|
178 |
except Exception as e:
|
179 |
print("Error 160: " + str(e))
|
180 |
log_params(final_prompt, scale, steps, controlnet_conditioning_scale, guidance_scale, seed, guidance_end, control_image, image)
|
@@ -182,7 +279,7 @@ def process_image(control_image, user_prompt, system_prompt, scale, steps,
|
|
182 |
except Exception as e:
|
183 |
print("Error: " + str(e))
|
184 |
yield f"Error: {str(e)}", None, None
|
185 |
-
|
186 |
with gr.Blocks(title="FLUX Turbo Upscaler", fill_height=True) as demo:
|
187 |
gr.Markdown("⚠️ WIP SPACE - UNFINISHED & BUGGY")
|
188 |
# status_box = gr.Markdown("🔄 Warming up...")
|
@@ -196,16 +293,21 @@ with gr.Blocks(title="FLUX Turbo Upscaler", fill_height=True) as demo:
|
|
196 |
with gr.Column(scale=1):
|
197 |
prompt = gr.Textbox(lines=4, info="Enter your prompt here or wait for auto-generation...", label="Image Description")
|
198 |
focus = gr.Textbox(label="Area(s) of Focus", info="e.g. 'face', 'eyes', 'hair', 'clothes', 'background', etc.", value="clothing material, textures, ethnicity")
|
199 |
-
scale = gr.Slider(1, 3, value=1, label="Scale (Upscale Factor)", step=0.
|
200 |
with gr.Row():
|
201 |
generate_button = gr.Button("Generate Image", variant="primary")
|
202 |
caption_button = gr.Button("Generate Caption", variant="secondary")
|
203 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
204 |
seed = gr.Slider(0, MAX_SEED, value=42, label="Seed", step=1)
|
205 |
steps = gr.Slider(2, 16, value=8, label="Steps", step=1)
|
206 |
controlnet_conditioning_scale = gr.Slider(0, 1, value=0.6, label="ControlNet Scale")
|
207 |
guidance_scale = gr.Slider(1, 30, value=3.5, label="Guidance Scale")
|
208 |
guidance_end = gr.Slider(0, 1, value=1.0, label="Guidance End")
|
|
|
|
|
209 |
with gr.Row():
|
210 |
with gr.Accordion("Auto-Caption settings", open=False, visible=False):
|
211 |
system_prompt = gr.Textbox(
|
@@ -220,11 +322,6 @@ with gr.Blocks(title="FLUX Turbo Upscaler", fill_height=True) as demo:
|
|
220 |
info="Higher values make the output more random, lower values make it more deterministic.",
|
221 |
visible=False # Changed to visible
|
222 |
)
|
223 |
-
top_p_slider = gr.Slider(
|
224 |
-
minimum=0.0, maximum=1.0, value=0.9, step=0.01,
|
225 |
-
label="Top-p",
|
226 |
-
visible=False # Changed to visible
|
227 |
-
)
|
228 |
max_tokens_slider = gr.Slider(
|
229 |
minimum=1, maximum=2048, value=368, step=1,
|
230 |
label="Max New Tokens",
|
@@ -234,65 +331,39 @@ with gr.Blocks(title="FLUX Turbo Upscaler", fill_height=True) as demo:
|
|
234 |
log_prompt = gr.Checkbox(value=True, label="Log", visible=False) # Changed to visible
|
235 |
|
236 |
gr.Markdown("**Tips:** 8 steps is all you need! Incredibly powerful tool, usage instructions coming soon.")
|
237 |
-
with gr.Accordion("
|
238 |
msg1 = gr.Markdown()
|
239 |
try_btn = gr.LoginButton()
|
240 |
-
try:
|
241 |
-
x_ip_token = request.headers['x-ip-token']
|
242 |
-
client = Client("LPX55/zerogpu-experiments", hf_token=huggingface_token, headers={"x-ip-token": x_ip_token})
|
243 |
-
cresult = client.predict(
|
244 |
-
n=3,
|
245 |
-
api_name="/predict"
|
246 |
-
)
|
247 |
-
print(f"X TOKEN: {x_ip_token}")
|
248 |
-
print(cresult)
|
249 |
-
except:
|
250 |
-
print("Guess we're just going to have to pretend that Spaces have been broken for almost a year now..")
|
251 |
|
252 |
-
#
|
253 |
-
#
|
254 |
-
#
|
255 |
-
#
|
256 |
-
#
|
257 |
-
#
|
258 |
-
#
|
259 |
-
# custom_resize_percentage=50,
|
260 |
-
# prompt_input="Hello!!",
|
261 |
-
# alignment="Middle",
|
262 |
-
# overlap_left=True,
|
263 |
-
# overlap_right=True,
|
264 |
-
# overlap_top=True,
|
265 |
-
# overlap_bottom=True,
|
266 |
-
# x_offset=0,
|
267 |
-
# y_offset=0,
|
268 |
-
# api_name="/infer"
|
269 |
# )
|
270 |
-
def hello(profile: gr.OAuthProfile | None) -> str:
|
271 |
-
if profile is None:
|
272 |
-
return "Hello guest! There is a bug with HF ZeroGPUs that are afffecting some usage on certain spaces. Testing out some possible solutions."
|
273 |
-
return f"You are logged in as {profile.name}. If you run into incorrect messages about ZeroGPU runtime credits being out, PLEASE give me a heads up so I can investigate further."
|
274 |
|
275 |
caption_state = gr.State()
|
276 |
focus_state = gr.State()
|
277 |
log_state = gr.State()
|
278 |
-
|
279 |
generate_button.click(
|
280 |
fn=process_image,
|
281 |
inputs=[
|
282 |
control_image, prompt, system_prompt, scale, steps,
|
283 |
controlnet_conditioning_scale, guidance_scale, seed,
|
284 |
-
guidance_end, temperature_slider,
|
285 |
],
|
286 |
outputs=[log_state, generated_image, prompt]
|
287 |
)
|
288 |
control_image.upload(
|
289 |
generate_caption,
|
290 |
inputs=[control_image],
|
291 |
-
outputs=[caption_state]
|
292 |
).then(
|
293 |
generate_focus,
|
294 |
inputs=[control_image, focus],
|
295 |
-
outputs=[focus_state]
|
296 |
).then(
|
297 |
combine_caption_focus,
|
298 |
inputs=[caption_state, focus_state],
|
@@ -301,15 +372,25 @@ with gr.Blocks(title="FLUX Turbo Upscaler", fill_height=True) as demo:
|
|
301 |
caption_button.click(
|
302 |
fn=generate_caption,
|
303 |
inputs=[control_image],
|
304 |
-
outputs=[prompt]
|
305 |
).then(
|
306 |
generate_focus,
|
307 |
inputs=[control_image, focus],
|
308 |
-
outputs=[focus_state]
|
309 |
).then(
|
310 |
combine_caption_focus,
|
311 |
inputs=[caption_state, focus_state],
|
312 |
outputs=[prompt]
|
313 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
demo.load(hello, inputs=None, outputs=msg1)
|
315 |
-
demo.queue().launch(show_error=True)
|
|
|
1 |
+
# app_v4.py
|
2 |
import gradio as gr
|
|
|
|
|
3 |
import torch
|
4 |
from gradio_client import Client, handle_file
|
5 |
+
import spaces
|
6 |
import os
|
7 |
import datetime
|
8 |
import io
|
9 |
+
import numpy as np
|
10 |
import moondream as md
|
11 |
from transformers import T5EncoderModel
|
12 |
+
from diffusers import FluxControlNetPipeline, FluxControlNetInpaintPipeline, FluxTransformer2DModel
|
|
|
|
|
13 |
from diffusers.utils import load_image
|
14 |
from PIL import Image
|
15 |
from threading import Thread
|
|
|
17 |
from huggingface_hub import CommitScheduler, HfApi
|
18 |
from debug import log_params, scheduler, save_image
|
19 |
from huggingface_hub.utils._runtime import dump_environment_info
|
20 |
+
import logging
|
21 |
|
22 |
+
#############################
|
23 |
+
os.environ.setdefault('GRADIO_ANALYTICS_ENABLED', 'False')
|
24 |
+
os.environ.setdefault('HF_HUB_DISABLE_TELEMETRY', '1')
|
25 |
|
26 |
+
logging.basicConfig(level=logging.DEBUG)
|
27 |
logger = logging.getLogger(__name__)
|
28 |
+
#############################
|
29 |
+
|
30 |
+
presets = {
|
31 |
+
"Strict Upscale": {
|
32 |
+
"scale": 1.0,
|
33 |
+
"steps": 8,
|
34 |
+
"controlnet_conditioning_scale": 0.75,
|
35 |
+
"guidance_scale": 4.0,
|
36 |
+
"guidance_end": 0.9
|
37 |
+
},
|
38 |
+
"Creative Upscale": {
|
39 |
+
"scale": 2.0,
|
40 |
+
"steps": 6,
|
41 |
+
"controlnet_conditioning_scale": 0.42,
|
42 |
+
"guidance_scale": 3.0,
|
43 |
+
"guidance_end": 0.5
|
44 |
+
},
|
45 |
+
"High Detail Upscale": {
|
46 |
+
"scale": 1.25,
|
47 |
+
"steps": 10,
|
48 |
+
"controlnet_conditioning_scale": 0.9,
|
49 |
+
"guidance_scale": 10.0,
|
50 |
+
"guidance_end": 0.9
|
51 |
+
}
|
52 |
+
}
|
53 |
|
|
|
54 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
55 |
MAX_SEED = 1000000
|
56 |
|
|
|
65 |
except Exception as e:
|
66 |
print(f"Error setting memory usage: {e}")
|
67 |
|
68 |
+
text_encoder_2_unquant = T5EncoderModel.from_pretrained(
|
69 |
+
"LPX55/FLUX.1-merged_lightning_v2",
|
70 |
+
subfolder="text_encoder_2",
|
71 |
+
torch_dtype=torch.bfloat16,
|
72 |
+
token=huggingface_token
|
73 |
)
|
74 |
+
transformer = FluxTransformer2DModel.from_pretrained(
|
75 |
+
"LPX55/FLUX.1-merged_lightning_v2", subfolder='transformer', torch_dytpe=torch.bfloat16
|
76 |
+
)
|
77 |
+
pipe_upscaler = FluxControlNetPipeline.from_pretrained(
|
78 |
+
"LPX55/FLUX.1M-8step_upscaler-cnet",
|
79 |
+
torch_dtype=torch.bfloat16,
|
80 |
+
text_encoder_2=text_encoder_2_unquant,
|
81 |
+
token=huggingface_token
|
82 |
+
)
|
83 |
+
pipe_upscaler.to("cuda")
|
84 |
|
85 |
+
controlnet = FluxControlNetModel.from_pretrained("alimama-creative/FLUX.1-dev-Controlnet-Inpainting-Beta", torch_dtype=torch.bfloat16)
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
+
pipe = FluxControlNetInpaintPipeline.from_pretrained(
|
88 |
+
"LPX55/FLUX.1-merged_lightning_v2",
|
89 |
+
controlnet=controlnet,
|
90 |
+
transformer=transformer,
|
91 |
+
torch_dtype=torch.bfloat16,
|
92 |
+
token=huggingface_token
|
|
|
|
|
|
|
|
|
|
|
93 |
)
|
94 |
pipe.to("cuda")
|
95 |
+
pipe.transformer.to(torch.bfloat16)
|
96 |
+
pipe.controlnet.to(torch.bfloat16)
|
97 |
|
98 |
try:
|
99 |
dump_environment_info()
|
100 |
except Exception as e:
|
101 |
print(f"Failed to dump env info: {e}")
|
102 |
+
|
103 |
+
def get_image_dimensions(image: Image) -> str:
|
104 |
+
width, height = image.size
|
105 |
+
return f"Original Image Dimensions: {width}x{height}"
|
106 |
+
|
107 |
+
def resize_image_to_max_side(image: Image, max_side_length=1024) -> Image:
|
108 |
+
width, height = image.size
|
109 |
+
ratio = min(max_side_length / width, max_side_length / height)
|
110 |
+
new_size = (int(width * ratio), int(height * ratio))
|
111 |
+
resized_image = image.resize(new_size, Image.BILINEAR)
|
112 |
+
return resized_image
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
def combine_caption_focus(caption, focus):
|
116 |
try:
|
|
|
122 |
except Exception as e:
|
123 |
print(f"Error combining caption and focus: {e}")
|
124 |
return "highly detailed photo, raw photography."
|
|
|
125 |
def generate_caption(control_image):
|
126 |
try:
|
127 |
if control_image is None:
|
128 |
+
return "Waiting for control image...", "Original Image Dimensions: N/A"
|
129 |
+
|
130 |
+
# Get original dimensions
|
131 |
+
original_dimensions = get_image_dimensions(control_image)
|
132 |
|
133 |
+
# Resize the image to a maximum longest side of 1024 pixels
|
134 |
+
control_image = resize_image_to_max_side(control_image, max_side_length=1024)
|
135 |
+
|
136 |
# Generate a detailed caption
|
137 |
mcaption = model.caption(control_image, length="short")
|
138 |
detailed_caption = mcaption["caption"]
|
139 |
print(f"Detailed caption: {detailed_caption}")
|
140 |
|
141 |
+
return detailed_caption, original_dimensions
|
142 |
except Exception as e:
|
143 |
print(f"Error generating caption: {e}")
|
144 |
+
return "A detailed photograph", "Original Image Dimensions: N/A"
|
145 |
|
146 |
def generate_focus(control_image, focus_list):
|
147 |
try:
|
148 |
if control_image is None:
|
149 |
+
return None, "Original Image Dimensions: N/A"
|
150 |
if focus_list is None:
|
151 |
+
return "", "Original Image Dimensions: N/A"
|
152 |
+
|
153 |
+
# Get original dimensions
|
154 |
+
original_dimensions = get_image_dimensions(control_image)
|
155 |
+
|
156 |
+
# Resize the image to a maximum longest side of 1024 pixels
|
157 |
+
control_image = resize_image_to_max_side(control_image, max_side_length=1024)
|
158 |
+
|
159 |
# Generate a detailed caption
|
160 |
focus_query = model.query(control_image, "Please provide a concise but illustrative description of the following area(s) of focus: " + focus_list)
|
161 |
focus_description = focus_query["answer"]
|
162 |
print(f"Areas of focus: {focus_description}")
|
163 |
+
return focus_description, original_dimensions
|
|
|
164 |
except Exception as e:
|
165 |
print(f"Error generating focus: {e}")
|
166 |
+
return "highly detailed photo, raw photography.", "Original Image Dimensions: N/A"
|
167 |
+
|
168 |
+
|
169 |
+
@spaces.GPU(duration=6, progress=gr.Progress(track_tqdm=True))
|
170 |
+
@torch.no_grad()
|
171 |
+
def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale, seed, guidance_end):
|
172 |
+
generator = torch.Generator().manual_seed(seed)
|
173 |
+
|
174 |
+
# Ensure transparency is preserved
|
175 |
+
control_image = control_image.convert("RGBA")
|
176 |
+
|
177 |
+
# Resize the image to a maximum longest side of 1024 pixels
|
178 |
+
control_image = resize_image_to_max_side(control_image, max_side_length=1024)
|
179 |
+
|
180 |
+
w, h = control_image.size
|
181 |
+
# Crop to nearest multiple of 32
|
182 |
+
w = w - w % 32
|
183 |
+
h = h - h % 32
|
184 |
+
# Corrected resizing code
|
185 |
+
control_image = control_image.resize((w, h), resample=2)
|
186 |
+
|
187 |
+
print(f"Resized image dimensions: {control_image.size[0]}x{control_image.size[1]}")
|
188 |
+
print(f"PromptLog: {repr(prompt)}")
|
189 |
+
|
190 |
+
# Convert image to RGB for processing, but keep alpha channel for transparency
|
191 |
+
control_image_rgb = control_image.convert("RGB")
|
192 |
+
control_image_alpha = control_image.split()[-1]
|
193 |
+
|
194 |
+
# Convert alpha channel to a mask (transparent = white, opaque = black)
|
195 |
+
# White corresponds to 1 (to be inpainted), black corresponds to 0 (to be preserved)
|
196 |
+
# Convert alpha to numpy array for processing
|
197 |
+
alpha_array = np.array(control_image_alpha)
|
198 |
+
# Create binary mask (1 for transparent, 0 for opaque)
|
199 |
+
mask = (alpha_array > 128).astype(np.float32) # 1 for transparent (to be inpainted), 0 for opaque
|
200 |
+
|
201 |
+
# Optional: Visualize the mask (for debugging purposes)
|
202 |
+
# mask_image = Image.fromarray((mask * 255).astype(np.uint8))
|
203 |
+
# mask_image.show()
|
204 |
+
|
205 |
+
with torch.inference_mode():
|
206 |
+
image = pipe(
|
207 |
+
image=control_image_rgb,
|
208 |
+
generator=generator,
|
209 |
+
prompt=prompt,
|
210 |
+
control_image=control_image_rgb,
|
211 |
+
mask_image=mask, # Pass the numpy array as the mask
|
212 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
213 |
+
num_inference_steps=steps,
|
214 |
+
guidance_scale=guidance_scale,
|
215 |
+
height=control_image.size[1],
|
216 |
+
width=control_image.size[0],
|
217 |
+
control_guidance_start=0.0,
|
218 |
+
control_guidance_end=guidance_end,
|
219 |
+
).images[0]
|
220 |
+
|
221 |
+
# Reapply the alpha channel to the generated image
|
222 |
+
image = image.convert("RGBA")
|
223 |
+
image.putalpha(control_image_alpha)
|
224 |
+
|
225 |
+
return image
|
226 |
+
|
227 |
+
def update_parameters(preset):
|
228 |
+
if preset in presets:
|
229 |
+
params = presets[preset]
|
230 |
+
return (
|
231 |
+
params["scale"],
|
232 |
+
params["steps"],
|
233 |
+
params["controlnet_conditioning_scale"],
|
234 |
+
params["guidance_scale"],
|
235 |
+
params["guidance_end"]
|
236 |
+
)
|
237 |
+
else:
|
238 |
+
# Default values if preset is not found
|
239 |
+
return 1.0, 8, 0.6, 3.5, 1.0
|
240 |
|
241 |
def process_image(control_image, user_prompt, system_prompt, scale, steps,
|
242 |
controlnet_conditioning_scale, guidance_scale, seed,
|
243 |
+
guidance_end, temperature, max_new_tokens, log_prompt):
|
244 |
# Initialize with empty caption
|
245 |
final_prompt = user_prompt.strip()
|
246 |
# If no user prompt provided, generate a caption first
|
247 |
if not final_prompt:
|
248 |
# Generate a detailed caption
|
249 |
+
|
250 |
mcaption = model.caption(control_image, length="normal")
|
251 |
detailed_caption = mcaption["caption"]
|
252 |
final_prompt = detailed_caption
|
253 |
yield f"Using caption: {final_prompt}", None, final_prompt
|
254 |
+
|
|
|
255 |
yield f"Generating with: {final_prompt}", None, final_prompt
|
256 |
+
|
257 |
# Generate the image
|
258 |
try:
|
259 |
image = generate_image(
|
|
|
266 |
seed=seed,
|
267 |
guidance_end=guidance_end
|
268 |
)
|
|
|
269 |
try:
|
270 |
+
# Ensure the image is saved with transparency
|
271 |
+
with io.BytesIO() as output:
|
272 |
+
image.save(output, format="PNG")
|
273 |
+
debug_img = Image.open(output).convert("RGBA")
|
274 |
+
save_image("/tmp/" + str(seed) + "output.png", debug_img)
|
275 |
except Exception as e:
|
276 |
print("Error 160: " + str(e))
|
277 |
log_params(final_prompt, scale, steps, controlnet_conditioning_scale, guidance_scale, seed, guidance_end, control_image, image)
|
|
|
279 |
except Exception as e:
|
280 |
print("Error: " + str(e))
|
281 |
yield f"Error: {str(e)}", None, None
|
282 |
+
|
283 |
with gr.Blocks(title="FLUX Turbo Upscaler", fill_height=True) as demo:
|
284 |
gr.Markdown("⚠️ WIP SPACE - UNFINISHED & BUGGY")
|
285 |
# status_box = gr.Markdown("🔄 Warming up...")
|
|
|
293 |
with gr.Column(scale=1):
|
294 |
prompt = gr.Textbox(lines=4, info="Enter your prompt here or wait for auto-generation...", label="Image Description")
|
295 |
focus = gr.Textbox(label="Area(s) of Focus", info="e.g. 'face', 'eyes', 'hair', 'clothes', 'background', etc.", value="clothing material, textures, ethnicity")
|
296 |
+
scale = gr.Slider(1, 3, value=1, label="Scale (Upscale Factor)", step=0.1)
|
297 |
with gr.Row():
|
298 |
generate_button = gr.Button("Generate Image", variant="primary")
|
299 |
caption_button = gr.Button("Generate Caption", variant="secondary")
|
300 |
with gr.Column(scale=1):
|
301 |
+
with gr.Row():
|
302 |
+
preset_choices = list(presets.keys())
|
303 |
+
preset_radio = gr.Radio(choices=preset_choices, label="Select Preset", value=preset_choices[0])
|
304 |
seed = gr.Slider(0, MAX_SEED, value=42, label="Seed", step=1)
|
305 |
steps = gr.Slider(2, 16, value=8, label="Steps", step=1)
|
306 |
controlnet_conditioning_scale = gr.Slider(0, 1, value=0.6, label="ControlNet Scale")
|
307 |
guidance_scale = gr.Slider(1, 30, value=3.5, label="Guidance Scale")
|
308 |
guidance_end = gr.Slider(0, 1, value=1.0, label="Guidance End")
|
309 |
+
original_dimensions = gr.Markdown(value="Original Image Dimensions: N/A") # New output for dimensions
|
310 |
+
|
311 |
with gr.Row():
|
312 |
with gr.Accordion("Auto-Caption settings", open=False, visible=False):
|
313 |
system_prompt = gr.Textbox(
|
|
|
322 |
info="Higher values make the output more random, lower values make it more deterministic.",
|
323 |
visible=False # Changed to visible
|
324 |
)
|
|
|
|
|
|
|
|
|
|
|
325 |
max_tokens_slider = gr.Slider(
|
326 |
minimum=1, maximum=2048, value=368, step=1,
|
327 |
label="Max New Tokens",
|
|
|
331 |
log_prompt = gr.Checkbox(value=True, label="Log", visible=False) # Changed to visible
|
332 |
|
333 |
gr.Markdown("**Tips:** 8 steps is all you need! Incredibly powerful tool, usage instructions coming soon.")
|
334 |
+
with gr.Accordion("Auth for those Getting ZeroGPU errors.", open=False, elem_id="zgpu"):
|
335 |
msg1 = gr.Markdown()
|
336 |
try_btn = gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
|
338 |
+
# sus = ['x-zerogpu-token', 'x-zerogpu-uuid', 'x-proxied-host', 'x-proxied-path', 'x-proxied-replica', 'x-request-id', 'x-ip-token']
|
339 |
+
# x_ip_token = request.headers['X-IP-TOKEN']
|
340 |
+
# print(str(x_ip_token))
|
341 |
+
# client = Client("LPX55/zerogpu-experiments", hf_token=huggingface_token, headers={"x-ip-token": x_ip_token})
|
342 |
+
# cresult = client.predict(
|
343 |
+
# n=3,
|
344 |
+
# api_name="/predict"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
345 |
# )
|
|
|
|
|
|
|
|
|
346 |
|
347 |
caption_state = gr.State()
|
348 |
focus_state = gr.State()
|
349 |
log_state = gr.State()
|
|
|
350 |
generate_button.click(
|
351 |
fn=process_image,
|
352 |
inputs=[
|
353 |
control_image, prompt, system_prompt, scale, steps,
|
354 |
controlnet_conditioning_scale, guidance_scale, seed,
|
355 |
+
guidance_end, temperature_slider, max_tokens_slider, log_prompt
|
356 |
],
|
357 |
outputs=[log_state, generated_image, prompt]
|
358 |
)
|
359 |
control_image.upload(
|
360 |
generate_caption,
|
361 |
inputs=[control_image],
|
362 |
+
outputs=[caption_state, original_dimensions]
|
363 |
).then(
|
364 |
generate_focus,
|
365 |
inputs=[control_image, focus],
|
366 |
+
outputs=[focus_state, original_dimensions]
|
367 |
).then(
|
368 |
combine_caption_focus,
|
369 |
inputs=[caption_state, focus_state],
|
|
|
372 |
caption_button.click(
|
373 |
fn=generate_caption,
|
374 |
inputs=[control_image],
|
375 |
+
outputs=[prompt, original_dimensions]
|
376 |
).then(
|
377 |
generate_focus,
|
378 |
inputs=[control_image, focus],
|
379 |
+
outputs=[focus_state, original_dimensions]
|
380 |
).then(
|
381 |
combine_caption_focus,
|
382 |
inputs=[caption_state, focus_state],
|
383 |
outputs=[prompt]
|
384 |
)
|
385 |
+
preset_radio.change(
|
386 |
+
fn=update_parameters,
|
387 |
+
inputs=[preset_radio],
|
388 |
+
outputs=[scale, steps, controlnet_conditioning_scale, guidance_scale, guidance_end]
|
389 |
+
)
|
390 |
+
def hello(profile: gr.OAuthProfile | None) -> str:
|
391 |
+
if profile is None:
|
392 |
+
return "Hello guest! There is a bug with HF ZeroGPUs that are afffecting some usage on certain spaces. Testing out some possible solutions."
|
393 |
+
return f"You are logged in as {profile.name}. If you run into incorrect messages about ZeroGPU runtime credits being out, PLEASE give me a heads up so I can investigate further."
|
394 |
+
|
395 |
demo.load(hello, inputs=None, outputs=msg1)
|
396 |
+
demo.queue().launch(show_error=True)
|