joyjonesmark commited on
Commit
b02cd71
·
1 Parent(s): 9f03742

Add application file

Browse files
Files changed (2) hide show
  1. app.py +67 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.")
requirements.txt ADDED
Binary file (2.29 kB). View file