-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (59 loc) · 1.66 KB
/
Copy pathscript.js
File metadata and controls
71 lines (59 loc) · 1.66 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
const inputText = document.getElementById("input");
const image = document.getElementById("image");
const garbage = "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";
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(data) {
const response = await fetch(
"https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image",
{
headers: {
Authorization: `Bearer ${garbage}`
},
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();
a.click();
})
.catch(()=> alert("Failed Download"))
}