Spaces:
Running
on
Zero
Running
on
Zero
Update raw.py
Browse files
raw.py
CHANGED
|
@@ -14,6 +14,7 @@ from peft import PeftModel, PeftConfig
|
|
| 14 |
# )
|
| 15 |
import gradio as gr
|
| 16 |
huggingface_token = os.getenv("HUGGINFACE_TOKEN")
|
|
|
|
| 17 |
|
| 18 |
quant_config = TransformersBitsAndBytesConfig(load_in_8bit=True,)
|
| 19 |
text_encoder_2_8bit = T5EncoderModel.from_pretrained(
|
|
@@ -55,15 +56,19 @@ pipe.unload_lora_weights()
|
|
| 55 |
# pipe.push_to_hub("fused-t-r")
|
| 56 |
|
| 57 |
@spaces.GPU
|
| 58 |
-
def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale):
|
|
|
|
|
|
|
| 59 |
# Load control image
|
| 60 |
control_image = load_image(control_image)
|
| 61 |
w, h = control_image.size
|
| 62 |
-
|
|
|
|
| 63 |
control_image = control_image.resize((int(w * scale), int(h * scale)), resample=2) # Resample.BILINEAR
|
| 64 |
print("Size to: " + str(control_image.size[0]) + ", " + str(control_image.size[1]))
|
| 65 |
with torch.inference_mode():
|
| 66 |
image = pipe(
|
|
|
|
| 67 |
prompt=prompt,
|
| 68 |
control_image=control_image,
|
| 69 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
|
@@ -71,6 +76,7 @@ def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_
|
|
| 71 |
guidance_scale=guidance_scale,
|
| 72 |
height=control_image.size[1],
|
| 73 |
width=control_image.size[0]
|
|
|
|
| 74 |
).images[0]
|
| 75 |
|
| 76 |
return image
|
|
@@ -86,12 +92,14 @@ with gr.Blocks(title="FLUX ControlNet Image Generation", fill_height=True) as if
|
|
| 86 |
with gr.Row():
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
prompt = gr.Textbox(lines=4, placeholder="Enter your prompt here...", label="Prompt")
|
|
|
|
| 89 |
generate_button = gr.Button("Generate Image", variant="primary")
|
| 90 |
with gr.Column(scale=1):
|
| 91 |
-
|
| 92 |
-
steps = gr.Slider(
|
| 93 |
-
guidance_scale = gr.Slider(1, 20, value=3.5, label="Guidance Scale")
|
| 94 |
controlnet_conditioning_scale = gr.Slider(0, 1, value=0.6, label="ControlNet Scale")
|
|
|
|
|
|
|
| 95 |
|
| 96 |
|
| 97 |
with gr.Row():
|
|
@@ -99,7 +107,7 @@ with gr.Blocks(title="FLUX ControlNet Image Generation", fill_height=True) as if
|
|
| 99 |
|
| 100 |
generate_button.click(
|
| 101 |
fn=generate_image,
|
| 102 |
-
inputs=[prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale],
|
| 103 |
outputs=[generated_image]
|
| 104 |
)
|
| 105 |
|
|
|
|
| 14 |
# )
|
| 15 |
import gradio as gr
|
| 16 |
huggingface_token = os.getenv("HUGGINFACE_TOKEN")
|
| 17 |
+
MAX_SEED = 1000000
|
| 18 |
|
| 19 |
quant_config = TransformersBitsAndBytesConfig(load_in_8bit=True,)
|
| 20 |
text_encoder_2_8bit = T5EncoderModel.from_pretrained(
|
|
|
|
| 56 |
# pipe.push_to_hub("fused-t-r")
|
| 57 |
|
| 58 |
@spaces.GPU
|
| 59 |
+
def generate_image(prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale, seed, guidance_end):
|
| 60 |
+
generator = torch.Generator().manual_seed(seed)
|
| 61 |
+
|
| 62 |
# Load control image
|
| 63 |
control_image = load_image(control_image)
|
| 64 |
w, h = control_image.size
|
| 65 |
+
w = w - w % 32
|
| 66 |
+
h = h - h % 32
|
| 67 |
control_image = control_image.resize((int(w * scale), int(h * scale)), resample=2) # Resample.BILINEAR
|
| 68 |
print("Size to: " + str(control_image.size[0]) + ", " + str(control_image.size[1]))
|
| 69 |
with torch.inference_mode():
|
| 70 |
image = pipe(
|
| 71 |
+
generator=generator
|
| 72 |
prompt=prompt,
|
| 73 |
control_image=control_image,
|
| 74 |
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
|
|
|
| 76 |
guidance_scale=guidance_scale,
|
| 77 |
height=control_image.size[1],
|
| 78 |
width=control_image.size[0]
|
| 79 |
+
control_guidance_end=guidance_end
|
| 80 |
).images[0]
|
| 81 |
|
| 82 |
return image
|
|
|
|
| 92 |
with gr.Row():
|
| 93 |
with gr.Column(scale=1):
|
| 94 |
prompt = gr.Textbox(lines=4, placeholder="Enter your prompt here...", label="Prompt")
|
| 95 |
+
scale = gr.Slider(1, 3, value=1, label="Scale", step=0.25)
|
| 96 |
generate_button = gr.Button("Generate Image", variant="primary")
|
| 97 |
with gr.Column(scale=1):
|
| 98 |
+
seed = gr.Slider(0, MAX_SEED, value=42, label="Seed", step=1)
|
| 99 |
+
steps = gr.Slider(2, 16, value=8, label="Steps")
|
|
|
|
| 100 |
controlnet_conditioning_scale = gr.Slider(0, 1, value=0.6, label="ControlNet Scale")
|
| 101 |
+
guidance_scale = gr.Slider(1, 20, value=3.5, label="Guidance Scale")
|
| 102 |
+
guidance_end = gr.Slider(0, 1, value=1.0, label="Guidance End")
|
| 103 |
|
| 104 |
|
| 105 |
with gr.Row():
|
|
|
|
| 107 |
|
| 108 |
generate_button.click(
|
| 109 |
fn=generate_image,
|
| 110 |
+
inputs=[prompt, scale, steps, control_image, controlnet_conditioning_scale, guidance_scale, seed, guidance_end],
|
| 111 |
outputs=[generated_image]
|
| 112 |
)
|
| 113 |
|