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

How to use with diffusers #22

Open
Swarzox opened this issue Aug 12, 2024 · 3 comments
Open

How to use with diffusers #22

Swarzox opened this issue Aug 12, 2024 · 3 comments

Comments

@Swarzox
Copy link

Swarzox commented Aug 12, 2024

It seems I can't get it work with diffusers, do you have any very simple example code to make it works?

Many thanks.

@antonioccostajr
Copy link

antonioccostajr commented Aug 19, 2024

I'm trying to use with diffusers as well, but I'm getting this error:

OSError: runwayml/stable-diffusion-v1-5 does not appear to have a file named config.json.

@antonioccostajr
Copy link

`import os
import torch
from diffusers import FluxTransformer2DModel, FluxPipeline, DDIMScheduler
from PIL import Image

def setup_flux_pipeline(checkpoint_file, torch_dtype=torch.bfloat16):
# Verifica se o arquivo existe
if not os.path.isfile(checkpoint_file):
raise ValueError(f"Checkpoint file not found: {checkpoint_file}")

# Carrega o modelo Flux a partir de um arquivo de checkpoint único
transformer = FluxTransformer2DModel.from_single_file(checkpoint_file, torch_dtype=torch_dtype).to("cuda")

# Carrega o scheduler (agendador) a partir de um subdiretório chamado "scheduler"
scheduler = DDIMScheduler.from_pretrained(checkpoint_file, subfolder="scheduler")

# Configura o pipeline com o modelo e o scheduler
pipe = FluxPipeline(unet=transformer, scheduler=scheduler)

return pipe

def generate_image(pipe, prompt, guidance_scale=3.5, height=816, width=1456, num_inference_steps=8, max_sequence_length=256, seed=0):
# Configura o gerador de números aleatórios com uma semente fixa para reprodutibilidade
generator = torch.Generator("cuda").manual_seed(seed)

# Gera a imagem usando o pipeline Flux
image = pipe(
    prompt,
    height=height,
    width=width,
    guidance_scale=guidance_scale,
    num_inference_steps=num_inference_steps,
    max_sequence_length=max_sequence_length,
    generator=generator
).images[0]

return image

def save_image(image, file_name):
image.save(file_name)
print(f"Image saved as {file_name}")

Exemplo de uso

if name == "main":
# Este é o caminho para o arquivo de checkpoint único
checkpoint_file = "/workspace/ComfyUI/models/checkpoints/FluxFusionDS_v0_nf4_clip-f16_vae.safetensors" # Ajuste para o caminho correto

# Inicializa o pipeline com o checkpoint e o scheduler
pipe = setup_flux_pipeline(checkpoint_file)

# Define o prompt para gerar a imagem
prompt = "A cat holding a sign that says hello world"

# Gera a imagem
image = generate_image(pipe, prompt, guidance_scale=3.5, height=816, width=1456, num_inference_steps=8, max_sequence_length=256, seed=0)

# Especifique o nome do arquivo para salvar a imagem
file_name = "flux-dev.png"
save_image(image, file_name)

`

@werruww
Copy link

werruww commented Nov 28, 2024

how to use with python without comfyui or ......?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants