Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| from transformers import pipeline | |
| import matplotlib.pyplot as plt | |
| import time | |
| # Load the sentiment analysis model | |
| sentiment_model = pipeline("sentiment-analysis", model="tabularisai/multilingual-sentiment-analysis") | |
| # Function to perform sentiment analysis | |
| def perform_sentiment_analysis(texts): | |
| sentiments = sentiment_model(texts) | |
| return sentiments | |
| # Function to plot the sentiment analysis results | |
| def plot_sentiment_analysis(sentiments): | |
| labels = [item['label'] for item in sentiments] | |
| label_counts = pd.Series(labels).value_counts() | |
| fig, ax = plt.subplots() | |
| ax.pie(label_counts, labels=label_counts.index, autopct='%1.1f%%', startangle=90) | |
| ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. | |
| st.pyplot(fig) | |
| # Streamlit UI | |
| st.title("Sentiment Analysis App") | |
| # File upload | |
| uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"]) | |
| if uploaded_file is not None: | |
| # Read the file | |
| if uploaded_file.name.endswith(".csv"): | |
| df = pd.read_csv(uploaded_file) | |
| else: | |
| df = pd.read_excel(uploaded_file, engine='openpyxl') | |
| # Check if 'text' column exists | |
| if 'text' not in df.columns: | |
| st.warning("Column 'text' not found. Please enter the column name containing the text values.") | |
| text_column = st.text_input("Enter the column name containing the text values") | |
| else: | |
| text_column = 'text' | |
| if text_column in df.columns: | |
| # Display the first few rows of the dataframe | |
| st.write("First few rows of the uploaded file:") | |
| st.write(df.head()) | |
| # Perform sentiment analysis | |
| if st.button("Run Sentiment Analysis"): | |
| texts = df[text_column].tolist() | |
| progress_bar = st.progress(0) | |
| # Simulate progress | |
| for i in range(100): | |
| time.sleep(0.05) | |
| progress_bar.progress(i + 1) | |
| sentiments = perform_sentiment_analysis(texts) | |
| st.success("Sentiment analysis completed!") | |
| # Plot the sentiment analysis results | |
| plot_sentiment_analysis(sentiments) | |
| else: | |
| st.error("The specified column does not exist in the uploaded file.") | |