Spaces:
Running
on
Zero
Running
on
Zero
| import os | |
| import subprocess | |
| import sys | |
| import io | |
| import gradio as gr | |
| import numpy as np | |
| import random | |
| import spaces | |
| import torch | |
| from diffusers import Flux2Pipeline, Flux2Transformer2DModel | |
| from diffusers import BitsAndBytesConfig as DiffBitsAndBytesConfig | |
| from optimization import optimize_pipeline_ | |
| import requests | |
| from PIL import Image | |
| import json | |
| dtype = torch.bfloat16 | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| MAX_SEED = np.iinfo(np.int32).max | |
| MAX_IMAGE_SIZE = 1024 | |
| def remote_text_encoder(prompts): | |
| response = requests.post( | |
| "https://remote-text-encoder-flux-2.huggingface.co/predict", | |
| json={"prompt": prompts}, | |
| headers={ | |
| "Authorization": f"Bearer {os.environ['HF_TOKEN']}", | |
| "Content-Type": "application/json" | |
| } | |
| ) | |
| assert response.status_code == 200, f"{response.status_code=}" | |
| prompt_embeds = torch.load(io.BytesIO(response.content)) | |
| return prompt_embeds | |
| # Load model | |
| repo_id = "black-forest-labs/FLUX.2-dev" | |
| dit = Flux2Transformer2DModel.from_pretrained( | |
| repo_id, | |
| subfolder="transformer", | |
| torch_dtype=torch.bfloat16 | |
| ) | |
| pipe = Flux2Pipeline.from_pretrained( | |
| repo_id, | |
| text_encoder=None, | |
| transformer=dit, | |
| torch_dtype=torch.bfloat16 | |
| ) | |
| pipe.to("cuda") | |
| pipe.transformer.set_attention_backend("_flash_3_hub") | |
| optimize_pipeline_( | |
| pipe, | |
| image=[Image.new("RGB", (1024, 1024))], | |
| prompt_embeds = remote_text_encoder("prompt").to("cuda"), | |
| guidance_scale=2.5, | |
| width=1024, | |
| height=1024, | |
| num_inference_steps=1 | |
| ) | |
| def infer(prompt, input_images, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=50, guidance_scale=2.5, progress=gr.Progress(track_tqdm=True)): | |
| if randomize_seed: | |
| seed = random.randint(0, MAX_SEED) | |
| # Get prompt embeddings from remote text encoder | |
| progress(0.1, desc="Encoding prompt...") | |
| prompt_embeds = remote_text_encoder(prompt).to("cuda") | |
| # Prepare image list (convert None or empty gallery to None) | |
| image_list = None | |
| if input_images is not None and len(input_images) > 0: | |
| image_list = [] | |
| for item in input_images: | |
| image_list.append(item[0]) | |
| # Generate image | |
| progress(0.3, desc="Generating image...") | |
| generator = torch.Generator(device=device).manual_seed(seed) | |
| image = pipe( | |
| prompt_embeds=prompt_embeds, | |
| image=image_list, | |
| width=width, | |
| height=height, | |
| num_inference_steps=num_inference_steps, | |
| guidance_scale=guidance_scale, | |
| generator=generator, | |
| ).images[0] | |
| return image, seed | |
| examples = [ | |
| ["Create a vase on a table in living room, the color of the vase is a gradient of color, starting with #02eb3c color and finishing with #edfa3c. The flowers inside the vase have the color #ff0088", None], | |
| ["Photorealistic infographic showing the complete Berlin TV Tower (Fernsehturm) from ground base to antenna tip, full vertical view with entire structure visible including concrete shaft, metallic sphere, and antenna spire. Slight upward perspective angle looking up toward the iconic sphere, perfectly centered on clean white background. Left side labels with thin horizontal connector lines: the text '368m' in extra large bold dark grey numerals (#2D3748) positioned at exactly the antenna tip with 'TOTAL HEIGHT' in small caps below. The text '207m' in extra large bold with 'TELECAFÉ' in small caps below, with connector line touching the sphere precisely at the window level. Right side label with horizontal connector line touching the sphere's equator: the text '32m' in extra large bold dark grey numerals with 'SPHERE DIAMETER' in small caps below. Bottom section arranged in three balanced columns: Left - Large text '986' in extra bold dark grey with 'STEPS' in caps below. Center - 'BERLIN TV TOWER' in bold caps with 'FERNSEHTURM' in lighter weight below. Right - 'INAUGURATED' in bold caps with 'OCTOBER 3, 1969' below. All typography in modern sans-serif font (such as Inter or Helvetica), color #2D3748, clean minimal technical diagram style. Horizontal connector lines are thin, precise, and clearly visible, touching the tower structure at exact corresponding measurement points. Professional architectural elevation drawing aesthetic with dynamic low angle perspective creating sense of height and grandeur, poster-ready infographic design with perfect visual hierarchy.", None], | |
| ["Soaking wet capybara taking shelter under a banana leaf in the rainy jungle, close up photo", None], | |
| ["A kawaii die-cut sticker of a chubby orange cat, featuring big sparkly eyes and a happy smile with paws raised in greeting and a heart-shaped pink nose. The design should have smooth rounded lines with black outlines and soft gradient shading with pink cheeks.", None], | |
| ] | |
| examples_images = [ | |
| # ["Replace the top of the person from image 1 with the one from image 2", ["person1.webp", "woman2.webp"]], | |
| ["The person from image 1 is petting the cat from image 2, the bird from image 3 is next to them", ["woman1.webp", "cat_window.webp", "bird.webp"]] | |
| ] | |
| css=""" | |
| #col-container { | |
| margin: 0 auto; | |
| max-width: 620px; | |
| } | |
| """ | |
| with gr.Blocks() as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.Markdown(f"""# FLUX.2 [dev] | |
| 32B param rectified flow transformer guidance-distilled from [FLUX.2 pro](#) [[model](#)], [[blog](#)] | |
| """) | |
| with gr.Accordion("Input image(s) (optional)", open=False): | |
| input_images = gr.Gallery( | |
| label="Input Image(s)", | |
| type="pil", | |
| columns=3, | |
| rows=1, | |
| ) | |
| with gr.Row(): | |
| prompt = gr.Text( | |
| label="Prompt", | |
| show_label=False, | |
| max_lines=2, | |
| placeholder="Enter your prompt", | |
| container=False, | |
| scale=3 | |
| ) | |
| run_button = gr.Button("Run", scale=1) | |
| 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(): | |
| num_inference_steps = gr.Slider( | |
| label="Number of inference steps", | |
| minimum=1, | |
| maximum=100, | |
| step=1, | |
| value=30, | |
| ) | |
| guidance_scale = gr.Slider( | |
| label="Guidance scale", | |
| minimum=0.0, | |
| maximum=10.0, | |
| step=0.1, | |
| value=4, | |
| ) | |
| gr.Examples( | |
| examples=examples, | |
| fn=infer, | |
| inputs=[prompt, input_images], | |
| outputs=[result, seed], | |
| cache_examples=True, | |
| cache_mode="lazy" | |
| ) | |
| gr.Examples( | |
| examples=examples_images, | |
| fn=infer, | |
| inputs=[prompt, input_images], | |
| outputs=[result, seed], | |
| cache_examples=True, | |
| cache_mode="lazy" | |
| ) | |
| gr.on( | |
| triggers=[run_button.click, prompt.submit], | |
| fn=infer, | |
| inputs=[prompt, input_images, seed, randomize_seed, width, height, num_inference_steps, guidance_scale], | |
| outputs=[result, seed] | |
| ) | |
| demo.launch(css=css) |