Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import numpy as np | |
| import random | |
| import spaces | |
| import torch | |
| from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler, AutoencoderTiny, AutoencoderKL | |
| from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast | |
| from live_preview_helpers import calculate_shift, retrieve_timesteps, flux_pipe_call_that_returns_an_iterable_of_images | |
| from llm_wrapper import run_gemini | |
| from huggingface_hub import hf_hub_download | |
| from safetensors.torch import load_file | |
| import subprocess | |
| subprocess.run("rm -rf /data-nvme/zerogpu-offload/*", env={}, shell=True) | |
| dtype = torch.bfloat16 | |
| device = "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) | |
| # PONIX mode load | |
| pipe.load_lora_weights('cwhuh/ponix-generator-v0.2.0', weight_name='pytorch_lora_weights.safetensors') | |
| embedding_path = hf_hub_download(repo_id='cwhuh/ponix-generator-v0.2.0', filename='./ponix-generator-v0.2.0_emb.safetensors', repo_type="model") | |
| state_dict = load_file(embedding_path) | |
| pipe.load_textual_inversion(state_dict["clip_l"], token=["<s0>", "<s1>", "<s2>"], text_encoder=pipe.text_encoder, tokenizer=pipe.tokenizer) | |
| torch.cuda.empty_cache() | |
| MAX_SEED = np.iinfo(np.int32).max | |
| MAX_IMAGE_SIZE = 2048 | |
| pipe.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe) | |
| 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) | |
| print(f"User Prompt: {prompt}") | |
| refined_prompt = run_gemini( | |
| target_prompt=prompt, | |
| prompt_in_path="prompt.json", | |
| ) | |
| print(f"Refined Prompt: {refined_prompt}") | |
| for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images( | |
| prompt=refined_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, seed | |
| examples = [ | |
| "κΈ°κ³κ³΅νκ³Ό(λ‘μΌ) ν¬λμ€", | |
| "λ°μ΄μ¬λ¦°μ μ°μ£Όνλ ν¬λμ€", | |
| "물리νμ μ°κ΅¬νλ ν¬λμ€", | |
| "μ»΄ν¨ν°κ³΅νκ³Ό ν¬λμ€" | |
| ] | |
| css=""" | |
| #col-container { | |
| margin: 0 auto; | |
| max-width: 580px; | |
| } | |
| .footer { | |
| text-align: center; | |
| margin-top: 20px; | |
| font-size: 0.8em; | |
| color: #666; | |
| } | |
| /* URL λ§ν¬ μ€νμΌ */ | |
| a { | |
| color: #666 !important; | |
| text-decoration: underline; | |
| } | |
| a:hover { | |
| color: rgb(200, 1, 80) !important; | |
| } | |
| /* κΈ°λ³Έ ν λ§ μμμ ν¬μ€ν λ λλ‘ λ³κ²½ */ | |
| :root { | |
| --primary-50: rgb(255, 240, 244); | |
| --primary-100: rgb(255, 200, 220); | |
| --primary-200: rgb(255, 150, 180); | |
| --primary-300: rgb(255, 100, 140); | |
| --primary-400: rgb(255, 50, 100); | |
| --primary-500: rgb(200, 1, 80); | |
| --primary-600: rgb(180, 1, 70); | |
| --primary-700: rgb(160, 1, 60); | |
| --primary-800: rgb(140, 1, 50); | |
| --primary-900: rgb(120, 1, 40); | |
| --primary-950: rgb(100, 1, 30); | |
| } | |
| """ | |
| with gr.Blocks(css=css, theme="soft") as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.Markdown(f"""# π [POSTECH] PONIX Generator | |
| **[[Github](https://github.com/posplexity/ponix-generator)]** **[[νΌλλ°±](https://docs.google.com/forms/d/1BccziUtYGF0ToTjZ8PmxZExJJgzpErCuWmrm6ui0COc/edit)]** | |
| [based on FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md) | |
| """) | |
| with gr.Group(): | |
| gr.Markdown(""" | |
| ### π μ¬μ© κ°μ΄λ | |
| - μμ±νκ³ μΆμ μ΄λ―Έμ§λ₯Ό νκΈλ‘ κ°λ¨νκ² μμ±ν΄μ£ΌμΈμ. | |
| - μ΄λ―Έμ§λ λ Έμ΄μ¦μμ μ μ°¨μ μΌλ‘ μμ±λ©λλ€. (40~50μ΄ μμ) | |
| - λ¬Έμλ μ΄λ©μΌλ‘ λΆνλ립λλ€: cw.huh@postech.ac.kr | |
| """) | |
| with gr.Group(): | |
| prompt = gr.Text( | |
| label="ν둬ννΈ μ λ ₯", | |
| max_lines=1, | |
| placeholder="μνλ ν¬λμ€ μ΄λ―Έμ§λ₯Ό νκΈλ‘ μ€λͺ ν΄μ£ΌμΈμ", | |
| container=True, | |
| ) | |
| run_button = gr.Button("π μμ±νκΈ°", variant="primary") | |
| result = gr.Image(label="μμ±λ μ΄λ―Έμ§") | |
| with gr.Accordion("π οΈ κ³ κΈ μ€μ ", open=False): | |
| with gr.Group(): | |
| use_prompt_refinement = gr.Checkbox( | |
| label="ν둬ννΈ μλ κ°μ ", | |
| value=True, | |
| info="AIκ° μ λ ₯ν ν둬ννΈλ₯Ό μλμΌλ‘ κ°μ ν©λλ€." | |
| ) | |
| with gr.Row(): | |
| seed = gr.Slider( | |
| label="μλ κ°", | |
| minimum=0, | |
| maximum=MAX_SEED, | |
| step=1, | |
| value=0, | |
| ) | |
| randomize_seed = gr.Checkbox(label="λλ€ μλ μ¬μ©", value=True) | |
| with gr.Row(): | |
| width = gr.Slider( | |
| label="λλΉ", | |
| minimum=256, | |
| maximum=MAX_IMAGE_SIZE, | |
| step=32, | |
| value=1024, | |
| ) | |
| height = gr.Slider( | |
| label="λμ΄", | |
| minimum=256, | |
| maximum=MAX_IMAGE_SIZE, | |
| step=32, | |
| value=1024, | |
| ) | |
| with gr.Row(): | |
| guidance_scale = gr.Slider( | |
| label="κ°μ΄λμ€ μ€μΌμΌ", | |
| minimum=1, | |
| maximum=15, | |
| step=0.1, | |
| value=3.5, | |
| ) | |
| num_inference_steps = gr.Slider( | |
| label="μΆλ‘ λ¨κ³ μ", | |
| minimum=1, | |
| maximum=50, | |
| step=1, | |
| value=28, | |
| ) | |
| gr.Markdown("### μμ ν둬ννΈ") | |
| gr.Examples( | |
| examples = examples, | |
| fn = infer, | |
| inputs = [prompt], | |
| outputs = [result, seed], | |
| cache_examples="lazy" | |
| ) | |
| gr.HTML(""" | |
| <div class="footer"> | |
| PONIX Generator by νμ±μ | POSTECH | |
| </div> | |
| """) | |
| 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() |