Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradio Diffusers example #76

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ dependencies:
- -e git+https://github.com/openai/CLIP.git@main#egg=clip
- openai
- gradio
- streamlit
- seaborn
- git+https://github.com/crowsonkb/k-diffusion.git
91 changes: 91 additions & 0 deletions gradio-diffusers-demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import gradio as gr
import numpy as np
from PIL import Image

import torch
from diffusers import StableDiffusionInstructPix2PixPipeline


def image_processing_app(prompt, num_inference_steps, guidance_scale, image_guidance_scale, input_image):
if input_image is not None:
output_image = run_function(input_image, num_inference_steps, guidance_scale, image_guidance_scale)
output_image = np.array(output_image).astype('uint8')

return input_image, output_image
else:
return None

def main():
model_id = "timbrooks/instruct-pix2pix"
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
model_id, torch_dtype=torch.float16, safety_checker=None
).to("cuda")
def generate(input_image, instruction, steps, randomize_seed, seed, randomize_cfg, text_cfg_scale, image_cfg_scale, **kwargs):
run = True
img = input_image.resize((512, 512), resample=Image.Resampling.LANCZOS).convert("RGB")
while run == True:
# Add your custom processing here
image = pipe(prompt=instruction, image=img, num_inference_steps=steps, guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale).images
return [seed, text_cfg_scale, image_cfg_scale, image[0]]
#yield target.right_column.image(image[0], use_column_width=True)

with gr.Blocks(css="footer {visibility: hidden}") as demo:
with gr.Row():
with gr.Column(scale=1, min_width=100):
generate_button = gr.Button("Generate")
with gr.Column(scale=1, min_width=100):
load_button = gr.Button("Load Example")
with gr.Column(scale=1, min_width=100):
reset_button = gr.Button("Reset")
with gr.Column(scale=3):
instruction = gr.Textbox(lines=1, label="Edit Instruction", interactive=True)

with gr.Row():
input_image = gr.Image(label="Input Image", type="pil", interactive=True)
edited_image = gr.Image(label=f"Edited Image", type="pil", interactive=False)
input_image.style(height=512, width=512)
edited_image.style(height=512, width=512)

with gr.Row():
steps = gr.Number(value=100, precision=0, label="Steps", interactive=True)
randomize_seed = gr.Radio(
["Fix Seed", "Randomize Seed"],
value="Randomize Seed",
type="index",
show_label=False,
interactive=True,
)
seed = gr.Number(value=1371, precision=0, label="Seed", interactive=True)
randomize_cfg = gr.Radio(
["Fix CFG", "Randomize CFG"],
value="Fix CFG",
type="index",
show_label=False,
interactive=True,
)
text_cfg_scale = gr.Number(value=7.5, label=f"Text CFG", interactive=True)
image_cfg_scale = gr.Number(value=1.5, label=f"Image CFG", interactive=True)

gr.Markdown("help")

generate_button.click(
fn=generate,
inputs=[
input_image,
instruction,
steps,
randomize_seed,
seed,
randomize_cfg,
text_cfg_scale,
image_cfg_scale,
],
outputs=[seed, text_cfg_scale, image_cfg_scale, edited_image],
)

demo.queue(concurrency_count=1)
demo.launch(share=False)


if __name__ == "__main__":
main()