multimodalart HF staff commited on
Commit
c70f6b6
1 Parent(s): 319776a

Create app.py

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