awacke1's picture
Update app.py
1c78f66
raw
history blame
4.86 kB
import streamlit as st
import pandas as pd
# Set up default data
sem_mem = [{"fact": "The Earth is round", "category": "science", "source": "NASA"}, {"fact": "Pizza is delicious", "category": "food", "source": "me"}]
epi_mem = [{"event": "I went to the beach", "sentiment": "happy", "date": "2022-02-28"}, {"event": "I had a fight with my friend", "sentiment": "sad", "date": "2022-02-25"}]
# Define function to save data to CSV file
def save_data():
sem_df = pd.DataFrame(sem_mem)
sem_df.to_csv("semantic_memory.csv", index=False)
epi_df = pd.DataFrame(epi_mem)
epi_df.to_csv("episodic_memory.csv", index=False)
# Define function to load data from CSV file
def load_data():
try:
sem_df = pd.read_csv("semantic_memory.csv")
sem_mem = sem_df.to_dict("records")
except:
sem_mem = [{"fact": "The Earth is round", "category": "science", "source": "NASA"}, {"fact": "Pizza is delicious", "category": "food", "source": "me"}]
try:
epi_df = pd.read_csv("episodic_memory.csv")
epi_mem = epi_df.to_dict("records")
except:
epi_mem = [{"event": "I went to the beach", "sentiment": "happy", "date": "2022-02-28"}, {"event": "I had a fight with my friend", "sentiment": "sad", "date": "2022-02-25"}]
return sem_mem, epi_mem
# Define function to add a new fact to semantic memory
def add_fact(fact, category, source):
sem_mem.append({"fact": fact, "category": category, "source": source})
# Define function to add a new event to episodic memory
def add_event(event, sentiment, date):
epi_mem.append({"event": event, "sentiment": sentiment, "date": date})
# Define function to display semantic memory
def display_sem_mem():
st.write("# Semantic Memory")
for item in sem_mem:
st.write(f"**{item['fact']}** ({item['category']}) - {item['source']}")
# Define function to display episodic memory
def display_epi_mem():
st.write("# Episodic Memory")
for item in epi_mem:
st.write(f"**{item['event']}** ({item['sentiment']}) - {item['date']}")
# Load data from CSV files
sem_mem, epi_mem = load_data()
# Set up the Streamlit app
st.title("Cognitive Agent")
option = st.sidebar.selectbox("Select an option", ["View Semantic Memory", "View Episodic Memory", "Add Fact to Semantic Memory", "Add Event to Episodic Memory"])
# Handle user input
if option == "View Semantic Memory":
display_sem_mem()
elif option == "View Episodic Memory":
display_epi_mem()
elif option == "Add Fact to Semantic Memory":
fact = st.text_input("Enter a fact")
category = st.text_input("Enter a category")
source = st.text_input("Enter a source")
if st.button("Add Fact"):
add_fact(fact, category, source)
save_data()
st.success("Fact added to semantic memory!")
st.sidebar.success("Fact added to semantic memory!")
elif option == "Add Event to Episodic Memory":
event = st.text_input("Enter an event")
sentiment = st.selectbox("Select a sentiment", ["happy", "sad", "neutral"])
date = st.date_input("Select a date")
if st.button("Add Event"):
add_event(event, sentiment, date)
save_data()
st.success("Event added to episodic memory!")
st.sidebar.success("Event added to episodic memory!")
else:
st.write("Please select an option from the sidebar.")
# This program uses Streamlit to create a web app that allows the user to view and add to both semantic and episodic memory. The semantic memory is stored as a list of dictionaries, where each dictionary represents a fact and includes the fact itself, the category it belongs to, and the source of the fact. The episodic memory is also stored as a list of dictionaries, where each dictionary represents an event and includes the event itself, the sentiment associated with the event, and the date the event occurred.
# The program allows the user to view both types of memory by selecting an option from the sidebar. If the user selects "View Semantic Memory", the program displays all of the facts stored in semantic memory. If the user selects "View Episodic Memory", the program displays all of the events stored in episodic memory.
# The program also allows the user to add new facts to semantic memory or new events to episodic memory by selecting an option from the sidebar and filling out a form with the relevant information. When the user clicks the "Add Fact" or "Add Event" button, the new fact or event is added to the appropriate list of dictionaries and saved to a CSV file. The program then displays a success message indicating that the fact or event was added to memory.
# Overall, this program demonstrates how semantic and episodic memory can be modeled using Python list dictionaries, and how these types of memory can be used to track both facts and observations, as well as sentiments associated with past experiences.