Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
4 |
+
from textblob import TextBlob
|
5 |
+
from transformers import pipeline
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import os
|
8 |
+
from wordcloud import WordCloud
|
9 |
+
|
10 |
+
# Function to analyze sentiment using the custom Hugging Face pipeline
|
11 |
+
def analyze_sentiment_hf(text):
|
12 |
+
hf_pipeline = pipeline("sentiment-analysis", "RohitBh/Sentimental_Analysis")
|
13 |
+
if len(text) > 512:
|
14 |
+
text = text[:511]
|
15 |
+
sentiment_result = hf_pipeline(text)
|
16 |
+
sentiment_label = sentiment_result[0]["label"]
|
17 |
+
if sentiment_label == "LABEL_1":
|
18 |
+
return "Positive"
|
19 |
+
elif sentiment_label == "LABEL_0":
|
20 |
+
return "Negative"
|
21 |
+
else:
|
22 |
+
return "Neutral"
|
23 |
+
|
24 |
+
# Function to analyze sentiment using VADER
|
25 |
+
def analyze_sentiment_vader(text):
|
26 |
+
sentiment_analyzer = SentimentIntensityAnalyzer()
|
27 |
+
sentiment_score = sentiment_analyzer.polarity_scores(text)["compound"]
|
28 |
+
if sentiment_score > 0:
|
29 |
+
return "Positive"
|
30 |
+
elif sentiment_score == 0:
|
31 |
+
return "Neutral"
|
32 |
+
else:
|
33 |
+
return "Negative"
|
34 |
+
|
35 |
+
# Function to analyze sentiment using TextBlob
|
36 |
+
def analyze_sentiment_textblob(text):
|
37 |
+
sentiment_analysis = TextBlob(text)
|
38 |
+
score = sentiment_analysis.sentiment.polarity
|
39 |
+
if score > 0:
|
40 |
+
return "Positive"
|
41 |
+
elif score == 0:
|
42 |
+
return "Neutral"
|
43 |
+
else:
|
44 |
+
return "Negative"
|
45 |
+
|
46 |
+
# Function to display DataFrame with sentiment
|
47 |
+
def display_results_dataframe(data_frame):
|
48 |
+
st.write(data_frame)
|
49 |
+
|
50 |
+
# Function to display a pie chart of sentiment distribution
|
51 |
+
def create_pie_chart(data_frame, sentiment_column):
|
52 |
+
sentiment_distribution = data_frame[sentiment_column].value_counts()
|
53 |
+
fig, ax = plt.subplots()
|
54 |
+
ax.pie(sentiment_distribution, labels=sentiment_distribution.index, autopct='%1.1f%%', startangle=90)
|
55 |
+
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
|
56 |
+
st.pyplot(fig)
|
57 |
+
|
58 |
+
# Function to display word cloud based on sentiment data
|
59 |
+
def create_word_cloud(sentiment_data):
|
60 |
+
wordcloud_generator = WordCloud(width=800, height=400).generate(sentiment_data)
|
61 |
+
fig, ax = plt.subplots(figsize=(10, 5))
|
62 |
+
ax.imshow(wordcloud_generator, interpolation='bilinear')
|
63 |
+
ax.axis('off')
|
64 |
+
st.pyplot(fig)
|
65 |
+
|
66 |
+
# Main UI setup
|
67 |
+
st.set_page_config(page_title="Sentiment Analysis Tool", page_icon=":bar_chart:")
|
68 |
+
st.title("Sentiment Analysis Tool")
|
69 |
+
|
70 |
+
# Sidebar configuration for user input options
|
71 |
+
st.sidebar.title("Analysis Options")
|
72 |
+
input_type = st.sidebar.selectbox("Choose Input Type", ["Text Input", "CSV Upload"])
|
73 |
+
model_choice = st.sidebar.selectbox("Choose Sentiment Analysis Model", ["Hugging Face", "VADER", "TextBlob"])
|
74 |
+
display_type = st.sidebar.selectbox("Choose Display Type", ["DataFrame", "Pie Chart", "Word Cloud"])
|
75 |
+
|
76 |
+
# Process input based on user choice
|
77 |
+
if input_type == "Text Input":
|
78 |
+
user_text = st.text_input("Enter text for sentiment analysis:")
|
79 |
+
if st.button("Analyze Sentiment"):
|
80 |
+
if user_text:
|
81 |
+
# Analyzing sentiment based on selected model
|
82 |
+
if model_choice == "Hugging Face":
|
83 |
+
sentiment = analyze_sentiment_hf(user_text)
|
84 |
+
elif model_choice == "VADER":
|
85 |
+
sentiment = analyze_sentiment_vader(user_text)
|
86 |
+
else:
|
87 |
+
sentiment = analyze_sentiment_textblob(user_text)
|
88 |
+
|
89 |
+
st.write("Detected Sentiment:", sentiment)
|
90 |
+
else:
|
91 |
+
st.warning("Please enter some text to analyze.")
|
92 |
+
elif input_type == "CSV Upload":
|
93 |
+
uploaded_file = st.file_uploader("Upload CSV file for analysis", type="csv")
|
94 |
+
if st.button("Start Analysis"):
|
95 |
+
if uploaded_file is not None:
|
96 |
+
data_frame = pd.read_csv(uploaded_file)
|
97 |
+
# Assuming the CSV has a column named 'text' for analysis
|
98 |
+
if 'text' in data_frame.columns:
|
99 |
+
data_frame['Sentiment'] = data_frame['text'].apply(lambda x: analyze_sentiment_hf(x) if model_choice == "Hugging Face" else (analyze_sentiment_vader(x) if model_choice == "VADER" else analyze_sentiment_textblob(x)))
|
100 |
+
|
101 |
+
if display_type == "DataFrame":
|
102 |
+
display_results_dataframe(data_frame)
|
103 |
+
elif display_type == "Pie Chart":
|
104 |
+
create_pie_chart(data_frame, 'Sentiment')
|
105 |
+
elif display_type == "Word Cloud":
|
106 |
+
combined_text = ' '.join(data_frame['text'])
|
107 |
+
create_word_cloud(combined_text)
|
108 |
+
else:
|
109 |
+
st.error("The uploaded CSV file must contain a 'text' column.")
|
110 |
+
else:
|
111 |
+
st.warning("Please upload a CSV file to proceed with analysis.")
|