File size: 7,934 Bytes
36cabaf
26e0ccb
 
 
 
 
 
 
 
 
 
 
af8def7
26e0ccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0845a
 
 
 
 
 
 
 
 
 
 
 
 
26e0ccb
36cabaf
26e0ccb
 
 
54e8f2c
26e0ccb
 
 
 
 
 
 
 
9d0845a
 
 
 
26e0ccb
 
 
 
 
 
 
 
 
 
 
 
54e8f2c
26e0ccb
 
 
 
d59b17d
26e0ccb
 
f8ff0a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26e0ccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0845a
26e0ccb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8ff0a0
 
1347566
 
9d0845a
 
 
f8ff0a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d0845a
 
b7b0985
9d0845a
1347566
 
f8ff0a0
26e0ccb
 
 
 
 
 
 
 
597a418
26e0ccb
5657972
f8ff0a0
6180e6c
398d68f
f8ff0a0
9d0845a
 
 
 
 
 
 
 
f8ff0a0
 
9d0845a
95ab3e4
f8ff0a0
 
9d0845a
 
 
 
 
f8ff0a0
1347566
9d0845a
 
1347566
 
f8ff0a0
276cbb8
f8ff0a0
 
 
 
 
 
 
 
26e0ccb
9d0845a
26e0ccb
 
dd48c02
26e0ccb
f8ff0a0
26e0ccb
 
 
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
import spaces
import argparse
import random

import gradio as gr
import numpy as np

# import spaces
import torch
from torchvision import transforms
from transformers import AutoModelForImageSegmentation

from inference_i2mv_sdxl import prepare_pipeline, remove_bg, run_pipeline

# Device and dtype
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"

# Hyperparameters
NUM_VIEWS = 6
HEIGHT = 768
WIDTH = 768
MAX_SEED = np.iinfo(np.int32).max

pipe = prepare_pipeline(
    base_model="stabilityai/stable-diffusion-xl-base-1.0",
    vae_model="madebyollin/sdxl-vae-fp16-fix",
    unet_model=None,
    lora_model=None,
    adapter_path="huanngzh/mv-adapter",
    scheduler=None,
    num_views=NUM_VIEWS,
    device=device,
    dtype=dtype,
)

# remove bg
# birefnet = AutoModelForImageSegmentation.from_pretrained(
#     "ZhengPeng7/BiRefNet", trust_remote_code=True
# )
# birefnet.to(device)
# transform_image = transforms.Compose(
#     [
#         transforms.Resize((1024, 1024)),
#         transforms.ToTensor(),
#         transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
#     ]
# )


@spaces.GPU()
def infer(
    prompt,
    image,
    do_rembg=False,
    seed=42,
    randomize_seed=False,
    guidance_scale=3.0,
    num_inference_steps=50,
    reference_conditioning_scale=1.0,
    negative_prompt="watermark, ugly, deformed, noisy, blurry, low contrast",
    progress=gr.Progress(track_tqdm=True),
):
    # if do_rembg:
    #     remove_bg_fn = lambda x: remove_bg(x, birefnet, transform_image, device)
    # else:
    #     remove_bg_fn = None
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    images, preprocessed_image = run_pipeline(
        pipe,
        num_views=NUM_VIEWS,
        text=prompt,
        image=image,
        height=HEIGHT,
        width=WIDTH,
        num_inference_steps=num_inference_steps,
        guidance_scale=guidance_scale,
        seed=seed,
        remove_bg_fn=None,
        reference_conditioning_scale=reference_conditioning_scale,
        negative_prompt=negative_prompt,
        device=device,
    )
    return images


# examples = [
#     [
#         "A decorative figurine of a young anime-style girl",
#         "assets/demo/i2mv/A_decorative_figurine_of_a_young_anime-style_girl.png",
#         True,
#         21,
#     ],
#     [
#         "A juvenile emperor penguin chick",
#         "assets/demo/i2mv/A_juvenile_emperor_penguin_chick.png",
#         True,
#         0,
#     ],
#     [
#         "A striped tabby cat with white fur sitting upright",
#         "assets/demo/i2mv/A_striped_tabby_cat_with_white_fur_sitting_upright.png",
#         True,
#         0,
#     ],
# ]


