Spaces:
Running
Running
File size: 15,964 Bytes
199beca 1ef2df5 | 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>dispatchAI — Tiny Model Dungeon</title>
<style>
:root {
--ink: #0A0F1A;
--off-white: #F5F7FA;
--electric-blue: #2E6BFF;
--cyan: #1FE0E6;
--dungeon-dark: #0d0d14;
--dungeon-bg: #14141f;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Courier New', 'SF Mono', monospace;
background: var(--dungeon-dark);
color: var(--off-white);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 10px;
}
.header {
text-align: center;
margin-bottom: 15px;
}
.title {
font-size: 1.8em;
font-weight: bold;
color: var(--cyan);
text-shadow: 0 0 20px rgba(31, 224, 230, 0.3);
}
.subtitle {
color: #666;
font-size: 0.75em;
margin-top: 3px;
}
.game-container {
width: 100%;
max-width: 800px;
background: var(--dungeon-bg);
border: 2px solid #1a2332;
border-radius: 8px;
overflow: hidden;
display: flex;
flex-direction: column;
height: 600px;
}
.game-header {
background: linear-gradient(135deg, rgba(46, 107, 255, 0.1), rgba(31, 224, 230, 0.1));
padding: 8px 15px;
border-bottom: 1px solid #1a2332;
display: flex;
justify-content: space-between;
align-items: center;
font-size: 0.8em;
}
.hp-bar {
display: flex;
align-items: center;
gap: 5px;
}
.hp-track {
width: 100px;
height: 8px;
background: #1a2332;
border-radius: 4px;
overflow: hidden;
}
.hp-fill {
height: 100%;
background: linear-gradient(90deg, #ff4444, #ffaa00);
transition: width 0.3s;
}
.game-screen {
flex: 1;
overflow-y: auto;
padding: 15px;
display: flex;
flex-direction: column;
gap: 8px;
}
.game-screen::-webkit-scrollbar { width: 6px; }
.game-screen::-webkit-scrollbar-thumb { background: #1a2332; border-radius: 3px; }
.narration {
color: #aab4c8;
font-size: 0.9em;
line-height: 1.6;
white-space: pre-wrap;
}
.narration.ai {
color: var(--cyan);
border-left: 3px solid var(--cyan);
padding-left: 10px;
}
.action {
color: var(--electric-blue);
font-size: 0.85em;
font-style: italic;
}
.system {
color: #555;
font-size: 0.75em;
text-align: center;
font-style: italic;
}
.input-area {
padding: 10px 15px;
border-top: 1px solid #1a2332;
display: flex;
gap: 8px;
}
.input-area input {
flex: 1;
background: var(--dungeon-dark);
border: 1px solid #1a2332;
color: var(--off-white);
padding: 10px 14px;
border-radius: 6px;
font-family: inherit;
font-size: 0.9em;
outline: none;
}
.input-area input:focus { border-color: var(--cyan); }
.input-area input:disabled { opacity: 0.5; }
.input-area button {
background: var(--cyan);
color: var(--ink);
border: none;
padding: 10px 20px;
border-radius: 6px;
font-family: inherit;
font-weight: bold;
font-size: 0.85em;
cursor: pointer;
}
.input-area button:disabled { opacity: 0.5; cursor: not-allowed; }
.quick-actions {
display: flex;
gap: 5px;
padding: 0 15px 10px;
flex-wrap: wrap;
}
.quick-btn {
background: #1a2332;
border: 1px solid #233;
color: #8892a6;
padding: 5px 12px;
border-radius: 4px;
font-family: inherit;
font-size: 0.75em;
cursor: pointer;
transition: all 0.2s;
}
.quick-btn:hover { border-color: var(--cyan); color: var(--cyan); }
.stats-bar {
width: 100%;
max-width: 800px;
margin-top: 10px;
display: flex;
gap: 8px;
flex-wrap: wrap;
justify-content: center;
}
.stat {
background: var(--dungeon-bg);
border: 1px solid #1a2332;
border-radius: 4px;
padding: 5px 10px;
font-size: 0.7em;
color: #666;
}
.stat span { color: var(--cyan); }
.footer {
margin-top: 15px;
text-align: center;
color: #444;
font-size: 0.7em;
}
.footer a { color: var(--electric-blue); text-decoration: none; }
.loading-overlay {
position: fixed;
inset: 0;
background: rgba(10, 15, 26, 0.95);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
z-index: 100;
gap: 15px;
}
.loading-overlay.hidden { display: none; }
.loading-spinner {
width: 40px;
height: 40px;
border: 3px solid #1a2332;
border-top-color: var(--cyan);
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.start-btn {
background: linear-gradient(135deg, var(--electric-blue), var(--cyan));
color: var(--ink);
border: none;
padding: 15px 40px;
border-radius: 8px;
font-family: inherit;
font-weight: bold;
font-size: 1.1em;
cursor: pointer;
}
</style>
</head>
<body>
<div class="header">
<div class="title">⚔️ Tiny Model Dungeon</div>
<div class="subtitle">An AI text adventure powered by a 360M parameter model running in your browser</div>
</div>
<div class="loading-overlay" id="loadingOverlay">
<div class="loading-spinner"></div>
<div id="loadingText" style="color: var(--cyan); font-size: 0.9em;">Loading the dungeon master (360M model)...</div>
<div style="color: #555; font-size: 0.75em;">~200MB download, cached for future visits</div>
</div>
<div class="game-container">
<div class="game-header">
<span style="color: var(--cyan);">Dungeon Level 1</span>
<div class="hp-bar">
<span style="color: #ff4444;">HP</span>
<div class="hp-track"><div class="hp-fill" id="hpFill" style="width: 100%"></div></div>
<span id="hpText" style="color: #8892a6; font-size: 0.85em;">100/100</span>
</div>
</div>
<div class="game-screen" id="gameScreen">
<div class="system">The dungeon awaits... Click "Enter" to begin your adventure.</div>
</div>
<div class="quick-actions">
<button class="quick-btn" onclick="quickAction('look around')" disabled>Look around</button>
<button class="quick-btn" onclick="quickAction('check inventory')" disabled>Inventory</button>
<button class="quick-btn" onclick="quickAction('attack')" disabled>Attack</button>
<button class="quick-btn" onclick="quickAction('flee')" disabled>Flee</button>
<button class="quick-btn" onclick="quickAction('search for treasure')" disabled>Search</button>
</div>
<div class="input-area">
<input type="text" id="input" placeholder="What do you do?" disabled autocomplete="off">
<button id="sendBtn" disabled>Go</button>
</div>
</div>
<div class="stats-bar">
<div class="stat">Model: <span>SmolLM2-360M</span></div>
<div class="stat">Size: <span>~200MB</span></div>
<div class="stat">Backend: <span id="backend">—</span></div>
<div class="stat">Tokens/s: <span id="tps">—</span></div>
</div>
<div class="footer">
The entire game AI fits in 200MB and runs on your device. No server, no cloud, no cost.<br>
<a href="https://huggingface.co/dispatchAI" target="_blank">dispatchAI</a> |
<a href="https://huggingface.co/dispatchAI/SmolLM2-360M-Instruct-mobile" target="_blank">Model</a> |
<a href="https://github.com/xenova/transformers.js" target="_blank">transformers.js</a>
</div>
<script type="module">
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.0';
env.allowLocalModels = false;
env.useBrowserCache = true;
let generator = null;
let isGenerating = false;
let hp = 100;
let gameState = { room: 1, inventory: [], hasSword: false, monstersDefeated: 0 };
const loadingOverlay = document.getElementById('loadingOverlay');
const loadingText = document.getElementById('loadingText');
const gameScreen = document.getElementById('gameScreen');
const input = document.getElementById('input');
const sendBtn = document.getElementById('sendBtn');
const hpFill = document.getElementById('hpFill');
const hpText = document.getElementById('hpText');
const tpsEl = document.getElementById('tps');
const backendEl = document.getElementById('backend');
const SYSTEM_PROMPT = `You are the dungeon master in a text adventure game. The player explores a dungeon. Describe rooms, monsters, and treasure vividly but concisely (2-3 sentences). React to player actions creatively. If the player attacks, describe the combat. If they search, describe what they find. Keep the adventure exciting and dangerous.`;
const MODEL_ID = 'onnx-community/SmolLM2-360M-Instruct-ONNX';
function addMessage(text, type) {
const div = document.createElement('div');
div.className = type;
div.textContent = text;
gameScreen.appendChild(div);
gameScreen.scrollTop = gameScreen.scrollHeight;
}
async function initGame() {
loadingText.textContent = 'Checking WebGPU support...';
let device = 'wasm';
let dtype = 'q8';
if (navigator.gpu) {
try {
const adapter = await navigator.gpu.requestAdapter();
if (adapter) { device = 'webgpu'; dtype = 'q4f16'; }
} catch(e) {}
}
backendEl.textContent = device.toUpperCase();
loadingText.textContent = `Downloading dungeon master (${device.toUpperCase()})...`;
const t0 = performance.now();
try {
generator = await pipeline('text-generation', MODEL_ID, {
device: device,
dtype: dtype,
});
const loadTime = ((performance.now() - t0) / 1000).toFixed(1);
loadingText.textContent = `Loaded in ${loadTime}s!`;
setTimeout(() => {
loadingOverlay.classList.add('hidden');
startGame();
}, 500);
} catch(e) {
loadingText.textContent = `Error: ${e.message}`;
console.error(e);
}
}
function startGame() {
addMessage('You stand before the entrance to an ancient dungeon. The air is cold. Darkness stretches below.', 'narration ai');
addMessage('Type your action or use the quick buttons above. Good luck, adventurer.', 'system');
input.disabled = false;
sendBtn.disabled = false;
document.querySelectorAll('.quick-btn').forEach(b => b.disabled = false);
input.focus();
}
async function takeAction(action) {
if (isGenerating || !generator) return;
if (!action.trim()) return;
input.value = '';
input.disabled = true;
sendBtn.disabled = true;
isGenerating = true;
document.querySelectorAll('.quick-btn').forEach(b => b.disabled = true);
addMessage(`> ${action}`, 'action');
const messages = [
{ role: 'system', content: SYSTEM_PROMPT },
{ role: 'user', content: action },
];
try {
const t0 = performance.now();
const output = await generator(messages, {
max_new_tokens: 100,
do_sample: true,
temperature: 0.8,
});
const elapsed = (performance.now() - t0) / 1000;
const text = output[0].generated_text[2].content;
const tokenCount = Math.ceil(text.length / 4);
const tps = (tokenCount / elapsed).toFixed(1);
tpsEl.textContent = `${tps}`;
// Type out the narration
const aiMsg = document.createElement('div');
aiMsg.className = 'narration ai';
gameScreen.appendChild(aiMsg);
let i = 0;
const typeInterval = setInterval(() => {
if (i < text.length) {
aiMsg.textContent = text.slice(0, i + 1);
gameScreen.scrollTop = gameScreen.scrollHeight;
i++;
} else {
clearInterval(typeInterval);
}
}, 15);
// Random HP changes for game feel
if (action.toLowerCase().includes('attack')) {
const dmg = Math.floor(Math.random() * 20) + 5;
hp = Math.max(0, hp - dmg);
updateHP();
if (hp <= 0) {
setTimeout(() => addMessage('You have fallen. The dungeon claims another soul... Refresh to try again.', 'system'), 2000);
}
}
if (action.toLowerCase().includes('search') || action.toLowerCase().includes('treasure')) {
if (Math.random() > 0.5) {
hp = Math.min(100, hp + 10);
updateHP();
}
}
} catch(e) {
addMessage(`Error: ${e.message}`, 'system');
} finally {
input.disabled = false;
sendBtn.disabled = false;
isGenerating = false;
document.querySelectorAll('.quick-btn').forEach(b => b.disabled = false);
input.focus();
}
}
function updateHP() {
hpFill.style.width = `${hp}%`;
hpText.textContent = `${hp}/100`;
}
window.quickAction = takeAction;
sendBtn.addEventListener('click', () => takeAction(input.value));
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') takeAction(input.value);
});
// Start loading immediately
initGame();
</script>
</body>
</html>
|