Spaces:
Runtime error
Runtime error
Marc-Alexandre Côté
commited on
Commit
•
d18b3bd
1
Parent(s):
fedaa78
First version of TWX with params selection
Browse files- app.py +140 -0
- packages.txt +2 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import streamlit.components.v1 as components
|
3 |
+
|
4 |
+
from textworld_express import TextWorldExpressEnv
|
5 |
+
|
6 |
+
description = """
|
7 |
+
[ArXiv Paper](https://arxiv.org/abs/2208.01174) | [Github Repo](https://github.com/cognitiveailab/TextWorldExpress)
|
8 |
+
"""
|
9 |
+
st.title("TextWorldExpress Demo")
|
10 |
+
st.markdown(description)
|
11 |
+
|
12 |
+
env = st.session_state.get("env")
|
13 |
+
if env is None:
|
14 |
+
env = TextWorldExpressEnv()
|
15 |
+
st.session_state["env"] = env
|
16 |
+
|
17 |
+
seed = st.session_state.get("seed")
|
18 |
+
obs = st.session_state.get("obs")
|
19 |
+
infos = st.session_state.get("infos")
|
20 |
+
history = st.session_state.get("history")
|
21 |
+
if history is None:
|
22 |
+
history = []
|
23 |
+
st.session_state["history"] = history
|
24 |
+
|
25 |
+
def clear_history():
|
26 |
+
history.clear()
|
27 |
+
|
28 |
+
|
29 |
+
with st.sidebar:
|
30 |
+
st.title("TextWorldExpress Demo")
|
31 |
+
st.markdown(description)
|
32 |
+
game = st.selectbox("Game:", env.getGameNames(), on_change=clear_history)
|
33 |
+
|
34 |
+
with st.expander("Settings"):
|
35 |
+
|
36 |
+
seed = st.number_input("Seed:", 0, 2**16, value=4242, on_change=clear_history)
|
37 |
+
|
38 |
+
if game == "cookingworld":
|
39 |
+
nb_ingredients = st.number_input("Ingredients:", 1, 5, value=3, on_change=clear_history,
|
40 |
+
help="The number of ingredients to use in generating the random recipe.")
|
41 |
+
nb_locations = st.number_input("Locations:", 1, 11, value=5, on_change=clear_history,
|
42 |
+
help="The number of locations in the environment.")
|
43 |
+
nb_distractors = st.number_input("Distractors:", 0, 10, value=5, on_change=clear_history,
|
44 |
+
help="The number of distractor ingredients (not used in the recipe) in the environment.")
|
45 |
+
with_doors = st.checkbox("With doors?", value=True, on_change=clear_history,
|
46 |
+
help="Whether rooms have doors that need to be opened.")
|
47 |
+
limited_inventory = st.checkbox("Limit inventory?", value=False, on_change=clear_history,
|
48 |
+
help="Whether the size of the inventory is limited.")
|
49 |
+
params = f"numLocations={nb_locations},numIngredients={nb_ingredients},numDistractorItems={nb_distractors},includeDoors={int(with_doors)},limitInventorySize={int(limited_inventory)}"
|
50 |
+
|
51 |
+
elif game == "twc":
|
52 |
+
nb_items = st.number_input("Items:", 1, 10, value=3, on_change=clear_history,
|
53 |
+
help="The number of items to put away.")
|
54 |
+
nb_locations = st.number_input("Locations:", 1, 3, value=3, on_change=clear_history,
|
55 |
+
help="The number of locations in the environment.")
|
56 |
+
with_doors = st.checkbox("With doors?", value=True, on_change=clear_history,
|
57 |
+
help="Whether rooms have doors that need to be opened.")
|
58 |
+
limited_inventory = st.checkbox("Limit inventory?", value=False, on_change=clear_history,
|
59 |
+
help="Whether the size of the inventory is limited.")
|
60 |
+
params = f"numLocations={nb_locations},numItemsToPutAway={nb_items},includeDoors={int(with_doors)},limitInventorySize={int(limited_inventory)}"
|
61 |
+
|
62 |
+
elif game == "coin":
|
63 |
+
nb_locations = st.number_input("Locations:", 1, 11, value=3, on_change=clear_history,
|
64 |
+
help="The number of locations in the environment.")
|
65 |
+
nb_distractors = st.number_input("Distractors:", 0, 10, value=5, on_change=clear_history,
|
66 |
+
help="The number of distractor (i.e. non-coin) items in the environment.")
|
67 |
+
with_doors = st.checkbox("With doors?", value=True, on_change=clear_history,
|
68 |
+
help="Whether rooms have doors that need to be opened.")
|
69 |
+
limited_inventory = st.checkbox("Limit inventory?", value=False, on_change=clear_history,
|
70 |
+
help="Whether the size of the inventory is limited.")
|
71 |
+
params = f"numLocations={nb_locations},numDistractorItems={nb_distractors},includeDoors={int(with_doors)},limitInventorySize={int(limited_inventory)}"
|
72 |
+
|
73 |
+
else:
|
74 |
+
params=""
|
75 |
+
|
76 |
+
|
77 |
+
if len(history) == 0:
|
78 |
+
obs, infos = env.reset(seed, gameFold="train", gameName=game, gameParams=params)
|
79 |
+
obs, reward, done, infos = env.step("look around")
|
80 |
+
st.session_state["obs"] = obs
|
81 |
+
st.session_state["infos"] = infos
|
82 |
+
history.append(("", env.getTaskDescription()))
|
83 |
+
history.append(("look around", obs))
|
84 |
+
|
85 |
+
def step():
|
86 |
+
act = st.session_state.action
|
87 |
+
if act:
|
88 |
+
obs, reward, done, infos = env.step(act)
|
89 |
+
history.append((act, obs))
|
90 |
+
st.session_state["obs"] = obs
|
91 |
+
st.session_state["infos"] = infos
|
92 |
+
|
93 |
+
if act == "reset":
|
94 |
+
clear_history()
|
95 |
+
|
96 |
+
|
97 |
+
with st.sidebar:
|
98 |
+
# st.warning(env.getTaskDescription())
|
99 |
+
st.success(f"Score: {infos['score']}")
|
100 |
+
|
101 |
+
valid_actions = [""] + sorted(infos["validActions"])
|
102 |
+
if infos['done']:
|
103 |
+
valid_actions = ["", "reset"]
|
104 |
+
|
105 |
+
#act = st.selectbox('Action:', options=valid_actions, index=0, on_change=step, key="action")
|
106 |
+
|
107 |
+
for act, obs in history:
|
108 |
+
if act:
|
109 |
+
st.write("> " + act)
|
110 |
+
|
111 |
+
if obs:
|
112 |
+
st.info(obs.replace('\n ', '\n- ').replace('\n\t', '\n- '))
|
113 |
+
|
114 |
+
act = st.selectbox('Action:', options=valid_actions, index=0, on_change=step, key="action")
|
115 |
+
|
116 |
+
|
117 |
+
if infos['tasksuccess']:
|
118 |
+
with st.sidebar:
|
119 |
+
st.balloons()
|
120 |
+
|
121 |
+
st.success("Congratulations! You have completed the task.")
|
122 |
+
|
123 |
+
elif infos['taskfailure']:
|
124 |
+
with st.sidebar:
|
125 |
+
st.snow()
|
126 |
+
|
127 |
+
st.error("You have failed the task.")
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
# # Auto scroll at the bottom of the page.
|
132 |
+
# components.html(
|
133 |
+
# f"""
|
134 |
+
# <p>{st.session_state.obs}</p>
|
135 |
+
# <script>
|
136 |
+
# window.parent.document.querySelector('section.main').scrollTo(0, window.parent.document.querySelector('section.main').scrollHeight);
|
137 |
+
# </script>
|
138 |
+
# """,
|
139 |
+
# height=0
|
140 |
+
# )
|
packages.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
default-jre
|
2 |
+
default-jdk
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
py4j
|
2 |
+
orjson
|
3 |
+
textworld-express
|
4 |
+
numpy
|