Yanzuo commited on
Commit
3ec2621
1 Parent(s): 0697a48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -36
app.py CHANGED
@@ -20,11 +20,9 @@ torch.backends.cuda.matmul.allow_tf32 = True
20
  class timer:
21
  def __init__(self, method_name="timed process"):
22
  self.method = method_name
23
-
24
  def __enter__(self):
25
  self.start = time.time()
26
  print(f"{self.method} starts")
27
-
28
  def __exit__(self, exc_type, exc_val, exc_tb):
29
  end = time.time()
30
  print(f"{self.method} took {str(round(end - self.start, 2))}s")
@@ -37,41 +35,71 @@ pipe.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8
37
  pipe.fuse_lora(lora_scale=0.125)
38
  pipe.to(device="cuda", dtype=torch.bfloat16)
39
 
40
- with gr.Blocks() as demo:
41
- with gr.Column():
42
- with gr.Row():
43
- with gr.Column():
44
- # num_images = gr.Slider(label="Number of Images", minimum=1, maximum=2, step=1, value=1, interactive=True)
45
- height = gr.Number(label="Image Height", value=1024, interactive=True)
46
- width = gr.Number(label="Image Width", value=1024, interactive=True)
47
- steps = gr.Slider(label="Inference Steps", minimum=6, maximum=25, step=1, value=8, interactive=True)
48
- scales = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=5.0, value=3.5, interactive=True)
49
- # eta = gr.Number(label="Eta (Corresponds to parameter eta (η) in the DDIM paper, i.e. 0.0 eqauls DDIM, 1.0 equals LCM)", value=1., interactive=True)
50
- prompt = gr.Text(label="Prompt", value="a photo of a cat", interactive=True)
51
- seed = gr.Number(label="Seed", value=3413, interactive=True)
52
- btn = gr.Button(value="run")
53
- with gr.Column():
54
- output = gr.Gallery(height=768)
55
-
56
- @spaces.GPU
57
- def process_image(height, width, steps, scales, prompt, seed):
58
- global pipe
59
- with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
60
- return pipe(
61
- prompt=[prompt],
62
- generator=torch.Generator().manual_seed(int(seed)),
63
- num_inference_steps=steps,
64
- guidance_scale=scales,
65
- height=int(height),
66
- width=int(width)
67
- ).images
68
-
69
- reactive_controls = [height, width, steps, scales, prompt, seed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
- # for control in reactive_controls:
72
- # control.change(fn=process_image, inputs=reactive_controls, outputs=[output])
 
 
 
 
 
 
 
 
 
 
73
 
74
- btn.click(process_image, inputs=reactive_controls, outputs=[output])
 
 
 
 
75
 
76
  if __name__ == "__main__":
77
- demo.launch()
 
20
  class timer:
21
  def __init__(self, method_name="timed process"):
22
  self.method = method_name
 
23
  def __enter__(self):
24
  self.start = time.time()
25
  print(f"{self.method} starts")
 
26
  def __exit__(self, exc_type, exc_val, exc_tb):
27
  end = time.time()
28
  print(f"{self.method} took {str(round(end - self.start, 2))}s")
 
35
  pipe.fuse_lora(lora_scale=0.125)
36
  pipe.to(device="cuda", dtype=torch.bfloat16)
37
 
38
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
39
+ gr.Markdown(
40
+ """
41
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
42
+ <h1 style="font-size: 2.5rem; font-weight: 700; margin-bottom: 1rem;">FLUX Image Generator</h1>
43
+ <p style="font-size: 1rem; margin-bottom: 1.5rem;">Create unique images with AI. Just describe what you want to see!</p>
44
+ </div>
45
+ """
46
+ )
47
+
48
+ with gr.Group():
49
+ prompt = gr.Textbox(
50
+ label="Your Image Description",
51
+ placeholder="E.g., A serene landscape with mountains and a lake at sunset",
52
+ lines=3
53
+ )
54
+
55
+ with gr.Accordion("Advanced Settings", open=False):
56
+ with gr.Group():
57
+ with gr.Row():
58
+ height = gr.Slider(label="Height", minimum=256, maximum=1024, step=64, value=1024)
59
+ width = gr.Slider(label="Width", minimum=256, maximum=1024, step=64, value=1024)
60
+
61
+ with gr.Row():
62
+ steps = gr.Slider(label="Inference Steps", minimum=6, maximum=25, step=1, value=8)
63
+ scales = gr.Slider(label="Guidance Scale", minimum=0.0, maximum=5.0, step=0.1, value=3.5)
64
+
65
+ seed = gr.Number(label="Seed (for reproducibility)", value=3413, precision=0)
66
+
67
+ generate_btn = gr.Button("Generate Image", variant="primary", scale=1)
68
+
69
+ output = gr.Image(label="Your Generated Image")
70
+
71
+ gr.Markdown(
72
+ """
73
+ <div style="max-width: 650px; margin: 2rem auto; padding: 1rem; border-radius: 10px; background-color: #f0f0f0;">
74
+ <h2 style="font-size: 1.5rem; margin-bottom: 1rem;">How to Use</h2>
75
+ <ol style="padding-left: 1.5rem;">
76
+ <li>Enter a detailed description of the image you want to create.</li>
77
+ <li>Adjust advanced settings if desired (tap to expand).</li>
78
+ <li>Tap "Generate Image" and wait for your creation!</li>
79
+ </ol>
80
+ <p style="margin-top: 1rem; font-style: italic;">Tip: Be specific in your description for best results!</p>
81
+ </div>
82
+ """
83
+ )
84
 
85
+ @spaces.GPU
86
+ def process_image(height, width, steps, scales, prompt, seed):
87
+ global pipe
88
+ with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
89
+ return pipe(
90
+ prompt=[prompt],
91
+ generator=torch.Generator().manual_seed(int(seed)),
92
+ num_inference_steps=int(steps),
93
+ guidance_scale=float(scales),
94
+ height=int(height),
95
+ width=int(width)
96
+ ).images
97
 
98
+ generate_btn.click(
99
+ process_image,
100
+ inputs=[height, width, steps, scales, prompt, seed],
101
+ outputs=[output]
102
+ )
103
 
104
  if __name__ == "__main__":
105
+ demo.launch()