Spaces:
Paused
Paused
Download app.py from ajsbsd/Krea-2-Realism-LoRA: direct link, hf CLI and curl.
- Browser
- Download file 4.98 kB
-
https://huggingface.co/spaces/ajsbsd/Krea-2-Realism-LoRA/resolve/main/app.py
- Command line
-
hf download hf://spaces/ajsbsd/Krea-2-Realism-LoRA/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ajsbsd/Krea-2-Realism-LoRA/resolve/main/app.py
4.98 kB
| import random | |
| import gradio as gr | |
| import numpy as np | |
| import spaces | |
| import torch | |
| from diffusers import Krea2Pipeline | |
| MODEL_ID = "krea/Krea-2-Turbo" | |
| LORA_REPO = "gokaygokay/Krea-2-Realism-LoRA" | |
| MAX_SEED = np.iinfo(np.int32).max | |
| MAX_IMAGE_SIZE = 1536 | |
| # --------------------------------------------------------------------------- | |
| # Model loading (CPU init). Weights go to CUDA only inside the @spaces.GPU | |
| # call, which is how ZeroGPU expects things to be structured. | |
| # --------------------------------------------------------------------------- | |
| pipe = Krea2Pipeline.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16) | |
| pipe.load_lora_weights(LORA_REPO, adapter_name="realism") | |
| pipe.to("cuda") | |
| def infer( | |
| prompt, | |
| negative_prompt="", | |
| seed=42, | |
| randomize_seed=True, | |
| width=1024, | |
| height=1024, | |
| num_inference_steps=8, | |
| guidance_scale=0.0, | |
| lora_scale=1.0, | |
| progress=gr.Progress(track_tqdm=True), | |
| ): | |
| if randomize_seed: | |
| seed = random.randint(0, MAX_SEED) | |
| generator = torch.Generator(device="cuda").manual_seed(seed) | |
| pipe.set_adapters(["realism"], adapter_weights=[lora_scale]) | |
| image = pipe( | |
| prompt=prompt, | |
| negative_prompt=negative_prompt if negative_prompt else None, | |
| width=width, | |
| height=height, | |
| num_inference_steps=num_inference_steps, | |
| guidance_scale=guidance_scale, | |
| generator=generator, | |
| ).images[0] | |
| return image, seed | |
| examples = [ | |
| "A woman with curly hair laughing while holding an open paperback in a cluttered secondhand bookshop, tall crowded shelves towering around her in soft warm lamplight", | |
| "An elderly fisherman mending a net on a weathered wooden dock at sunrise, mist rising off the harbor water", | |
| "A close-up portrait of a street musician playing violin in the rain, neon signs reflecting in puddles behind him", | |
| ] | |
| css = """ | |
| #col-container { | |
| margin: 0 auto; | |
| max-width: 720px; | |
| } | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Column(elem_id="col-container"): | |
| gr.Markdown( | |
| """ | |
| # Krea 2 Turbo + Realism LoRA | |
| Fast photorealistic generation with [Krea-2-Turbo](https://huggingface.co/krea/Krea-2-Turbo) | |
| and the [Realism LoRA](https://huggingface.co/gokaygokay/Krea-2-Realism-LoRA) by gokaygokay. | |
| Runs on ZeroGPU. | |
| """ | |
| ) | |
| with gr.Row(): | |
| prompt = gr.Text( | |
| label="Prompt", | |
| show_label=False, | |
| max_lines=1, | |
| placeholder="Describe the image you want to generate", | |
| container=False, | |
| ) | |
| run_button = gr.Button("Generate", variant="primary", scale=0) | |
| result = gr.Image(label="Result", show_label=False) | |
| with gr.Accordion("Advanced Settings", open=False): | |
| negative_prompt = gr.Text( | |
| label="Negative prompt", | |
| max_lines=1, | |
| placeholder="Enter a negative prompt (optional)", | |
| ) | |
| 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=512, | |
| maximum=MAX_IMAGE_SIZE, | |
| step=64, | |
| value=1024, | |
| ) | |
| height = gr.Slider( | |
| label="Height", | |
| minimum=512, | |
| maximum=MAX_IMAGE_SIZE, | |
| step=64, | |
| value=1024, | |
| ) | |
| with gr.Row(): | |
| guidance_scale = gr.Slider( | |
| label="Guidance scale", | |
| minimum=0.0, | |
| maximum=10.0, | |
| step=0.1, | |
| value=0.0, | |
| ) | |
| num_inference_steps = gr.Slider( | |
| label="Number of inference steps", | |
| minimum=1, | |
| maximum=28, | |
| step=1, | |
| value=8, | |
| ) | |
| lora_scale = gr.Slider( | |
| label="LoRA scale (Realism strength)", | |
| minimum=0.0, | |
| maximum=1.5, | |
| step=0.05, | |
| value=1.0, | |
| ) | |
| gr.Examples(examples=examples, inputs=[prompt]) | |
| gr.on( | |
| triggers=[run_button.click, prompt.submit], | |
| fn=infer, | |
| inputs=[ | |
| prompt, | |
| negative_prompt, | |
| seed, | |
| randomize_seed, | |
| width, | |
| height, | |
| num_inference_steps, | |
| guidance_scale, | |
| lora_scale, | |
| ], | |
| outputs=[result, seed], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |