MegaTronX commited on
Commit
935c534
Β·
verified Β·
1 Parent(s): 7cc23e2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import FluxPipeline, FluxTransformer2DModel, FlowMatchEulerDiscreteScheduler
4
+ from huggingface_hub import hf_hub_download
5
+ from PIL import Image
6
+ import numpy as np
7
+ import random
8
+ import spaces
9
+
10
+ # Constants
11
+ BASE_MODEL = "black-forest-labs/FLUX.1-dev"
12
+ LORA_MODEL = "MegaTronX/SuicideGirl-FLUX" # Replace with your LoRA path
13
+ MAX_SEED = np.iinfo(np.int32).max
14
+
15
+ # Initialize model and scheduler
16
+ if torch.cuda.is_available():
17
+ transformer = FluxTransformer2DModel.from_single_file(
18
+ "https://huggingface.co/MegaTronX/SuicideGirl-FLUX/blob/main/SuicideGirls.safetensors",
19
+ torch_dtype=torch.bfloat16
20
+ )
21
+ pipe = FluxPipeline.from_pretrained(
22
+ BASE_MODEL,
23
+ transformer=transformer,
24
+ torch_dtype=torch.bfloat16
25
+ )
26
+ pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
27
+ pipe.scheduler.config, use_beta_sigmas=True
28
+ )
29
+ pipe.to("cuda")
30
+
31
+ # Load and apply LoRA weights
32
+ pipe.load_lora_weights(LORA_MODEL)
33
+
34
+ @spaces.GPU
35
+ def generate_image(
36
+ prompt,
37
+ width=768,
38
+ height=1024,
39
+ guidance_scale=3.5,
40
+ num_inference_steps=24,
41
+ seed=-1,
42
+ num_images=1,
43
+ progress=gr.Progress(track_tqdm=True)
44
+ ):
45
+ if seed == -1:
46
+ seed = random.randint(0, MAX_SEED)
47
+ generator = torch.Generator().manual_seed(seed)
48
+
49
+ images = pipe(
50
+ prompt,
51
+ width=width,
52
+ height=height,
53
+ guidance_scale=guidance_scale,
54
+ num_inference_steps=num_inference_steps,
55
+ generator=generator,
56
+ output_type="pil",
57
+ max_sequence_length=512,
58
+ num_images_per_prompt=num_images,
59
+ ).images
60
+
61
+ return images, seed
62
+
63
+ # Gradio Interface
64
+ with gr.Blocks() as demo:
65
+ gr.HTML("<h1><center>Flux LoRA Image Generator</center></h1>")
66
+
67
+ with gr.Group():
68
+ prompt = gr.Textbox(label='Enter Your Prompt', lines=3)
69
+ generate_button = gr.Button("Generate", variant='primary')
70
+
71
+ with gr.Row():
72
+ image_output = gr.Gallery(label="Generated Images", columns=2, preview=True)
73
+ seed_output = gr.Number(label="Seed Used")
74
+
75
+ with gr.Accordion("Advanced Options", open=False):
76
+ width = gr.Slider(label="Width", minimum=512, maximum=1280, step=8, value=768)
77
+ height = gr.Slider(label="Height", minimum=512, maximum=1280, step=8, value=1024)
78
+ guidance_scale = gr.Slider(label="Guidance Scale", minimum=0, maximum=50, step=0.1, value=3.5)
79
+ num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=24)
80
+ seed = gr.Slider(label="Seed (-1 for random)", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
81
+ num_images = gr.Slider(label="Number of Images", minimum=1, maximum=4, step=1, value=1)
82
+
83
+ generate_button.click(
84
+ fn=generate_image,
85
+ inputs=[prompt, width, height, guidance_scale, num_inference_steps, seed, num_images],
86
+ outputs=[image_output, seed_output]
87
+ )
88
+
89
+ demo.launch()