Views
No views yet
A photo-realistic image of a cat3.00.020None421776x5121{
2 "algo": "lokr",
3 "multiplier": 1.0,
4 "linear_dim": 10000,
5 "linear_alpha": 1,
6 "factor": 16,
7 "apply_preset": {
8 "target_module": [
9 "Attention",
10 "FeedForward"
11 ],
12 "module_algo_map": {
13 "Attention": {
14 "factor": 16
15 },
16 "FeedForward": {
17 "factor": 8
18 }
19 }
20 }
21}1import argparse
2import torch
3from helpers.models.flux.pipeline import FluxPipeline as DiffusionPipeline
4from lycoris import create_lycoris_from_weights
5from huggingface_hub import hf_hub_download
6
7def generate_image(pipeline, prompt, output_file, num_inference_steps, width, height, guidance_scale, seed, device):
8 # Set device
9 pipeline.to(device)
10
11 # Generate image
12 generator = torch.Generator(device=device).manual_seed(seed)
13 image = pipeline(
14 prompt=prompt,
15 num_inference_steps=num_inference_steps,
16 generator=generator,
17 width=width,
18 height=height,
19 guidance_scale=guidance_scale,
20 ).images[0]
21
22 # Save image
23 output_file = "output.png"
24 image.save(output_file, format="PNG")
25 print(f"Image saved as {output_file}")
26
27def main():
28 parser = argparse.ArgumentParser(description="Generate images using a custom diffusion pipeline with LoRA weights.")
29 parser.add_argument("--model_id", type=str, default='black-forest-labs/FLUX.1-dev', help="Model ID from Hugging Face Hub.")
30 parser.add_argument("--adapter_id", type=str, default='pytorch_lora_weights.safetensors', help="LoRA weights file.")
31 parser.add_argument("--lora_scale", type=float, default=1.0, help="Scale for LoRA weights.")
32 parser.add_argument("--output_file", type=str, default="output.png", help="Output file name for the generated image.")
33 parser.add_argument("--num_inference_steps", type=int, default=30, help="Number of inference steps.")
34 parser.add_argument("--guidance_scale", type=float, default=3.5, help="Guidance scale for the generation.")
35 parser.add_argument("--seed", type=int, default=1641421826, help="Random seed for reproducibility.")
36 parser.add_argument("--device", type=str, default='cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu', help="Device to run the model on.")
37
38 args = parser.parse_args()
39
40 # Load model and weights
41 hf_hub_download(repo_id="terminusresearch/flux-lokr-garfield-nomask", filename=args.adapter_id, local_dir="./")
42 pipeline = DiffusionPipeline.from_pretrained(args.model_id, torch_dtype=torch.bfloat16)
43
44 # Apply LoRA weights
45 wrapper, _ = create_lycoris_from_weights(args.lora_scale, args.adapter_id, pipeline.transformer)
46 wrapper.merge_to()
47
48 print("Model loaded successfully. Ready to generate images.")
49
50 while True:
51 user_input = input("Enter a prompt or 'quit' to exit: ")
52 if user_input.lower() == 'quit':
53 break
54
55 # Check for resolution command
56 if user_input.startswith("resolution:"):
57 resolution = user_input.split(":")[1]
58 width, height = map(int, resolution.split("x"))
59 print(f"Resolution set to {width}x{height}")
60 continue
61
62 prompt = user_input
63 output_file = args.output_file.replace(".png", f"_{prompt.replace(' ', '_')}.png")
64
65 # Use default or previously set resolution
66 width = locals().get('width', 1024)
67 height = locals().get('height', 1024)
68
69 generate_image(
70 pipeline=pipeline,
71 prompt=prompt,
72 output_file=output_file,
73 num_inference_steps=args.num_inference_steps,
74 width=width,
75 height=height,
76 guidance_scale=args.guidance_scale,
77 seed=args.seed,
78 device=args.device
79 )
80
81if __name__ == "__main__":
82 main()