velmurugan1122 commited on
Commit
3254df2
·
1 Parent(s): a514d75

fix: changes

Browse files
Files changed (1) hide show
  1. app.py +48 -57
app.py CHANGED
@@ -1,76 +1,67 @@
1
  import streamlit as st
2
  import pandas as pd
3
- from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
4
- import time
5
  import matplotlib.pyplot as plt
6
- import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
 
 
 
8
  st.title("Sentiment Analysis App")
9
- st.write("Upload a CSV or Excel file containing text data for sentiment analysis.")
10
 
11
- # File uploader
12
  uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
13
 
14
- # Load sentiment analysis model
15
- try:
16
- sentiment_pipeline = pipeline(
17
- "sentiment-analysis",
18
- model="distilbert-base-uncased-finetuned-sst-2-english"
19
- )
20
- st.success("Sentiment analysis model loaded successfully!")
21
- except Exception as e:
22
- st.error(f"Error loading model: {e}")
23
- st.stop()
24
-
25
- if uploaded_file:
26
- # Check file type
27
- if uploaded_file.name.endswith('.csv'):
28
  df = pd.read_csv(uploaded_file)
29
- elif uploaded_file.name.endswith('.xlsx'):
30
- df = pd.read_excel(uploaded_file)
31
  else:
32
- st.error("Unsupported file format.")
33
- st.stop()
34
-
35
- st.write("Data Preview:", df.head())
36
 
37
- # Check for 'text' column
38
  if 'text' not in df.columns:
39
- text_column = st.text_input("Enter the name of the column containing text values:")
40
- if text_column not in df.columns:
41
- st.error(f"Column '{text_column}' not found in the file.")
42
- st.stop()
43
- else:
44
- df.rename(columns={text_column: 'text'}, inplace=True)
45
  else:
46
  text_column = 'text'
47
 
48
- if st.button("Run Sentiment Analysis"):
49
- # Progress bar
50
- progress_bar = st.progress(0)
 
51
 
52
- sentiments = []
53
- for i, text in enumerate(df[text_column]):
54
- try:
55
- result = sentiment_pipeline(text)[0]
56
- sentiments.append(result['label'])
57
- except Exception as e:
58
- sentiments.append("Error")
59
- st.error(f"Error processing text at row {i + 1}: {e}")
60
 
61
- progress_bar.progress((i + 1) / len(df))
62
- time.sleep(0.1) # Simulating processing time
 
 
63
 
64
- df['Sentiment'] = sentiments
65
- st.write("Sentiment Analysis Output:", df[['text', 'Sentiment']])
66
-
67
- # Pie chart
68
- sentiment_counts = df['Sentiment'].value_counts()
69
- fig, ax = plt.subplots()
70
- ax.pie(sentiment_counts, labels=sentiment_counts.index, autopct='%1.1f%%', startangle=90)
71
- ax.axis('equal')
72
- st.pyplot(fig)
73
-
74
- # Clear progress bar
75
- progress_bar.empty()
76
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
+ from transformers import pipeline
 
4
  import matplotlib.pyplot as plt
5
+ import time
6
+
7
+ # Load the sentiment analysis model
8
+ sentiment_model = pipeline("sentiment-analysis", model="tabularisai/multilingual-sentiment-analysis")
9
+
10
+ # Function to perform sentiment analysis
11
+ def perform_sentiment_analysis(texts):
12
+ sentiments = sentiment_model(texts)
13
+ return sentiments
14
+
15
+ # Function to plot the sentiment analysis results
16
+ def plot_sentiment_analysis(sentiments):
17
+ labels = [item['label'] for item in sentiments]
18
+ label_counts = pd.Series(labels).value_counts()
19
+
20
+ fig, ax = plt.subplots()
21
+ ax.pie(label_counts, labels=label_counts.index, autopct='%1.1f%%', startangle=90)
22
+ ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
23
 
24
+ st.pyplot(fig)
25
+
26
+ # Streamlit UI
27
  st.title("Sentiment Analysis App")
 
28
 
29
+ # File upload
30
  uploaded_file = st.file_uploader("Upload a CSV or Excel file", type=["csv", "xlsx"])
31
 
32
+ if uploaded_file is not None:
33
+ # Read the file
34
+ if uploaded_file.name.endswith(".csv"):
 
 
 
 
 
 
 
 
 
 
 
35
  df = pd.read_csv(uploaded_file)
 
 
36
  else:
37
+ df = pd.read_excel(uploaded_file, engine='openpyxl')
 
 
 
38
 
39
+ # Check if 'text' column exists
40
  if 'text' not in df.columns:
41
+ st.warning("Column 'text' not found. Please enter the column name containing the text values.")
42
+ text_column = st.text_input("Enter the column name containing the text values")
 
 
 
 
43
  else:
44
  text_column = 'text'
45
 
46
+ if text_column in df.columns:
47
+ # Display the first few rows of the dataframe
48
+ st.write("First few rows of the uploaded file:")
49
+ st.write(df.head())
50
 
51
+ # Perform sentiment analysis
52
+ if st.button("Run Sentiment Analysis"):
53
+ texts = df[text_column].tolist()
54
+ progress_bar = st.progress(0)
 
 
 
 
55
 
56
+ # Simulate progress
57
+ for i in range(100):
58
+ time.sleep(0.05)
59
+ progress_bar.progress(i + 1)
60
 
61
+ sentiments = perform_sentiment_analysis(texts)
62
+ st.success("Sentiment analysis completed!")
 
 
 
 
 
 
 
 
 
 
63
 
64
+ # Plot the sentiment analysis results
65
+ plot_sentiment_analysis(sentiments)
66
+ else:
67
+ st.error("The specified column does not exist in the uploaded file.")