Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import time | |
| import plotly.express as px | |
| from utils.file_handler import validate_file | |
| from models.sentiment_model import load_model | |
| # Constants | |
| MAX_FILE_SIZE_MB = 200 | |
| # Load sentiment analysis pipeline | |
| sentiment_pipeline = load_model() | |
| st.title("π Sentiment Analysis App") | |
| # File upload | |
| uploaded_file = st.file_uploader("Upload a CSV file (Max: 200MB)", type=["csv"]) | |
| if uploaded_file is not None: | |
| if validate_file(uploaded_file, MAX_FILE_SIZE_MB): | |
| df = pd.read_csv(uploaded_file) | |
| # Check for 'text' column or ask user for correct column | |
| if "text" not in df.columns: | |
| text_column = st.selectbox("Select the column containing text values", df.columns) | |
| else: | |
| text_column = "text" | |
| if st.button("Analyze Sentiment"): | |
| st.write("Processing sentiment analysis...") | |
| progress_bar = st.progress(0) | |
| sentiments = [] | |
| for i, text in enumerate(df[text_column].dropna()): | |
| result = sentiment_pipeline(text) | |
| sentiments.append(result[0]["label"]) | |
| progress_bar.progress((i + 1) / len(df)) | |
| time.sleep(0.1) | |
| df["Sentiment"] = sentiments | |
| # Display results | |
| st.write("Sentiment Analysis Results:") | |
| st.dataframe(df[[text_column, "Sentiment"]]) | |
| # Create pie chart | |
| sentiment_counts = df["Sentiment"].value_counts().reset_index() | |
| sentiment_counts.columns = ["Sentiment", "Count"] | |
| fig = px.pie(sentiment_counts, names="Sentiment", values="Count", title="Sentiment Distribution") | |
| st.plotly_chart(fig) | |
| # Allow CSV download | |
| # st.download_button("Download Results", df.to_csv(index=False), "sentiment_results.csv", "text/csv") | |
| # Download option | |
| st.download_button("Download Results", df.to_csv(index=False).encode('utf-8'), "sentiment_results.csv", "text/csv") | |
| else: | |
| st.error("File exceeds the maximum allowed size of 200MB. Please upload a smaller file.") | |