realrebelai commited on
Commit
03ee4ab
·
verified ·
1 Parent(s): d23011e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+ import gradio as gr
4
+ import spaces
5
+ import torch
6
+ from diffusers import Krea2Pipeline
7
+ from transformers import AutoTokenizer
8
+
9
+ # ----------------------------------------------------------------------------
10
+ # Krea-R-Turbo = Krea 2 Turbo + RebelReal @ 0.31 + RebelMidjourney @ 0.13
11
+ # LoRAs are fused at startup, so inference cost is identical to the base model.
12
+ # ----------------------------------------------------------------------------
13
+ BASE = "krea/Krea-2-Turbo"
14
+
15
+ # Krea's repo ships only tokenizer.json (fast format), but the pipeline's
16
+ # model_index points at the slow Qwen2Tokenizer, which needs vocab.json and
17
+ # crashes with vocab_file=None. Load the fast tokenizer ourselves and hand it in.
18
+ tokenizer = AutoTokenizer.from_pretrained(BASE, subfolder="tokenizer", use_fast=True)
19
+
20
+ pipe = Krea2Pipeline.from_pretrained(BASE, tokenizer=tokenizer,
21
+ torch_dtype=torch.bfloat16)
22
+
23
+ pipe.load_lora_weights("realrebelai/RebelReal_LoRA_Collection",
24
+ weight_name="RebelReal_(Krea-2).safetensors",
25
+ adapter_name="rebelreal")
26
+ pipe.load_lora_weights("realrebelai/RebelMidjourney_LoRA_Collection",
27
+ weight_name="RebelMidjourney_(Krea-2).safetensors",
28
+ adapter_name="rebelmj")
29
+ pipe.set_adapters(["rebelreal", "rebelmj"], adapter_weights=[0.31, 0.13])
30
+ # Bake the LoRAs into the weights (same math as an offline merge), then drop
31
+ # the adapter machinery so sampling has zero LoRA overhead.
32
+ pipe.fuse_lora(adapter_names=["rebelreal", "rebelmj"])
33
+ pipe.unload_lora_weights()
34
+
35
+ pipe.to("cuda")
36
+
37
+ MAX_SEED = 2**31 - 1
38
+
39
+
40
+ @spaces.GPU(duration=60)
41
+ def generate(prompt, width, height, steps, seed, randomize_seed,
42
+ progress=gr.Progress(track_tqdm=True)):
43
+ if randomize_seed:
44
+ seed = random.randint(0, MAX_SEED)
45
+ generator = torch.Generator("cuda").manual_seed(int(seed))
46
+ image = pipe(
47
+ prompt=prompt,
48
+ width=int(width),
49
+ height=int(height),
50
+ num_inference_steps=int(steps),
51
+ guidance_scale=0.0, # Turbo is CFG-free
52
+ generator=generator,
53
+ ).images[0]
54
+ return image, seed
55
+
56
+
57
+ with gr.Blocks(title="Krea-R-Turbo") as demo:
58
+ gr.Markdown(
59
+ """
60
+ # Krea-R-Turbo
61
+ **Krea 2 Turbo** with RealRebelAI's **RebelReal** (0.31) and
62
+ **RebelMidjourney** (0.13) LoRAs fused in. 8-step, CFG-free.
63
+
64
+ GGUF quants for local ComfyUI (8 GB VRAM+):
65
+ [realrebelai/Krea-R-Turbo](https://huggingface.co/realrebelai/Krea-R-Turbo)
66
+ """
67
+ )
68
+ with gr.Row():
69
+ with gr.Column():
70
+ prompt = gr.Textbox(label="Prompt", lines=3,
71
+ placeholder="a cinematic photo of ...")
72
+ run = gr.Button("Generate", variant="primary")
73
+ with gr.Accordion("Settings", open=False):
74
+ with gr.Row():
75
+ width = gr.Slider(512, 2048, value=1280, step=64, label="Width")
76
+ height = gr.Slider(512, 2048, value=1024, step=64, label="Height")
77
+ steps = gr.Slider(4, 12, value=8, step=1, label="Steps")
78
+ with gr.Row():
79
+ seed = gr.Slider(0, MAX_SEED, value=0, step=1, label="Seed")
80
+ randomize_seed = gr.Checkbox(value=True, label="Random seed")
81
+ with gr.Column():
82
+ out = gr.Image(label="Result", format="png")
83
+ used_seed = gr.Number(label="Seed used", interactive=False)
84
+
85
+ run.click(generate,
86
+ inputs=[prompt, width, height, steps, seed, randomize_seed],
87
+ outputs=[out, used_seed])
88
+ prompt.submit(generate,
89
+ inputs=[prompt, width, height, steps, seed, randomize_seed],
90
+ outputs=[out, used_seed])
91
+
92
+ demo.launch()