fantos commited on
Commit
4f076f3
β€’
1 Parent(s): b545cb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -32
app.py CHANGED
@@ -9,12 +9,11 @@ torch.backends.cudnn.deterministic = True
9
  torch.backends.cudnn.benchmark = False
10
  torch.backends.cuda.matmul.allow_tf32 = True
11
 
12
- # Initialize the base model and specific LoRA
13
  base_model = "black-forest-labs/FLUX.1-dev"
14
  pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
15
 
16
  lora_repo = "strangerzonehf/Flux-Pixel-Background-LoRA"
17
- trigger_word = "" # Leave trigger_word blank if not used.
18
  pipe.load_lora_weights(lora_repo)
19
 
20
  pipe.to("cuda")
@@ -23,21 +22,16 @@ MAX_SEED = 2**32-1
23
 
24
  @spaces.GPU()
25
  def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
26
- # Set random seed for reproducibility
27
  if randomize_seed:
28
  seed = random.randint(0, MAX_SEED)
29
  generator = torch.Generator(device="cuda").manual_seed(seed)
30
 
31
- # Update progress bar (0% saat mulai)
32
  progress(0, "Starting image generation...")
33
 
34
- # Generate image with progress updates
35
  for i in range(1, steps + 1):
36
- # Simulate the processing step (in a real scenario, you would integrate this with your image generation process)
37
- if i % (steps // 10) == 0: # Update every 10% of the steps
38
  progress(i / steps * 100, f"Processing step {i} of {steps}...")
39
 
40
- # Generate image using the pipeline
41
  image = pipe(
42
  prompt=f"{prompt} {trigger_word}",
43
  num_inference_steps=steps,
@@ -48,13 +42,10 @@ def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora
48
  joint_attention_kwargs={"scale": lora_scale},
49
  ).images[0]
50
 
51
- # Final update (100%)
52
  progress(100, "Completed!")
 
53
 
54
- yield image, seed
55
-
56
- # Example cached image and settings
57
- example_image_path = "example0.webp" # Replace with the actual path to the example image
58
  example_prompt = """Pixel Background, a silhouette of a surfer is seen riding a wave on a red surfboard. The surfers shadow is cast on the left side of the image, adding a touch of depth to the composition. The background is a vibrant orange, pink, and blue, with a sun setting in the upper right corner of the frame. The silhouette of the surfer, a palm tree casts a shadow onto the wave, adding depth and contrast to the scene."""
59
  example_cfg_scale = 3.2
60
  example_steps = 32
@@ -64,28 +55,105 @@ example_seed = 3981632454
64
  example_lora_scale = 0.85
65
 
66
  def load_example():
67
- # Load example image from file
68
  example_image = Image.open(example_image_path)
69
  return example_prompt, example_cfg_scale, example_steps, True, example_seed, example_width, example_height, example_lora_scale, example_image
70
 
71
- with gr.Blocks() as app:
72
- gr.Markdown("# MY First 'Flux ART Image Generator'")
73
- with gr.Row():
74
- with gr.Column(scale=3):
75
- prompt = gr.TextArea(label="Prompt", placeholder="Type a prompt", lines=5)
76
- generate_button = gr.Button("Generate")
77
- cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=example_cfg_scale)
78
- steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=example_steps)
79
- width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=example_width)
80
- height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=example_height)
81
- randomize_seed = gr.Checkbox(True, label="Randomize seed")
82
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=example_seed)
83
- lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=example_lora_scale)
84
- with gr.Column(scale=1):
85
- result = gr.Image(label="Generated Image")
86
-
87
- # Automatically load example data and image when the interface is launched
88
- app.load(load_example, inputs=[], outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  generate_button.click(
91
  run_lora,
 
9
  torch.backends.cudnn.benchmark = False
10
  torch.backends.cuda.matmul.allow_tf32 = True
11
 
 
12
  base_model = "black-forest-labs/FLUX.1-dev"
13
  pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
14
 
15
  lora_repo = "strangerzonehf/Flux-Pixel-Background-LoRA"
16
+ trigger_word = ""
17
  pipe.load_lora_weights(lora_repo)
18
 
19
  pipe.to("cuda")
 
22
 
23
  @spaces.GPU()
24
  def run_lora(prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
  generator = torch.Generator(device="cuda").manual_seed(seed)
28
 
 
29
  progress(0, "Starting image generation...")
30
 
 
31
  for i in range(1, steps + 1):
32
+ if i % (steps // 10) == 0:
 
33
  progress(i / steps * 100, f"Processing step {i} of {steps}...")
34
 
 
35
  image = pipe(
36
  prompt=f"{prompt} {trigger_word}",
37
  num_inference_steps=steps,
 
42
  joint_attention_kwargs={"scale": lora_scale},
43
  ).images[0]
44
 
 
45
  progress(100, "Completed!")
46
+ return image, seed
47
 
48
+ example_image_path = "example0.webp"
 
 
 
49
  example_prompt = """Pixel Background, a silhouette of a surfer is seen riding a wave on a red surfboard. The surfers shadow is cast on the left side of the image, adding a touch of depth to the composition. The background is a vibrant orange, pink, and blue, with a sun setting in the upper right corner of the frame. The silhouette of the surfer, a palm tree casts a shadow onto the wave, adding depth and contrast to the scene."""
50
  example_cfg_scale = 3.2
51
  example_steps = 32
 
55
  example_lora_scale = 0.85
56
 
57
  def load_example():
 
58
  example_image = Image.open(example_image_path)
59
  return example_prompt, example_cfg_scale, example_steps, True, example_seed, example_width, example_height, example_lora_scale, example_image
60
 
61
+ css = """
62
+ .container {max-width: 1200px; margin: auto; padding: 20px;}
63
+ .header {text-align: center; margin-bottom: 30px;}
64
+ .generate-btn {background-color: #2ecc71 !important; color: white !important;}
65
+ .generate-btn:hover {background-color: #27ae60 !important;}
66
+ .parameter-box {background-color: #f5f6fa; padding: 20px; border-radius: 10px; margin: 10px 0;}
67
+ .result-box {background-color: #f5f6fa; padding: 20px; border-radius: 10px;}
68
+ """
69
+
70
+ with gr.Blocks(css=css) as app:
71
+ with gr.Column(elem_classes="container"):
72
+ gr.Markdown("# 🎨 Flux ART Image Generator", elem_classes="header")
73
+
74
+ with gr.Row(equal_height=True):
75
+ with gr.Column(scale=3):
76
+ with gr.Group(elem_classes="parameter-box"):
77
+ prompt = gr.TextArea(
78
+ label="✍️ Your Prompt",
79
+ placeholder="Describe the image you want to generate...",
80
+ lines=5
81
+ )
82
+
83
+ with gr.Group(elem_classes="parameter-box"):
84
+ gr.Markdown("### πŸŽ›οΈ Generation Parameters")
85
+ with gr.Row():
86
+ with gr.Column():
87
+ cfg_scale = gr.Slider(
88
+ label="CFG Scale",
89
+ minimum=1,
90
+ maximum=20,
91
+ step=0.5,
92
+ value=example_cfg_scale
93
+ )
94
+ steps = gr.Slider(
95
+ label="Steps",
96
+ minimum=1,
97
+ maximum=100,
98
+ step=1,
99
+ value=example_steps
100
+ )
101
+ lora_scale = gr.Slider(
102
+ label="LoRA Scale",
103
+ minimum=0,
104
+ maximum=1,
105
+ step=0.01,
106
+ value=example_lora_scale
107
+ )
108
+
109
+ with gr.Group(elem_classes="parameter-box"):
110
+ gr.Markdown("### πŸ“ Image Dimensions")
111
+ with gr.Row():
112
+ width = gr.Slider(
113
+ label="Width",
114
+ minimum=256,
115
+ maximum=1536,
116
+ step=64,
117
+ value=example_width
118
+ )
119
+ height = gr.Slider(
120
+ label="Height",
121
+ minimum=256,
122
+ maximum=1536,
123
+ step=64,
124
+ value=example_height
125
+ )
126
+
127
+ with gr.Group(elem_classes="parameter-box"):
128
+ gr.Markdown("### 🎲 Seed Settings")
129
+ with gr.Row():
130
+ randomize_seed = gr.Checkbox(
131
+ True,
132
+ label="Randomize seed"
133
+ )
134
+ seed = gr.Slider(
135
+ label="Seed",
136
+ minimum=0,
137
+ maximum=MAX_SEED,
138
+ step=1,
139
+ value=example_seed
140
+ )
141
+
142
+ generate_button = gr.Button(
143
+ "πŸš€ Generate Image",
144
+ elem_classes="generate-btn"
145
+ )
146
+
147
+ with gr.Column(scale=2):
148
+ with gr.Group(elem_classes="result-box"):
149
+ gr.Markdown("### πŸ–ΌοΈ Generated Image")
150
+ result = gr.Image(label="Result")
151
+
152
+ app.load(
153
+ load_example,
154
+ inputs=[],
155
+ outputs=[prompt, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, result]
156
+ )
157
 
158
  generate_button.click(
159
  run_lora,