File size: 5,764 Bytes
44df93e
 
 
 
 
 
 
 
 
 
 
 
22ba7a7
d9a9ea3
 
 
44df93e
 
 
 
 
 
 
 
 
 
d9a9ea3
44df93e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22ba7a7
44df93e
 
5b9df98
 
 
 
 
44df93e
 
 
d6a404f
44df93e
 
 
 
 
 
 
 
22ba7a7
 
 
 
 
 
 
 
 
 
 
d9a9ea3
 
 
 
 
 
 
 
 
 
 
890d938
3095ffa
7c67c9f
5b9df98
d9a9ea3
 
5b9df98
 
 
 
d9a9ea3
 
5b9df98
d9a9ea3
 
 
 
 
5b9df98
d9a9ea3
22ba7a7
 
d9a9ea3
 
 
 
 
 
 
d6a404f
44df93e
d6a404f
44df93e
 
d6a404f
 
 
 
44df93e
 
 
 
 
 
 
05397a1
 
 
 
 
d9a9ea3
44df93e
 
 
 
 
 
 
 
 
05397a1
44df93e
 
 
 
 
 
 
 
 
 
d9a9ea3
44df93e
 
 
 
 
 
 
 
 
 
 
 
 
 
05397a1
 
 
 
 
 
 
 
 
44df93e
05397a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44df93e
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
from htbuilder import HtmlElement, div, ul, li, br, hr, a, p, img, styles, classes, fonts
from htbuilder.units import percent, px
from htbuilder.funcs import rgba, rgb
import streamlit as st
import os
import sys
import argparse
import clip
import numpy as np
from PIL import Image
from dalle.models import Dalle
from dalle.utils.utils import set_seed, clip_score
import streamlit.components.v1 as components
import torch 
from IPython.display import display
import random

def link(link, text, **style):
    return a(_href=link, _target="_blank", style=styles(**style))(text)

def layout(*args):

    style = """
    <style>
      # MainMenu {visibility: hidden;}
      footer {visibility: hidden;}
     .stApp { bottom: 125px; }
    </style>
    """

    style_div = styles(
        position="fixed",
        left=0,
        bottom=0,
        margin=px(0, 0, 0, 0),
        width=percent(100),
        color="black",
        text_align="center",
        height="auto",
        opacity=1
    )

    style_hr = styles(
        display="block",
        margin=px(8, 8, "auto", "auto"),
        border_style="inset",
        border_width=px(2)
    )

    body = p()
    foot = div(
        style=style_div
    )(
        hr(
            style=style_hr
        ),
        body
    )

    st.markdown(style, unsafe_allow_html=True)

    for arg in args:
        if isinstance(arg, str):
            body(arg)

        elif isinstance(arg, HtmlElement):
            body(arg)

    st.markdown(str(foot), unsafe_allow_html=True)


def footer():
    myargs = [
        "This app uses the ",
        link("https://github.com/kuprel/min-dalle", "min(DALL·E)"),
        " port of ",
        link("https://github.com/borisdayma/dalle-mini", "DALL·E mini"),
        br(),
        "Created by ",
        link("https://jonathanmalott.com", "Jonathan Malott"),
        br(),
        link("https://bridgingbarriers.utexas.edu/good-systems", "Good Systems Grand Challenge"),
        ", The University of Texas at Austin.",
        " Advised by Dr. Junfeng Jiao.",
        br(),
        br(),
    ]
    layout(*myargs)


    components.html(
    """
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-SB6NJ9DQS7"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-SB6NJ9DQS7');
    </script>
    """
    )


from min_dalle import MinDalle

def generate2(prompt,crazy,k):

    
    mm = MinDalle(
        models_root='./pretrained',
        dtype=torch.float32,
        device='cpu',
        is_mega=False, 
        is_reusable=True
    )

    # Sampling
    newPrompt = prompt
    if("architecture" not in prompt.lower() ):
        newPrompt += " architecture"

    image = mm.generate_image(
        text=newPrompt,
        seed=np.random.randint(0,10000),
        grid_size=1,
        is_seamless=False,
        temperature=crazy,
        top_k=k,#2128,
        supercondition_factor=32,
        is_verbose=False
    )

    item = {}
    item['prompt'] = prompt
    item['crazy'] = crazy
    item['k'] = k
    item['image'] = image
    st.session_state.results.append(item)

model = False
def generate(prompt,crazy,k):
    global model

    device = 'cpu'
    if(model == False):
        model = Dalle.from_pretrained('minDALL-E/1.3B')  # This will automatically download the pretrained model.
        model.to(device=device)
    
    num_candidates = 1

    images = []
    
    set_seed(np.random.randint(0,10000))

    # Sampling
    newPrompt = prompt
    if("architecture" not in prompt.lower() ):
        newPrompt += " architecture"

    images = model.sampling(prompt=newPrompt,
                            top_k=256,
                            top_p=None,
                            softmax_temperature=crazy,
                            num_candidates=num_candidates,
                            device=device).cpu().numpy()
    images = np.transpose(images, (0, 2, 3, 1))

    # CLIP Re-ranking
    model_clip, preprocess_clip = clip.load("ViT-B/32", device=device)
    model_clip.to(device=device)
    rank = clip_score(prompt=newPrompt,
                      images=images,
                      model_clip=model_clip,
                      preprocess_clip=preprocess_clip,
                      device=device)

    result = images[rank]

    item = {}
    item['prompt'] = prompt
    item['crazy'] = crazy
    item['k'] = 20
    item['image'] = Image.fromarray((result*255).astype(np.uint8))
    st.session_state.results.append(item)


def drawGrid():
    master = {}
    
    for r in st.session_state.results[::-1]:
        _txt = r['prompt']+" "+str(r['crazy'])+" "+str(r['k'])
        if(_txt not in master):
            master[_txt] = [r]
        else:
            master[_txt].append(r)


    for i in st.session_state.images:
        im = st.empty()


    placeholder = st.empty()
    with placeholder.container():
        
        for m in master:
            
            txt = master[m][0]['prompt']+" (temperature:"+ str(master[m][0]['crazy']) + ", top k:" + str(master[m][0]['k']) + ")"
            st.subheader(txt)
            col1, col2, col3 = st.columns(3)  

            for ix, item in enumerate(master[m]):
                if ix % 3 == 0: 
                    with col1:
                        st.session_state.images.append(st.image(item["image"]))  
                if ix % 3 == 1:
                    with col2:
                        st.session_state.images.append(st.image(item["image"]))
                if ix % 3 == 2:
                    with col3:
                        st.session_state.images.append(st.image(item["image"]))