giannisdaras commited on
Commit
da0e65b
·
verified ·
1 Parent(s): 5e3850c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import torch
5
+ import torch
6
+ from micro_diffusion.models.model import create_latent_diffusion
7
+ from huggingface_hub import hf_hub_download
8
+ from safetensors import safe_open
9
+ from PIL import Image
10
+
11
+ # Init model
12
+ params = {
13
+ 'latent_res': 64,
14
+ 'in_channels': 4,
15
+ 'pos_interp_scale': 2.0,
16
+ }
17
+ model = create_latent_diffusion(**params).to('cuda')
18
+
19
+ # Download weights from HF
20
+ model_dict_path = hf_hub_download(repo_id="giannisdaras/ambient-o", filename="model.safetensors")
21
+ model_dict = {}
22
+ with safe_open(model_dict_path, framework="pt", device="cpu") as f:
23
+ for key in f.keys():
24
+ model_dict[key] = f.get_tensor(key)
25
+
26
+ # Convert parameters to float32 + load
27
+ float_model_params = {
28
+ k: v.to(torch.float32) for k, v in model_dict.items()
29
+ }
30
+ model.dit.load_state_dict(float_model_params)
31
+ model = model.eval()
32
+
33
+
34
+ dtype = torch.bfloat16
35
+ device = "cuda" if torch.cuda.is_available() else "cpu"
36
+
37
+ torch.cuda.empty_cache()
38
+
39
+ MAX_SEED = np.iinfo(np.int32).max
40
+ MAX_IMAGE_SIZE = 2048
41
+
42
+
43
+ def infer(prompt, seed=42, randomize_seed=False, guidance_scale=5.0, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
44
+ if randomize_seed:
45
+ seed = random.randint(0, MAX_SEED)
46
+ images = model.generate(prompt=[prompt], num_inference_steps=num_inference_steps, guidance_scale=guidance_scale, seed=seed)
47
+ image = images[0]
48
+ image = image.detach().cpu()
49
+ image = image.permute(1, 2, 0) # [H, W, C]
50
+ image = (image * 255).clamp(0, 255).to(torch.uint8).numpy()
51
+ image = Image.fromarray(image)
52
+ return image, seed
53
+
54
+
55
+ examples = [
56
+ "a tiny astronaut hatching from an egg on the moon",
57
+ "a cat holding a sign that says hello world",
58
+ "an anime illustration of a wiener schnitzel",
59
+ ]
60
+
61
+ css="""
62
+ #col-container {
63
+ margin: 0 auto;
64
+ max-width: 520px;
65
+ }
66
+ """
67
+
68
+ with gr.Blocks(css=css) as demo:
69
+
70
+ with gr.Column(elem_id="col-container"):
71
+ gr.Markdown(f"""# Ambient-o text2image model
72
+ [[paper](https://arxiv.org/abs/2506.10038)]
73
+ [[blog](https://giannisdaras.github.io/publication/ambient_omni)] [[model](https://huggingface.co/giannisdaras/ambient-o)] [[license](https://github.com/giannisdaras/ambient-omni/blob/main/text-to-image/LICENSE)]
74
+ """)
75
+
76
+ with gr.Row():
77
+
78
+ prompt = gr.Text(
79
+ label="Prompt",
80
+ show_label=False,
81
+ max_lines=1,
82
+ placeholder="Enter your prompt",
83
+ container=False,
84
+ )
85
+
86
+ run_button = gr.Button("Run", scale=0)
87
+
88
+ result = gr.Image(label="Result", show_label=False)
89
+
90
+ with gr.Accordion("Advanced Settings", open=False):
91
+
92
+ seed = gr.Slider(
93
+ label="Seed",
94
+ minimum=0,
95
+ maximum=MAX_SEED,
96
+ step=1,
97
+ value=0,
98
+ )
99
+
100
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
+
102
+ with gr.Row():
103
+
104
+ guidance_scale = gr.Slider(
105
+ label="Guidance Scale",
106
+ minimum=1,
107
+ maximum=15,
108
+ step=0.1,
109
+ value=5.0,
110
+ )
111
+
112
+ num_inference_steps = gr.Slider(
113
+ label="Number of inference steps",
114
+ minimum=1,
115
+ maximum=50,
116
+ step=1,
117
+ value=28,
118
+ )
119
+
120
+ gr.Examples(
121
+ examples = examples,
122
+ fn = infer,
123
+ inputs = [prompt],
124
+ outputs = [result, seed],
125
+ cache_examples="lazy"
126
+ )
127
+
128
+ gr.on(
129
+ triggers=[run_button.click, prompt.submit],
130
+ fn = infer,
131
+ inputs = [prompt, seed, randomize_seed, guidance_scale, num_inference_steps],
132
+ outputs = [result, seed]
133
+ )
134
+
135
+ demo.launch()