Ezhil commited on
Commit
2ed4404
·
0 Parent(s):

Initial commit

Browse files
README.md ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Streamlit Sentiment Analysis
3
+ emoji: 🐠
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: docker
7
+ pinned: false
8
+ ---
main.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import time
4
+ import plotly.express as px
5
+ from utils.file_handler import validate_file
6
+ from models.sentiment_model import load_model
7
+
8
+ # Constants
9
+ MAX_FILE_SIZE_MB = 500
10
+
11
+ # Load sentiment analysis pipeline
12
+ sentiment_pipeline = load_model()
13
+
14
+ st.title("📊 Sentiment Analysis App")
15
+
16
+ # File upload
17
+ uploaded_file = st.file_uploader("Upload a CSV file (Max: 500MB)", type=["csv"])
18
+
19
+ if uploaded_file is not None:
20
+ if validate_file(uploaded_file, MAX_FILE_SIZE_MB):
21
+ df = pd.read_csv(uploaded_file)
22
+
23
+ # Check for 'text' column or ask user for correct column
24
+ if "text" not in df.columns:
25
+ text_column = st.selectbox("Select the column containing text values", df.columns)
26
+ else:
27
+ text_column = "text"
28
+
29
+ if st.button("Analyze Sentiment"):
30
+ st.write("Processing sentiment analysis...")
31
+ progress_bar = st.progress(0)
32
+
33
+ sentiments = []
34
+ for i, text in enumerate(df[text_column].dropna()):
35
+ result = sentiment_pipeline(text)
36
+ sentiments.append(result[0]["label"])
37
+ progress_bar.progress((i + 1) / len(df))
38
+ time.sleep(0.1)
39
+
40
+ df["Sentiment"] = sentiments
41
+
42
+ # Display results
43
+ st.write("Sentiment Analysis Results:")
44
+ st.dataframe(df[[text_column, "Sentiment"]])
45
+
46
+ # Create pie chart
47
+ sentiment_counts = df["Sentiment"].value_counts().reset_index()
48
+ sentiment_counts.columns = ["Sentiment", "Count"]
49
+ fig = px.pie(sentiment_counts, names="Sentiment", values="Count", title="Sentiment Distribution")
50
+ st.plotly_chart(fig)
51
+
52
+ # Allow CSV download
53
+ st.download_button("Download Results", df.to_csv(index=False), "sentiment_results.csv", "text/csv")
54
+ else:
55
+ st.error("File exceeds the maximum allowed size of 500MB. Please upload a smaller file.")
models/__pycache__/sentiment_model.cpython-310.pyc ADDED
Binary file (492 Bytes). View file
 
models/sentiment_model.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import streamlit as st
3
+
4
+ @st.cache_resource
5
+ def load_model():
6
+ """Load the sentiment analysis model."""
7
+ return pipeline("sentiment-analysis", model="tabularisai/multilingual-sentiment-analysis")
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ transformers
4
+ torch
5
+ plotly
utils/__pycache__/file_handler.cpython-310.pyc ADDED
Binary file (605 Bytes). View file
 
utils/file_handler.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ def validate_file(uploaded_file, max_size_mb):
4
+ """
5
+ Validates the uploaded file size.
6
+ :param uploaded_file: The uploaded file object.
7
+ :param max_size_mb: The maximum allowed file size in MB.
8
+ :return: True if file size is within limit, else False.
9
+ """
10
+ max_size_bytes = max_size_mb * 1024 * 1024 # Convert MB to Bytes
11
+ if uploaded_file.size > max_size_bytes:
12
+ return False
13
+ return True