Spaces:
Sleeping

RuthBebe's picture
Update app.py
e5a88ed
Raw History Blame Contribute Delete
3.58 kB
import nltk
nltk.download('stopwords')
from textblob import TextBlob
import pandas as pd
import streamlit as st
import cleantext
st.title("Sentiment Analysis")
#st.header('Sentiment Analysis')
st.sidebar.image("sent.png")
st.sidebar.title("Main Menu")
app_mode = st.sidebar.selectbox("Options",
["About", "Analyze Text", "Analyze CSV"])
if app_mode == "About":
st.subheader("Project Description")
st.markdown("The sentiment analysis project aims to develop a machine learning model that can accurately classify and analyze the sentiment expressed in textual data. ")
st.markdown("By utilizing natural language processing techniques, the project will focus on training a model to understand and interpret emotions, opinions, and attitudes conveyed in the text. The model will be trained on a diverse dataset consisting of comment reviews, social media posts, and other textual data sources. ")
st.markdown("This project is aimed at providing valuable insights into sentiment trends, enabling people to better understand feedback, improve decision-making processes, and enhance overall user experiences.")
st.image("sentiment.jpeg")
elif app_mode == "Analyze Text":
st.subheader("Analyze Text")
st.markdown("Enter a sentence which is to be analyzed.")
st.markdown("The Polarity and the Subjectivity of the sentence will be calculated to determine if the sentence carries weight or not.")
with st.expander('Analyze Text'):
text = st.text_input('Text here: ')
if text:
blob = TextBlob(text)
st.write('Polarity: ', round(blob.sentiment.polarity,2))
st.write('Subjectivity: ', round(blob.sentiment.subjectivity,2))
pre = st.text_input('Clean Text: ')
if pre:
cleaned_text=cleantext.clean(pre, extra_spaces=True ,
stopwords=True ,lowercase=True ,numbers=True , punct=True)
st.write(cleaned_text)
else:
with st.expander('Analyze CSV'):
upl = st.file_uploader('Upload file')
def score(x):
blob1 = TextBlob(x)
return blob1.sentiment.polarity
#
def analyze(x):
print("Polarity: ", x)
if x > 0.5:
return 'Positive'
elif x < -0.5:
return 'Negative'
else:
return 'Neutral'
#
if upl:
df = pd.read_excel(upl)
if 'Unnamed: 0' in df.columns:
df.drop('Unnamed: 0', axis=1, inplace=True)
#del df['Unnamed: 0']
# if 'tweets'in df.columns:
df['score'] = df['tweets'].apply(score)
df['analysis'] = df['score'].apply(analyze)
st.write(df.head(10))
#@st.cache
@st.cache_data
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode('utf-8')
csv = convert_df(df)
st.download_button(
label="Download data as CSV",
data=csv,
file_name='sentiment.csv',
mime='text/csv',
)
#else:
#st.write('Error: The uploaded file does not contain a "tweets" column.')