multimodalart HF staff commited on
Commit
484fad2
1 Parent(s): 893ab21

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+
4
+ import spaces
5
+ import torch
6
+ import spaces
7
+ import random
8
+
9
+ from diffusers import FluxControlPipeline, FluxTransformer2DModel
10
+ from controlnet_aux import CannyDetector
11
+
12
+ MAX_SEED = np.iinfo(np.int32).max
13
+ MAX_IMAGE_SIZE = 2048
14
+
15
+ pipe = FluxControlPipeline.from_pretrained("black-forest-labs/FLUX.1-Canny-dev", revision="refs/pr/1", torch_dtype=torch.bfloat16).to("cuda")
16
+ processor = CannyDetector()
17
+
18
+ @spaces.GPU
19
+ def infer(control_image, prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
20
+
21
+ if randomize_seed:
22
+ seed = random.randint(0, MAX_SEED)
23
+
24
+ control_image = processor(control_image, low_threshold=50, high_threshold=200, detect_resolution=1024, image_resolution=1024)
25
+ image = pipe(
26
+ prompt=prompt,
27
+ control_image=control_image,
28
+ height=height,
29
+ width=width,
30
+ num_inference_steps=num_inference_steps,
31
+ guidance_scale=guidance_scale,
32
+ generator=torch.Generator().manual_seed(seed),
33
+ ).images[0]
34
+ return image, seed
35
+
36
+ css="""
37
+ #col-container {
38
+ margin: 0 auto;
39
+ max-width: 520px;
40
+ }
41
+ """
42
+
43
+ with gr.Blocks(css=css) as demo:
44
+
45
+ with gr.Column(elem_id="col-container"):
46
+ gr.Markdown(f"""# FLUX.1 Canny [dev]
47
+ 12B param rectified flow transformer structural conditioning tuned, guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
48
+ [[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
49
+ """)
50
+
51
+ control_image = gr.Image(label="Upload the image for control", type="pil")
52
+ with gr.Row():
53
+
54
+ prompt = gr.Text(
55
+ label="Prompt",
56
+ show_label=False,
57
+ max_lines=1,
58
+ placeholder="Enter your prompt",
59
+ container=False,
60
+ )
61
+
62
+ run_button = gr.Button("Run", scale=0)
63
+
64
+ result = gr.Image(label="Result", show_label=False)
65
+
66
+ with gr.Accordion("Advanced Settings", open=False):
67
+
68
+ seed = gr.Slider(
69
+ label="Seed",
70
+ minimum=0,
71
+ maximum=MAX_SEED,
72
+ step=1,
73
+ value=0,
74
+ )
75
+
76
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
77
+
78
+ with gr.Row():
79
+
80
+ width = gr.Slider(
81
+ label="Width",
82
+ minimum=256,
83
+ maximum=MAX_IMAGE_SIZE,
84
+ step=32,
85
+ value=1024,
86
+ )
87
+
88
+ height = gr.Slider(
89
+ label="Height",
90
+ minimum=256,
91
+ maximum=MAX_IMAGE_SIZE,
92
+ step=32,
93
+ value=1024,
94
+ )
95
+
96
+ with gr.Row():
97
+
98
+ guidance_scale = gr.Slider(
99
+ label="Guidance Scale",
100
+ minimum=1,
101
+ maximum=15,
102
+ step=0.1,
103
+ value=3.5,
104
+ )
105
+
106
+ num_inference_steps = gr.Slider(
107
+ label="Number of inference steps",
108
+ minimum=1,
109
+ maximum=50,
110
+ step=1,
111
+ value=28,
112
+ )
113
+
114
+ gr.on(
115
+ triggers=[run_button.click, prompt.submit],
116
+ fn = infer,
117
+ inputs = [control_image, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
118
+ outputs = [result, seed]
119
+ )
120
+
121
+ demo.launch()