Update app.py
Browse files2 unmodified lines34567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401412 unmodified linesimport randomimport spacesimport torchfrom diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKLfrom transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFastfrom live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_imagesdtype = torch.bfloat16device = "cuda" if torch.cuda.is_available() else "cpu"taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)good_vae = AutoencoderKL.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype).to(device)pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype, vae=taef1).to(device)torch.cuda.empty_cache()MAX_SEED = np.iinfo(np.int32).maxMAX_IMAGE_SIZE = 2048pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe)@spaces.GPU(duration=75)def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):if randomize_seed:seed = random.randint(0, MAX_SEED)generator = torch.Generator().manual_seed(seed)for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(prompt=prompt,guidance_scale=guidance_scale,num_inference_steps=num_inference_steps,width=width,height=height,generator=generator,output_type="pil",good_vae=good_vae,):yield img, seedexamples = ["a tiny astronaut hatching from an egg on the moon","a cat holding a sign that says hello world","an anime illustration of a wiener schnitzel",]css="""#col-container {margin: 0 auto;max-width: 520px;}"""with gr.Blocks(css=css) as demo:with gr.Column(elem_id="col-container"):gr.Markdown(f"""> FLUX.2 [dev] is here! ✨ [Try it out here](https://huggingface.co/spaces/black-forest-labs/FLUX.2-dev)# FLUX.1 [dev]12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)[[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)]""")with gr.Row():prompt = gr.Text(label="Prompt",show_label=False,max_lines=1,placeholder="Enter your prompt",container=False,)run_button = gr.Button("Run", scale=0)result = gr.Image(label="Result", show_label=False)with gr.Accordion("Advanced Settings", open=False):seed = gr.Slider(label="Seed",minimum=0,maximum=MAX_SEED,step=1,value=0,)randomize_seed = gr.Checkbox(label="Randomize seed", value=True)with gr.Row():width = gr.Slider(label="Width",minimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=1024,)height = gr.Slider(label="Height",minimum=256,maximum=MAX_IMAGE_SIZE,step=32,value=1024,)with gr.Row():guidance_scale = gr.Slider(label="Guidance Scale",minimum=1,maximum=15,step=0.1,value=3.5,)num_inference_steps = gr.Slider(label="Number of inference steps",minimum=1,maximum=50,step=1,value=28,)gr.Examples(examples = examples,fn = infer,inputs = [prompt],outputs = [result, seed],cache_examples="lazy")gr.on(triggers=[run_button.click, prompt.submit],fn = infer,inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],outputs = [result, seed])demo.launch()2 unmodified lines345678910111213141516171819202122232425262728293031323334353637382 unmodified linesimport randomimport spacesimport torchfrom diffusers import FluxPipelinedtype = torch.bfloat16device = "cuda" if torch.cuda.is_available() else "cpu"pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=dtype).to(device)MAX_SEED = np.iinfo(np.int32).max@spaces.GPU(duration=75)def infer(prompt, progress=gr.Progress(track_tqdm=True)):seed = random.randint(0, MAX_SEED)generator = torch.Generator().manual_seed(seed)image = pipe(prompt=prompt,width=1024,height=1024,guidance_scale=3.5,num_inference_steps=28,generator=generator,).images[0]return imagewith gr.Blocks() as demo:prompt = gr.Textbox(label="Prompt")run_button = gr.Button("Run")result = gr.Image(label="Result")run_button.click(fn=infer, inputs=[prompt], outputs=[result])demo.launch()
app.py+21-124