Daankular commited on
Commit
0f7386e
·
verified ·
1 Parent(s): 96db97d

Upload external/MV-Adapter/scripts/gradio_demo_t2mv.py with huggingface_hub

Browse files
external/MV-Adapter/scripts/gradio_demo_t2mv.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import random
3
+
4
+ import gradio as gr
5
+ import numpy as np
6
+
7
+ # import spaces
8
+ import torch
9
+
10
+ from .inference_t2mv_sdxl import prepare_pipeline, run_pipeline
11
+
12
+ # Base model
13
+ parser = argparse.ArgumentParser()
14
+ parser.add_argument(
15
+ "--base_model", type=str, default="stabilityai/stable-diffusion-xl-base-1.0"
16
+ )
17
+ parser.add_argument("--scheduler", type=str, default=None)
18
+ args = parser.parse_args()
19
+ base_model = args.base_model
20
+ scheduler = args.scheduler
21
+
22
+ # Device and dtype
23
+ dtype = torch.bfloat16
24
+ device = "cuda" if torch.cuda.is_available() else "cpu"
25
+
26
+ # Hyperparameters
27
+ NUM_VIEWS = 6
28
+ HEIGHT = 768
29
+ WIDTH = 768
30
+ MAX_SEED = np.iinfo(np.int32).max
31
+
32
+ pipe = prepare_pipeline(
33
+ base_model=base_model,
34
+ vae_model="madebyollin/sdxl-vae-fp16-fix",
35
+ unet_model=None,
36
+ lora_model=None,
37
+ adapter_path="huanngzh/mv-adapter",
38
+ scheduler=scheduler,
39
+ num_views=NUM_VIEWS,
40
+ device=device,
41
+ dtype=dtype,
42
+ )
43
+
44
+
45
+ # @spaces.GPU()
46
+ def infer(
47
+ prompt,
48
+ seed=42,
49
+ randomize_seed=False,
50
+ guidance_scale=7.0,
51
+ num_inference_steps=50,
52
+ negative_prompt="watermark, ugly, deformed, noisy, blurry, low contrast",
53
+ progress=gr.Progress(track_tqdm=True),
54
+ ):
55
+ if randomize_seed:
56
+ seed = random.randint(0, MAX_SEED)
57
+ images = run_pipeline(
58
+ pipe,
59
+ num_views=NUM_VIEWS,
60
+ text=prompt,
61
+ height=HEIGHT,
62
+ width=WIDTH,
63
+ num_inference_steps=num_inference_steps,
64
+ guidance_scale=guidance_scale,
65
+ seed=seed,
66
+ negative_prompt=negative_prompt,
67
+ device=device,
68
+ )
69
+ return images, seed
70
+
71
+
72
+ examples = {
73
+ "stabilityai/stable-diffusion-xl-base-1.0": [
74
+ ["An astronaut riding a horse", 42],
75
+ ["A DSLR photo of a frog wearing a sweater", 42],
76
+ ],
77
+ "cagliostrolab/animagine-xl-3.1": [
78
+ [
79
+ "1girl, izayoi sakuya, touhou, solo, maid headdress, maid, apron, short sleeves, dress, closed mouth, white apron, serious face, upper body, masterpiece, best quality, very aesthetic, absurdres",
80
+ 0,
81
+ ],
82
+ [
83
+ "1boy, male focus, ikari shinji, neon genesis evangelion, solo, serious face,(masterpiece), (best quality), (ultra-detailed), very aesthetic, illustration, disheveled hair, moist skin, intricate details",
84
+ 0,
85
+ ],
86
+ [
87
+ "1girl, pink hair, pink shirts, smile, shy, masterpiece, anime",
88
+ 0,
89
+ ],
90
+ ],
91
+ "Lykon/dreamshaper-xl-1-0": [
92
+ ["the warrior Aragorn from Lord of the Rings, film grain, 8k hd", 0],
93
+ [
94
+ "Oil painting, masterpiece, regal, fancy. A well-dressed dog named Puproy Doggerson III wearing reading glasses types an important letter on a typewriter and enjoys a cup of coffee with the newspaper.",
95
+ 42,
96
+ ],
97
+ ],
98
+ }
99
+
100
+ css = """
101
+ #col-container {
102
+ margin: 0 auto;
103
+ max-width: 600px;
104
+ }
105
+ """
106
+
107
+ with gr.Blocks(css=css) as demo:
108
+
109
+ with gr.Column(elem_id="col-container"):
110
+ gr.Markdown(
111
+ f"""# MV-Adapter [Text-to-Multi-View]
112
+ Generate 768x768 multi-view images using {base_model} <br>
113
+ [[page](https://huanngzh.github.io/MV-Adapter-Page/)] [[repo](https://github.com/huanngzh/MV-Adapter)]
114
+ """
115
+ )
116
+
117
+ with gr.Row():
118
+ prompt = gr.Text(
119
+ label="Prompt",
120
+ show_label=False,
121
+ max_lines=1,
122
+ placeholder="Enter your prompt",
123
+ container=False,
124
+ )
125
+
126
+ run_button = gr.Button("Run", scale=0)
127
+
128
+ result = gr.Gallery(
129
+ label="Result",
130
+ show_label=False,
131
+ columns=[3],
132
+ rows=[2],
133
+ object_fit="contain",
134
+ height="auto",
135
+ )
136
+
137
+ with gr.Accordion("Advanced Settings", open=False):
138
+ seed = gr.Slider(
139
+ label="Seed",
140
+ minimum=0,
141
+ maximum=MAX_SEED,
142
+ step=1,
143
+ value=0,
144
+ )
145
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
146
+
147
+ with gr.Row():
148
+ num_inference_steps = gr.Slider(
149
+ label="Number of inference steps",
150
+ minimum=1,
151
+ maximum=50,
152
+ step=1,
153
+ value=50,
154
+ )
155
+
156
+ with gr.Row():
157
+ guidance_scale = gr.Slider(
158
+ label="CFG scale",
159
+ minimum=0.0,
160
+ maximum=10.0,
161
+ step=0.1,
162
+ value=7.0,
163
+ )
164
+
165
+ with gr.Row():
166
+ negative_prompt = gr.Textbox(
167
+ label="Negative prompt",
168
+ placeholder="Enter your negative prompt",
169
+ value="watermark, ugly, deformed, noisy, blurry, low contrast",
170
+ )
171
+
172
+ if base_model in examples:
173
+ gr.Examples(
174
+ examples=examples[base_model],
175
+ fn=infer,
176
+ inputs=[prompt, seed],
177
+ outputs=[result, seed],
178
+ cache_examples=True,
179
+ )
180
+
181
+ gr.on(
182
+ triggers=[run_button.click, prompt.submit],
183
+ fn=infer,
184
+ inputs=[
185
+ prompt,
186
+ seed,
187
+ randomize_seed,
188
+ guidance_scale,
189
+ num_inference_steps,
190
+ negative_prompt,
191
+ ],
192
+ outputs=[result, seed],
193
+ )
194
+
195
+ demo.launch()