-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
173 lines (149 loc) · 4.7 KB
/
main.js
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
const app = new Vue({
el: '#app',
data: {
im2txtCaption: '',
MobileNetCategory: '',
AttnGANImages: [],
BigGANImages: []
}
})
const CAPTION_DELAY = 2200;
async function main() {
document.getElementById('asterisk').onclick = (e) => {
document.getElementById('info-modal').classList.toggle('hidden')
}
const attnGAN = new RunwayHostedModel(`https://attngan.hosted-models.runwayml.cloud/v1`, 'e5iKhIk5Ly90LElSND8M5g==')
const bigGAN = new RunwayHostedModel(`https://biggan.hosted-models.runwayml.cloud/v1`, 'Tmg5rPCP4fi8M8jyYPDIXw==')
const im2txt = new RunwayHostedModel(`https://im2txt.hosted-models.runwayml.cloud/v1`, 'OotKQhfwTCW8xEIQSMTV8w==')
console.log('Waiting for models to wake up')
await Promise.all([
attnGAN.awaken(true),
bigGAN.awaken(true),
im2txt.awaken(true),
])
console.log('Models are awake')
let output = await bigGAN.query({
category: 'stingray',
z: randomZVector()
})
let image, caption, category
while (true) {
console.log('[BigGAN] Received an image')
image = output.generated_output
addBigGANImage(image)
output = await im2txt.query({ image })
caption = output.caption
console.log(`[im2txt] ${caption}`)
addIm2txtCaption(caption)
await delay(CAPTION_DELAY)
output = await attnGAN.query({ caption })
image = output.result
console.log('[AttnGAN] Received an image')
addAttnGANImage(image)
output = await queryMobileNet(image)
category = output[0].className
console.log(`[MobileNet] ${category}`)
addMobileNetCategory(category)
await delay(CAPTION_DELAY)
output = await bigGAN.query({
category,
z: randomZVector()
})
}
}
async function loadImage(base64Image) {
return new Promise((resolve, reject) => {
const img = document.createElement('img')
img.hidden = true
img.onload = () => resolve(img)
img.onerror = reject
img.src = base64Image
})
}
let mobile = null
async function queryMobileNet(base64image) {
const img = await loadImage(base64image)
if (!mobile) mobile = await mobilenet.load()
const predictions = await mobile.classify(img)
return predictions
}
function randomZVector() {
const vec = []
for (let i = 0; i < 128; i++) vec[i] = randomGaussian()
return vec
}
let gaussianCache = false
function randomGaussian(mean, sd) {
let y1, x1, x2, w;
if (gaussianCache) {
y1 = y2;
gaussianCache = false;
} else {
do {
x1 = Math.random() * 2 - 1;
x2 = Math.random() * 2 - 1;
w = x1 * x1 + x2 * x2;
} while (w >= 1);
w = Math.sqrt(-2 * Math.log(w) / w);
y1 = x1 * w;
y2 = x2 * w;
gaussianCache = true;
}
const m = mean || 0;
const s = sd || 1;
return y1 * s + m;
}
function addBigGANImage(base64) {
shiftImages(app.BigGANImages)
const imgs = document.getElementsByClassName('biggan-image')
fadeImages(imgs)
app.BigGANImages.push(base64)
}
function addIm2txtCaption(caption) {
caption = caption.replace(' .', '.')
if (caption[caption.length - 1] !== '.') caption = caption + '.'
app.im2txtCaption = `That's ${caption}`
const el = document.getElementById('im2txt-caption')
el.classList.add('fade-text-in')
setTimeout(() => {
el.classList.remove('fade-text-in')
el.classList.add('fade-text-out')
setTimeout(() => el.classList.remove('fade-text-out'), 500)
}, CAPTION_DELAY)
}
function addAttnGANImage(base64) {
shiftImages(app.AttnGANImages)
const imgs = document.getElementsByClassName('attngan-image')
fadeImages(imgs)
app.AttnGANImages.push(base64)
}
function addMobileNetCategory(caption) {
const first = caption.split(',')[0]
app.MobileNetCategory = `That's a ${first}.`
const el = document.getElementById('mobilenet-caption')
el.classList.add('fade-text-in')
setTimeout(() => {
el.classList.remove('fade-text-in')
el.classList.add('fade-text-out')
setTimeout(() => el.classList.remove('fade-text-out'), 500)
}, CAPTION_DELAY)
}
function shiftImages(arrayOfBase64) {
if (arrayOfBase64.length > 4) {
arrayOfBase64.shift()
}
}
function fadeImages(imageElements) {
let i = 1
for (img of imageElements) {
img.style.opacity = 0 + (i / imageElements.length) - 0.1
i++
}
}
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function delay(millis) {
return new Promise(resolve => setTimeout(resolve, millis))
}
main()