File size: 1,458 Bytes
4b9df77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import gradio as gr
import torch
import spaces
from diffusers import FluxPipeline
from safetensors.torch import load_file

# Load the Flux Dev model
model_id = "black-forest-labs/FLUX.1-dev"
pipe = FluxPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe.to("cuda")

# Load the LoRA weights
lora_path = "MegaTronX/SuicideGirl-FLUX"
lora_weights = load_file(lora_path)

# Apply LoRA weights to the model
pipe.unet.load_attn_procs(lora_weights)

@spaces.GPU
def generate_image(prompt, negative_prompt, guidance_scale, num_inference_steps, lora_scale):
    with torch.inference_mode():
        image = pipe(
            prompt=prompt,
            negative_prompt=negative_prompt,
            num_inference_steps=num_inference_steps,
            guidance_scale=guidance_scale,
            cross_attention_kwargs={"scale": lora_scale},
        ).images[0]
    return image

# Create the Gradio interface
iface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Prompt"),
        gr.Textbox(label="Negative Prompt"),
        gr.Slider(1, 20, value=7.5, label="Guidance Scale"),
        gr.Slider(1, 100, value=50, step=1, label="Number of Inference Steps"),
        gr.Slider(0, 1, value=0.75, label="LoRA Scale"),
    ],
    outputs=gr.Image(type="pil"),
    title="Flux Dev with Custom LoRA Image Generator",
    description="Generate images using Flux Dev model with a custom LoRA trained on Civitai",
)

iface.launch()