File size: 11,423 Bytes
de9e5b3
 
 
 
 
0348a07
de9e5b3
0348a07
de9e5b3
 
 
0348a07
de9e5b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8830247
de9e5b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f7382b
de9e5b3
 
 
 
 
 
 
 
 
 
 
 
 
2ec361c
de9e5b3
 
9f7382b
de9e5b3
 
 
 
 
 
 
 
 
2ec361c
de9e5b3
2ec361c
de9e5b3
2ec361c
 
 
 
 
 
 
de9e5b3
9f7382b
de9e5b3
 
 
 
 
 
 
 
9f7382b
de9e5b3
 
 
 
 
 
 
 
 
 
9f7382b
de9e5b3
 
 
 
 
 
 
2ec361c
9f7382b
de9e5b3
 
 
 
 
 
55a7e84
 
 
 
 
 
de9e5b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f7382b
de9e5b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import os
import base64
import io
from typing import TypedDict
import requests
import gradio as gr
from PIL import Image

# Read Baseten configuration from environment variables.
BTEN_API_KEY = os.getenv("API_KEY")
URL = os.getenv("URL")

def image_to_base64(image: Image.Image) -> str:
    """Convert a PIL image to a base64-encoded PNG string."""
    with io.BytesIO() as buffer:
        image.save(buffer, format="PNG")
        return base64.b64encode(buffer.getvalue()).decode("utf-8")


def ensure_image(img) -> Image.Image:
    """
    Ensure the input is a PIL Image.
    If it's already a PIL Image, return it.
    If it's a string (file path), open it.
    If it's a dict with a "name" key, open the file at that path.
    """
    if isinstance(img, Image.Image):
        return img
    elif isinstance(img, str):
        return Image.open(img)
    elif isinstance(img, dict) and "name" in img:
        return Image.open(img["name"])
    else:
        raise ValueError("Cannot convert input to a PIL Image.")


def call_baseten_generate(
    image: Image.Image,
    prompt: str,
    steps: int,
    strength: float,
    height: int,
    width: int,
    lora_name: str,
    remove_bg: bool,
) -> Image.Image | None:
    """
    Call the Baseten /predict endpoint with provided parameters and return the generated image.
    """
    image = ensure_image(image)
    b64_image = image_to_base64(image)
    payload = {
        "image": b64_image,
        "prompt": prompt,
        "steps": steps,
        "strength": strength,
        "height": height,
        "width": width,
        "lora_name": lora_name,
        "bgrm": remove_bg,
    }
    if not BTEN_API_KEY:
        headers = {"Authorization": f"Api-Key {os.getenv('API_KEY')}"}
    else:
        headers = {"Authorization": f"Api-Key {BTEN_API_KEY}"}
    try:
        if not URL:
            raise ValueError("The URL environment variable is not set.")

        response = requests.post(URL, headers=headers, json=payload)
        if response.status_code == 200:
            data = response.json()
            gen_b64 = data.get("generated_image", None)
            if gen_b64:
                return Image.open(io.BytesIO(base64.b64decode(gen_b64)))
            else:
                return None
        else:
            print(f"Error: HTTP {response.status_code}\n{response.text}")
            return None
    except Exception as e:
        print(f"Error: {e}")
        return None


# Mode defaults for each tab.

Mode = TypedDict(
    "Mode",
    {
        "model": str,
        "prompt": str,
        "default_strength": float,
        "default_height": int,
        "default_width": int,
        "models": list[str],
        "remove_bg": bool,
    },
)

MODE_DEFAULTS: dict[str, Mode] = {
    "Subject Generation": {
        "model": "subject_99000_512",
        "prompt": "A detailed portrait with soft lighting",
        "default_strength": 1.2,
        "default_height": 512,
        "default_width": 512,
        "models": [
            "zendsd_512_146000",
            "subject_99000_512",
            # "zen_pers_11000",
            "zen_26000_512",
        ],
        "remove_bg": True,
    },
    "Background Generation": {
        "model": "gen_back_3000_1024",
        "prompt": "A vibrant background with dynamic lighting and textures",
        "default_strength": 1.2,
        "default_height": 1024,
        "default_width": 1024,
        "models": [
            "bgwlight_15000_1024",
            # "rmgb_12000_1024",
            "bg_canny_58000_1024",
            # "gen_back_3000_1024",
            "gen_back_7000_1024",
            # "gen_bckgnd_18000_512",
            # "gen_bckgnd_18000_512",
            # "loose_25000_512",
            # "looser_23000_1024",
            # "looser_bg_gen_21000_1280",
            # "old_looser_46000_1024",
            # "relight_bg_gen_31000_1024",
        ],
        "remove_bg": True,
    },
    "Canny": {
        "model": "canny_21000_1024",
        "prompt": "A futuristic cityscape with neon lights",
        "default_strength": 1.2,
        "default_height": 1024,
        "default_width": 1024,
        "models": ["canny_21000_1024"],
        "remove_bg": True,
    },
    "Depth": {
        "model": "depth_9800_1024",
        "prompt": "A scene with pronounced depth and perspective",
        "default_strength": 1.2,
        "default_height": 1024,
        "default_width": 1024,
        "models": [
            "depth_9800_1024",
        ],
        "remove_bg": True,
    },
    "Deblurring": {
        "model": "slight_deblurr_18000",
        "prompt": "A scene with pronounced depth and perspective",
        "default_strength": 1.2,
        "default_height": 1024,
        "default_width": 1024,
        "models": ["deblurr_1024_10000"],  # "slight_deblurr_18000",
        "remove_bg": False,
    },
}


