Sugam7 commited on
Commit
5cfddf4
·
verified ·
1 Parent(s): 6bd9f26

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -0
app.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import spaces
5
+ import torch
6
+ from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
7
+ from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
8
+
9
+ # Initialize model and settings
10
+ dtype = torch.bfloat16
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
13
+
14
+ # Constants
15
+ MAX_SEED = np.iinfo(np.int32).max
16
+ MAX_IMAGE_SIZE = 2048
17
+
18
+ @spaces.GPU(duration=190)
19
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=5.0, num_inference_steps=28, output_format="png", progress=gr.Progress(track_tqdm=True)):
20
+ if randomize_seed:
21
+ seed = random.randint(0, MAX_SEED)
22
+ generator = torch.Generator(device=device).manual_seed(seed)
23
+ image = pipe(
24
+ prompt=prompt,
25
+ width=width,
26
+ height=height,
27
+ num_inference_steps=num_inference_steps,
28
+ generator=generator,
29
+ guidance_scale=guidance_scale
30
+ ).images[0]
31
+
32
+ # Convert image to desired format
33
+ if output_format.lower() != "png":
34
+ image = image.convert(output_format.upper())
35
+
36
+ return image, seed
37
+
38
+ examples = [
39
+ "a tiny astronaut hatching from an egg on the moon",
40
+ "a cat holding a sign that says hello world",
41
+ "an anime illustration of a wiener schnitzel",
42
+ ]
43
+
44
+ css = """
45
+ #col-container {
46
+ margin: 0 auto;
47
+ max-width: 800px;
48
+ padding: 20px;
49
+ background-color: #f9f9f9;
50
+ border-radius: 10px;
51
+ box-shadow: 0px 0px 20px rgba(0,0,0,0.1);
52
+ }
53
+
54
+ #title {
55
+ font-family: 'Arial', sans-serif;
56
+ color: #333;
57
+ text-align: center;
58
+ margin-bottom: 20px;
59
+ }
60
+
61
+ #advanced-settings {
62
+ background-color: #f1f1f1;
63
+ border-radius: 8px;
64
+ padding: 10px;
65
+ }
66
+
67
+ #output-container {
68
+ text-align: center;
69
+ margin-top: 20px;
70
+ }
71
+ """
72
+
73
+ with gr.Blocks(css=css) as demo:
74
+ with gr.Column(elem_id="col-container"):
75
+
76
+ # Title and Description
77
+ gr.Markdown(f"""
78
+ <h1 id="title">FLUX.1 [dev] - Advanced Text-to-Image Generator</h1>
79
+ <p style="text-align:center; color:#555;">
80
+ Experience the power of a 12B param rectified flow transformer. Customize your prompts and settings to generate unique images every time.
81
+ </p>
82
+ """)
83
+
84
+ with gr.Row():
85
+ prompt = gr.Textbox(
86
+ label="Prompt",
87
+ show_label=False,
88
+ max_lines=1,
89
+ placeholder="Enter your creative prompt...",
90
+ container=False,
91
+ interactive=True
92
+ )
93
+
94
+ run_button = gr.Button("Generate", elem_id="generate-button")
95
+
96
+ # Output image and settings
97
+ with gr.Row(elem_id="output-container"):
98
+ result = gr.Image(label="Generated Image", show_label=False).style(height=400)
99
+ output_format = gr.Radio(
100
+ label="Output Format",
101
+ choices=["png", "jpeg", "bmp"],
102
+ value="png",
103
+ interactive=True
104
+ )
105
+
106
+ # Advanced settings
107
+ with gr.Accordion("Advanced Settings", open=False, elem_id="advanced-settings"):
108
+ seed = gr.Slider(
109
+ label="Seed",
110
+ minimum=0,
111
+ maximum=MAX_SEED,
112
+ step=1,
113
+ value=0,
114
+ interactive=True
115
+ )
116
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True, interactive=True)
117
+
118
+ with gr.Row():
119
+ width = gr.Slider(
120
+ label="Width",
121
+ minimum=256,
122
+ maximum=MAX_IMAGE_SIZE,
123
+ step=32,
124
+ value=1024,
125
+ interactive=True
126
+ )
127
+ height = gr.Slider(
128
+ label="Height",
129
+ minimum=256,
130
+ maximum=MAX_IMAGE_SIZE,
131
+ step=32,
132
+ value=1024,
133
+ interactive=True
134
+ )
135
+
136
+ with gr.Row():
137
+ guidance_scale = gr.Slider(
138
+ label="Guidance Scale",
139
+ minimum=1,
140
+ maximum=15,
141
+ step=0.1,
142
+ value=5.0,
143
+ interactive=True
144
+ )
145
+ num_inference_steps = gr.Slider(
146
+ label="Number of Inference Steps",
147
+ minimum=1,
148
+ maximum=50,
149
+ step=1,
150
+ value=28,
151
+ interactive=True
152
+ )
153
+
154
+ # Interactive Examples
155
+ gr.Examples(
156
+ examples=examples,
157
+ fn=infer,
158
+ inputs=[prompt],
159
+ outputs=[result, seed],
160
+ cache_examples="lazy",
161
+ label="Try these examples:"
162
+ )
163
+
164
+ # Link button to trigger inference
165
+ run_button.click(
166
+ fn=infer,
167
+ inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, output_format],
168
+ outputs=[result, seed]
169
+ )
170
+
171
+ demo.launch()