with gr.Blocks() as demo:
    with gr.Row():
        gr.Markdown(
            f"""# MV-Adapter [Image-to-Multi-View]
Generate 768x768 multi-view images from a single image using SDXL <br>
[[page](https://huanngzh.github.io/MV-Adapter-Page/)] [[repo](https://github.com/huanngzh/MV-Adapter)]
        """
        )

    with gr.Row():
        with gr.Column():
            with gr.Row():
                input_image = gr.Image(
                    label="Input Image",
                    sources=["upload", "webcam", "clipboard"],
                    type="pil",
                )
                preprocessed_image = gr.Image(label="Preprocessed Image", type="pil")

            prompt = gr.Textbox(
                label="Prompt", placeholder="Enter your prompt", value="high quality"
            )
            # do_rembg = gr.Checkbox(label="Remove background", value=True)
            run_button = gr.Button("Run")

            with gr.Accordion("Advanced Settings", open=False):
                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():
                    num_inference_steps = gr.Slider(
                        label="Number of inference steps",
                        minimum=1,
                        maximum=50,
                        step=1,
                        value=50,
                    )

                with gr.Row():
                    guidance_scale = gr.Slider(
                        label="CFG scale",
                        minimum=0.0,
                        maximum=10.0,
                        step=0.1,
                        value=3.0,
                    )

                with gr.Row():
                    reference_conditioning_scale = gr.Slider(
                        label="Image conditioning scale",
                        minimum=0.0,
                        maximum=2.0,
                        step=0.1,
                        value=1.0,
                    )

                with gr.Row():
                    negative_prompt = gr.Textbox(
                        label="Negative prompt",
                        placeholder="Enter your negative prompt",
                        value="watermark, ugly, deformed, noisy, blurry, low contrast",
                    )

        def use_orientation(selected_image:gr.SelectData):
            return selected_image.value['image']['path']

        # def add_orientation(imgs, index):
        #     index = int(index)
        #     return imgs[index]

        # def get_image_by_index(imgs, index):
        #     """
        #     Retrieves the image from the imgs list based on the provided index.
        #     """
        #     try:
        #         index = int(index)
        #         if 0 <= index < len(imgs):
        #             return imgs[index]
        #         else:
        #             return None  # Or return a placeholder image
        #     except (ValueError, TypeError):
        #         return None  # Or return a placeholder image


        # def set_slider_index(selected_image):
        #     return selected_image.index
        
    
        
        # imgs = gr.State()

        with gr.Column():
            result = gr.Gallery(
                label="Result",
                show_label=False,
                columns=[3],
                rows=[2],
                object_fit="contain",
                height="auto",
                allow_preview=False,
            )

            

            if result:
                # Slider to select image index
                # slider = gr.Slider(
                #     minimum=0,
                #     maximum=5,
                #     step=1,
                #     label="Select Image Index",
                #     value=0,  # Default value
                #     interactive=True
                # )
                
                input_fg = gr.Image(type="numpy", label="Selected Image", height=480)
                selected = gr.Number(visible=True)
                result.select(use_orientation, inputs=None, outputs=input_fg)
                # add_button = gr.Button("Select orientation for processing")
                # add_button.click(add_orientation, [imgs, selected], input_fg)
                # result.select(
                #     fn=set_slider_index,
                #     inputs=result,
                #     outputs=slider
                # )
                
                # slider.change(
                #     fn=use_orientation,
                #     inputs=[result, slider],
                #     outputs=input_fg
                # )
        

    # with gr.Row():
        # gr.Examples(
        #     examples=examples,
        #     fn=infer,
        #     inputs=[prompt, input_image, do_rembg, seed],
        #     outputs=[result, preprocessed_image, seed],
        #     cache_examples=True,
        # )

    run_button.click(fn=infer,
        inputs=[
            prompt,
            input_image,
        ],
        outputs=[result],
    )

demo.launch()