header = """
# 🌍 ZenCtrl / FLUX
<div align="center" style="line-height: 1;">
    <a href="https://github.com/FotographerAI/ZenCtrl/tree/main" target="_blank" style="margin: 2px;" name="github_repo_link"><img src="https://img.shields.io/badge/GitHub-Repo-181717.svg" alt="GitHub Repo" style="display: inline-block; vertical-align: middle;"></a>
    <a href="https://huggingface.co/spaces/fotographerai/ZenCtrl" target="_blank" name="huggingface_space_link"><img src="https://img.shields.io/badge/🤗_HuggingFace-Space-ffbd45.svg" alt="HuggingFace Space" style="display: inline-block; vertical-align: middle;"></a>
    <a href="https://discord.com/invite/b9RuYQ3F8k" target="_blank" style="margin: 2px;" name="discord_link"><img src="https://img.shields.io/badge/Discord-Join-7289da.svg?logo=discord" alt="Discord" style="display: inline-block; vertical-align: middle;"></a>
    <a href="https://fotographer.ai/" target="_blank" style="margin: 2px;" name="lp_link"><img src="https://img.shields.io/badge/Website-Landing_Page-blue" alt="LP" style="display: inline-block; vertical-align: middle;"></a>
    <a href="https://x.com/FotographerAI" target="_blank" style="margin: 2px;" name="twitter_link"><img src="https://img.shields.io/twitter/follow/FotographerAI?style=social" alt="X" style="display: inline-block; vertical-align: middle;"></a>
</div>
"""

defaults = MODE_DEFAULTS["Subject Generation"]


with gr.Blocks(title="🌍 ZenCtrl") as demo:
    gr.Markdown(header)
    gr.Markdown(
        """
        # ZenCtrl Demo
        [WIP] One Agent to Generate multi-view, diverse-scene, and task-specific high-resolution images from a single subject image—without fine-tuning.
        We are first releasing some of the task specific weights and will release the codes soon.
        The goal is to unify all of the visual content generation tasks with a single LLM...
        
        **Modes:**
        - **Subject Generation:** Focuses on generating detailed subject portraits.
        - **Background Generation:** Creates dynamic, vibrant backgrounds:
            You can generate part of the image from sketch while keeping part of it as it is.
        - **Canny:** Emphasizes strong edge detection.
        - **Depth:** Produces images with realistic depth and perspective.
        
        For more details, shoot us a message on discord.
        """
    )
    with gr.Tabs():
        for mode in MODE_DEFAULTS:
            with gr.Tab(mode):
                defaults = MODE_DEFAULTS[mode]
                gr.Markdown(f"### {mode} Mode")
                gr.Markdown(f"**Default Model:** {defaults['model']}")

                with gr.Row():
                    with gr.Column(scale=2, min_width=370):
                        input_image = gr.Image(
                            label="Upload Image",
                            type="pil",
                            scale=3,
                            height=370,
                            min_width=100,
                        )
                        generate_button = gr.Button("Generate")
                        with gr.Blocks(title="Options"):
                            model_dropdown = gr.Dropdown(
                                label="Model",
                                choices=defaults["models"],
                                value=defaults["model"],
                                interactive=True,
                            )
                            remove_bg_checkbox = gr.Checkbox(
                                label="Remove Background", value=defaults["remove_bg"]
                            )

                    with gr.Column(scale=2):
                        output_image = gr.Image(
                            label="Generated Image",
                            type="pil",
                            height=573,
                            scale=4,
                            min_width=100,
                        )

                gr.Markdown("#### Prompt")
                prompt_box = gr.Textbox(
                    label="Prompt", value=defaults["prompt"], lines=2
                )

                # Wrap generation parameters in an Accordion for collapsible view.
                with gr.Accordion("Generation Parameters", open=False):
                    with gr.Row():
                        step_slider = gr.Slider(
                            minimum=2, maximum=28, value=2, step=2, label="Steps"
                        )
                        strength_slider = gr.Slider(
                            minimum=0.5,
                            maximum=2.0,
                            value=defaults["default_strength"],
                            step=0.1,
                            label="Strength",
                        )
                    with gr.Row():
                        height_slider = gr.Slider(
                            minimum=512,
                            maximum=1360,
                            value=defaults["default_height"],
                            step=1,
                            label="Height",
                        )
                        width_slider = gr.Slider(
                            minimum=512,
                            maximum=1360,
                            value=defaults["default_width"],
                            step=1,
                            label="Width",
                        )

                def on_generate_click(
                    model_name,
                    prompt,
                    steps,
                    strength,
                    height,
                    width,
                    remove_bg,
                    image,
                ):
                    return call_baseten_generate(
                        image,
                        prompt,
                        steps,
                        strength,
                        height,
                        width,
                        model_name,
                        remove_bg,
                    )

                generate_button.click(
                    fn=on_generate_click,
                    inputs=[
                        model_dropdown,
                        prompt_box,
                        step_slider,
                        strength_slider,
                        height_slider,
                        width_slider,
                        remove_bg_checkbox,
                        input_image,
                    ],
                    outputs=[output_image],
                )


if __name__ == "__main__":
    demo.launch()