Spaces:
Sleeping
Sleeping
Download app.py from MRM5/Autonomous_Agentic_AI: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/spaces/MRM5/Autonomous_Agentic_AI/resolve/main/app.py
- Command line
-
hf download hf://spaces/MRM5/Autonomous_Agentic_AI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MRM5/Autonomous_Agentic_AI/resolve/main/app.py
2.96 kB
| import gradio as gr | |
| import numpy as np | |
| import pickle | |
| import matplotlib.pyplot as plt | |
| # 1. LOAD THE BRAIN | |
| # This assumes 'agent_q_table.pkl' is in your root directory | |
| with open('agent_q_table.pkl', 'rb') as f: | |
| q_table = pickle.load(f) | |
| # Environment parameters from the project script | |
| grid_size = (5, 5) | |
| goal = (4, 4) | |
| pits = [(1, 1), (2, 2), (3, 3)] | |
| walls = [(0, 1), (1, 0)] | |
| actions = ['up', 'down', 'left', 'right'] | |
| # 2. SIMULATION LOGIC WITH VALIDATION | |
| def simulate_agent(start_pos): | |
| try: | |
| # Parse and validate coordinates (Must be 0-4 for a 5x5 grid) | |
| r, c = map(int, start_pos.split(',')) | |
| if not (0 <= r < 5 and 0 <= c < 5): | |
| return None, " Error: Coordinates must be between 0 and 4 (e.g., '0,2')." | |
| current_state = (r, c) | |
| except Exception: | |
| return None, " Invalid format. Please use 'row,col' (e.g., 0,0)." | |
| path = [current_state] | |
| steps = 0 | |
| done = False | |
| # Run simulation using the learned Q-table | |
| while not done and steps < 20: | |
| action_idx = np.argmax(q_table[current_state[0], current_state[1]]) | |
| action = actions[action_idx] | |
| r, c = current_state | |
| if action == 'up': r = max(0, r - 1) | |
| elif action == 'down': r = min(grid_size[0] - 1, r + 1) | |
| elif action == 'left': c = max(0, c - 1) | |
| elif action == 'right': c = min(grid_size[1] - 1, c + 1) | |
| new_state = (r, c) | |
| if new_state in walls: new_state = current_state # Stay put if hitting a wall | |
| current_state = new_state | |
| path.append(current_state) | |
| if current_state == goal or current_state in pits: | |
| done = True | |
| steps += 1 | |
| # 3. VISUALIZATION | |
| fig, ax = plt.subplots(figsize=(5, 5)) | |
| grid = np.zeros(grid_size) | |
| for p in pits: grid[p] = -1 # Pits marked as negative | |
| grid[goal] = 1 # Goal marked as positive | |
| ax.imshow(grid, cmap='RdYlGn', origin='upper') | |
| # Plot the agent's path | |
| path_r, path_c = zip(*path) | |
| ax.plot(path_c, path_r, marker='o', color='blue', linewidth=2, label='Agent Path') | |
| status = "Success! " if path[-1] == goal else "Fell in a Pit " | |
| ax.set_title(f"Simulation: {status}") | |
| plt.legend() | |
| return fig, f"Steps taken: {steps} | Full Path: {path}" | |
| # 4. AESTHETIC INTERFACE | |
| # Uses gr.themes.Soft() for a clean look and custom CSS to hide the share button | |
| demo = gr.Interface( | |
| fn=simulate_agent, | |
| inputs=gr.Textbox(label="Starting Position (row,col)", value="0,0", placeholder="Enter 0-4 coordinates"), | |
| outputs=[gr.Plot(label="Navigation Map"), gr.Text(label="Path Details")], | |
| title=" Autonomous Grid Navigator", | |
| description="The agent uses Reinforcement Learning (Q-Learning) to navigate to the green goal while avoiding pits.", | |
| theme=gr.themes.Soft(), | |
| css=".share-button { display: none !important; }" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |