-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (58 loc) · 1.9 KB
/
Copy pathscript.js
File metadata and controls
66 lines (58 loc) · 1.9 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
const key = "hf_sAryooCEObWMHTaHOOeMMupOimDQBVCZyhl";
const inputText = document.getElementById("input");
const image = document.getElementById("image");
const GenBtn = document.getElementById("btn");
const svg = document.getElementById("svg");
const load = document.getElementById("loading");
const ResetBtn = document.getElementById("reset");
const downloadBtn = document.getElementById("download");
async function query() {
const response = await fetch(
"https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image",
{
headers: {
Authorization: Bearer ${key}
},
method: "POST",
body: JSON.stringify({ "inputs": inputText.value }),
}
);
const result = await response.blob();
image.style.display = "block";
load.style.display = "none";
return result;
}
async function generate() {
load.style.display = "block";
query().then((response) => {
const objectUrl = URL.createObjectURL(response);
image.src = objectUrl;
downloadBtn.addEventListener("click", () => {
download(objectUrl);
});
});
}
GenBtn.addEventListener("click", () => {
generate();
svg.style.display = "none";
});
inputText.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
generate();
svg.style.display = "none";
}
});
ResetBtn.addEventListener("click", () => {
inputText.value = "";
window.location.reload();
});
function download(objectUrl) {
fetch(objectUrl).then(res => res.blob())
.then(file => {
let a = document.createElement("a");
a.href = URL.createObjectURL(file);
a.download = new Date().getTime(); // Generates a timestamp as filename
a.click();
})
.catch(() => alert("Failed to download"));
}