multimodalart HF staff commited on
Commit
4d0ad40
·
verified ·
1 Parent(s): 72e66c1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+
4
+ MAX_SEED = np.iinfo(np.int32).max
5
+ MAX_IMAGE_SIZE = 2048
6
+
7
+ pipe = FluxFillPipeline.from_pretrained("black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16, revision="refs/pr/4").to("cuda")
8
+
9
+ def infer(edit_images, prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
10
+ image = edit_images[0]
11
+ mask = edit_images[1]
12
+ if randomize_seed:
13
+ seed = random.randint(0, MAX_SEED)
14
+ image = pipe(
15
+ prompt=prompt,
16
+ image=image,
17
+ mask_image=mask,
18
+ height=width,
19
+ width=height,
20
+ guidance_scale=guidance_scale,
21
+ num_inference_steps=num_inference_steps,
22
+ generator=torch.Generator("cpu").manual_seed(seed)
23
+ ).images[0]
24
+ return image, seed
25
+
26
+ examples = [
27
+ "a tiny astronaut hatching from an egg on the moon",
28
+ "a cat holding a sign that says hello world",
29
+ "an anime illustration of a wiener schnitzel",
30
+ ]
31
+
32
+ css="""
33
+ #col-container {
34
+ margin: 0 auto;
35
+ max-width: 520px;
36
+ }
37
+ """
38
+
39
+ with gr.Blocks(css=css) as demo:
40
+
41
+ with gr.Column(elem_id="col-container"):
42
+ gr.Markdown(f"""# FLUX.1 Fill [dev]
43
+ 12B param rectified flow transformer structural conditioning tuned, guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
44
+ [[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)]
45
+ """)
46
+
47
+ edit_image = gr.ImageEditor(
48
+ label='Upload and draw mask for inpainting',
49
+ type='pil',
50
+ sources=["upload", "webcam"],
51
+ image_mode='RGB',
52
+ layers=False,
53
+ brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"))
54
+ with gr.Row():
55
+
56
+ prompt = gr.Text(
57
+ label="Prompt",
58
+ show_label=False,
59
+ max_lines=1,
60
+ placeholder="Enter your prompt",
61
+ container=False,
62
+ )
63
+
64
+ run_button = gr.Button("Run", scale=0)
65
+
66
+ result = gr.Image(label="Result", show_label=False)
67
+
68
+ with gr.Accordion("Advanced Settings", open=False):
69
+
70
+ seed = gr.Slider(
71
+ label="Seed",
72
+ minimum=0,
73
+ maximum=MAX_SEED,
74
+ step=1,
75
+ value=0,
76
+ )
77
+
78
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
79
+
80
+ with gr.Row():
81
+
82
+ width = gr.Slider(
83
+ label="Width",
84
+ minimum=256,
85
+ maximum=MAX_IMAGE_SIZE,
86
+ step=32,
87
+ value=1024,
88
+ )
89
+
90
+ height = gr.Slider(
91
+ label="Height",
92
+ minimum=256,
93
+ maximum=MAX_IMAGE_SIZE,
94
+ step=32,
95
+ value=1024,
96
+ )
97
+
98
+ with gr.Row():
99
+
100
+ guidance_scale = gr.Slider(
101
+ label="Guidance Scale",
102
+ minimum=1,
103
+ maximum=15,
104
+ step=0.1,
105
+ value=3.5,
106
+ )
107
+
108
+ num_inference_steps = gr.Slider(
109
+ label="Number of inference steps",
110
+ minimum=1,
111
+ maximum=50,
112
+ step=1,
113
+ value=28,
114
+ )
115
+
116
+ gr.on(
117
+ triggers=[run_button.click, prompt.submit],
118
+ fn = infer,
119
+ inputs = [edit_image, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
120
+ outputs = [result, seed]
121
+ )
122
+
123
+ demo.launch()