Update app.py
Browse files
app.py
CHANGED
|
@@ -8,16 +8,12 @@ os.system("pip install -U peft")
|
|
| 8 |
import spaces
|
| 9 |
import gradio as gr
|
| 10 |
import torch
|
| 11 |
-
import numpy as np
|
| 12 |
-
import cv2
|
| 13 |
from diffusers import StableDiffusionXLPipeline
|
| 14 |
-
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline
|
| 15 |
from PIL import Image
|
| 16 |
from ip_adapter import IPAdapterXL
|
| 17 |
base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 18 |
device = "cuda"
|
| 19 |
|
| 20 |
-
|
| 21 |
image_encoder_path = donwload_repo_loc #"sdxl_models/image_encoder"
|
| 22 |
ip_ckpt = "./models/ip-adapter_sdxl.bin"
|
| 23 |
# load SDXL pipeline
|
|
@@ -28,50 +24,6 @@ pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
| 28 |
)
|
| 29 |
|
| 30 |
|
| 31 |
-
controlnet_path = "diffusers/controlnet-canny-sdxl-1.0"
|
| 32 |
-
controlnet = ControlNetModel.from_pretrained(controlnet_path, use_safetensors=False, torch_dtype=torch.float16).to(device)
|
| 33 |
-
|
| 34 |
-
contronet_pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
| 35 |
-
base_model_path,
|
| 36 |
-
controlnet=controlnet,
|
| 37 |
-
torch_dtype=torch.float16,
|
| 38 |
-
add_watermarker=False,
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
@spaces.GPU(enable_queue=True)
|
| 44 |
-
def create_image_controlnet(image_pil,input_image,target,prompt,n_prompt,scale, control_scale, guidance_scale,num_samples,num_inference_steps,seed):
|
| 45 |
-
# load ip-adapter
|
| 46 |
-
ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device, target_blocks=["up_blocks.0.attentions.1", "down_blocks.2.attentions.1"])
|
| 47 |
-
|
| 48 |
-
image_pil=image_pil.resize((512, 512))
|
| 49 |
-
cv_input_image = pil_to_cv2(input_image)
|
| 50 |
-
detected_map = cv2.Canny(cv_input_image, 50, 200)
|
| 51 |
-
canny_map = Image.fromarray(cv2.cvtColor(detected_map, cv2.COLOR_BGR2RGB))
|
| 52 |
-
|
| 53 |
-
images = ip_model.generate(pil_image=image_pil,
|
| 54 |
-
prompt=prompt,
|
| 55 |
-
negative_prompt=n_prompt,
|
| 56 |
-
scale=scale,
|
| 57 |
-
guidance_scale=guidance_scale,
|
| 58 |
-
num_samples=num_samples,
|
| 59 |
-
num_inference_steps=num_inference_steps,
|
| 60 |
-
seed=seed,
|
| 61 |
-
image=canny_map,
|
| 62 |
-
controlnet_conditioning_scale=control_scale,
|
| 63 |
-
)
|
| 64 |
-
del ip_model
|
| 65 |
-
|
| 66 |
-
return images
|
| 67 |
-
|
| 68 |
-
def pil_to_cv2(image_pil):
|
| 69 |
-
image_np = np.array(image_pil)
|
| 70 |
-
image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
| 71 |
-
|
| 72 |
-
return image_cv2
|
| 73 |
-
|
| 74 |
-
|
| 75 |
# generate image variations with only image prompt
|
| 76 |
@spaces.GPU(enable_queue=True)
|
| 77 |
def create_image(image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed):
|
|
@@ -114,12 +66,12 @@ This is a demo of https://github.com/InstantStyle/InstantStyle.
|
|
| 114 |
|
| 115 |
block = gr.Blocks(css="footer {visibility: hidden}").queue(max_size=10)
|
| 116 |
with block:
|
| 117 |
-
with gr.
|
| 118 |
-
with gr.Row():
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
with gr.Row():
|
| 124 |
with gr.Column():
|
| 125 |
image_pil = gr.Image(label="Style Image", type='pil')
|
|
@@ -137,34 +89,5 @@ with block:
|
|
| 137 |
|
| 138 |
generate_button.click(fn=create_image, inputs=[image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed],
|
| 139 |
outputs=[generated_image])
|
| 140 |
-
with gr.Tab("Image stylization Style"):
|
| 141 |
-
with gr.Row():
|
| 142 |
-
|
| 143 |
-
with gr.Column():
|
| 144 |
-
gr.Markdown("""
|
| 145 |
-
# Imagestylization-Style: Free Lunch towards Style-Preserving in Text-to-Image Generation
|
| 146 |
-
**Demo by [ameer azam] - [Twitter](https://twitter.com/Ameerazam18) - [GitHub](https://github.com/AMEERAZAM08)) - [Hugging Face](https://huggingface.co/ameerazam08)**
|
| 147 |
-
This is a demo of https://github.com/InstantStyle/InstantStyle.
|
| 148 |
-
""")
|
| 149 |
-
with gr.Row():
|
| 150 |
-
with gr.Column():
|
| 151 |
-
src_image_pil = gr.Image(label="Source Image", type='pil')
|
| 152 |
-
with gr.Column():
|
| 153 |
-
image_pil = gr.Image(label="Style Image", type='pil')
|
| 154 |
-
prompt = gr.Textbox(label="Prompt",value="masterpiece, best quality, high quality")
|
| 155 |
-
n_prompt = gr.Textbox(label="Neg Prompt",value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry")
|
| 156 |
-
scale = gr.Slider(minimum=0,maximum=2.0, step=0.01,value=1.0, label="scale")
|
| 157 |
-
control_scale = gr.Slider(minimum=0,maximum=1.0, step=0.01,value=0.6, label="controlnet conditioning scale")
|
| 158 |
-
guidance_scale = gr.Slider(minimum=1,maximum=15.0, step=0.01,value=5.0, label="guidance scale")
|
| 159 |
-
num_samples= gr.Slider(minimum=1,maximum=4.0, step=1.0,value=1.0, label="num samples")
|
| 160 |
-
num_inference_steps = gr.Slider(minimum=5,maximum=50.0, step=1.0,value=30, label="num inference steps")
|
| 161 |
-
seed = gr.Slider(minimum=-1000000,maximum=1000000,value=1, step=1, label="Seed Value")
|
| 162 |
-
generate_button = gr.Button("Generate Image")
|
| 163 |
-
with gr.Column():
|
| 164 |
-
generated_image = gr.Gallery(label="Generated Image")
|
| 165 |
-
|
| 166 |
-
generate_button.click(fn=create_image_controlnet,
|
| 167 |
-
inputs=[image_pil,src_image_pil,prompt,n_prompt,scale, control_scale, guidance_scale,num_samples,num_inference_steps,seed],
|
| 168 |
-
outputs=[generated_image])
|
| 169 |
|
| 170 |
-
block.launch()
|
|
|
|
| 8 |
import spaces
|
| 9 |
import gradio as gr
|
| 10 |
import torch
|
|
|
|
|
|
|
| 11 |
from diffusers import StableDiffusionXLPipeline
|
|
|
|
| 12 |
from PIL import Image
|
| 13 |
from ip_adapter import IPAdapterXL
|
| 14 |
base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 15 |
device = "cuda"
|
| 16 |
|
|
|
|
| 17 |
image_encoder_path = donwload_repo_loc #"sdxl_models/image_encoder"
|
| 18 |
ip_ckpt = "./models/ip-adapter_sdxl.bin"
|
| 19 |
# load SDXL pipeline
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# generate image variations with only image prompt
|
| 28 |
@spaces.GPU(enable_queue=True)
|
| 29 |
def create_image(image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed):
|
|
|
|
| 66 |
|
| 67 |
block = gr.Blocks(css="footer {visibility: hidden}").queue(max_size=10)
|
| 68 |
with block:
|
| 69 |
+
with gr.Row():
|
|
|
|
| 70 |
|
| 71 |
+
with gr.Column():
|
| 72 |
+
# gr.Markdown("## <h1 align='center'>InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation </h1>")
|
| 73 |
+
gr.Markdown(DESCRIPTION)
|
| 74 |
+
with gr.Tabs():
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column():
|
| 77 |
image_pil = gr.Image(label="Style Image", type='pil')
|
|
|
|
| 89 |
|
| 90 |
generate_button.click(fn=create_image, inputs=[image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed],
|
| 91 |
outputs=[generated_image])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
block.launch()
|