awacke1 commited on
Commit
7bfbbae
Β·
verified Β·
1 Parent(s): 3e43c6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -75
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 = create_markdown_preview(history_df)
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 Visualization
171
- def create_knowledge_graph(history_df):
 
172
  nodes = []
173
  edges = []
174
  node_ids = {}
175
  score = 0 # To keep track of the score for success nodes
176
 
177
- for index, row in history_df.iterrows():
178
- situation_node_id = f"situation_{row['situation_id']}"
179
- attempt_node_id = f"attempt_{index}"
 
 
 
180
 
181
- # Situation node
 
 
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"{row['situation_emoji']} {row['situation_name']}"},
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
- outcome_str = 'βœ… Success' if row['outcome'] else '❌ Failure'
234
- stars = '⭐' * int(row['score']) if row['outcome'] else ''
235
- markdown += f"Attempt {row['attempt']}: {row['action_emoji']} **{row['action_name']}**: {outcome_str} {stars}\n"
236
- markdown += f"{row['conclusion']}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  markdown += "\n"
238
- return markdown
 
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(create_markdown_preview(game_state['history_df']))
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,