ajsbsd commited on
Commit
4fc3d79
·
verified ·
1 Parent(s): dbfc74f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -0
app.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+ import gradio as gr
4
+ import numpy as np
5
+ import spaces
6
+ import torch
7
+ from diffusers import Krea2Pipeline
8
+
9
+ MODEL_ID = "krea/Krea-2-Turbo"
10
+ LORA_REPO = "gokaygokay/Krea-2-Realism-LoRA"
11
+ MAX_SEED = np.iinfo(np.int32).max
12
+ MAX_IMAGE_SIZE = 1536
13
+
14
+ # ---------------------------------------------------------------------------
15
+ # Model loading (CPU init). Weights go to CUDA only inside the @spaces.GPU
16
+ # call, which is how ZeroGPU expects things to be structured.
17
+ # ---------------------------------------------------------------------------
18
+ pipe = Krea2Pipeline.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
19
+ pipe.load_lora_weights(LORA_REPO, adapter_name="realism")
20
+ pipe.to("cuda")
21
+
22
+
23
+ @spaces.GPU(duration=60)
24
+ def infer(
25
+ prompt,
26
+ negative_prompt="",
27
+ seed=42,
28
+ randomize_seed=True,
29
+ width=1024,
30
+ height=1024,
31
+ num_inference_steps=8,
32
+ guidance_scale=0.0,
33
+ lora_scale=1.0,
34
+ progress=gr.Progress(track_tqdm=True),
35
+ ):
36
+ if randomize_seed:
37
+ seed = random.randint(0, MAX_SEED)
38
+
39
+ generator = torch.Generator(device="cuda").manual_seed(seed)
40
+
41
+ pipe.set_adapters(["realism"], adapter_weights=[lora_scale])
42
+
43
+ image = pipe(
44
+ prompt=prompt,
45
+ negative_prompt=negative_prompt if negative_prompt else None,
46
+ width=width,
47
+ height=height,
48
+ num_inference_steps=num_inference_steps,
49
+ guidance_scale=guidance_scale,
50
+ generator=generator,
51
+ ).images[0]
52
+
53
+ return image, seed
54
+
55
+
56
+ examples = [
57
+ "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",
58
+ "An elderly fisherman mending a net on a weathered wooden dock at sunrise, mist rising off the harbor water",
59
+ "A close-up portrait of a street musician playing violin in the rain, neon signs reflecting in puddles behind him",
60
+ ]
61
+
62
+ css = """
63
+ #col-container {
64
+ margin: 0 auto;
65
+ max-width: 720px;
66
+ }
67
+ """
68
+
69
+ with gr.Blocks(css=css) as demo:
70
+ with gr.Column(elem_id="col-container"):
71
+ gr.Markdown(
72
+ """
73
+ # Krea 2 Turbo + Realism LoRA
74
+ Fast photorealistic generation with [Krea-2-Turbo](https://huggingface.co/krea/Krea-2-Turbo)
75
+ and the [Realism LoRA](https://huggingface.co/gokaygokay/Krea-2-Realism-LoRA) by gokaygokay.
76
+ Runs on ZeroGPU.
77
+ """
78
+ )
79
+
80
+ with gr.Row():
81
+ prompt = gr.Text(
82
+ label="Prompt",
83
+ show_label=False,
84
+ max_lines=1,
85
+ placeholder="Describe the image you want to generate",
86
+ container=False,
87
+ )
88
+ run_button = gr.Button("Generate", variant="primary", scale=0)
89
+
90
+ result = gr.Image(label="Result", show_label=False)
91
+
92
+ with gr.Accordion("Advanced Settings", open=False):
93
+ negative_prompt = gr.Text(
94
+ label="Negative prompt",
95
+ max_lines=1,
96
+ placeholder="Enter a negative prompt (optional)",
97
+ )
98
+
99
+ seed = gr.Slider(
100
+ label="Seed",
101
+ minimum=0,
102
+ maximum=MAX_SEED,
103
+ step=1,
104
+ value=0,
105
+ )
106
+
107
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
108
+
109
+ with gr.Row():
110
+ width = gr.Slider(
111
+ label="Width",
112
+ minimum=512,
113
+ maximum=MAX_IMAGE_SIZE,
114
+ step=64,
115
+ value=1024,
116
+ )
117
+ height = gr.Slider(
118
+ label="Height",
119
+ minimum=512,
120
+ maximum=MAX_IMAGE_SIZE,
121
+ step=64,
122
+ value=1024,
123
+ )
124
+
125
+ with gr.Row():
126
+ guidance_scale = gr.Slider(
127
+ label="Guidance scale",
128
+ minimum=0.0,
129
+ maximum=10.0,
130
+ step=0.1,
131
+ value=0.0,
132
+ )
133
+ num_inference_steps = gr.Slider(
134
+ label="Number of inference steps",
135
+ minimum=1,
136
+ maximum=28,
137
+ step=1,
138
+ value=8,
139
+ )
140
+
141
+ lora_scale = gr.Slider(
142
+ label="LoRA scale (Realism strength)",
143
+ minimum=0.0,
144
+ maximum=1.5,
145
+ step=0.05,
146
+ value=1.0,
147
+ )
148
+
149
+ gr.Examples(examples=examples, inputs=[prompt])
150
+
151
+ gr.on(
152
+ triggers=[run_button.click, prompt.submit],
153
+ fn=infer,
154
+ inputs=[
155
+ prompt,
156
+ negative_prompt,
157
+ seed,
158
+ randomize_seed,
159
+ width,
160
+ height,
161
+ num_inference_steps,
162
+ guidance_scale,
163
+ lora_scale,
164
+ ],
165
+ outputs=[result, seed],
166
+ )
167
+
168
+ if __name__ == "__main__":
169
+ demo.launch()