EmotionCSV / app.py
umang018's picture
Update app.py
0cf0150 verified
raw
history blame
2.7 kB
import streamlit as st
import pandas as pd
import csv
import sys
# Increase the field size limit
csv.field_size_limit(sys.maxsize)
# Load the CSV data
@st.cache_data
def load_data(file_path):
def custom_csv_parser(file_path):
parsed_data = []
with open(file_path, 'r', newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in reader:
parsed_row = []
for field in row:
if (field.startswith('"') and field.endswith('"')) or (field.startswith('[') and field.endswith(']')):
parsed_row.append(field)
else:
parsed_row.append(field)
parsed_data.append(parsed_row)
return pd.DataFrame(parsed_data[1:], columns=parsed_data[0])
return custom_csv_parser(file_path)
# Paginate function
def paginate_data(df, page_number, page_size):
start_index = page_number * page_size
end_index = start_index + page_size
return df[start_index:end_index]
# Streamlit app
def main():
st.title("Emotion Filter and Pagination App")
# Load the data
file_path = 'enron_kaggle_clean.csv' # Ensure this is the correct path to your uploaded CSV file
df = load_data(file_path)
# Dropdown for selecting emotion
unique_emotions = ["admiration", "amusement", "anger", "annoyance", "approval",
"caring", "confusion", "curiosity", "desire", "disappointment",
"disapproval", "disgust", "embarrassment", "excitement", "fear",
"gratitude", "grief", "joy", "love", "nervousness", "optimism",
"pride", "realization", "relief", "remorse", "sadness", "surprise",
"neutral"]
selected_emotion = st.selectbox('Select Emotion', unique_emotions)
# Filter the data based on the selected emotion and sort by score
filtered_data = df[df['emotion'] == selected_emotion].sort_values(by='emoscore', ascending=False).head(100)
# Pagination settings
page_size = 10
total_pages = (len(filtered_data) // page_size) + 1
page_number = st.number_input('Page number', 0, total_pages - 1, 0)
# Paginate data
paginated_data = paginate_data(filtered_data, page_number, page_size)
# Display total counts of each emotion
emotion_counts = df['emotion'].value_counts()
st.write("### Total Emails of Each Emotion")
st.write(emotion_counts)
# Display the paginated data
st.write(f"Showing page {page_number + 1} of {total_pages}")
st.write(paginated_data)
if __name__ == "__main__":
main()