Lamapi commited on
Commit
90141fb
·
verified ·
1 Parent(s): 0975a5c

Publish tested Next AI Lab release

Browse files
Files changed (4) hide show
  1. README.md +11 -10
  2. index.html +82 -19
  3. model.json +490 -0
  4. next_reward.py +58 -0
README.md CHANGED
@@ -1,10 +1,11 @@
1
- ---
2
- title: Next Reward
3
- emoji: 👀
4
- colorFrom: yellow
5
- colorTo: green
6
- sdk: static
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
+ ---
2
+ title: Next Reward
3
+ sdk: static
4
+ app_file: index.html
5
+ colorFrom: green
6
+ colorTo: gray
7
+ models:
8
+ - thelamapi/Next-Reward
9
+ ---
10
+
11
+ # Next Reward
index.html CHANGED
@@ -1,19 +1,82 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <meta charset="utf-8">
4
+ <meta name="viewport" content="width=device-width,initial-scale=1">
5
+ <title>Next Reward</title>
6
+ <style>
7
+ *{box-sizing:border-box}body{margin:0;background:#f3f6f1;color:#142c27;font:16px system-ui}
8
+ main{max-width:960px;margin:auto;padding:36px 20px}h1{font-size:clamp(42px,8vw,78px);letter-spacing:-.06em;margin:0}
9
+ p{line-height:1.55;color:#415a52}canvas,video{width:100%;display:block;border-radius:16px;background:#e4eee5}
10
+ button,select,input,textarea{font:inherit}button,select,input,textarea{padding:9px;border:1px solid #aabdaf;border-radius:8px;background:white}
11
+ button{background:#08775e;color:white;cursor:pointer;margin:8px 0}button:disabled{opacity:.5}
12
+ textarea{width:100%;font:16px monospace}a{color:#08644f}label{display:inline-block;margin:6px}
13
+ </style>
14
+ <main><h1>Next Reward</h1><p>Run a learned routing policy on an editable grid.</p>
15
+ <label>Grid <textarea id="board" rows="6" spellcheck="false">............
16
+ .####.......
17
+ .....#...X..
18
+ .....#......
19
+ .....#####..
20
+ ...........G</textarea></label>
21
+ <p>The agent starts in the top-left cell. Use . for floor, # for walls, X for pits and G for the goal.</p>
22
+ <button id="run" disabled>Run learned policy</button>
23
+ <p id="status" role="status">Loading checkpoint...</p>
24
+ <canvas id="map" width="720" height="380" aria-label="Grid and route"></canvas>
25
+ <script type="module">
26
+ const el=id=>document.getElementById(id), ctx=el("map").getContext("2d");
27
+ let py;
28
+ function draw(board,route){
29
+ ctx.fillStyle="#e4eee5";ctx.fillRect(0,0,720,380);
30
+ const k=Math.min(660/board[0].length,330/board.length);
31
+ const x=(720-k*board[0].length)/2,y=(380-k*board.length)/2;
32
+ const colors={".":"#f9fbf6","#":"#a8bbae","X":"#e2a08d","G":"#397bb5"};
33
+ board.forEach((row,r)=>[...row].forEach((tile,c)=>{
34
+ ctx.fillStyle=colors[tile];ctx.fillRect(x+c*k,y+r*k,k-1,k-1);
35
+ }));
36
+ ctx.beginPath();route.forEach(([r,c],i)=>i?ctx.lineTo(x+(c+.5)*k,y+(r+.5)*k):ctx.moveTo(x+(c+.5)*k,y+(r+.5)*k));
37
+ ctx.strokeStyle="#08775e";ctx.lineWidth=4;ctx.lineJoin="round";ctx.stroke();
38
+ }
39
+ function run(){
40
+ const board=el("board").value.trim().split(/\r?\n/);
41
+ try{
42
+ if(board.length>60||board[0].length>60||board[0][0]!==".")throw Error("Use a floor start and a grid up to 60 × 60.");
43
+ py.globals.set("board_json",JSON.stringify(board));
44
+ const [route,ok]=JSON.parse(py.runPython("rollout(board_json)"));
45
+ draw(board,route);el("status").textContent=ok?"Goal reached in "+(route.length-1)+" steps.":"The policy did not reach the goal.";
46
+ }catch(error){el("status").textContent=error.message;}
47
+ }
48
+ el("run").onclick=run;
49
+ try{
50
+ const indexURL="https://cdn.jsdelivr.net/pyodide/v0.27.7/full/";
51
+ py=await (await import(indexURL+"pyodide.mjs")).loadPyodide({indexURL});
52
+ await py.loadPackage("numpy");
53
+ for(const name of ["next_reward.py","model.json"]){
54
+ const response=await fetch(name);if(!response.ok)throw Error(name+" unavailable");
55
+ py.FS.writeFile(name,(await response.text()).replace(/^\uFEFF/,""));
56
+ }
57
+ py.runPython(`from next_reward import NextReward, MOVES
58
+ import json
59
+ agent = NextReward.load("model.json")
60
+ def rollout(raw):
61
+ board = json.loads(raw)
62
+ policy = agent.policy(board)
63
+ path = [(0, 0)]
64
+ seen = set(path)
65
+ while True:
66
+ r, c = path[-1]
67
+ dy, dx = MOVES[int(policy[r, c])]
68
+ nxt = r+dy, c+dx
69
+ nr, nc = nxt
70
+ if not (0 <= nr < len(board) and 0 <= nc < len(board[0])) or nxt in seen or board[nr][nc] in "#X":
71
+ return json.dumps([path, False])
72
+ path.append(nxt)
73
+ seen.add(nxt)
74
+ if board[nr][nc] == "G":
75
+ return json.dumps([path, True])
76
+ `);
77
+ el("run").disabled=false;run();
78
+ }catch(error){el("status").textContent="Could not load checkpoint. "+error.message}
79
+ </script>
80
+
81
+ <p><a href="https://huggingface.co/thelamapi/Next-Reward">Model card and source</a></p>
82
+ </main></html>
model.json ADDED
@@ -0,0 +1,490 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ ],
26
+ [
27
+ 0,
28
+ 0,
29
+ 0,
30
+ 2031,
31
+ 0
32
+ ]
33
+ ],
34
+ [
35
+ [
36
+ 0,
37
+ 0,
38
+ 0,
39
+ 0,
40
+ 1435
41
+ ],
42
+ [
43
+ 0,
44
+ 1399,
45
+ 0,
46
+ 0,
47
+ 0
48
+ ],
49
+ [
50
+ 0,
51
+ 0,
52
+ 1331,
53
+ 0,
54
+ 0
55
+ ],
56
+ [
57
+ 0,
58
+ 0,
59
+ 0,
60
+ 1460,
61
+ 0
62
+ ]
63
+ ],
64
+ [
65
+ [
66
+ 1308,
67
+ 0,
68
+ 0,
69
+ 0,
70
+ 0
71
+ ],
72
+ [
73
+ 0,
74
+ 0,
75
+ 0,
76
+ 0,
77
+ 1336
78
+ ],
79
+ [
80
+ 0,
81
+ 0,
82
+ 1356,
83
+ 0,
84
+ 0
85
+ ],
86
+ [
87
+ 0,
88
+ 0,
89
+ 0,
90
+ 1312,
91
+ 0
92
+ ]
93
+ ],
94
+ [
95
+ [
96
+ 0,
97
+ 0,
98
+ 0,
99
+ 0,
100
+ 928
101
+ ],
102
+ [
103
+ 0,
104
+ 0,
105
+ 0,
106
+ 0,
107
+ 976
108
+ ],
109
+ [
110
+ 0,
111
+ 0,
112
+ 1023,
113
+ 0,
114
+ 0
115
+ ],
116
+ [
117
+ 0,
118
+ 0,
119
+ 0,
120
+ 942,
121
+ 0
122
+ ]
123
+ ],
124
+ [
125
+ [
126
+ 1375,
127
+ 0,
128
+ 0,
129
+ 0,
130
+ 0
131
+ ],
132
+ [
133
+ 0,
134
+ 1349,
135
+ 0,
136
+ 0,
137
+ 0
138
+ ],
139
+ [
140
+ 0,
141
+ 0,
142
+ 0,
143
+ 0,
144
+ 1399
145
+ ],
146
+ [
147
+ 0,
148
+ 0,
149
+ 0,
150
+ 1278,
151
+ 0
152
+ ]
153
+ ],
154
+ [
155
+ [
156
+ 0,
157
+ 0,
158
+ 0,
159
+ 0,
160
+ 834
161
+ ],
162
+ [
163
+ 0,
164
+ 831,
165
+ 0,
166
+ 0,
167
+ 0
168
+ ],
169
+ [
170
+ 0,
171
+ 0,
172
+ 0,
173
+ 0,
174
+ 836
175
+ ],
176
+ [
177
+ 0,
178
+ 0,
179
+ 0,
180
+ 806,
181
+ 0
182
+ ]
183
+ ],
184
+ [
185
+ [
186
+ 1066,
187
+ 0,
188
+ 0,
189
+ 0,
190
+ 0
191
+ ],
192
+ [
193
+ 0,
194
+ 0,
195
+ 0,
196
+ 0,
197
+ 1037
198
+ ],
199
+ [
200
+ 0,
201
+ 0,
202
+ 0,
203
+ 0,
204
+ 989
205
+ ],
206
+ [
207
+ 0,
208
+ 0,
209
+ 0,
210
+ 1069,
211
+ 0
212
+ ]
213
+ ],
214
+ [
215
+ [
216
+ 0,
217
+ 0,
218
+ 0,
219
+ 0,
220
+ 634
221
+ ],
222
+ [
223
+ 0,
224
+ 0,
225
+ 0,
226
+ 0,
227
+ 675
228
+ ],
229
+ [
230
+ 0,
231
+ 0,
232
+ 0,
233
+ 0,
234
+ 687
235
+ ],
236
+ [
237
+ 0,
238
+ 0,
239
+ 0,
240
+ 649,
241
+ 0
242
+ ]
243
+ ],
244
+ [
245
+ [
246
+ 1256,
247
+ 0,
248
+ 0,
249
+ 0,
250
+ 0
251
+ ],
252
+ [
253
+ 0,
254
+ 1280,
255
+ 0,
256
+ 0,
257
+ 0
258
+ ],
259
+ [
260
+ 0,
261
+ 0,
262
+ 1326,
263
+ 0,
264
+ 0
265
+ ],
266
+ [
267
+ 0,
268
+ 0,
269
+ 0,
270
+ 0,
271
+ 1262
272
+ ]
273
+ ],
274
+ [
275
+ [
276
+ 0,
277
+ 0,
278
+ 0,
279
+ 0,
280
+ 1000
281
+ ],
282
+ [
283
+ 0,
284
+ 1053,
285
+ 0,
286
+ 0,
287
+ 0
288
+ ],
289
+ [
290
+ 0,
291
+ 0,
292
+ 972,
293
+ 0,
294
+ 0
295
+ ],
296
+ [
297
+ 0,
298
+ 0,
299
+ 0,
300
+ 0,
301
+ 1003
302
+ ]
303
+ ],
304
+ [
305
+ [
306
+ 752,
307
+ 0,
308
+ 0,
309
+ 0,
310
+ 0
311
+ ],
312
+ [
313
+ 0,
314
+ 0,
315
+ 0,
316
+ 0,
317
+ 775
318
+ ],
319
+ [
320
+ 0,
321
+ 0,
322
+ 752,
323
+ 0,
324
+ 0
325
+ ],
326
+ [
327
+ 0,
328
+ 0,
329
+ 0,
330
+ 0,
331
+ 804
332
+ ]
333
+ ],
334
+ [
335
+ [
336
+ 0,
337
+ 0,
338
+ 0,
339
+ 0,
340
+ 660
341
+ ],
342
+ [
343
+ 0,
344
+ 0,
345
+ 0,
346
+ 0,
347
+ 645
348
+ ],
349
+ [
350
+ 0,
351
+ 0,
352
+ 657,
353
+ 0,
354
+ 0
355
+ ],
356
+ [
357
+ 0,
358
+ 0,
359
+ 0,
360
+ 0,
361
+ 663
362
+ ]
363
+ ],
364
+ [
365
+ [
366
+ 1098,
367
+ 0,
368
+ 0,
369
+ 0,
370
+ 0
371
+ ],
372
+ [
373
+ 0,
374
+ 1052,
375
+ 0,
376
+ 0,
377
+ 0
378
+ ],
379
+ [
380
+ 0,
381
+ 0,
382
+ 0,
383
+ 0,
384
+ 1036
385
+ ],
386
+ [
387
+ 0,
388
+ 0,
389
+ 0,
390
+ 0,
391
+ 1065
392
+ ]
393
+ ],
394
+ [
395
+ [
396
+ 0,
397
+ 0,
398
+ 0,
399
+ 0,
400
+ 643
401
+ ],
402
+ [
403
+ 0,
404
+ 646,
405
+ 0,
406
+ 0,
407
+ 0
408
+ ],
409
+ [
410
+ 0,
411
+ 0,
412
+ 0,
413
+ 0,
414
+ 668
415
+ ],
416
+ [
417
+ 0,
418
+ 0,
419
+ 0,
420
+ 0,
421
+ 648
422
+ ]
423
+ ],
424
+ [
425
+ [
426
+ 627,
427
+ 0,
428
+ 0,
429
+ 0,
430
+ 0
431
+ ],
432
+ [
433
+ 0,
434
+ 0,
435
+ 0,
436
+ 0,
437
+ 666
438
+ ],
439
+ [
440
+ 0,
441
+ 0,
442
+ 0,
443
+ 0,
444
+ 683
445
+ ],
446
+ [
447
+ 0,
448
+ 0,
449
+ 0,
450
+ 0,
451
+ 680
452
+ ]
453
+ ],
454
+ [
455
+ [
456
+ 0,
457
+ 0,
458
+ 0,
459
+ 0,
460
+ 525
461
+ ],
462
+ [
463
+ 0,
464
+ 0,
465
+ 0,
466
+ 0,
467
+ 578
468
+ ],
469
+ [
470
+ 0,
471
+ 0,
472
+ 0,
473
+ 0,
474
+ 584
475
+ ],
476
+ [
477
+ 0,
478
+ 0,
479
+ 0,
480
+ 0,
481
+ 545
482
+ ]
483
+ ]
484
+ ],
485
+ "rewards": [
486
+ -0.019999999999986806,
487
+ 1.0,
488
+ -1.0
489
+ ]
490
+ }
next_reward.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Inference for Next Reward. Boards use . (floor), # (wall), X (pit), G (goal)."""
2
+ import json
3
+ from pathlib import Path
4
+ import numpy as np
5
+
6
+ MOVES = ((1,0),(0,1),(-1,0),(0,-1),(0,0))
7
+ ACTIONS = ("down","right","up","left")
8
+
9
+ def geometry(board):
10
+ if not board or not board[0] or any(len(row)!=len(board[0]) for row in board):
11
+ raise ValueError("Expected a nonempty rectangular board")
12
+ if sum(row.count("G") for row in board)!=1 or any(set(row)-set(".X#G") for row in board):
13
+ raise ValueError("Use exactly one G and symbols . # X")
14
+ grid=np.array([list(row) for row in board])
15
+ h,w=grid.shape
16
+ y,x=np.indices((h,w))
17
+ dest=[]
18
+ mask=np.zeros((h,w),dtype=int)
19
+ for i,(dy,dx) in enumerate(MOVES):
20
+ ny,nx=y+dy,x+dx
21
+ blocked=(ny<0)|(ny>=h)|(nx<0)|(nx>=w)|(grid[ny.clip(0,h-1),nx.clip(0,w-1)]=="#")
22
+ if i<4: mask |= blocked.astype(int)<<i
23
+ dest.append(np.where(blocked,y*w+x,ny*w+nx))
24
+ return grid,mask,np.stack(dest,axis=-1)
25
+
26
+ class NextReward:
27
+ def __init__(self,counts,rewards):
28
+ counts=np.asarray(counts,dtype=float)
29
+ self.rewards=np.asarray(rewards,dtype=float)
30
+ if counts.shape!=(16,4,5) or not np.isfinite(counts).all() or np.any(counts<0) or np.any(counts.sum(-1)==0):
31
+ raise ValueError("Missing or invalid transition counts")
32
+ if self.rewards.shape!=(3,) or not np.isfinite(self.rewards).all():
33
+ raise ValueError("Invalid learned rewards")
34
+ self.counts=counts
35
+ self.prob=counts/counts.sum(-1,keepdims=True)
36
+
37
+ @classmethod
38
+ def load(cls,path):
39
+ d=json.loads(Path(path).read_text())
40
+ if d["format"]!="next-reward-v1": raise ValueError("Unsupported model format")
41
+ return cls(d["counts"],d["rewards"])
42
+
43
+ def save(self,path):
44
+ Path(path).write_text(json.dumps(dict(format="next-reward-v1",counts=self.counts.astype(int).tolist(),
45
+ rewards=self.rewards.tolist()),indent=1))
46
+
47
+ def policy(self,board):
48
+ grid,mask,dest=geometry(board)
49
+ reward=np.where(grid=="G",self.rewards[1],np.where(grid=="X",self.rewards[2],self.rewards[0]))
50
+ value=np.zeros(grid.shape)
51
+ for _ in range(grid.size+1):
52
+ outcomes=(reward+0.98*value).ravel()[dest]
53
+ q=np.einsum("ijak,ijk->ija",self.prob[mask],outcomes)
54
+ updated=np.where(grid==".",q.max(-1),0.)
55
+ if np.max(np.abs(updated-value))<1e-12: break
56
+ value=updated
57
+ return np.where(grid==".",q.argmax(-1),-1)
58
+