CoolFace
Apppublic

sbaxi/test1

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
app.py165 linesDownload Raw Back to root
1import streamlit as st2# x = st.slider('Select a value')3# st.write(x, 'squared is', x * x)4 5import pandas as pd 6import numpy as np 7import nltk8#import re9#install needed packages10import snorkel11import textblob12#Snorkel13from snorkel.labeling import LabelingFunction14from snorkel.preprocess import preprocessor15from textblob import TextBlob16from snorkel.labeling import PandasLFApplier17from snorkel.labeling.model import LabelModel18from snorkel.labeling import LFAnalysis19from snorkel.labeling import filter_unlabeled_dataframe20from snorkel.labeling import labeling_function21 22 23def main():24    st.title('Sentiment Analysis')25    st.markdown('Upload an csv file to get sentiment analytics')26 27    file_train = st.file_uploader("Upload a csv training file", type=['csv'])28    review_column = None29    df = None30    # class_names = None  # New variable for class names31 32    if file_train is not None:33        try:34            #get both train and test datasets35            df = pd.read_csv(file_train, delimiter='\t')36            # Drop rows where all columns are NaN37            df = df.dropna(how='all')38            # Replace blank spaces with NaN, then drop rows where all columns are NaN again39            df = df.replace(r'^\s*$', np.nan, regex=True)40            df = df.dropna(how='all')41            review_column = st.selectbox('Select the column from your csv file containing text', df.columns)42            df[review_column] = df[review_column].astype(str)43            # filter_words_input = st.text_input('Enter words to filter the data by, separated by comma (or leave empty)')  # New input field for filter words44            # filter_words = [] if filter_words_input.strip() == "" else process_filter_words(filter_words_input)  # Process the filter words45            # class_names = st.text_input('Enter the possible class names separated by comma')  # New input field for class names46            # df = filter_dataframe(df, review_column, filter_words)  # Filter the DataFrame47        except Exception as e:48            st.write("An error occurred while reading the uploaded file. Please make sure it's a valid csv file.")49            return50 51    start_button = st.button('Start Analysis')52 53    54    if start_button is not None and df is not None:55        # Drop rows with NaN or blank values in the review_column56        df = df[df[review_column].notna()]57        df = df[df[review_column].str.strip() != '']58        # remove punctuation, numbers and special characters59        df[review_column] = df[review_column].str.replace("[^a-zA-Z#]", " ")60        #remove stop words like 'oh', 'hmm' of <3 characters61        df[review_column] = df[review_column].apply(lambda x: ' '.join([w for w in x.split() if len(w)>3]))62    63        # class_names = [name.strip() for name in class_names.split(',')]  # Split class names into a list64        # for name in class_names:  # Add a new column for each class name65        #     if name not in df.columns:66        #         df[name] = 0.067    68        if review_column in df.columns:69 70            with st.spinner('Performing Snorkel Technique...'):71                #positive news might contain the following words' 72                keyword_positive = make_keyword_lf(keywords=['boosts', 'great', 'develops', 'promising', 'ambitious', 'delighted', 'record', 'win', 'breakthrough', 'recover', 'achievement', 'peace', 'party', 'hope', 'flourish', 'respect', 'partnership', 'champion', 'positive', 'happy', 'bright', 'confident', 'encouraged', 'perfect', 'complete', 'assured' ])73                #negative news might contain the following words74                keyword_negative = make_keyword_lf(keywords=['war','solidiers', 'turmoil', 'injur','trouble', 'aggressive', 'killed', 'coup', 'evasion', 'strike', 'troops', 'dismisses', 'attacks', 'defeat', 'damage', 'dishonest', 'dead', 'fear', 'foul', 'fails', 'hostile', 'cuts', 'accusations', 'victims',  'death', 'unrest', 'fraud', 'dispute', 'destruction', 'battle', 'unhappy', 'bad', 'alarming', 'angry', 'anxious', 'dirty', 'pain', 'poison', 'unfair', 'unhealthy'75                                                              ], label=NEGATIVE)76 77            #set up a preprocessor function to determine polarity & subjectivity using textlob pretrained classifier 78            @preprocessor(memoize=True)79            def textblob_sentiment(x):80                scores = TextBlob(x.text)81                x.polarity = scores.sentiment.polarity82                x.subjectivity = scores.sentiment.subjectivity83                return x84            #find polarity85            @labeling_function(pre=[textblob_sentiment])86            def textblob_polarity(x):87                return POSITIVE if x.polarity > 0.6 else ABSTAIN88            #find subjectivity 89            @labeling_function(pre=[textblob_sentiment])90            def textblob_subjectivity(x):91                return POSITIVE if x.subjectivity >= 0.5 else ABSTAIN92 93            #combine all the labeling functions 94            lfs = [keyword_positive, keyword_negative, textblob_polarity, textblob_subjectivity ]95            #apply the lfs on the dataframe96            applier = PandasLFApplier(lfs=lfs)97            L_snorkel = applier.apply(df=df)98            #apply the label model99            label_model = LabelModel(cardinality=2, verbose=True)100            #fit on the data101            label_model.fit(L_snorkel)102            #predict and create the labels103            df["label"] = label_model.predict(L=L_snorkel)104            #Filtering out unlabeled data points105            df= df.loc[df.label.isin([0,1]), :]106            #find the label counts 107            df['label'].value_counts()108 109            with st.spinner('Performing tokenisation and stemming...'):110                tokenized_column = tokenise(df, review_column)111                df_display = stemming(df, tokenized_column, label)112                113            display(df, review_column)  # updated this line  114            115        else:116            st.write(f'No column named "{review_column}" found in the uploaded file.')117            118def tokenise(df, review_column):119    #split string into tokens for stemming120    tokenized_col = df[review_column].apply(lambda x: x.split())121    return tokenized_col122 123def stemming(df, tokenized_col, review_column):124    #strip suffixes like 'ing', 'ly', etc.125    from nltk import PorterStemmer126    ps = PorterStemmer()127    tokenized_col = tokenized_col.apply(lambda x: [ps.stem(i) for i in x])128 129    #stitch back the tokens130    for i in range(len(tokenized_col)):131        tokenized_col[i] = ' '.join(tokenized_col[i])132 133    df[review_column] = tokenized_col134    return df135 136#define constants to represent the class labels :positive, negative, and abstain137POSITIVE = 1138NEGATIVE = 0139ABSTAIN = -1140#define function which looks into the input words to represent a proper label141def keyword_lookup(x, keywords, label):  142    if any(word in x.text.lower() for word in keywords):143        return label144    return ABSTAIN145#define function which assigns a correct label146def make_keyword_lf(keywords, label=POSITIVE):147    return LabelingFunction(148        name=f"keyword_{keywords[0]}",149        f=keyword_lookup,150        resources=dict(keywords=keywords, label=label))151#resource: https://www.snorkel.org/use-cases/01-spam-tutorial#3-writing-more-labeling-functions152#these two lists can be further extended 153 154 155    156def display(df, tokenized_column, label):157    cols = st.columns(5)158    cols[1].markdown(f"### {df[tokenized_column]}")159    cols[2].markdown(f"### {df[label]}")160 161 162if __name__ == "__main__":163    main()164 165