Spaces:
Running
Running
File size: 10,052 Bytes
c99cc8d 62c50cd c99cc8d b95cc56 c99cc8d 0818503 c99cc8d c4d103b c99cc8d 62c50cd c99cc8d 62c50cd c99cc8d 1e16a1f c99cc8d 0818503 c99cc8d c4d103b c99cc8d 0818503 c99cc8d 0818503 c99cc8d 0818503 c99cc8d 1e16a1f c99cc8d 0818503 c99cc8d 62c50cd c99cc8d 62c50cd c99cc8d 62c50cd c99cc8d ab77dc3 c99cc8d 62c50cd c99cc8d 62c50cd c99cc8d 0818503 62c50cd e7b5357 9c554be c99cc8d 62c50cd c99cc8d 0818503 62c50cd e7b5357 9c554be c99cc8d 0818503 c99cc8d |
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
<script lang="ts">
import { onMount, onDestroy } from "svelte";
import type { IViewer } from "./viewers/IViewer";
import { createViewer } from "./viewers/ViewerFactory";
import { Cube, WatsonHealth3DPrintMesh } from "carbon-icons-svelte";
interface Data {
input: string;
input_path: string;
model1: string;
model1_path: string;
model2: string;
model2_path: string;
}
let viewerA: IViewer;
let viewerB: IViewer;
let canvasA: HTMLCanvasElement;
let canvasB: HTMLCanvasElement;
let containerA: HTMLDivElement;
let containerB: HTMLDivElement;
let overlayA: HTMLDivElement;
let overlayB: HTMLDivElement;
let normalToggleA: HTMLInputElement;
let normalToggleB: HTMLInputElement;
let wireframeToggleA: HTMLInputElement;
let wireframeToggleB: HTMLInputElement;
let loadingBarFillA: HTMLDivElement;
let loadingBarFillB: HTMLDivElement;
let voteOverlay: boolean = false;
let voteOverlayA: HTMLDivElement;
let voteOverlayB: HTMLDivElement;
let statusMessage: string = "Loading...";
let errorMessage: string = "";
let data: Data;
async function fetchScenes() {
statusMessage = "Loading...";
errorMessage = "";
try {
const url = "/api/fetchScenes";
const response = await fetch(url, {
method: "GET",
headers: {
"Cache-Control": "no-cache",
},
});
const result = await response.json();
if (result.input) {
data = result;
statusMessage = "";
return true;
} else {
statusMessage = "Voting complete.";
return false;
}
} catch (error) {
errorMessage = "Failed to fetch pair.";
statusMessage = "";
return false;
}
}
async function loadScenes() {
const success = await fetchScenes();
if (!success) return;
overlayA.style.display = "flex";
overlayB.style.display = "flex";
const baseUrl = "https://huggingface.co/datasets/dylanebert/3d-arena/resolve/main/";
const model1_path = `${baseUrl}${data.model1_path}`;
const model2_path = `${baseUrl}${data.model2_path}`;
try {
[viewerA, viewerB] = await Promise.all([
createViewer(model1_path, canvasA, (progress) => {
loadingBarFillA.style.width = `${progress * 100}%`;
}),
createViewer(model2_path, canvasB, (progress) => {
loadingBarFillB.style.width = `${progress * 100}%`;
}),
]);
window.addEventListener("resize", handleResize);
handleResize();
} catch (error) {
console.log(error);
errorMessage = "Failed to load scenes.";
}
overlayA.style.display = "none";
overlayB.style.display = "none";
}
async function vote(option: "A" | "B") {
voteOverlay = true;
voteOverlayA.classList.add("show");
voteOverlayB.classList.add("show");
const payload = {
input: data.input,
better: option == "A" ? data.model1 : data.model2,
worse: option == "A" ? data.model2 : data.model1,
};
const url = `/api/vote`;
const startTime = Date.now();
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Cache-Control": "no-cache",
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
const elapsedTime = Date.now() - startTime;
const remainingTime = Math.max(1200 - elapsedTime, 0);
if (response.ok) {
const result = await response.json();
console.log(result);
setTimeout(() => {
voteOverlayA.classList.remove("show");
voteOverlayB.classList.remove("show");
voteOverlay = false;
loadScenes();
}, remainingTime);
} else {
if (response.status === 401) {
statusMessage = "Unauthorized. Redirecting to login...";
await new Promise((resolve) => setTimeout(resolve, 1000));
window.location.href = "/api/authorize";
} else {
errorMessage = "Failed to process vote.";
}
}
} catch (error) {
errorMessage = "Failed to process vote.";
statusMessage = "";
}
}
function skip() {
loadScenes();
}
function handleResize() {
requestAnimationFrame(() => {
if (canvasA && containerA) {
canvasA.width = containerA.clientWidth;
canvasA.height = containerA.clientHeight;
}
if (canvasB && containerB) {
canvasB.width = containerB.clientWidth;
canvasB.height = containerB.clientHeight;
}
});
}
function setRenderMode(viewer: IViewer, mode: string) {
viewer.setRenderMode(mode);
}
onMount(loadScenes);
onDestroy(() => {
viewerA?.dispose();
viewerB?.dispose();
if (typeof window !== "undefined") {
window.removeEventListener("resize", handleResize);
}
});
</script>
{#if errorMessage}
<p class="center-title muted" style="color: red;">{errorMessage}</p>
{:else if statusMessage}
<p class="center-title muted">{statusMessage}</p>
{:else}
<div class="vote-input">
<img
src={`https://huggingface.co/datasets/dylanebert/3d-arena/resolve/main/${data.input_path}`}
class="input-image"
alt="Input"
/>
</div>
<h2 class="center-title">Which is better?</h2>
<p class="center-subtitle">Use mouse/touch to change the view.</p>
<div class="voting-container">
<div bind:this={containerA} class="canvas-container">
<div bind:this={overlayA} class="loading-overlay">
<div class="loading-bar">
<div bind:this={loadingBarFillA} class="loading-bar-fill" />
</div>
</div>
<div bind:this={voteOverlayA} class="vote-overlay">{data.model1}</div>
<canvas bind:this={canvasA} class="viewer-canvas" id="canvas1"> </canvas>
<div class="stats">
{#if viewerA}
<p>vertex count: {viewerA.vertexCount}</p>
{/if}
</div>
{#if viewerA && !viewerA.topoOnly}
<div class="mode-toggle">
<label>
<input
type="radio"
name="modeA"
value="default"
checked
bind:this={normalToggleA}
on:change={() => setRenderMode(viewerA, "default")}
/>
<Cube class="mode-toggle-icon" />
</label>
<label>
<input
type="radio"
name="modeA"
value="wireframe"
bind:this={wireframeToggleA}
on:change={() => setRenderMode(viewerA, "wireframe")}
/>
<WatsonHealth3DPrintMesh class="mode-toggle-icon" />
</label>
</div>
{/if}
</div>
<div bind:this={containerB} class="canvas-container">
<div bind:this={overlayB} class="loading-overlay">
<div class="loading-bar">
<div bind:this={loadingBarFillB} class="loading-bar-fill" />
</div>
</div>
<div bind:this={voteOverlayB} class="vote-overlay">{data.model2}</div>
<canvas bind:this={canvasB} class="viewer-canvas" id="canvas2"></canvas>
<div class="stats">
{#if viewerB}
<p>vertex count: {viewerB.vertexCount}</p>
{/if}
</div>
{#if viewerB && !viewerB.topoOnly}
<div class="mode-toggle">
<label>
<input
type="radio"
name="modeB"
value="default"
checked
bind:this={normalToggleB}
on:change={() => setRenderMode(viewerB, "default")}
/>
<Cube class="mode-toggle-icon" />
</label>
<label>
<input
type="radio"
name="modeB"
value="wireframe"
bind:this={wireframeToggleB}
on:change={() => setRenderMode(viewerB, "wireframe")}
/>
<WatsonHealth3DPrintMesh class="mode-toggle-icon" />
</label>
</div>
{/if}
</div>
</div>
{#if !voteOverlay}
<div class="vote-buttons-container">
<button class="vote-button" on:click={() => vote("A")}>A is Better</button>
<button class="vote-button" on:click={() => vote("B")}>B is Better</button>
</div>
<div class="skip-container">
<button class="vote-button" on:click={() => skip()}>Skip</button>
</div>
{/if}
{/if}
|