-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.js
More file actions
172 lines (148 loc) · 5.22 KB
/
Copy pathjs.js
File metadata and controls
172 lines (148 loc) · 5.22 KB
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
const MOD_LABELS = [
"negativest",
"negativer",
"negative",
"neutral",
"positive",
"positiver",
"positivest",
];
const API_HOST = "api.yinyang.computerpho.be";
const IMG_HOST = "images.yinyang.computerpho.be";
function onUrlChange(e) {
document.getElementById("sourceImage").src = e.target.value;
}
function _updatePrompts(resultBody) {
const {
sentences,
results: { good, bad },
meta,
} = resultBody;
const imageDesc = document.getElementById("imageDesc");
while (imageDesc.firstChild) {
imageDesc.removeChild(imageDesc.firstChild);
}
sentences.forEach(({ sentence, sentiment: { good } }) => {
const ele = document.createElement("span");
ele.style.color = good ? "#d946ef" : "#00d4ff";
ele.innerText = sentence + ".";
imageDesc.appendChild(ele);
});
document.getElementById("goodPrompt").innerText = good.prompt;
document.getElementById("badPrompt").innerText = bad.prompt;
document.getElementById("metadata").innerText =
`OpenAI tokens used: ${meta.openai_tokens_used}`;
}
async function loader(reqIdParam, _event) {
document
.getElementById("yinyang")
.parentElement.removeChild(document.getElementById("yinyang"));
const urlEle = document.getElementById("yinyangUrl");
urlEle.removeEventListener("change", onUrlChange);
const imageUrl = urlEle.value;
document.getElementById("yinyangUrlRO").href = imageUrl;
const sourceDiv = document.getElementById("sourceDiv");
sourceDiv.style.display = "block";
sourceDiv.style.visibility = "visible";
document.getElementById("thresMod").disabled = true;
document.getElementById("sourceImage").src = imageUrl;
document.title = `⏳${document.title}`;
const url = `https://${API_HOST}/${reqIdParam ?? ""}`;
const result = await fetch(
url,
reqIdParam
? {}
: {
method: "POST",
body: imageUrl,
headers: {
"X-Yinyang-OpenAI-Key": "78e74446041c490996d087c5af270f6cca9ed7a57f264a219e0125a3c57057e1",
"X-Yinyang-Threshold-Mod":
document.getElementById("thresMod").value,
},
},
);
if (result.status > 299) {
document.getElementById("metadata").innerText =
`Request failed! Please try again soon`;
document.getElementById("goodImage").src = document.getElementById(
"badImage",
).src = "";
return;
}
let resultBody;
try {
resultBody = await result.json();
} catch (e) {
if (reqIdParam) {
return reqBottomHalf(reqIdParam);
}
console.error("Bad body", e);
return;
}
const { requestId } = resultBody;
_updatePrompts(resultBody);
reqBottomHalf(requestId, reqIdParam ? resultBody : null);
}
async function reqBottomHalf(requestId, resultBody) {
if (!resultBody) {
const result = await fetch(`https://${API_HOST}/${requestId}`);
if (result.status === 202) {
setTimeout(() => reqBottomHalf(requestId), 1337);
return;
}
resultBody = await result.json();
}
document.title = document.title.replace("⏳", "");
const {
input: { url },
results: { good, bad },
} = resultBody;
_updatePrompts(resultBody);
const goodImage = document.getElementById("goodImage");
const badImage = document.getElementById("badImage");
goodImage.title = badImage.title = 'Click to start again with this image as the input';
const goodImageUrl = goodImage.src = `https://${IMG_HOST}/${good.imageBucketId}`;
const badImageUrl = badImage.src = `https://${IMG_HOST}/${bad.imageBucketId}`;
document.getElementById("goodImageLink").href = `/?pl=${goodImageUrl}`;
document.getElementById("badImageLink").href = `/?pl=${badImageUrl}`;
document.getElementById("yinyangUrl").value = url;
onUrlChange({ target: { value: url } });
document.getElementById("reqId").style.display = "block";
const reqIdLink = document.getElementById("reqIdLink");
reqIdLink.innerText = requestId;
reqIdLink.href = `${window.location.origin}/?req=${requestId}&pl=${document.getElementById('yinyangUrl').value}`;
const { searchParams } = new URL(window.location.href);
if (!searchParams.get("req")) {
window.location.href = reqIdLink.href;
}
}
document.addEventListener("DOMContentLoaded", (e) => {
document.getElementById("yinyangUrl").addEventListener("change", onUrlChange);
document.getElementById("yinyangUrl").addEventListener("focus", (e) => {
e.target.select();
});
document.getElementById("thresMod").addEventListener("change", (e) => {
document.getElementById("thresModLabel").innerText =
MOD_LABELS[Number.parseInt(e.target.value) + Number.parseInt(e.target.max)];
});
document
.getElementById("yinyang")
.addEventListener("click", loader.bind(null, null));
const { searchParams } = new URL(window.location.href);
const reqParam = searchParams.get("req");
const preloadUrlParam = searchParams.get("pl");
if (preloadUrlParam) {
document.getElementById('yinyangUrl').value = preloadUrlParam;
}
if (reqParam) {
loader(reqParam);
const startOverLink = document.createElement("a");
startOverLink.innerText = "Start Over";
startOverLink.href = window.location.origin;
if (preloadUrlParam) {
startOverLink.href += `?pl=${preloadUrlParam}`;
}
document.getElementById("inputDiv").appendChild(startOverLink);
}
});