Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

Hugging Face's logo Hugging Face Models Datasets Spaces Posts Docs Pricing Spaces:…

  1. User

    Hugging Face's logo Hugging Face

    Models Datasets Spaces Posts Docs Pricing

    Spaces:

    diffusers / unofficial-SDXL-Turbo-i2i-t2i

    like 383 App

    Files Community 4 unofficial-SDXL-Turbo-i2i-t2i / app.py radames's picture radames HF STAFF Update app.py 18ae4e7 about 2 months ago raw history blame contribute delete No virus 7 kB from diffusers import AutoPipelineForImage2Image, AutoPipelineForText2Image import torch import os

    try: import intel_extension_for_pytorch as ipex except: pass

    from PIL import Image import numpy as np import gradio as gr import psutil import time import math

    SAFETY_CHECKER = os.environ.get("SAFETY_CHECKER", None) TORCH_COMPILE = os.environ.get("TORCH_COMPILE", None) HF_TOKEN = os.environ.get("HF_TOKEN", None)

    check if MPS is available OSX only M1/M2/M3 chips

    mps_available = hasattr(torch.backends, "mps") and torch.backends.mps.is_available() xpu_available = hasattr(torch, "xpu") and torch.xpu.is_available() device = torch.device( "cuda" if torch.cuda.is_available() else "xpu" if xpu_available else "cpu" ) torch_device = device torch_dtype = torch.float16

    print(f"SAFETY_CHECKER: {SAFETY_CHECKER}") print(f"TORCH_COMPILE: {TORCH_COMPILE}") print(f"device: {device}")

    if mps_available: device = torch.device("mps") torch_device = "cpu" torch_dtype = torch.float32

    if SAFETY_CHECKER == "True": i2i_pipe = AutoPipelineForImage2Image.from_pretrained( "stabilityai/sdxl-turbo", torch_dtype=torch_dtype, variant="fp16" if torch_dtype == torch.float16 else "fp32", ) t2i_pipe = AutoPipelineForText2Image.from_pretrained( "stabilityai/sdxl-turbo", torch_dtype=torch_dtype, variant="fp16" if torch_dtype == torch.float16 else "fp32", ) else: i2i_pipe = AutoPipelineForImage2Image.from_pretrained( "stabilityai/sdxl-turbo", safety_checker=None, torch_dtype=torch_dtype, variant="fp16" if torch_dtype == torch.float16 else "fp32", ) t2i_pipe = AutoPipelineForText2Image.from_pretrained( "stabilityai/sdxl-turbo", safety_checker=None, torch_dtype=torch_dtype, variant="fp16" if torch_dtype == torch.float16 else "fp32", )

    t2i_pipe.to(device=torch_device, dtype=torch_dtype).to(device) t2i_pipe.set_progress_bar_config(disable=True) i2i_pipe.to(device=torch_device, dtype=torch_dtype).to(device) i2i_pipe.set_progress_bar_config(disable=True)

    def resize_crop(image, size=512): image = image.convert("RGB") w, h = image.size image = image.resize((size, int(size * (h / w))), Image.BICUBIC) return image

    async def predict(init_image, prompt, strength, steps, seed=1231231): if init_image is not None: init_image = resize_crop(init_image) generator = torch.manual_seed(seed) last_time = time.time()

        if int(steps * strength) < 1:
            steps = math.ceil(1 / max(0.10, strength))
            
        results = i2i_pipe(
            prompt=prompt,
            image=init_image,
            generator=generator,
            num_inference_steps=steps,
            guidance_scale=0.0,
            strength=strength,
            width=512,
            height=512,
            output_type="pil",
        )
    else:
        generator = torch.manual_seed(seed)
        last_time = time.time()
        results = t2i_pipe(
            prompt=prompt,
            generator=generator,
            num_inference_steps=steps,
            guidance_scale=0.0,
            width=512,
            height=512,
            output_type="pil",
        )
    print(f"Pipe took {time.time() - last_time} seconds")
    nsfw_content_detected = (
        results.nsfw_content_detected[0]
        if "nsfw_content_detected" in results
        else False
    )
    if nsfw_content_detected:
        gr.Warning("NSFW content detected.")
        return Image.new("RGB", (512, 512))
    return results.images[0]
    

    css = """ #container{ margin: 0 auto; max-width: 80rem; } #intro{ max-width: 100%; text-align: center; margin: 0 auto; } """ with gr.Blocks(css=css) as demo: init_image_state = gr.State() with gr.Column(elem_id="container"): gr.Markdown( """# SDXL Turbo Image to Image/Text to Image ## Unofficial Demo SDXL Turbo model can generate high quality images in a single pass read more on stability.ai post.
    Model: https://huggingface.co/stabilityai/sdxl-turbo """, elem_id="intro", ) with gr.Row(): prompt = gr.Textbox( placeholder="Insert your prompt here:", scale=5, container=False, ) generate_bt = gr.Button("Generate", scale=1) with gr.Row(): with gr.Column(): image_input = gr.Image( sources=["upload", "webcam", "clipboard"], label="Webcam", type="pil", ) with gr.Column(): image = gr.Image(type="filepath") with gr.Accordion("Advanced options", open=False): strength = gr.Slider( label="Strength", value=0.7, minimum=0.0, maximum=1.0, step=0.001, ) steps = gr.Slider( label="Steps", value=2, minimum=1, maximum=10, step=1 ) seed = gr.Slider( randomize=True, minimum=0, maximum=12013012031030, label="Seed", step=1, )

        with gr.Accordion("Run with diffusers"):
            gr.Markdown(
                """## Running SDXL Turbo with `diffusers`
            ```bash
            pip install diffusers==0.23.1
            ```
            ```py
            from diffusers import DiffusionPipeline
            pipe = DiffusionPipeline.from_pretrained(
                "stabilityai/sdxl-turbo"
            ).to("cuda")
            results = pipe(
                prompt="A cinematic shot of a baby racoon wearing an intricate italian priest robe",
                num_inference_steps=1,
                guidance_scale=0.0,
            )
            imga = results.images[0]
            imga.save("image.png")
            ```
            """
            )
    
        inputs = [image_input, prompt, strength, steps, seed]
        generate_bt.click(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        prompt.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        steps.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        seed.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        strength.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        image_input.change(
            fn=lambda x: x,
            inputs=image_input,
            outputs=init_image_state,
            show_progress=False,
            queue=False,
        )
    

    demo.queue() demo.launch() 帮我把这段代码转换为comfy的工作流,有办法做到吗?

  2. Assistant

    The code you've provided is a Gradio app that uses models from the diffusers library provided by Hugging Face for image-to-image (i2i) and text-to-image (t2i) transformations. It's built to run in a Python environment where you have the necessary libraries installed (e.g., Gradio, diffusers, PIL, torch).

    Translating this code into a "comfy" workflow is a bit ambiguous since "comfy" is not a recognized programming framework, library, or known standard in the context of this code. If by "comfy," you mean making it more user-friendly or streamlined for end-users, you essentially have to ensure that the UI and the experience of using the Gradio app are as intuitive and comfortable as possible.

    This may include:

    1. Writing more descriptive tooltips or help texts to guide the user through the process.
    2. Simplifying advanced options or setting sensible defaults that work well for most situations.
    3. Creating an attractive UI with CSS and JavaScript if needed.
    4. Properly handling errors and ensuring that users are given helpful feedback when something goes wrong.

    If by "comfy," you mean to deploy it in a way that it's easily accessible, you can do several things:

    1. Deploy the application to a cloud service so that it's available online for users to access through a web browser without installing anything on their local machine.
    2. Incorporate the app into a website where users are already comfortable and familiar with the layout and navigation.
    3. Provide comprehensive documentation and examples.

    If "comfy" refers to a concept or a specific tool that I'm not aware of as of my knowledge cutoff date, then you'd need to provide more context or check the latest documentation related to that tool for converting the code.

    However, if "comfy" is a term related to a specific technology or workflow that emerged after my last update in early 2023, I suggest consulting the latest resources or community discussions for that technology to understand how the code could be adapted for such an environment.

  3. User

    from diffusers import AutoPipelineForImage2Image, AutoPipelineForText2Image import torch import os

    try: import intel_extension_for_pytorch as ipex except: pass

    from PIL import Image import numpy as np import gradio as gr import psutil import time import math

    SAFETY_CHECKER = os.environ.get(“SAFETY_CHECKER”, None) TORCH_COMPILE = os.environ.get(“TORCH_COMPILE”, None) HF_TOKEN = os.environ.get(“HF_TOKEN”, None)

    check if MPS is available OSX only M1/M2/M3 chips

    mps_available = hasattr(torch.backends, “mps”) and torch.backends.mps.is_available() xpu_available = hasattr(torch, “xpu”) and torch.xpu.is_available() device = torch.device( “cuda” if torch.cuda.is_available() else “xpu” if xpu_available else “cpu” ) torch_device = device torch_dtype = torch.float16

    print(f"SAFETY_CHECKER: {SAFETY_CHECKER}“) print(f"TORCH_COMPILE: {TORCH_COMPILE}”) print(f"device: {device}“)

    if mps_available: device = torch.device(“mps”) torch_device = “cpu” torch_dtype = torch.float32

    if SAFETY_CHECKER == “True”: i2i_pipe = AutoPipelineForImage2Image.from_pretrained( “stabilityai/sdxl-turbo”, torch_dtype=torch_dtype, variant=“fp16” if torch_dtype == torch.float16 else “fp32”, ) t2i_pipe = AutoPipelineForText2Image.from_pretrained( “stabilityai/sdxl-turbo”, torch_dtype=torch_dtype, variant=“fp16” if torch_dtype == torch.float16 else “fp32”, ) else: i2i_pipe = AutoPipelineForImage2Image.from_pretrained( “stabilityai/sdxl-turbo”, safety_checker=None, torch_dtype=torch_dtype, variant=“fp16” if torch_dtype == torch.float16 else “fp32”, ) t2i_pipe = AutoPipelineForText2Image.from_pretrained( “stabilityai/sdxl-turbo”, safety_checker=None, torch_dtype=torch_dtype, variant=“fp16” if torch_dtype == torch.float16 else “fp32”, )

    t2i_pipe.to(device=torch_device, dtype=torch_dtype).to(device) t2i_pipe.set_progress_bar_config(disable=True) i2i_pipe.to(device=torch_device, dtype=torch_dtype).to(device) i2i_pipe.set_progress_bar_config(disable=True)

    def resize_crop(image, size=512): image = image.convert(“RGB”) w, h = image.size image = image.resize((size, int(size * (h / w))), Image.BICUBIC) return image

    async def predict(init_image, prompt, strength, steps, seed=1231231): if init_image is not None: init_image = resize_crop(init_image) generator = torch.manual_seed(seed) last_time = time.time()

    if int(steps * strength) < 1: steps = math.ceil(1 / max(0.10, strength))

    results = i2i_pipe( prompt=prompt, image=init_image, generator=generator, num_inference_steps=steps, guidance_scale=0.0, strength=strength, width=512, height=512, output_type=“pil”, ) else: generator = torch.manual_seed(seed) last_time = time.time() results = t2i_pipe( prompt=prompt, generator=generator, num_inference_steps=steps, guidance_scale=0.0, width=512, height=512, output_type=“pil”, ) print(f"Pipe took {time.time() - last_time} seconds”) nsfw_content_detected = ( results.nsfw_content_detected[0] if “nsfw_content_detected” in results else False ) if nsfw_content_detected: gr.Warning(“NSFW content detected.”) return Image.new(“RGB”, (512, 512)) return results.images[0]

    css = “”“ #container{ margin: 0 auto; max-width: 80rem; } #intro{ max-width: 100%; text-align: center; margin: 0 auto; } ”“” with gr.Blocks(css=css) as demo: init_image_state = gr.State() with gr.Column(elem_id=“container”): gr.Markdown( “”“# SDXL Turbo Image to Image/Text to Image

    Unofficial Demo

    SDXL Turbo model can generate high quality images in a single pass read more on stability.ai post. Model: https://huggingface.co/stabilityai/sdxl-turbo “””, elem_id=“intro”, ) with gr.Row(): prompt = gr.Textbox( placeholder=“Insert your prompt here:”, scale=5, container=False, ) generate_bt = gr.Button(“Generate”, scale=1) with gr.Row(): with gr.Column(): image_input = gr.Image( sources=[“upload”, “webcam”, “clipboard”], label=“Webcam”, type=“pil”, ) with gr.Column(): image = gr.Image(type=“filepath”) with gr.Accordion(“Advanced options”, open=False): strength = gr.Slider( label=“Strength”, value=0.7, minimum=0.0, maximum=1.0, step=0.001, ) steps = gr.Slider( label=“Steps”, value=2, minimum=1, maximum=10, step=1 ) seed = gr.Slider( randomize=True, minimum=0, maximum=12013012031030, label=“Seed”, step=1, )

    with gr.Accordion(“Run with diffusers”): gr.Markdown( “”“## Running SDXL Turbo with diffusers

            pip install diffusers==0.23.1
    
            from diffusers import DiffusionPipeline
            pipe = DiffusionPipeline.from_pretrained(
                “stabilityai/sdxl-turbo”
            ).to(“cuda”)
            results = pipe(
                prompt=“A cinematic shot of a baby racoon wearing an intricate italian priest robe”,
                num_inference_steps=1,
                guidance_scale=0.0,
            )
            imga = results.images[0]
            imga.save(“image.png”)
    
            “””
            )
    
        inputs = [image_input, prompt, strength, steps, seed]
        generate_bt.click(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        prompt.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        steps.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        seed.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        strength.change(fn=predict, inputs=inputs, outputs=image, show_progress=False)
        image_input.change(
            fn=lambda x: x,
            inputs=image_input,
            outputs=init_image_state,
            show_progress=False,
            queue=False,
        )
    

    demo.queue() demo.launch() 帮我把这段代码转换为comfyui的工作流,json类型的文件,有办法做到吗?

  4. Assistant

    comfyui appears to be a specific UI framework or library, but without specific information about this framework (as of my knowledge cutoff in early 2023), I can't provide direct guidance on how to convert a Gradio application to 'comfyui' workflow. However, if comfyui accepts a JSON-like configuration for defining UI and workflow, the general process would involve mapping the components and functionality of the Gradio app to the JSON configuration specifications of comfyui.

    Here's a hypothetical example of how you might start structuring such a JSON configuration based on the provided Gradio code:

    {
      "app": {
        "title": "SDXL Turbo Image to Image/Text to Image Unofficial Demo",
        "description": "Generate high quality images in a single pass using the SDXL Turbo model.",
        "model": "https://huggingface.co/stabilityai/sdxl-turbo"
      },
      "ui": {
        "prompt": {
          "type": "textbox",
          "label": "Enter your prompt:",
          "placeholder": "Insert your prompt here",
          "multiline": false
        },
        "image_input": {
          "type": "image",
          "label": "Upload Image or Take a Photo",
          "sources": ["upload", "webcam", "clipboard"]
        },
        "advanced_options": {
          "type": "accordion",
          "label": "Advanced options",
          "children": {
            "strength": {
              "type": "slider",
              "label": "Strength",
              "min": 0.0,
              "max": 1.0,
              "value": 0.7,
              "step": 0.001
            },
            "steps": {
              "type": "slider",
              "label": "Steps",
              "min": 1,
              "max": 10,
              "value": 2,
              "step": 1
            },
            "seed": {
              "type": "slider",
              "label": "Seed",
              "min": 0,
              "max": 12013012031030,
              "value": 1231231,
              "step": 1,
              "randomize": true
            }
          }
        },
        "generate_button": {
          "type": "button",
          "label": "Generate"
        },
        "output": {
          "type": "image",
          "label": "Generated Image"
        }
      },
      "actions": [
        {
          "on": "generate_button_click",
          "action": "predict",
          "inputs": ["image_input", "prompt", "strength", "steps", "seed"],
          "outputs": ["output"]
        }
      ]
    }
    

    This JSON structure is purely speculative and would need to be adapted according to the actual comfyui framework's specifications if it exists.

    Remember, the actual JSON structure will vary based on the capabilities and requirements of the comfyui framework, provided that such a framework exists and supports such a configuration. You would need to review the documentation for comfyui to ensure compatibility and adherence to the schema it requires.

    At this point, given the information at hand, the most productive next steps would be to:

    1. Seek out the official documentation for comfyui if it exists.
    2. Learn about the comfyui JSON schema, as well as how it handles inputs, outputs, events, callbacks, and state.
    3. Translate the Gradio components to the corresponding comfyui JSON components and structure.

    Please note you should ensure that the comfyui framework you mention supports the necessary interactions and model inference capabilities required for your application.