Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,176 @@ import torch
|
|
6 |
from diffusers import DiffusionPipeline, FluxPipeline, AutoencoderTiny, AutoencoderKL
|
7 |
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
8 |
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Define models and their configurations
|
11 |
models = {
|
@@ -183,8 +353,8 @@ Select a model to generate images using the FLUX pipeline.
|
|
183 |
|
184 |
demo.launch()
|
185 |
|
186 |
-
|
187 |
-
|
188 |
|
189 |
'''
|
190 |
import gradio as gr
|
|
|
6 |
from diffusers import DiffusionPipeline, FluxPipeline, AutoencoderTiny, AutoencoderKL
|
7 |
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
8 |
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
9 |
+
from download import download_all_models, models, download_vaes
|
10 |
+
|
11 |
+
# Call the download function at the start of the app
|
12 |
+
download_all_models()
|
13 |
+
download_vaes()
|
14 |
+
|
15 |
+
dtype = torch.bfloat16
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
|
18 |
+
# Load VAEs - these can be reused across models
|
19 |
+
taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
|
20 |
+
good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)
|
21 |
+
|
22 |
+
def load_model(model_name):
|
23 |
+
model_info = models[model_name]
|
24 |
+
pipeline_class = model_info["pipeline_class"]
|
25 |
+
model_id = model_info["model_id"]
|
26 |
+
config = model_info["config"]
|
27 |
+
|
28 |
+
pipeline = pipeline_class.from_pretrained(model_id, **config, vae=taef1).to(device)
|
29 |
+
|
30 |
+
# Assign the custom function for live preview if it's a FluxPipeline or DiffusionPipeline
|
31 |
+
if pipeline_class in (FluxPipeline, DiffusionPipeline):
|
32 |
+
pipeline.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipeline)
|
33 |
+
|
34 |
+
return pipeline, model_info.get("description", "No description available.")
|
35 |
+
|
36 |
+
# Initialize with default model
|
37 |
+
current_model_name = "FLUX.1-dev"
|
38 |
+
pipe, model_description = load_model(current_model_name)
|
39 |
+
torch.cuda.empty_cache()
|
40 |
+
|
41 |
+
MAX_SEED = np.iinfo(np.int32).max
|
42 |
+
MAX_IMAGE_SIZE = 2048
|
43 |
+
|
44 |
+
@spaces.GPU(duration=75)
|
45 |
+
def infer(model_name, prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
46 |
+
global pipe, current_model_name # Access the global pipe and model name
|
47 |
+
|
48 |
+
if randomize_seed:
|
49 |
+
seed = random.randint(0, MAX_SEED)
|
50 |
+
generator = torch.Generator().manual_seed(seed)
|
51 |
+
|
52 |
+
# Only reload the model if a different one is selected
|
53 |
+
if model_name != current_model_name:
|
54 |
+
pipe, _ = load_model(model_name)
|
55 |
+
current_model_name = model_name
|
56 |
+
|
57 |
+
for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
|
58 |
+
prompt=prompt,
|
59 |
+
guidance_scale=guidance_scale,
|
60 |
+
num_inference_steps=num_inference_steps,
|
61 |
+
width=width,
|
62 |
+
height=height,
|
63 |
+
generator=generator,
|
64 |
+
output_type="pil",
|
65 |
+
good_vae=good_vae,
|
66 |
+
):
|
67 |
+
yield img, seed
|
68 |
+
|
69 |
+
examples = [
|
70 |
+
["FLUX.1-dev", "a tiny astronaut hatching from an egg on the moon"],
|
71 |
+
["FLUX.1-schnell", "a cat holding a sign that says hello world"],
|
72 |
+
["Flux.1-lite-8B-alpha", "an anime illustration of a wiener schnitzel"],
|
73 |
+
]
|
74 |
+
|
75 |
+
css="""
|
76 |
+
#col-container {
|
77 |
+
margin: 0 auto;
|
78 |
+
max-width: 520px;
|
79 |
+
}
|
80 |
+
"""
|
81 |
+
|
82 |
+
with gr.Blocks(css=css) as demo:
|
83 |
+
with gr.Column(elem_id="col-container"):
|
84 |
+
gr.Markdown(f"""# FLUX.1 Model Selector
|
85 |
+
Select a model to generate images using the FLUX pipeline.
|
86 |
+
""")
|
87 |
+
model_selector = gr.Dropdown(
|
88 |
+
choices=list(models.keys()),
|
89 |
+
value=current_model_name,
|
90 |
+
label="Select Model",
|
91 |
+
)
|
92 |
+
model_description_box = gr.Markdown(model_description)
|
93 |
+
with gr.Row():
|
94 |
+
prompt = gr.Text(
|
95 |
+
label="Prompt",
|
96 |
+
show_label=False,
|
97 |
+
max_lines=1,
|
98 |
+
placeholder="Enter your prompt",
|
99 |
+
container=False,
|
100 |
+
)
|
101 |
+
run_button = gr.Button("Run", scale=0)
|
102 |
+
result = gr.Image(label="Result", show_label=False)
|
103 |
+
with gr.Accordion("Advanced Settings", open=False):
|
104 |
+
seed = gr.Slider(
|
105 |
+
label="Seed",
|
106 |
+
minimum=0,
|
107 |
+
maximum=MAX_SEED,
|
108 |
+
step=1,
|
109 |
+
value=0,
|
110 |
+
)
|
111 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
112 |
+
with gr.Row():
|
113 |
+
width = gr.Slider(
|
114 |
+
label="Width",
|
115 |
+
minimum=256,
|
116 |
+
maximum=MAX_IMAGE_SIZE,
|
117 |
+
step=32,
|
118 |
+
value=1024,
|
119 |
+
)
|
120 |
+
height = gr.Slider(
|
121 |
+
label="Height",
|
122 |
+
minimum=256,
|
123 |
+
maximum=MAX_IMAGE_SIZE,
|
124 |
+
step=32,
|
125 |
+
value=1024,
|
126 |
+
)
|
127 |
+
with gr.Row():
|
128 |
+
guidance_scale = gr.Slider(
|
129 |
+
label="Guidance Scale",
|
130 |
+
minimum=1,
|
131 |
+
maximum=15,
|
132 |
+
step=0.1,
|
133 |
+
value=3.5,
|
134 |
+
)
|
135 |
+
num_inference_steps = gr.Slider(
|
136 |
+
label="Number of inference steps",
|
137 |
+
minimum=1,
|
138 |
+
maximum=50,
|
139 |
+
step=1,
|
140 |
+
value=28,
|
141 |
+
)
|
142 |
+
|
143 |
+
def update_description(selected_model):
|
144 |
+
return models[selected_model]["description"]
|
145 |
+
|
146 |
+
model_selector.change(
|
147 |
+
fn=update_description,
|
148 |
+
inputs=[model_selector],
|
149 |
+
outputs=[model_description_box],
|
150 |
+
)
|
151 |
+
|
152 |
+
gr.Examples(
|
153 |
+
examples=examples,
|
154 |
+
fn=infer,
|
155 |
+
inputs=[model_selector, prompt], # Correct order of inputs
|
156 |
+
outputs=[result, seed],
|
157 |
+
cache_examples=False,
|
158 |
+
)
|
159 |
+
|
160 |
+
gr.on(
|
161 |
+
triggers=[run_button.click, prompt.submit],
|
162 |
+
fn=infer,
|
163 |
+
inputs=[model_selector, prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
164 |
+
outputs=[result, seed],
|
165 |
+
)
|
166 |
+
|
167 |
+
demo.launch()
|
168 |
+
|
169 |
+
#working1
|
170 |
+
''''
|
171 |
+
import gradio as gr
|
172 |
+
import numpy as np
|
173 |
+
import random
|
174 |
+
import spaces
|
175 |
+
import torch
|
176 |
+
from diffusers import DiffusionPipeline, FluxPipeline, AutoencoderTiny, AutoencoderKL
|
177 |
+
from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
|
178 |
+
from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images
|
179 |
|
180 |
# Define models and their configurations
|
181 |
models = {
|
|
|
353 |
|
354 |
demo.launch()
|
355 |
|
356 |
+
'''
|
357 |
+
#orginal
|
358 |
|
359 |
'''
|
360 |
import gradio as gr
|