Spaces:
Runtime error
Runtime error
added image generation from prompt
Browse files
app.py
CHANGED
|
@@ -34,42 +34,6 @@ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
|
| 34 |
scheduler=eulera_scheduler,
|
| 35 |
)
|
| 36 |
|
| 37 |
-
# Load lora (giving it a name makes it active when using the name in the prompt)
|
| 38 |
-
pipe.load_lora_weights("ostris/ikea-instructions-lora-sdxl", weight_name="ikea_instructions_xl_v1_5.safetensors", adapter_name="ikea")
|
| 39 |
-
pipe.load_lora_weights("nerijs/pixel-art-xl", weight_name="pixel-art-xl.safetensors", adapter_name="pixel")
|
| 40 |
-
pipe.load_lora_weights('e-n-v-y/envy-junkworld-xl-01', weight_name='EnvyJunkworldXL01.safetensors', adapter_name="junkworld")
|
| 41 |
-
|
| 42 |
-
pipe.disable_lora()
|
| 43 |
-
|
| 44 |
-
def activate_ikea_lora():
|
| 45 |
-
print("Activating IKEA LoRa")
|
| 46 |
-
pipe.disable_lora()
|
| 47 |
-
while pipe.get_active_adapters()[0] != "ikea":
|
| 48 |
-
pipe.set_adapters("ikea")
|
| 49 |
-
pipe.enable_lora()
|
| 50 |
-
print("IKEA LoRa active!")
|
| 51 |
-
|
| 52 |
-
def activate_pixel_lora():
|
| 53 |
-
print("Activating PixelArt LoRa")
|
| 54 |
-
pipe.disable_lora()
|
| 55 |
-
while pipe.get_active_adapters()[0] != "pixel":
|
| 56 |
-
pipe.set_adapters("pixel")
|
| 57 |
-
pipe.enable_lora()
|
| 58 |
-
print("PixelArt LoRa active!")
|
| 59 |
-
|
| 60 |
-
def activate_junkworld_lora():
|
| 61 |
-
print("Activating JunkWorld LoRa")
|
| 62 |
-
pipe.disable_lora()
|
| 63 |
-
while pipe.get_active_adapters()[0] != "junkworld":
|
| 64 |
-
pipe.set_adapters("junkworld")
|
| 65 |
-
pipe.enable_lora()
|
| 66 |
-
print("JunkWorld LoRa active!")
|
| 67 |
-
|
| 68 |
-
def disable_loras():
|
| 69 |
-
print("Deactivating LoRas")
|
| 70 |
-
pipe.disable_lora()
|
| 71 |
-
print("All LoRas deactivated!")
|
| 72 |
-
|
| 73 |
pipe.to(device)
|
| 74 |
|
| 75 |
# πΈ Edge detection function using OpenCV (Canny)
|
|
@@ -81,12 +45,10 @@ def apply_canny(image, low_threshold, high_threshold):
|
|
| 81 |
image = np.concatenate([image, image, image], axis=2)
|
| 82 |
return Image.fromarray(image)
|
| 83 |
|
| 84 |
-
# π¨ Image generation function
|
| 85 |
@spaces.GPU
|
| 86 |
def generate_image(prompt, input_image, low_threshold, high_threshold, strength, guidance, controlnet_conditioning_scale):
|
| 87 |
|
| 88 |
-
print(pipe.get_active_adapters())
|
| 89 |
-
|
| 90 |
# Apply edge detection
|
| 91 |
edge_detected = apply_canny(input_image, low_threshold, high_threshold)
|
| 92 |
|
|
@@ -102,6 +64,21 @@ def generate_image(prompt, input_image, low_threshold, high_threshold, strength,
|
|
| 102 |
|
| 103 |
return edge_detected, result
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# π₯οΈ Gradio UI
|
| 106 |
with gr.Blocks() as demo:
|
| 107 |
gr.Markdown("# ποΈ 3D Screenshot to Styled Render with ControlNet")
|
|
@@ -117,44 +94,27 @@ with gr.Blocks() as demo:
|
|
| 117 |
strength = gr.Slider(0.1, 1.0, value=0.7, label="Denoising Strength")
|
| 118 |
guidance = gr.Slider(1, 20, value=7.5, label="Guidance Scale (Creativity)")
|
| 119 |
controlnet_conditioning_scale = gr.Slider(0, 1, value=0.5, step=0.01, label="ControlNet Conditioning Scale")
|
| 120 |
-
|
| 121 |
-
with gr.Row():
|
| 122 |
-
ikea_lora_button = gr.Button("IKEA Instructions")
|
| 123 |
-
pixel_lora_button = gr.Button("Pixel Art")
|
| 124 |
-
junkworld_lora_button = gr.Button("Junk World")
|
| 125 |
-
disable_lora_button = gr.Button("Disable LoRas")
|
| 126 |
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
|
| 129 |
with gr.Column():
|
| 130 |
edge_output = gr.Image(label="Edge Detected Image")
|
| 131 |
result_output = gr.Image(label="Generated Styled Image")
|
| 132 |
|
| 133 |
# π Generate Button Action
|
| 134 |
-
|
| 135 |
fn=generate_image,
|
| 136 |
inputs=[prompt, input_image, low_threshold, high_threshold, strength, guidance, controlnet_conditioning_scale],
|
| 137 |
outputs=[edge_output, result_output]
|
| 138 |
)
|
| 139 |
|
| 140 |
-
|
| 141 |
-
fn
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
pixel_lora_button.click(
|
| 145 |
-
fn = activate_pixel_lora,
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
junkworld_lora_button.click(
|
| 149 |
-
fn = activate_junkworld_lora,
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
disable_lora_button.click(
|
| 153 |
-
fn = disable_loras,
|
| 154 |
)
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
|
| 159 |
# π Launch the app
|
| 160 |
demo.launch()
|
|
|
|
| 34 |
scheduler=eulera_scheduler,
|
| 35 |
)
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
pipe.to(device)
|
| 38 |
|
| 39 |
# πΈ Edge detection function using OpenCV (Canny)
|
|
|
|
| 45 |
image = np.concatenate([image, image, image], axis=2)
|
| 46 |
return Image.fromarray(image)
|
| 47 |
|
| 48 |
+
# π¨ Image generation function from image
|
| 49 |
@spaces.GPU
|
| 50 |
def generate_image(prompt, input_image, low_threshold, high_threshold, strength, guidance, controlnet_conditioning_scale):
|
| 51 |
|
|
|
|
|
|
|
| 52 |
# Apply edge detection
|
| 53 |
edge_detected = apply_canny(input_image, low_threshold, high_threshold)
|
| 54 |
|
|
|
|
| 64 |
|
| 65 |
return edge_detected, result
|
| 66 |
|
| 67 |
+
# π¨ Image generation function from prompt
|
| 68 |
+
@spaces.GPU
|
| 69 |
+
def generate_prompt(prompt, strength, guidance):
|
| 70 |
+
|
| 71 |
+
# Generate styled image from prompt
|
| 72 |
+
result = pipe(
|
| 73 |
+
prompt=prompt,
|
| 74 |
+
num_inference_steps=30,
|
| 75 |
+
guidance_scale=guidance,
|
| 76 |
+
strength=strength
|
| 77 |
+
).images[0]
|
| 78 |
+
|
| 79 |
+
return result
|
| 80 |
+
|
| 81 |
+
|
| 82 |
# π₯οΈ Gradio UI
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
gr.Markdown("# ποΈ 3D Screenshot to Styled Render with ControlNet")
|
|
|
|
| 94 |
strength = gr.Slider(0.1, 1.0, value=0.7, label="Denoising Strength")
|
| 95 |
guidance = gr.Slider(1, 20, value=7.5, label="Guidance Scale (Creativity)")
|
| 96 |
controlnet_conditioning_scale = gr.Slider(0, 1, value=0.5, step=0.01, label="ControlNet Conditioning Scale")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
generate_img_button = gr.Button("Generate from Image")
|
| 99 |
+
generate_prompt_button = gr.Button("Generate from Prompt")
|
| 100 |
+
|
| 101 |
|
| 102 |
with gr.Column():
|
| 103 |
edge_output = gr.Image(label="Edge Detected Image")
|
| 104 |
result_output = gr.Image(label="Generated Styled Image")
|
| 105 |
|
| 106 |
# π Generate Button Action
|
| 107 |
+
generate_img_button.click(
|
| 108 |
fn=generate_image,
|
| 109 |
inputs=[prompt, input_image, low_threshold, high_threshold, strength, guidance, controlnet_conditioning_scale],
|
| 110 |
outputs=[edge_output, result_output]
|
| 111 |
)
|
| 112 |
|
| 113 |
+
generate_prompt_button.click(
|
| 114 |
+
fn=generate_image,
|
| 115 |
+
inputs=[prompt, input_image, low_threshold, high_threshold, strength, guidance, controlnet_conditioning_scale],
|
| 116 |
+
outputs=[result_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
)
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
# π Launch the app
|
| 120 |
demo.launch()
|