Spaces:
Runtime error
Runtime error
Download app.py from Seba882089/MAIchelangelo: direct link, hf CLI and curl.
- Browser
- Download file 4.43 kB
-
https://huggingface.co/spaces/Seba882089/MAIchelangelo/resolve/main/app.py
- Command line
-
hf download hf://spaces/Seba882089/MAIchelangelo/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Seba882089/MAIchelangelo/resolve/main/app.py
4.43 kB
| import math | |
| import random | |
| import gradio as gr | |
| import torch | |
| from PIL import Image, ImageOps | |
| from diffusers import StableDiffusionInstructPix2PixPipeline | |
| help_text = """ | |
| """ | |
| model_id = "timbrooks/instruct-pix2pix" | |
| def main(): | |
| pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(model_id, torch_dtype=torch.float16, safety_checker=None).to("cuda") | |
| def generate( | |
| input_image: Image.Image, | |
| instruction: str, | |
| steps: int, | |
| randomize_seed: bool, | |
| seed: int, | |
| randomize_cfg: bool, | |
| text_cfg_scale: float, | |
| image_cfg_scale: float, | |
| ): | |
| seed = random.randint(0, 100000) if randomize_seed else seed | |
| text_cfg_scale = round(random.uniform(6.0, 9.0), ndigits=2) if randomize_cfg else text_cfg_scale | |
| image_cfg_scale = round(random.uniform(1.2, 1.8), ndigits=2) if randomize_cfg else image_cfg_scale | |
| width, height = input_image.size | |
| factor = 512 / max(width, height) | |
| factor = math.ceil(min(width, height) * factor / 64) * 64 / min(width, height) | |
| width = int((width * factor) // 64) * 64 | |
| height = int((height * factor) // 64) * 64 | |
| input_image = ImageOps.fit(input_image, (width, height), method=Image.Resampling.LANCZOS) | |
| if instruction == "": | |
| return [input_image, seed] | |
| generator = torch.manual_seed(seed) | |
| edited_image = pipe( | |
| instruction, image=input_image, | |
| guidance_scale=text_cfg_scale, image_guidance_scale=image_cfg_scale, | |
| num_inference_steps=steps, generator=generator, | |
| ).images[0] | |
| return [seed, text_cfg_scale, image_cfg_scale, edited_image] | |
| def reset(): | |
| return [0, "Randomize Seed", 1371, "Fix CFG", 7.5, 1.5, None] | |
| with gr.Blocks() as demo: | |
| gr.HTML("""<h1 style="font-weight: 900; margin-bottom: 7px;"> | |
| MAIchelangelo | |
| </h1> | |
| <p> | |
| <br/> | |
| <a href=""> | |
| <img style="margin-top: 0em; margin-bottom: 0em" src="" alt=""></a> | |
| <p/>""") | |
| with gr.Row(): | |
| with gr.Column(scale=1, min_width=100): | |
| generate_button = gr.Button("Generate") | |
| with gr.Column(scale=1, min_width=100): | |
| reset_button = gr.Button("Reset",visible=False) | |
| with gr.Column(scale=3): | |
| instruction = gr.Textbox(lines=1, label="Edit Instruction", interactive=True,value="Make into a Picasso painting") | |
| with gr.Row(): | |
| input_image = gr.Image(label="Input Image", source="webcam", 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=50, precision=0, label="Steps", interactive=True,visible=False) | |
| randomize_seed = gr.Radio( | |
| ["Fix Seed", "Randomize Seed"], | |
| value="Randomize Seed", | |
| type="index", | |
| show_label=False, | |
| interactive=True, | |
| visible=False, | |
| ) | |
| seed = gr.Number(value=1371, precision=0, label="Seed", interactive=True,visible=False) | |
| randomize_cfg = gr.Radio( | |
| ["Fix CFG", "Randomize CFG"], | |
| value="Fix CFG", | |
| type="index", | |
| show_label=False, | |
| interactive=True, | |
| visible=False, | |
| ) | |
| text_cfg_scale = gr.Number(value=16.5, label=f"Text CFG", interactive=True,visible=True) | |
| image_cfg_scale = gr.Number(value=1.5, label=f"Image CFG", interactive=True,visible=False) | |
| gr.Markdown(help_text) | |
| 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], | |
| ) | |
| reset_button.click( | |
| fn=reset, | |
| inputs=[], | |
| outputs=[steps, randomize_seed, seed, randomize_cfg, text_cfg_scale, image_cfg_scale, edited_image], | |
| ) | |
| demo.queue(concurrency_count=1) | |
| demo.launch(share=True) | |
| if __name__ == "__main__": | |
| main() |