| | import os |
| | donwload_repo_loc= "./models/image_encoder/" |
| | os.system("pip install -U peft") |
| | |
| | |
| | |
| |
|
| | import spaces |
| | import gradio as gr |
| | import torch |
| | from diffusers import StableDiffusionXLPipeline |
| | from PIL import Image |
| | from ip_adapter import IPAdapterXL |
| | base_model_path = "stabilityai/stable-diffusion-xl-base-1.0" |
| | device = "cuda" |
| |
|
| | image_encoder_path = donwload_repo_loc |
| | ip_ckpt = "./models/ip-adapter_sdxl.bin" |
| | |
| | pipe = StableDiffusionXLPipeline.from_pretrained( |
| | base_model_path, |
| | torch_dtype=torch.float16, |
| | add_watermarker=False, |
| | ) |
| |
|
| |
|
| | |
| | @spaces.GPU(enable_queue=True) |
| | def create_image(image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed): |
| | |
| | if target =="Load original IP-Adapter": |
| | |
| | ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device, target_blocks=["blocks"]) |
| | elif target=="Load only style blocks": |
| | |
| | ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device, target_blocks=["up_blocks.0.attentions.1"]) |
| | elif target == "Load style+layout block": |
| | |
| | ip_model = IPAdapterXL(pipe, image_encoder_path, ip_ckpt, device, target_blocks=["up_blocks.0.attentions.1", "down_blocks.2.attentions.1"]) |
| | |
| |
|
| | image_pil=image_pil.resize((512, 512)) |
| | images = ip_model.generate(pil_image=image_pil, |
| | prompt=prompt, |
| | negative_prompt=n_prompt, |
| | scale=scale, |
| | guidance_scale=guidance_scale, |
| | num_samples=num_samples, |
| | num_inference_steps=num_inference_steps, |
| | seed=seed, |
| | |
| | |
| | ) |
| |
|
| | |
| | del ip_model |
| | |
| | return images |
| |
|
| |
|
| | DESCRIPTION = """ |
| | # InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation |
| | **Demo by [ameer azam] - [Twitter](https://twitter.com/Ameerazam18) - [GitHub](https://github.com/AMEERAZAM08)) - [Hugging Face](https://huggingface.co/ameerazam08)** |
| | This is a demo of https://github.com/InstantStyle/InstantStyle. |
| | """ |
| |
|
| | block = gr.Blocks(css="footer {visibility: hidden}").queue(max_size=10) |
| | with block: |
| | with gr.Row(): |
| | |
| | with gr.Column(): |
| | |
| | gr.Markdown(DESCRIPTION) |
| | with gr.Tabs(): |
| | with gr.Row(): |
| | with gr.Column(): |
| | image_pil = gr.Image(label="Style Image", type='pil') |
| | target = gr.Dropdown(["Load original IP-Adapter","Load only style blocks","Load style+layout block"], label="Load Style", info="IP-Adapter Layers") |
| | prompt = gr.Textbox(label="Prompt",value="a cat, masterpiece, best quality, high quality") |
| | n_prompt = gr.Textbox(label="Neg Prompt",value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry") |
| | scale = gr.Slider(minimum=0,maximum=2.0, step=0.01,value=1.0, label="scale") |
| | guidance_scale = gr.Slider(minimum=1,maximum=15.0, step=0.01,value=5.0, label="guidance_scale") |
| | num_samples= gr.Slider(minimum=1,maximum=3.0, step=1.0,value=1.0, label="num_samples") |
| | num_inference_steps = gr.Slider(minimum=5,maximum=50.0, step=1.0,value=30, label="num_inference_steps") |
| | seed = gr.Slider(minimum=-1000000,maximum=1000000,value=1, step=1, label="Seed Value") |
| | generate_button = gr.Button("Generate Image") |
| | with gr.Column(): |
| | generated_image = gr.Gallery(label="Generated Image") |
| |
|
| | generate_button.click(fn=create_image, inputs=[image_pil,target,prompt,n_prompt,scale, guidance_scale,num_samples,num_inference_steps,seed], |
| | outputs=[generated_image]) |
| |
|
| | block.launch() |