Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,27 +9,6 @@ from streamlit_flow import streamlit_flow
|
|
9 |
from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
|
10 |
from streamlit_flow.layouts import TreeLayout
|
11 |
|
12 |
-
# 1. Configuration
|
13 |
-
Site_Name = 'π¦CatRiderπ'
|
14 |
-
title="π¦CatRiderπbyπ€Aaron Wacker"
|
15 |
-
helpURL='https://huggingface.co/awacke1'
|
16 |
-
bugURL='https://huggingface.co/spaces/awacke1'
|
17 |
-
icons='π¦'
|
18 |
-
|
19 |
-
useConfig=True
|
20 |
-
if useConfig: # Component code - useConfig=False should allow it to work if re-evaluating UI elements due to code modify
|
21 |
-
st.set_page_config(
|
22 |
-
page_title=title,
|
23 |
-
page_icon=icons,
|
24 |
-
layout="wide",
|
25 |
-
#initial_sidebar_state="expanded",
|
26 |
-
initial_sidebar_state="auto",
|
27 |
-
menu_items={
|
28 |
-
'Get Help': helpURL,
|
29 |
-
'Report a bug': bugURL,
|
30 |
-
'About': title
|
31 |
-
}
|
32 |
-
)
|
33 |
# π± Cat Rider and Gear Data
|
34 |
CAT_RIDERS = [
|
35 |
{"name": "Whiskers", "type": "Speed", "emoji": "πΎ", "strength": 3, "skill": 7},
|
@@ -110,7 +89,7 @@ FAILURE_CONCLUSIONS = [
|
|
110 |
def save_history_to_file(history_df, user_id):
|
111 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
112 |
filename = f"history_{user_id}_{timestamp}.md"
|
113 |
-
markdown =
|
114 |
with open(filename, 'w', encoding='utf-8') as f:
|
115 |
f.write(markdown)
|
116 |
return filename
|
@@ -167,75 +146,74 @@ def update_character_stats(game_state, outcome):
|
|
167 |
game_state['gear_strength'] = max(1, game_state['gear_strength'] - 0.1)
|
168 |
return game_state
|
169 |
|
170 |
-
# π³ Journey
|
171 |
-
def
|
|
|
172 |
nodes = []
|
173 |
edges = []
|
174 |
node_ids = {}
|
175 |
score = 0 # To keep track of the score for success nodes
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
180 |
|
181 |
-
|
|
|
|
|
182 |
if situation_node_id not in node_ids:
|
183 |
situation_node = StreamlitFlowNode(
|
184 |
situation_node_id,
|
185 |
pos=(0, 0),
|
186 |
-
data={'content': f"{
|
187 |
type='default',
|
188 |
shape='ellipse'
|
189 |
)
|
190 |
nodes.append(situation_node)
|
191 |
node_ids[situation_node_id] = situation_node_id
|
192 |
|
193 |
-
# Update score if outcome is success
|
194 |
-
if row['outcome']:
|
195 |
-
score = row['score']
|
196 |
-
|
197 |
-
# Attempt node
|
198 |
-
outcome_content = 'β
Success' if row['outcome'] else 'β Failure'
|
199 |
-
stars = 'β' * int(row['score'])
|
200 |
-
if row['outcome']:
|
201 |
-
# For success nodes, show current score and journey icons
|
202 |
-
attempt_content = f"Attempt {row['attempt']}: {row['action_emoji']} {row['action_name']} - {outcome_content}\n{stars}"
|
203 |
-
else:
|
204 |
-
attempt_content = f"Attempt {row['attempt']}: {row['action_emoji']} {row['action_name']} - {outcome_content}"
|
205 |
-
|
206 |
-
attempt_node = StreamlitFlowNode(
|
207 |
-
attempt_node_id,
|
208 |
-
pos=(0, 0),
|
209 |
-
data={'content': attempt_content},
|
210 |
-
type='default',
|
211 |
-
shape='rectangle'
|
212 |
-
)
|
213 |
-
nodes.append(attempt_node)
|
214 |
-
|
215 |
-
# Edge from situation to attempt
|
216 |
-
edges.append(StreamlitFlowEdge(
|
217 |
-
id=f"edge_{situation_node_id}_{attempt_node_id}",
|
218 |
-
source=situation_node_id,
|
219 |
-
target=attempt_node_id,
|
220 |
-
animated=True
|
221 |
-
))
|
222 |
-
|
223 |
-
return nodes, edges
|
224 |
-
|
225 |
-
# π Markdown Preview with Subpoints for Each Action
|
226 |
-
def create_markdown_preview(history_df):
|
227 |
-
markdown = "## π³ Journey Preview\n\n"
|
228 |
-
grouped = history_df.groupby(['situation_name'], sort=False)
|
229 |
-
|
230 |
-
for situation_name, group in grouped:
|
231 |
-
markdown += f"### {group.iloc[0]['situation_emoji']} **{situation_name}**\n"
|
232 |
for _, row in group.iterrows():
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
markdown += "\n"
|
238 |
-
|
|
|
239 |
|
240 |
# π Update game state with the result of the action
|
241 |
def update_game_state(game_state, situation, action, outcome, timestamp):
|
@@ -441,12 +419,14 @@ def main():
|
|
441 |
|
442 |
# Integration point for both functions
|
443 |
if not game_state['history_df'].empty:
|
|
|
|
|
|
|
444 |
# π Display Markdown Preview
|
445 |
-
st.markdown(
|
446 |
|
447 |
# π³ Display Knowledge Journey Graph
|
448 |
st.markdown("## π³ Your Journey (Knowledge Graph)")
|
449 |
-
nodes, edges = create_knowledge_graph(game_state['history_df'])
|
450 |
try:
|
451 |
streamlit_flow('cat_rider_flow',
|
452 |
nodes,
|
|
|
9 |
from streamlit_flow.elements import StreamlitFlowNode, StreamlitFlowEdge
|
10 |
from streamlit_flow.layouts import TreeLayout
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# π± Cat Rider and Gear Data
|
13 |
CAT_RIDERS = [
|
14 |
{"name": "Whiskers", "type": "Speed", "emoji": "πΎ", "strength": 3, "skill": 7},
|
|
|
89 |
def save_history_to_file(history_df, user_id):
|
90 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
91 |
filename = f"history_{user_id}_{timestamp}.md"
|
92 |
+
markdown, _, _ = process_journey_history(history_df)
|
93 |
with open(filename, 'w', encoding='utf-8') as f:
|
94 |
f.write(markdown)
|
95 |
return filename
|
|
|
146 |
game_state['gear_strength'] = max(1, game_state['gear_strength'] - 0.1)
|
147 |
return game_state
|
148 |
|
149 |
+
# π³ Process Journey History to Create Markdown and Graph Data
|
150 |
+
def process_journey_history(history_df):
|
151 |
+
markdown = "## π³ Journey Preview\n\n"
|
152 |
nodes = []
|
153 |
edges = []
|
154 |
node_ids = {}
|
155 |
score = 0 # To keep track of the score for success nodes
|
156 |
|
157 |
+
grouped = history_df.groupby(['situation_name'], sort=False)
|
158 |
+
|
159 |
+
for situation_name, group in grouped:
|
160 |
+
situation_emoji = group.iloc[0]['situation_emoji']
|
161 |
+
situation_id = group.iloc[0]['situation_id']
|
162 |
+
situation_node_id = f"situation_{situation_id}"
|
163 |
|
164 |
+
markdown += f"### {situation_emoji} **{situation_name}**\n"
|
165 |
+
|
166 |
+
# Create situation node if not already created
|
167 |
if situation_node_id not in node_ids:
|
168 |
situation_node = StreamlitFlowNode(
|
169 |
situation_node_id,
|
170 |
pos=(0, 0),
|
171 |
+
data={'content': f"{situation_emoji} {situation_name}"},
|
172 |
type='default',
|
173 |
shape='ellipse'
|
174 |
)
|
175 |
nodes.append(situation_node)
|
176 |
node_ids[situation_node_id] = situation_node_id
|
177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
for _, row in group.iterrows():
|
179 |
+
attempt = row['attempt']
|
180 |
+
action_emoji = row['action_emoji']
|
181 |
+
action_name = row['action_name']
|
182 |
+
outcome = row['outcome']
|
183 |
+
outcome_str = 'β
Success' if outcome else 'β Failure'
|
184 |
+
stars = 'β' * int(row['score']) if outcome else ''
|
185 |
+
conclusion = row['conclusion']
|
186 |
+
|
187 |
+
markdown += f"Attempt {attempt}: {action_emoji} **{action_name}**: {outcome_str} {stars}\n"
|
188 |
+
markdown += f"{conclusion}\n"
|
189 |
+
|
190 |
+
# Create attempt node
|
191 |
+
attempt_node_id = f"attempt_{situation_id}_{attempt}_{row['action_id']}"
|
192 |
+
if outcome:
|
193 |
+
attempt_content = f"Attempt {attempt}: {action_emoji} {action_name} - {outcome_str}\n{stars}"
|
194 |
+
else:
|
195 |
+
attempt_content = f"Attempt {attempt}: {action_emoji} {action_name} - {outcome_str}"
|
196 |
+
|
197 |
+
attempt_node = StreamlitFlowNode(
|
198 |
+
attempt_node_id,
|
199 |
+
pos=(0, 0),
|
200 |
+
data={'content': attempt_content},
|
201 |
+
type='default',
|
202 |
+
shape='rectangle'
|
203 |
+
)
|
204 |
+
nodes.append(attempt_node)
|
205 |
+
|
206 |
+
# Edge from situation to attempt
|
207 |
+
edges.append(StreamlitFlowEdge(
|
208 |
+
id=f"edge_{situation_node_id}_{attempt_node_id}",
|
209 |
+
source=situation_node_id,
|
210 |
+
target=attempt_node_id,
|
211 |
+
animated=True
|
212 |
+
))
|
213 |
+
|
214 |
markdown += "\n"
|
215 |
+
|
216 |
+
return markdown, nodes, edges
|
217 |
|
218 |
# π Update game state with the result of the action
|
219 |
def update_game_state(game_state, situation, action, outcome, timestamp):
|
|
|
419 |
|
420 |
# Integration point for both functions
|
421 |
if not game_state['history_df'].empty:
|
422 |
+
# π Process Journey History to get markdown and graph data
|
423 |
+
markdown_preview, nodes, edges = process_journey_history(game_state['history_df'])
|
424 |
+
|
425 |
# π Display Markdown Preview
|
426 |
+
st.markdown(markdown_preview)
|
427 |
|
428 |
# π³ Display Knowledge Journey Graph
|
429 |
st.markdown("## π³ Your Journey (Knowledge Graph)")
|
|
|
430 |
try:
|
431 |
streamlit_flow('cat_rider_flow',
|
432 |
nodes,
|