Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,6 @@ actions = ['up', 'down', 'left', 'right']
|
|
| 16 |
|
| 17 |
# 2. SIMULATION LOGIC
|
| 18 |
def simulate_agent(start_pos):
|
| 19 |
-
# Convert string input like "0,0" to tuple (0,0)
|
| 20 |
try:
|
| 21 |
current_state = tuple(map(int, start_pos.split(',')))
|
| 22 |
except:
|
|
@@ -26,12 +25,11 @@ def simulate_agent(start_pos):
|
|
| 26 |
steps = 0
|
| 27 |
done = False
|
| 28 |
|
|
|
|
| 29 |
while not done and steps < 20:
|
| 30 |
-
# Get best action from Q-table
|
| 31 |
action_idx = np.argmax(q_table[current_state[0], current_state[1]])
|
| 32 |
action = actions[action_idx]
|
| 33 |
|
| 34 |
-
# Calculate new state
|
| 35 |
r, c = current_state
|
| 36 |
if action == 'up': r = max(0, r - 1)
|
| 37 |
elif action == 'down': r = min(grid_size[0] - 1, r + 1)
|
|
@@ -56,22 +54,23 @@ def simulate_agent(start_pos):
|
|
| 56 |
|
| 57 |
ax.imshow(grid, cmap='RdYlGn', origin='upper')
|
| 58 |
|
| 59 |
-
#
|
| 60 |
path_r, path_c = zip(*path)
|
| 61 |
ax.plot(path_c, path_r, marker='o', color='blue', linewidth=2, label='Agent Path')
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
plt.legend()
|
| 65 |
|
| 66 |
-
return fig, f"
|
| 67 |
|
| 68 |
# 4. GRADIO INTERFACE
|
| 69 |
demo = gr.Interface(
|
| 70 |
fn=simulate_agent,
|
| 71 |
inputs=gr.Textbox(label="Starting Position (row,col)", value="0,0"),
|
| 72 |
-
outputs=[gr.Plot(label="
|
| 73 |
title="🤖 RL Gridworld Navigator",
|
| 74 |
-
description="
|
| 75 |
theme=gr.themes.Soft(),
|
| 76 |
css=".share-button { display: none !important; }"
|
| 77 |
)
|
|
|
|
| 16 |
|
| 17 |
# 2. SIMULATION LOGIC
|
| 18 |
def simulate_agent(start_pos):
|
|
|
|
| 19 |
try:
|
| 20 |
current_state = tuple(map(int, start_pos.split(',')))
|
| 21 |
except:
|
|
|
|
| 25 |
steps = 0
|
| 26 |
done = False
|
| 27 |
|
| 28 |
+
# Run the simulation using the learned Q-table
|
| 29 |
while not done and steps < 20:
|
|
|
|
| 30 |
action_idx = np.argmax(q_table[current_state[0], current_state[1]])
|
| 31 |
action = actions[action_idx]
|
| 32 |
|
|
|
|
| 33 |
r, c = current_state
|
| 34 |
if action == 'up': r = max(0, r - 1)
|
| 35 |
elif action == 'down': r = min(grid_size[0] - 1, r + 1)
|
|
|
|
| 54 |
|
| 55 |
ax.imshow(grid, cmap='RdYlGn', origin='upper')
|
| 56 |
|
| 57 |
+
# Plot the agent's path
|
| 58 |
path_r, path_c = zip(*path)
|
| 59 |
ax.plot(path_c, path_r, marker='o', color='blue', linewidth=2, label='Agent Path')
|
| 60 |
|
| 61 |
+
status = "Success! 🏁" if path[-1] == goal else "Failed/Fell 🕳️"
|
| 62 |
+
ax.set_title(f"Status: {status}")
|
| 63 |
plt.legend()
|
| 64 |
|
| 65 |
+
return fig, f"Steps taken: {steps} | Full Path: {path}"
|
| 66 |
|
| 67 |
# 4. GRADIO INTERFACE
|
| 68 |
demo = gr.Interface(
|
| 69 |
fn=simulate_agent,
|
| 70 |
inputs=gr.Textbox(label="Starting Position (row,col)", value="0,0"),
|
| 71 |
+
outputs=[gr.Plot(label="Navigation Map"), gr.Text(label="Path Details")],
|
| 72 |
title="🤖 RL Gridworld Navigator",
|
| 73 |
+
description="The agent uses its learned Q-table to find the green goal. Enter a start point like '0,0' or '4,0'.",
|
| 74 |
theme=gr.themes.Soft(),
|
| 75 |
css=".share-button { display: none !important; }"
|
| 76 |
)
|