-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.js
More file actions
180 lines (180 loc) · 6.03 KB
/
generate.js
File metadata and controls
180 lines (180 loc) · 6.03 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
173
174
175
176
177
178
179
180
// generate.js
(function(){
console.log("[generate.js] Loaded");
const BACKEND="https://genex-api-world-initialization.nvidia-oci.saturnenterprise.io";
const EXPLORE_BACKEND="https://genex-api-world-exploration.nvidia-oci.saturnenterprise.io";
const KEY="genex-super-secret-key";
let CID=localStorage.getItem("genex_cid")||crypto.randomUUID();
localStorage.setItem("genex_cid",CID);
window.GENEX={BACKEND,EXPLORE_BACKEND,KEY,CID,panorama:null,panoramaBlob:null};
const promptBox=document.getElementById("prompt");
const promptBtn=document.getElementById("promptBtn");
const dropZone=document.getElementById("dropZone");
const fileInput=document.getElementById("fileInput");
const preview=document.getElementById("preview");
const dzText=document.getElementById("dzText");
const imageBtn=document.getElementById("imageBtn");
const statusT=document.getElementById("statusText");
const timerSp=document.getElementById("timer");
const logBox=document.getElementById("log");
let tHandle=null;
function log(msg){
console.log("[generate]",msg);
logBox.textContent+=msg+"\n";
logBox.scrollTop=logBox.scrollHeight;
}
function startTimer(){
let s=0;
timerSp.textContent="00:00";
clearInterval(tHandle);
tHandle=setInterval(()=>{
s++;
timerSp.textContent=`${String(Math.floor(s/60)).padStart(2,"0")}:${String(s%60).padStart(2,"0")}`;
},1000);
}
function stopTimer(){
clearInterval(tHandle);
timerSp.textContent="00:00";
}
function showPanorama(src){
const old=window.GENEX.panorama;
const yaw=old?.getYaw()??0;
const pitch=old?.getPitch()??0;
if(old){
old.destroy();
} else {
document.getElementById("placeholder")?.remove();
}
window.GENEX.panorama=pannellum.viewer("panorama",{
type:"equirectangular",
panorama:src,
autoLoad:true,
showZoomCtrl:true,
sceneFadeDuration:0,
yaw,
pitch
});
}
async function loadBlobAndShow(blob){
window.GENEX.panoramaBlob=blob;
showPanorama(URL.createObjectURL(blob));
imageBtn.disabled=false;
document.getElementById("exploreBtn").disabled=false;
}
(async()=>{
log("Initial load: fetching history");
try{
const hdr={"x-api-key":KEY,"x-client-id":CID};
const hResp=await fetch(`${BACKEND}/history/`,{headers:hdr});
if(!hResp.ok) return;
const{total}=await hResp.json();
if(total===0) return;
const rResp=await fetch(`${BACKEND}/recent/?count=1`,{headers:hdr});
if(!rResp.ok) return;
const{recent}=await rResp.json();
if(recent?.length){
const b64=recent[0].image_base64;
const blob=await (await fetch(`data:image/png;base64,${b64}`)).blob();
await loadBlobAndShow(blob);
}
}catch(e){}
})();
["dragenter","dragover"].forEach(ev=>dropZone.addEventListener(ev,e=>{e.preventDefault();dropZone.classList.add("hover");}));
["dragleave","drop"].forEach(ev=>dropZone.addEventListener(ev,e=>{e.preventDefault();dropZone.classList.remove("hover");}));
dropZone.addEventListener("drop",e=>{
if(e.dataTransfer.files.length){
fileInput.files=e.dataTransfer.files;
updatePreview();
}
});
dropZone.addEventListener("click",()=>fileInput.click());
fileInput.addEventListener("change",updatePreview);
function updatePreview(){
if(fileInput.files.length){
preview.src=URL.createObjectURL(fileInput.files[0]);
preview.style.display="block";
dzText.style.display="none";
imageBtn.disabled=false;
log("preview updated");
}else{
preview.style.display="none";
dzText.style.display="block";
imageBtn.disabled=true;
}
}
document.querySelectorAll("#examples img").forEach(img=>{
img.addEventListener("click",async()=>{
document.querySelectorAll("#examples img").forEach(i=>i.classList.remove("selected"));
img.classList.add("selected");
const b=await (await fetch(img.dataset.src)).blob();
const f=new File([b],"example.png",{type:b.type});
const dt=new DataTransfer();
dt.items.add(f);
fileInput.files=dt.files;
updatePreview();
});
});
promptBtn.addEventListener("click",async()=>{
const prompt=promptBox.value.trim();
if(!prompt){ alert("Enter a prompt"); return; }
log("Text→World prompt:"+prompt);
statusT.textContent="Generating…";
startTimer();
try{
const res = await fetch(`${BACKEND}/generate_from_text/`, {
method: "POST",
headers: {
"x-api-key": KEY,
"x-client-id": CID,
"Content-Type": "application/json"
},
body: JSON.stringify({ prompt })
});
const ct=res.headers.get("content-type")||"";
if(ct.includes("application/json")){
const p=await res.json();
const uri=p.images?.[0]?.url;
if(!uri) throw "no images[0].url";
const blob=await (await fetch(uri)).blob();
await loadBlobAndShow(blob);
} else {
const blob=await res.blob();
await loadBlobAndShow(blob);
}
statusT.textContent="Done ✔";
}catch(e){
statusT.textContent="Idle";
}finally{
stopTimer();
}
});
imageBtn.addEventListener("click",async()=>{
if(!fileInput.files.length) return;
const file=fileInput.files[0];
log("Image→World file:"+file);
statusT.textContent="Generating…";
startTimer();
try{
const form=new FormData();
form.append("image",file);
const res=await fetch(`${BACKEND}/generate/`,{
method:"POST",
headers:{"x-api-key":KEY,"x-client-id":CID},
body:form
});
if(res.status===503||res.status===429){ statusT.textContent="Idle"; return; }
if(res.ok&&res.headers.get("content-type")?.startsWith("image/")){
const blob=await res.blob();
await loadBlobAndShow(blob);
statusT.textContent="Done ✔";
} else {
statusT.textContent="Idle";
}
}catch(e){
statusT.textContent="Idle";
}finally{
stopTimer();
}
});
console.log("[generate.js] Ready");
})();