omaryashraf commited on
Commit
6e845f4
·
verified ·
1 Parent(s): 9abdea0

Upload folder using huggingface_hub

Browse files
envs/warehouse_env/server/app.py CHANGED
@@ -39,6 +39,51 @@ app = create_app(warehouse_env, WarehouseAction, WarehouseObservation, env_name=
39
 
40
 
41
  # Add custom render endpoints
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  @app.get("/render")
43
  async def render():
44
  """Get ASCII visualization of warehouse state."""
 
39
 
40
 
41
  # Add custom render endpoints
42
+ @app.post("/set-difficulty")
43
+ async def set_difficulty(request: dict):
44
+ """Change the difficulty level and reset the environment."""
45
+ try:
46
+ difficulty = int(request.get("difficulty", 2))
47
+ if difficulty < 1 or difficulty > 5:
48
+ return JSONResponse(
49
+ status_code=400,
50
+ content={"error": "Difficulty must be between 1 and 5"}
51
+ )
52
+
53
+ # Recreate the warehouse environment with new difficulty
54
+ global warehouse_env
55
+ warehouse_env = WarehouseEnvironment(
56
+ difficulty_level=difficulty,
57
+ grid_width=None,
58
+ grid_height=None,
59
+ num_packages=None,
60
+ max_steps=None,
61
+ random_seed=None,
62
+ )
63
+
64
+ # Reset the environment
65
+ observation = warehouse_env.reset()
66
+
67
+ return JSONResponse(content={
68
+ "success": True,
69
+ "difficulty": difficulty,
70
+ "grid_size": (warehouse_env.grid_width, warehouse_env.grid_height),
71
+ "num_packages": warehouse_env.num_packages,
72
+ "max_steps": warehouse_env.max_steps,
73
+ "observation": {
74
+ "step_count": observation.step_count,
75
+ "packages_delivered": observation.packages_delivered,
76
+ "total_packages": observation.total_packages,
77
+ "robot_position": observation.robot_position,
78
+ }
79
+ })
80
+ except Exception as e:
81
+ return JSONResponse(
82
+ status_code=500,
83
+ content={"error": f"Failed to set difficulty: {str(e)}"}
84
+ )
85
+
86
+
87
  @app.get("/render")
88
  async def render():
89
  """Get ASCII visualization of warehouse state."""
envs/warehouse_env/server/demo.html CHANGED
@@ -196,6 +196,7 @@
196
  <option value="4">Level 4 - Hard (15×15, 5 packages)</option>
197
  <option value="5">Level 5 - Expert (20×20, 8 packages)</option>
198
  </select>
 
199
  </div>
200
 
201
  <div class="stats">
@@ -419,6 +420,34 @@
419
  document.getElementById('stopBtn').addEventListener('click', stopAutoPlay);
420
  document.getElementById('resetBtn').addEventListener('click', reset);
421
  document.getElementById('resetManualBtn').addEventListener('click', reset);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
 
423
  // Initialize on load
424
  reset();
 
196
  <option value="4">Level 4 - Hard (15×15, 5 packages)</option>
197
  <option value="5">Level 5 - Expert (20×20, 8 packages)</option>
198
  </select>
199
+ <button class="btn-secondary" id="applyDifficulty">Apply</button>
200
  </div>
201
 
202
  <div class="stats">
 
420
  document.getElementById('stopBtn').addEventListener('click', stopAutoPlay);
421
  document.getElementById('resetBtn').addEventListener('click', reset);
422
  document.getElementById('resetManualBtn').addEventListener('click', reset);
423
+ document.getElementById('applyDifficulty').addEventListener('click', async () => {
424
+ stopAutoPlay();
425
+ const difficulty = parseInt(document.getElementById('difficulty').value);
426
+ showMessage(`Changing to difficulty level ${difficulty}...`, 'info');
427
+
428
+ try {
429
+ const response = await fetch(`${baseUrl}/set-difficulty`, {
430
+ method: 'POST',
431
+ headers: { 'Content-Type': 'application/json' },
432
+ body: JSON.stringify({ difficulty: difficulty })
433
+ });
434
+ const data = await response.json();
435
+
436
+ if (data.success) {
437
+ showMessage(
438
+ `Difficulty changed! Grid: ${data.grid_size[0]}×${data.grid_size[1]}, ` +
439
+ `Packages: ${data.num_packages}, Max Steps: ${data.max_steps}`,
440
+ 'success'
441
+ );
442
+ updateStats(data.observation);
443
+ await refreshVisualization();
444
+ } else {
445
+ showMessage('Failed to change difficulty: ' + data.error, 'error');
446
+ }
447
+ } catch (error) {
448
+ showMessage('Error changing difficulty: ' + error.message, 'error');
449
+ }
450
+ });
451
 
452
  // Initialize on load
453
  reset();
server/app.py CHANGED
@@ -39,6 +39,51 @@ app = create_app(warehouse_env, WarehouseAction, WarehouseObservation, env_name=
39
 
40
 
41
  # Add custom render endpoints
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  @app.get("/render")
43
  async def render():
44
  """Get ASCII visualization of warehouse state."""
 
39
 
40
 
41
  # Add custom render endpoints
42
+ @app.post("/set-difficulty")
43
+ async def set_difficulty(request: dict):
44
+ """Change the difficulty level and reset the environment."""
45
+ try:
46
+ difficulty = int(request.get("difficulty", 2))
47
+ if difficulty < 1 or difficulty > 5:
48
+ return JSONResponse(
49
+ status_code=400,
50
+ content={"error": "Difficulty must be between 1 and 5"}
51
+ )
52
+
53
+ # Recreate the warehouse environment with new difficulty
54
+ global warehouse_env
55
+ warehouse_env = WarehouseEnvironment(
56
+ difficulty_level=difficulty,
57
+ grid_width=None,
58
+ grid_height=None,
59
+ num_packages=None,
60
+ max_steps=None,
61
+ random_seed=None,
62
+ )
63
+
64
+ # Reset the environment
65
+ observation = warehouse_env.reset()
66
+
67
+ return JSONResponse(content={
68
+ "success": True,
69
+ "difficulty": difficulty,
70
+ "grid_size": (warehouse_env.grid_width, warehouse_env.grid_height),
71
+ "num_packages": warehouse_env.num_packages,
72
+ "max_steps": warehouse_env.max_steps,
73
+ "observation": {
74
+ "step_count": observation.step_count,
75
+ "packages_delivered": observation.packages_delivered,
76
+ "total_packages": observation.total_packages,
77
+ "robot_position": observation.robot_position,
78
+ }
79
+ })
80
+ except Exception as e:
81
+ return JSONResponse(
82
+ status_code=500,
83
+ content={"error": f"Failed to set difficulty: {str(e)}"}
84
+ )
85
+
86
+
87
  @app.get("/render")
88
  async def render():
89
  """Get ASCII visualization of warehouse state."""
server/demo.html CHANGED
@@ -196,6 +196,7 @@
196
  <option value="4">Level 4 - Hard (15×15, 5 packages)</option>
197
  <option value="5">Level 5 - Expert (20×20, 8 packages)</option>
198
  </select>
 
199
  </div>
200
 
201
  <div class="stats">
@@ -419,6 +420,34 @@
419
  document.getElementById('stopBtn').addEventListener('click', stopAutoPlay);
420
  document.getElementById('resetBtn').addEventListener('click', reset);
421
  document.getElementById('resetManualBtn').addEventListener('click', reset);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
 
423
  // Initialize on load
424
  reset();
 
196
  <option value="4">Level 4 - Hard (15×15, 5 packages)</option>
197
  <option value="5">Level 5 - Expert (20×20, 8 packages)</option>
198
  </select>
199
+ <button class="btn-secondary" id="applyDifficulty">Apply</button>
200
  </div>
201
 
202
  <div class="stats">
 
420
  document.getElementById('stopBtn').addEventListener('click', stopAutoPlay);
421
  document.getElementById('resetBtn').addEventListener('click', reset);
422
  document.getElementById('resetManualBtn').addEventListener('click', reset);
423
+ document.getElementById('applyDifficulty').addEventListener('click', async () => {
424
+ stopAutoPlay();
425
+ const difficulty = parseInt(document.getElementById('difficulty').value);
426
+ showMessage(`Changing to difficulty level ${difficulty}...`, 'info');
427
+
428
+ try {
429
+ const response = await fetch(`${baseUrl}/set-difficulty`, {
430
+ method: 'POST',
431
+ headers: { 'Content-Type': 'application/json' },
432
+ body: JSON.stringify({ difficulty: difficulty })
433
+ });
434
+ const data = await response.json();
435
+
436
+ if (data.success) {
437
+ showMessage(
438
+ `Difficulty changed! Grid: ${data.grid_size[0]}×${data.grid_size[1]}, ` +
439
+ `Packages: ${data.num_packages}, Max Steps: ${data.max_steps}`,
440
+ 'success'
441
+ );
442
+ updateStats(data.observation);
443
+ await refreshVisualization();
444
+ } else {
445
+ showMessage('Failed to change difficulty: ' + data.error, 'error');
446
+ }
447
+ } catch (error) {
448
+ showMessage('Error changing difficulty: ' + error.message, 'error');
449
+ }
450
+ });
451
 
452
  // Initialize on load
453
  reset();