Spaces:
Build error
Build error
Commit
·
fbd53e3
1
Parent(s):
785f73c
more
Browse files- app.py +53 -9
- draft_2.py +13 -0
- draft_gradio_with_animation.py +42 -0
- drafts_1.py +37 -0
- plot_functions.py +24 -0
- warehouse_env.py +147 -0
app.py
CHANGED
|
@@ -1,18 +1,62 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def process_text(text):
|
| 4 |
-
return text.upper()
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
with gr.Row():
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
| 18 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import matplotlib.animation as animation
|
| 5 |
+
import tempfile
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def create_animation():
|
| 9 |
+
fig, ax = plt.subplots(figsize=(7, 7))
|
| 10 |
+
xdata, ydata = [], []
|
| 11 |
+
ln, = plt.plot([], [], 'b-', animated=True)
|
| 12 |
|
| 13 |
+
def init():
|
| 14 |
+
ax.set_xlim(0, 2*np.pi)
|
| 15 |
+
ax.set_ylim(-1.1, 1.1)
|
| 16 |
+
return ln,
|
| 17 |
+
|
| 18 |
+
def update(frame):
|
| 19 |
+
xdata.append(frame)
|
| 20 |
+
ydata.append(np.sin(frame))
|
| 21 |
+
ln.set_data(xdata, ydata)
|
| 22 |
+
return ln,
|
| 23 |
+
|
| 24 |
+
ani = animation.FuncAnimation(
|
| 25 |
+
fig, update, frames=np.linspace(0, 2*np.pi, 100),
|
| 26 |
+
init_func=init, blit=True, repeat=False
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Save to MP4
|
| 30 |
+
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 31 |
+
ani.save(temp_video.name, writer='ffmpeg', fps=20)
|
| 32 |
+
plt.close(fig)
|
| 33 |
+
|
| 34 |
+
return temp_video.name
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def load_image_on_start():
|
| 39 |
+
return np.random.rand(700, 700)
|
| 40 |
+
# return None
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
gr.Markdown("## Agent Control with Language")
|
| 44 |
+
gr.Markdown('## Say the agent where to go and what to do')
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
+
with gr.Column():
|
| 48 |
+
request_audio = gr.Audio()
|
| 49 |
+
send_btn = gr.Button(value='Send Request')
|
| 50 |
+
request_text = gr.Textbox(label="Request:", lines=2, interactive=False)
|
| 51 |
+
request_target = gr.Textbox(label='Target:', lines=2)
|
| 52 |
+
request_plan = gr.Textbox(label='Plan status:', lines=2)
|
| 53 |
+
with gr.Column():
|
| 54 |
+
output_env = gr.Video(label="Env:", autoplay=True)
|
| 55 |
|
| 56 |
+
# EVENTS:
|
| 57 |
+
# gr.on(triggers=["load"], fn=load_image_on_start, outputs=output_env_image)
|
| 58 |
+
# demo.load(fn=load_image_on_start, outputs=output_env_image)
|
| 59 |
+
demo.load(fn=create_animation, outputs=output_env)
|
| 60 |
|
| 61 |
+
demo.launch()
|
| 62 |
demo.launch()
|
draft_2.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
# angle_deg = 350 # for example
|
| 4 |
+
# angle_rad = np.deg2rad(angle_deg)
|
| 5 |
+
#
|
| 6 |
+
# vector = np.array([np.cos(angle_rad), np.sin(angle_rad)])
|
| 7 |
+
# print(vector)
|
| 8 |
+
|
| 9 |
+
input_angle = 0.5
|
| 10 |
+
angle_rad = 2 * np.pi * input_angle
|
| 11 |
+
vector_2 = np.array([np.cos(angle_rad), np.sin(angle_rad)])
|
| 12 |
+
print(vector_2)
|
| 13 |
+
|
draft_gradio_with_animation.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt
|
| 2 |
+
import matplotlib.animation as animation
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
def create_animation():
|
| 8 |
+
fig, ax = plt.subplots()
|
| 9 |
+
xdata, ydata = [], []
|
| 10 |
+
ln, = plt.plot([], [], 'b-', animated=True)
|
| 11 |
+
|
| 12 |
+
def init():
|
| 13 |
+
ax.set_xlim(0, 2*np.pi)
|
| 14 |
+
ax.set_ylim(-1.1, 1.1)
|
| 15 |
+
return ln,
|
| 16 |
+
|
| 17 |
+
def update(frame):
|
| 18 |
+
xdata.append(frame)
|
| 19 |
+
ydata.append(np.sin(frame))
|
| 20 |
+
ln.set_data(xdata, ydata)
|
| 21 |
+
return ln,
|
| 22 |
+
|
| 23 |
+
ani = animation.FuncAnimation(
|
| 24 |
+
fig, update, frames=np.linspace(0, 2*np.pi, 100),
|
| 25 |
+
init_func=init, blit=True, repeat=False
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Save to MP4
|
| 29 |
+
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 30 |
+
ani.save(temp_video.name, writer='ffmpeg', fps=20)
|
| 31 |
+
plt.close(fig)
|
| 32 |
+
|
| 33 |
+
return temp_video.name
|
| 34 |
+
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
with gr.Row():
|
| 37 |
+
btn = gr.Button("Generate Animation")
|
| 38 |
+
vid = gr.Video(label="Animated Plot", autoplay=True)
|
| 39 |
+
|
| 40 |
+
btn.click(fn=create_animation, outputs=vid)
|
| 41 |
+
|
| 42 |
+
demo.launch()
|
drafts_1.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import vmas
|
| 2 |
+
|
| 3 |
+
# Create the environment
|
| 4 |
+
env = vmas.make_env(
|
| 5 |
+
# scenario="waterfall", # can be scenario name or BaseScenario class
|
| 6 |
+
scenario="dropout",
|
| 7 |
+
# scenario="transport",
|
| 8 |
+
# scenario="wheel",
|
| 9 |
+
# scenario="drone",
|
| 10 |
+
# scenario="kinematic_bicycle",
|
| 11 |
+
# scenario="road_traffic",
|
| 12 |
+
# scenario="multi_give_way",
|
| 13 |
+
# scenario="football",
|
| 14 |
+
# scenario="give_way",
|
| 15 |
+
# scenario="simple",
|
| 16 |
+
# scenario="simple_adversary",
|
| 17 |
+
num_envs=1,
|
| 18 |
+
device="cpu", # Or "cuda" for GPU
|
| 19 |
+
continuous_actions=True,
|
| 20 |
+
max_steps=None, # Defines the horizon. None is infinite horizon.
|
| 21 |
+
seed=None, # Seed of the environment
|
| 22 |
+
n_agents=1 # Additional arguments you want to pass to the scenario
|
| 23 |
+
)
|
| 24 |
+
# Reset itr
|
| 25 |
+
obs = env.reset()
|
| 26 |
+
|
| 27 |
+
# Step it with deterministic actions (all agents take their maximum range action)
|
| 28 |
+
for i in range(1000):
|
| 29 |
+
obs, rews, dones, info = env.step(env.get_random_actions())
|
| 30 |
+
print(i)
|
| 31 |
+
env.render(
|
| 32 |
+
# mode="rgb_array", # "rgb_array" returns image, "human" renders in display
|
| 33 |
+
mode="human", # "rgb_array" returns image, "human" renders in display
|
| 34 |
+
# agent_index_focus=4, # If None keep all agents in camera, else focus camera on specific agent
|
| 35 |
+
# index=0, # Index of batched environment to render
|
| 36 |
+
# visualize_when_rgb=True, # Also run human visualization when mode=="rgb_array"
|
| 37 |
+
)
|
plot_functions.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt
|
| 2 |
+
import matplotlib
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def plot_env(ax, info):
|
| 8 |
+
ax.cla()
|
| 9 |
+
env = info['env']
|
| 10 |
+
ax.plot([1, 1], [1, 2], '.', color='b', alpha=0.5, linewidth=5, markersize=20)
|
| 11 |
+
# ax.set_xlim([min(n_agents_list) - 20, max(n_agents_list) + 20])
|
| 12 |
+
ax.set_xlim([0, 100])
|
| 13 |
+
ax.set_ylim([0, 100])
|
| 14 |
+
# ax.set_xticks(n_agents_list)
|
| 15 |
+
# ax.set_xlabel('N agents', fontsize=27)
|
| 16 |
+
# ax.set_ylabel('Success Rate', fontsize=27)
|
| 17 |
+
# ax.set_title(f'{img_dir[:-4]} Map | time limit: {time_to_think_limit} sec.')
|
| 18 |
+
# set_plot_title(ax, f'{img_dir[:-4]} Map | time limit: {time_to_think_limit} sec.', size=11)
|
| 19 |
+
ax.set_title(f'Warehouse', fontweight="bold", size=30)
|
| 20 |
+
# set_legend(ax, size=18)
|
| 21 |
+
# labelsize = 20
|
| 22 |
+
# ax.xaxis.set_tick_params(labelsize=labelsize)
|
| 23 |
+
# ax.yaxis.set_tick_params(labelsize=labelsize)
|
| 24 |
+
plt.tight_layout()
|
warehouse_env.py
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import gymnasium as gym
|
| 3 |
+
import numpy as np
|
| 4 |
+
from gymnasium import spaces
|
| 5 |
+
from stable_baselines3.common.env_checker import check_env
|
| 6 |
+
from stable_baselines3 import PPO
|
| 7 |
+
from stable_baselines3.common.env_util import make_vec_env
|
| 8 |
+
from plot_functions import *
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class WarehouseEnv(gym.Env):
|
| 12 |
+
"""
|
| 13 |
+
WarehouseEnv Environment that follows gym interface.
|
| 14 |
+
No inertia.
|
| 15 |
+
State:
|
| 16 |
+
x_a, y_a - current position [0, 100], [0, 100]
|
| 17 |
+
x_rel, y_rel - relative to target position [0, 100], [0, 100]
|
| 18 |
+
Action:
|
| 19 |
+
alpha - an angle (direction) [0, 1]
|
| 20 |
+
v - velocity [0, 1]
|
| 21 |
+
Reward:
|
| 22 |
+
-1 -> not in target radius
|
| 23 |
+
10 -> in target radius
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
metadata = {"render_modes": ["human"], "render_fps": 30}
|
| 27 |
+
|
| 28 |
+
def __init__(self, render_mode):
|
| 29 |
+
super().__init__()
|
| 30 |
+
self.render_mode = render_mode
|
| 31 |
+
self.to_render = self.render_mode == 'human'
|
| 32 |
+
self.ACTIONS: int = 2
|
| 33 |
+
self.N_CHANNELS: int = 4
|
| 34 |
+
self.SIDE: int = 100
|
| 35 |
+
self.RADIUS_COVERAGE: int = 5
|
| 36 |
+
self.MAX_STEPS: int = 200
|
| 37 |
+
self.DIAG: float = math.sqrt(self.SIDE ** 2 + self.SIDE ** 2)
|
| 38 |
+
self.action_space = spaces.Box(low=-1.0, high=1.0, shape=(self.ACTIONS,), dtype=np.float32)
|
| 39 |
+
self.observation_space = spaces.Box(low=-1, high=1, shape=(self.N_CHANNELS,), dtype=np.float64)
|
| 40 |
+
self.field = np.zeros((self.SIDE, self.SIDE))
|
| 41 |
+
|
| 42 |
+
# Agent
|
| 43 |
+
self.agent_x = None
|
| 44 |
+
self.agent_y = None
|
| 45 |
+
self.goal_x = None
|
| 46 |
+
self.goal_y = None
|
| 47 |
+
self.step_counter = None
|
| 48 |
+
self.terminated = True
|
| 49 |
+
self.truncated = True
|
| 50 |
+
|
| 51 |
+
# to render
|
| 52 |
+
if self.to_render:
|
| 53 |
+
self.fig, self.ax = plt.subplots(2, 2, figsize=(17, 10))
|
| 54 |
+
|
| 55 |
+
@property
|
| 56 |
+
def rel_x(self) -> int:
|
| 57 |
+
return self.agent_x - self.goal_x
|
| 58 |
+
|
| 59 |
+
@property
|
| 60 |
+
def rel_y(self) -> int:
|
| 61 |
+
return self.agent_y - self.goal_y
|
| 62 |
+
|
| 63 |
+
def reset(self, seed=None, options=None):
|
| 64 |
+
self.agent_x = np.random.uniform(0, self.SIDE)
|
| 65 |
+
self.agent_y = np.random.uniform(0, self.SIDE)
|
| 66 |
+
self.goal_x = np.random.uniform(0, self.SIDE)
|
| 67 |
+
self.goal_y = np.random.uniform(0, self.SIDE)
|
| 68 |
+
self.step_counter = 0
|
| 69 |
+
self.terminated = False
|
| 70 |
+
self.truncated = False
|
| 71 |
+
observation = np.array([self.agent_x / self.SIDE, self.agent_y / self.SIDE, self.rel_x / self.SIDE, self.rel_y / self.SIDE])
|
| 72 |
+
info = {}
|
| 73 |
+
return observation, info
|
| 74 |
+
|
| 75 |
+
def step(self, action):
|
| 76 |
+
if self.terminated:
|
| 77 |
+
raise RuntimeError('reset the env')
|
| 78 |
+
|
| 79 |
+
# --- execute action ---
|
| 80 |
+
input_angle, input_vel = action
|
| 81 |
+
# reshape between 0 and 1
|
| 82 |
+
input_angle = (input_angle + 1) / 2
|
| 83 |
+
input_vel = (input_vel + 1) / 2
|
| 84 |
+
# execute
|
| 85 |
+
angle_rad = 2 * np.pi * input_angle
|
| 86 |
+
mov_x, mov_y = np.array([np.cos(angle_rad), np.sin(angle_rad)])
|
| 87 |
+
self.agent_x += input_vel * mov_x
|
| 88 |
+
self.agent_y += input_vel * mov_y
|
| 89 |
+
|
| 90 |
+
rel_x, rel_y = self.rel_x, self.rel_y
|
| 91 |
+
distance = math.sqrt(rel_x**2 + rel_y**2)
|
| 92 |
+
|
| 93 |
+
# obs
|
| 94 |
+
observation = np.array([self.agent_x / self.SIDE, self.agent_y / self.SIDE, rel_x / self.SIDE, rel_y / self.SIDE])
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# terminated + reward
|
| 98 |
+
if not (0 <= self.agent_x < self.SIDE) or not (0 <= self.agent_y < self.SIDE):
|
| 99 |
+
self.terminated = True
|
| 100 |
+
reward = -10
|
| 101 |
+
elif distance < self.RADIUS_COVERAGE:
|
| 102 |
+
self.terminated = True
|
| 103 |
+
reward = 10
|
| 104 |
+
else:
|
| 105 |
+
reward = - (distance / self.DIAG)
|
| 106 |
+
|
| 107 |
+
# truncated
|
| 108 |
+
if self.step_counter > self.MAX_STEPS:
|
| 109 |
+
self.truncated = True
|
| 110 |
+
self.step_counter += 1
|
| 111 |
+
|
| 112 |
+
# info
|
| 113 |
+
info = {}
|
| 114 |
+
return observation, reward, self.terminated, self.truncated, info
|
| 115 |
+
|
| 116 |
+
def render(self):
|
| 117 |
+
plot_env(self.ax[0, 0], info={'env': self})
|
| 118 |
+
plt.tight_layout()
|
| 119 |
+
plt.pause(0.01)
|
| 120 |
+
|
| 121 |
+
def close(self):
|
| 122 |
+
pass
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def main():
|
| 126 |
+
env = WarehouseEnv(render_mode='human')
|
| 127 |
+
# It will check your custom environment and output additional warnings if needed
|
| 128 |
+
# check_env(env)
|
| 129 |
+
|
| 130 |
+
# vec_env = make_vec_env(env, n_envs=4)
|
| 131 |
+
# model = PPO("MlpPolicy", env, verbose=1)
|
| 132 |
+
# model.learn(total_timesteps=25000)
|
| 133 |
+
# model.save("ppo_warehouse")
|
| 134 |
+
#
|
| 135 |
+
# del model # remove to demonstrate saving and loading
|
| 136 |
+
|
| 137 |
+
model = PPO.load("ppo_warehouse")
|
| 138 |
+
vec_env = model.get_env()
|
| 139 |
+
obs, info = env.reset()
|
| 140 |
+
while True:
|
| 141 |
+
action, _states = model.predict(obs)
|
| 142 |
+
obs, rewards, done, trunc, info = env.step(action)
|
| 143 |
+
env.render()
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
if __name__ == '__main__':
|
| 147 |
+
main()
|