awacke1 commited on
Commit
160123f
β€’
1 Parent(s): 38ac651

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -34
app.py CHANGED
@@ -1,11 +1,28 @@
1
  import streamlit as st
2
  import pandas as pd
3
 
 
 
 
 
 
 
 
4
  def load_data():
5
- sem_df = pd.read_csv("semantic_memory.csv")
6
- sem_mem = sem_df.to_dict("records")
7
- epi_df = pd.read_csv("episodic_memory.csv")
8
- epi_mem = epi_df.to_dict("records")
 
 
 
 
 
 
 
 
 
 
9
  return sem_mem, epi_mem
10
 
11
  def save_data(sem_mem, epi_mem):
@@ -14,48 +31,54 @@ def save_data(sem_mem, epi_mem):
14
  epi_df = pd.DataFrame(epi_mem)
15
  epi_df.to_csv("episodic_memory.csv", index=False)
16
 
17
- def add_fact(sem_mem, fact, category, source):
18
- sem_mem.append({"fact": fact, "category": category, "source": source})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- def add_event(epi_mem, event, sentiment, date):
21
- epi_mem.append({"event": event, "sentiment": sentiment, "date": date})
 
 
 
 
 
 
 
22
 
23
  def run_app():
24
  sem_mem, epi_mem = load_data()
25
 
26
  st.title("Cognitive Agent")
27
- option = st.sidebar.selectbox("Select an option", ["View Semantic Memory", "View Episodic Memory", "Add Fact to Semantic Memory", "Add Event to Episodic Memory"])
 
 
28
 
29
  if option == "View Semantic Memory":
30
- st.write("# Semantic Memory")
31
- for item in sem_mem:
32
- st.write(f"**{item['fact']}** ({item['category']}) - {item['source']}")
33
  elif option == "View Episodic Memory":
34
- st.write("# Episodic Memory")
35
- for item in epi_mem:
36
- st.write(f"**{item['event']}** ({item['sentiment']}) - {item['date']}")
37
  elif option == "Add Fact to Semantic Memory":
38
- fact = st.text_input("Enter a fact")
39
- category = st.text_input("Enter a category")
40
- source = st.text_input("Enter a source")
41
- if st.button("Add Fact"):
42
- add_fact(sem_mem, fact, category, source)
43
- save_data(sem_mem, epi_mem)
44
- st.success("Fact added to semantic memory!")
45
- st.sidebar.success("Fact added to semantic memory!")
46
  elif option == "Add Event to Episodic Memory":
47
- event = st.text_input("Enter an event")
48
- sentiment = st.selectbox("Select a sentiment", ["happy", "sad", "neutral"])
49
- date = st.date_input("Select a date")
50
- if st.button("Add Event"):
51
- add_event(epi_mem, event, sentiment, date)
52
- save_data(sem_mem, epi_mem)
53
- st.success("Event added to episodic memory!")
54
- st.sidebar.success("Event added to episodic memory!")
55
- else:
56
- st.write("Please select an option from the sidebar.")
57
 
58
- if __name__ == "__main__":
59
  run_app()
60
 
61
  # AW: Restructure the code listing into four functions. shorten the code by eliminating comments and unnecessary whitespace and empty lines.
 
1
  import streamlit as st
2
  import pandas as pd
3
 
4
+ # Define functions
5
+ def create_empty_csv_files():
6
+ sem_df = pd.DataFrame(columns=["fact", "category", "source"])
7
+ sem_df.to_csv("semantic_memory.csv", index=False)
8
+ epi_df = pd.DataFrame(columns=["event", "sentiment", "date"])
9
+ epi_df.to_csv("episodic_memory.csv", index=False)
10
+
11
  def load_data():
12
+ try:
13
+ sem_df = pd.read_csv("semantic_memory.csv")
14
+ sem_mem = sem_df.to_dict("records")
15
+ except:
16
+ create_empty_csv_files()
17
+ sem_mem = [{"fact": "The Earth is round", "category": "science", "source": "NASA"},
18
+ {"fact": "Pizza is delicious", "category": "food", "source": "me"}]
19
+ try:
20
+ epi_df = pd.read_csv("episodic_memory.csv")
21
+ epi_mem = epi_df.to_dict("records")
22
+ except:
23
+ create_empty_csv_files()
24
+ epi_mem = [{"event": "I went to the beach", "sentiment": "happy", "date": "2022-02-28"},
25
+ {"event": "I had a fight with my friend", "sentiment": "sad", "date": "2022-02-25"}]
26
  return sem_mem, epi_mem
27
 
28
  def save_data(sem_mem, epi_mem):
 
31
  epi_df = pd.DataFrame(epi_mem)
32
  epi_df.to_csv("episodic_memory.csv", index=False)
33
 
34
+ def view_semantic_memory(sem_mem):
35
+ st.write("# Semantic Memory")
36
+ for item in sem_mem:
37
+ st.write(f"**{item['fact']}** ({item['category']}) - {item['source']}")
38
+
39
+ def view_episodic_memory(epi_mem):
40
+ st.write("# Episodic Memory")
41
+ for item in epi_mem:
42
+ st.write(f"**{item['event']}** ({item['sentiment']}) - {item['date']}")
43
+
44
+ def add_fact_to_semantic_memory(sem_mem):
45
+ fact = st.text_input("Enter a fact")
46
+ category = st.text_input("Enter a category")
47
+ source = st.text_input("Enter a source")
48
+ if st.button("Add Fact"):
49
+ add_fact(sem_mem, fact, category, source)
50
+ save_data(sem_mem, epi_mem)
51
+ st.success("Fact added to semantic memory!")
52
+ st.sidebar.success("Fact added to semantic memory!")
53
 
54
+ def add_event_to_episodic_memory(epi_mem):
55
+ event = st.text_input("Enter an event")
56
+ sentiment = st.selectbox("Select a sentiment", ["happy", "sad", "neutral"])
57
+ date = st.date_input("Select a date")
58
+ if st.button("Add Event"):
59
+ add_event(epi_mem, event, sentiment, date)
60
+ save_data(sem_mem, epi_mem)
61
+ st.success("Event added to episodic memory!")
62
+ st.sidebar.success("Event added to episodic memory!")
63
 
64
  def run_app():
65
  sem_mem, epi_mem = load_data()
66
 
67
  st.title("Cognitive Agent")
68
+ option = st.sidebar.selectbox("Select an option",
69
+ ["View Semantic Memory", "View Episodic Memory", "Add Fact to Semantic Memory",
70
+ "Add Event to Episodic Memory"])
71
 
72
  if option == "View Semantic Memory":
73
+ view_semantic_memory(sem_mem)
 
 
74
  elif option == "View Episodic Memory":
75
+ view_episodic_memory(epi_mem)
 
 
76
  elif option == "Add Fact to Semantic Memory":
77
+ add_fact_to_semantic_memory(sem_mem)
 
 
 
 
 
 
 
78
  elif option == "Add Event to Episodic Memory":
79
+ add_event_to_episodic_memory(epi_mem)
 
 
 
 
 
 
 
 
 
80
 
81
+ if __name__ == '__main__':
82
  run_app()
83
 
84
  # AW: Restructure the code listing into four functions. shorten the code by eliminating comments and unnecessary whitespace and empty lines.