Spaces:
Running
Running
File size: 8,682 Bytes
c99cc8d be445b3 c99cc8d 62c50cd c99cc8d be445b3 c99cc8d be445b3 c99cc8d 62c50cd c99cc8d 62c50cd c99cc8d d4f23a0 c99cc8d 62c50cd c99cc8d 62c50cd c99cc8d 62c50cd c99cc8d 62c50cd c99cc8d 62c50cd c99cc8d 62c50cd e7b5357 62c50cd c99cc8d 62c50cd c99cc8d 62c50cd e7b5357 62c50cd c99cc8d 62c50cd d898c0f 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 |
<script lang="ts">
import { onMount, onDestroy } from "svelte";
import { v4 as uuidv4 } from "uuid";
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 loadingBarFillA: HTMLDivElement;
let loadingBarFillB: HTMLDivElement;
let statusMessage: string = "Loading...";
let errorMessage: string = "";
let data: Data;
function getUsername() {
let storedUsername = sessionStorage.getItem("username");
if (!storedUsername) {
storedUsername = uuidv4();
sessionStorage.setItem("username", storedUsername);
}
return storedUsername;
}
async function fetchScenes() {
statusMessage = "Loading...";
errorMessage = "";
try {
const username = getUsername();
console.log(`Fetching with username: ${username}`);
const url = `https://dylanebert-3d-arena-backend.hf.space/pair?username=${username}`;
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: "Bearer " + import.meta.env.VITE_HF_TOKEN,
"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) {
errorMessage = "Failed to load scenes.";
}
overlayA.style.display = "none";
overlayB.style.display = "none";
}
async function vote(option: "A" | "B") {
statusMessage = "Processing vote...";
errorMessage = "";
const payload = {
username: getUsername(),
input: data.input,
better: option == "A" ? data.model1 : data.model2,
worse: option == "A" ? data.model2 : data.model1,
};
const url = `https://dylanebert-3d-arena-backend.hf.space/vote`;
try {
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: "Bearer " + import.meta.env.VITE_HF_TOKEN,
"Cache-Control": "no-cache",
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
});
if (response.ok) {
const result = await response.json();
console.log(result);
loadScenes();
} else {
errorMessage = "Failed to process vote.";
}
} catch (error) {
errorMessage = "Failed to process vote.";
statusMessage = "";
}
}
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}
<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>
<canvas bind:this={canvasA} class="viewer-canvas" id="canvas1"> </canvas>
<div class="stats">
{#if viewerA}
<p>vertex count: {viewerA.vertexCount}</p>
{/if}
</div>
<div class="mode-toggle">
<label>
<input
type="radio"
name="modeA"
value="default"
checked
on:change={() => setRenderMode(viewerA, "default")}
/>
<Cube class="mode-toggle-icon" />
</label>
<label>
<input
type="radio"
name="modeA"
value="wireframe"
on:change={() => setRenderMode(viewerA, "wireframe")}
/>
<WatsonHealth3DPrintMesh class="mode-toggle-icon" />
</label>
</div>
</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>
<canvas bind:this={canvasB} class="viewer-canvas" id="canvas2"></canvas>
<div class="stats">
{#if viewerB}
<p>vertex count: {viewerB.vertexCount}</p>
{/if}
</div>
<div class="mode-toggle">
<label>
<input
type="radio"
name="modeB"
value="default"
checked
on:change={() => setRenderMode(viewerB, "default")}
/>
<Cube class="mode-toggle-icon" />
</label>
<label>
<input
type="radio"
name="modeB"
value="wireframe"
on:change={() => setRenderMode(viewerB, "wireframe")}
/>
<WatsonHealth3DPrintMesh class="mode-toggle-icon" />
</label>
</div>
</div>
</div>
<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={() => loadScenes()}>Skip</button>
</div>
{/if}
|