macrdel commited on
Commit
a4b3779
·
0 Parent(s):

create app

Browse files
Files changed (2) hide show
  1. .gitignore +5 -0
  2. app.py +153 -0
.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Include
2
+ Lib
3
+ Scripts
4
+ share
5
+ *env*
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+
5
+ # import spaces #[uncomment to use ZeroGPU]
6
+ from diffusers import DiffusionPipeline, FluxPipeline
7
+ import torch
8
+
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
11
+ model_repo = "black-forest-labs/FLUX.1-dev" # Replace to the model you would like to use
12
+
13
+ pipe = FluxPipeline.from_pretrained(model_repo, torch_dtype=torch_dtype).to(device)
14
+ pipe.enable_model_cpu_offload()
15
+
16
+ MAX_SEED = np.iinfo(np.int32).max
17
+ MAX_IMAGE_SIZE = 1024
18
+
19
+
20
+ # @spaces.GPU #[uncomment to use ZeroGPU]
21
+ def infer(
22
+ prompt,
23
+ negative_prompt,
24
+ seed,
25
+ randomize_seed,
26
+ width,
27
+ height,
28
+ guidance_scale,
29
+ num_inference_steps,
30
+ progress=gr.Progress(track_tqdm=True),
31
+ ):
32
+ if randomize_seed:
33
+ seed = random.randint(0, MAX_SEED)
34
+
35
+ generator = torch.Generator().manual_seed(seed)
36
+
37
+ image = pipe(
38
+ prompt=prompt,
39
+ negative_prompt=negative_prompt,
40
+ guidance_scale=guidance_scale,
41
+ num_inference_steps=num_inference_steps,
42
+ width=width,
43
+ height=height,
44
+ generator=generator,
45
+ ).images[0]
46
+
47
+ return image, seed
48
+
49
+
50
+ examples = [
51
+ "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
52
+ "An astronaut riding a green horse",
53
+ "A delicious ceviche cheesecake slice",
54
+ ]
55
+
56
+ css = """
57
+ #col-container {
58
+ margin: 0 auto;
59
+ max-width: 640px;
60
+ }
61
+ """
62
+
63
+ with gr.Blocks(css=css) as demo:
64
+ with gr.Column(elem_id="col-container"):
65
+ gr.Markdown(" # Text2Img Gradio Template")
66
+ gr.Markdown(f" ## Model '{model_repo}'")
67
+
68
+ with gr.Row():
69
+ prompt = gr.Text(
70
+ label="Prompt",
71
+ show_label=False,
72
+ max_lines=1,
73
+ placeholder="Enter your prompt",
74
+ container=False,
75
+ )
76
+
77
+ run_button = gr.Button("Run", scale=0, variant="primary")
78
+
79
+ result = gr.Image(label="Result", show_label=False)
80
+
81
+ with gr.Accordion("Advanced Settings", open=True):
82
+ with gr.Row():
83
+ negative_prompt = gr.Text(
84
+ label="Negative prompt",
85
+ max_lines=1,
86
+ placeholder="Enter a negative prompt",
87
+ visible=True,
88
+ )
89
+
90
+ seed = gr.Slider(
91
+ label="Seed",
92
+ minimum=0,
93
+ maximum=MAX_SEED,
94
+ step=1,
95
+ value=0,
96
+ )
97
+
98
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
99
+
100
+ with gr.Row():
101
+ width = gr.Slider(
102
+ label="Width",
103
+ minimum=256,
104
+ maximum=MAX_IMAGE_SIZE,
105
+ step=32,
106
+ value=1024, # Replace with defaults that work for your model
107
+ )
108
+
109
+ height = gr.Slider(
110
+ label="Height",
111
+ minimum=256,
112
+ maximum=MAX_IMAGE_SIZE,
113
+ step=32,
114
+ value=1024, # Replace with defaults that work for your model
115
+ )
116
+
117
+ with gr.Row():
118
+ guidance_scale = gr.Slider(
119
+ label="Guidance scale (CFG)",
120
+ minimum=1.0,
121
+ maximum=10.0,
122
+ step=0.5,
123
+ value=3.5, # Replace with defaults that work for your model
124
+ )
125
+
126
+ num_inference_steps = gr.Slider(
127
+ label="Number of inference steps",
128
+ minimum=1,
129
+ maximum=50,
130
+ step=1,
131
+ value=1, # Replace with defaults that work for your model
132
+ )
133
+
134
+ gr.Examples(examples=examples, inputs=[prompt])
135
+
136
+ gr.on(
137
+ triggers=[run_button.click, prompt.submit],
138
+ fn=infer,
139
+ inputs=[
140
+ prompt,
141
+ negative_prompt,
142
+ seed,
143
+ randomize_seed,
144
+ width,
145
+ height,
146
+ guidance_scale,
147
+ num_inference_steps,
148
+ ],
149
+ outputs=[result, seed],
150
+ )
151
+
152
+ if __name__ == "__main__":
153
+ demo.launch()