aimersion commited on
Commit
a45321d
1 Parent(s): b59176e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -1
app.py CHANGED
@@ -1,3 +1,136 @@
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- gr.load("models/black-forest-labs/FLUX.1-dev").launch()
 
 
 
 
 
 
1
+
2
+
3
  import gradio as gr
4
+ import numpy as np
5
+ import random
6
+ import spaces
7
+ import torch
8
+ from diffusers import DiffusionPipeline
9
+
10
+ dtype = torch.bfloat16
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+
13
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype).to(device)
14
+
15
+ MAX_SEED = np.iinfo(np.int32).max
16
+ MAX_IMAGE_SIZE = 2048
17
+
18
+ @spaces.GPU()
19
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, guidance_scale=7.5, progress=gr.Progress(track_tqdm=True)):
20
+ if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
21
+ raise ValueError("Image size exceeds the maximum allowed dimensions.")
22
+
23
+ if randomize_seed:
24
+ seed = random.randint(0, MAX_SEED)
25
+ generator = torch.Generator().manual_seed(seed)
26
+
27
+ try:
28
+ image = pipe(
29
+ prompt=prompt,
30
+ width=width,
31
+ height=height,
32
+ num_inference_steps=num_inference_steps,
33
+ generator=generator,
34
+ guidance_scale=guidance_scale
35
+ ).images[0]
36
+ except Exception as e:
37
+ return None, seed, f"Error: {str(e)}"
38
+
39
+ return image, seed, None
40
+
41
+ examples = [
42
+ "a tiny astronaut hatching from an egg on the moon",
43
+ "a cat holding a sign that says hello world",
44
+ "an anime illustration of a wiener schnitzel",
45
+ # Add more diverse examples
46
+ ]
47
+
48
+ css = """
49
+ #col-container {
50
+ margin: 0 auto;
51
+ max-width: 520px;
52
+ }
53
+ """
54
+
55
+ with gr.Blocks(css=css) as demo:
56
+
57
+ with gr.Column(elem_id="col-container"):
58
+ gr.Markdown(f"""# Custom Image Creator
59
+ 12B param rectified flow transformer distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/) for 4 step generation
60
+ [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1)]
61
+ """)
62
+
63
+ with gr.Row():
64
+ prompt = gr.Textbox(
65
+ label="Prompt",
66
+ show_label=False,
67
+ max_lines=1,
68
+ placeholder="Enter your prompt",
69
+ container=False,
70
+ )
71
+ run_button = gr.Button("Run", scale=0)
72
+
73
+ result = gr.Image(label="Result", show_label=False)
74
+
75
+ with gr.Accordion("Advanced Settings", open=False):
76
+ seed = gr.Slider(
77
+ label="Seed",
78
+ minimum=0,
79
+ maximum=MAX_SEED,
80
+ step=1,
81
+ value=0,
82
+ )
83
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
84
+
85
+ with gr.Row():
86
+ width = gr.Slider(
87
+ label="Width",
88
+ minimum=256,
89
+ maximum=MAX_IMAGE_SIZE,
90
+ step=32,
91
+ value=1024,
92
+ )
93
+ height = gr.Slider(
94
+ label="Height",
95
+ minimum=256,
96
+ maximum=MAX_IMAGE_SIZE,
97
+ step=32,
98
+ value=1024,
99
+ )
100
+
101
+ with gr.Row():
102
+ num_inference_steps = gr.Slider(
103
+ label="Number of inference steps",
104
+ minimum=1,
105
+ maximum=50,
106
+ step=1,
107
+ value=4,
108
+ )
109
+ guidance_scale = gr.Slider(
110
+ label="Guidance Scale",
111
+ minimum=0.0,
112
+ maximum=20.0,
113
+ step=0.5,
114
+ value=7.5,
115
+ )
116
+
117
+ gr.Examples(
118
+ examples=examples,
119
+ fn=infer,
120
+ inputs=[prompt],
121
+ outputs=[result, seed],
122
+ cache_examples="lazy"
123
+ )
124
+
125
+ run_button.click(
126
+ fn=infer,
127
+ inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
128
+ outputs=[result, seed],
129
+ )
130
 
131
+ gr.Markdown("""
132
+ ## Save Your Image
133
+ Right-click on the image and select 'Save As' to download the generated image.
134
+ """)
135
+
136
+ demo.launch()