Spaces:
Sleeping
Sleeping
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 | |
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) | |
header = next(reader) | |
parsed_data.append(header) | |
num_columns = len(header) | |
for line_number, row in enumerate(reader, start=1): | |
if len(row) == num_columns: | |
parsed_data.append(row) | |
else: | |
# Print the line number (0th value of the line) | |
print(f"Line number {line_number} (row 0th value: {row[0] if row else 'Empty'}) has mismatched columns") | |
# Handle rows with mismatched columns | |
if len(row) < num_columns: | |
row.extend([''] * (num_columns - len(row))) # Pad with empty strings | |
else: | |
row = row[:num_columns] # Truncate to the correct number of columns | |
parsed_data.append(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 = 'emo_inferred_full.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() | |