Spaces:
Running
on
Zero
Running
on
Zero
Upload app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Reference: https://huggingface.co/spaces/FoundationVision/LlamaGen/blob/main/app.py
|
2 |
+
from PIL import Image
|
3 |
+
import gradio as gr
|
4 |
+
from imagenet_classes import imagenet_idx2classname
|
5 |
+
import torch
|
6 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
7 |
+
torch.backends.cudnn.allow_tf32 = True
|
8 |
+
import time
|
9 |
+
import demo_util
|
10 |
+
from utils.train_utils import create_pretrained_tokenizer
|
11 |
+
import os
|
12 |
+
import spaces
|
13 |
+
from huggingface_hub import hf_hub_download
|
14 |
+
|
15 |
+
os.system("pip3 install -U numpy")
|
16 |
+
|
17 |
+
|
18 |
+
hf_hub_download(repo_id="fun-research/TiTok", filename="maskgit-vqgan-imagenet-f16-256.bin", local_dir="./")
|
19 |
+
hf_hub_download(repo_id="yucornetto/RAR", filename="rar_b.bin", local_dir="./")
|
20 |
+
|
21 |
+
# @spaces.GPU
|
22 |
+
def load_model():
|
23 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
+
# load config
|
25 |
+
rar_model_size = "rar_b"
|
26 |
+
config = demo_util.get_config("configs/training/generator/rar.yaml")
|
27 |
+
config.experiment.generator_checkpoint = f"{rar_model_size}.bin"
|
28 |
+
config.model.generator.hidden_size = {"rar_b": 768, "rar_l": 1024, "rar_xl": 1280, "rar_xxl": 1408}[rar_model_size]
|
29 |
+
config.model.generator.num_hidden_layers = {"rar_b": 24, "rar_l": 24, "rar_xl": 32, "rar_xxl": 40}[rar_model_size]
|
30 |
+
config.model.generator.num_attention_heads = 16
|
31 |
+
config.model.generator.intermediate_size = {"rar_b": 3072, "rar_l": 4096, "rar_xl": 5120, "rar_xxl": 6144}[rar_model_size]
|
32 |
+
|
33 |
+
print(config)
|
34 |
+
tokenizer = create_pretrained_tokenizer(config)
|
35 |
+
print(tokenizer)
|
36 |
+
generator = demo_util.get_rar_generator(config)
|
37 |
+
print(generator)
|
38 |
+
|
39 |
+
tokenizer = tokenizer.to(device)
|
40 |
+
generator = generator.to(device)
|
41 |
+
return tokenizer, generator
|
42 |
+
|
43 |
+
tokenizer, generator = load_model()
|
44 |
+
|
45 |
+
@spaces.GPU
|
46 |
+
def demo_infer(
|
47 |
+
guidance_scale, randomize_temperature, guidance_scale_pow,
|
48 |
+
class_label, seed):
|
49 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
50 |
+
n = 4
|
51 |
+
class_labels = [class_label for _ in range(n)]
|
52 |
+
torch.manual_seed(seed)
|
53 |
+
torch.cuda.manual_seed(seed)
|
54 |
+
t1 = time.time()
|
55 |
+
generated_image = demo_util.sample_fn(
|
56 |
+
generator=generator,
|
57 |
+
tokenizer=tokenizer,
|
58 |
+
labels=class_labels,
|
59 |
+
guidance_scale=guidance_scale,
|
60 |
+
randomize_temperature=randomize_temperature,
|
61 |
+
guidance_scale_pow=guidance_scale_pow,
|
62 |
+
device=device
|
63 |
+
)
|
64 |
+
sampling_time = time.time() - t1
|
65 |
+
print(f"generation takes about {sampling_time:.2f} seconds.")
|
66 |
+
samples = [Image.fromarray(sample) for sample in generated_image]
|
67 |
+
return samples
|
68 |
+
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
gr.Markdown("<h1 style='text-align: center'>An Image is Worth 32 Tokens for Reconstruction and Generation</h1>")
|
71 |
+
|
72 |
+
with gr.Tabs():
|
73 |
+
with gr.TabItem('Generate'):
|
74 |
+
with gr.Row():
|
75 |
+
with gr.Column():
|
76 |
+
with gr.Row():
|
77 |
+
i1k_class = gr.Dropdown(
|
78 |
+
list(imagenet_idx2classname.values()),
|
79 |
+
value='Eskimo dog, husky',
|
80 |
+
type="index", label='ImageNet-1K Class'
|
81 |
+
)
|
82 |
+
guidance_scale = gr.Slider(minimum=1, maximum=25, step=0.1, value=10.0, label='Classifier-free Guidance Scale')
|
83 |
+
randomize_temperature = gr.Slider(minimum=0.8, maximum=1.2, step=0.01, value=1.0, label='randomize_temperature')
|
84 |
+
guidance_scale_pow = gr.Slider(minimum=0.0, maximum=4.0, step=0.25, value=0.0, label='guidance_scale_pow')
|
85 |
+
seed = gr.Slider(minimum=0, maximum=1000, step=1, value=42, label='Seed')
|
86 |
+
button = gr.Button("Generate", variant="primary")
|
87 |
+
with gr.Column():
|
88 |
+
output = gr.Gallery(label='Generated Images',
|
89 |
+
columns=4,
|
90 |
+
rows=1,
|
91 |
+
height=256, object_fit="scale-down")
|
92 |
+
button.click(demo_infer, inputs=[
|
93 |
+
guidance_scale, randomize_temperature, guidance_scale_pow,
|
94 |
+
i1k_class, seed],
|
95 |
+
outputs=[output])
|
96 |
+
demo.queue()
|
97 |
+
demo.launch(debug=True)
|