File size: 5,793 Bytes
183e8ed
 
bcea786
 
 
183e8ed
 
7cd68df
b90d486
 
bcea786
183e8ed
8a50e53
 
 
 
 
 
 
7cd68df
2ce66a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cd68df
2ce66a7
 
 
183e8ed
8a50e53
7b9f051
183e8ed
180070a
 
 
d8c1cd4
1d08678
180070a
1d08678
 
 
 
 
 
 
 
 
8e64a1a
7cd68df
8e64a1a
 
183e8ed
8e64a1a
180070a
8e64a1a
 
 
7cd68df
8e64a1a
49c60b0
183e8ed
49c60b0
 
183e8ed
 
 
49c60b0
 
183e8ed
 
f5be05d
fcb60ac
183e8ed
49c60b0
 
183e8ed
 
 
 
 
2ce66a7
6d0cd46
183e8ed
 
 
 
 
49c60b0
4d18ca7
183e8ed
4d18ca7
49c60b0
4d18ca7
183e8ed
49c60b0
08b60fe
 
 
 
 
 
 
 
 
 
7cd68df
7b9f051
7cd68df
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#importing the libraries needed
#for the classification
import streamlit as st
import pandas as pd
from transformers import pipeline
#for the pre-processing of uploaded data
import string 
#adding the css presentation from the styles file in utils
from utils.styles import load_css
load_css()

#calling and loading the fine tuned model from the hugging face account
@st.cache_resource
def load_classifier():
    return pipeline(
        "text-classification",
        model="SuchithTissera/text_analytics_model",
        truncation=True
    )
#adding stop word based on nlk library - look at limitations for the reason
stop_words = {'most', "i've", 't', "we'll", 'mustn', 'we', 'into', 'they', 'on', "they'll", 
              'haven', 'after', 'had', 'myself', 'she', 'too', 'won', 'over', 'any', "you've", 
              'not', 'me', "they're", "wouldn't", 'other', 'have', 'once', 'yourselves', 'doing', 
              'mightn', 'been', "he'll", "she'll", 'the', "wasn't", 'to', 'further', "hadn't", "i'll", 
              'my', 'or', 'against', "doesn't", 'can', 'you', 'down', 'of', 'ain', 'below', 'these', "they'd", 
              'but', 'here', 'was', 'and', 'both', 'so', 'your', 'needn', 'has', "she's", 'as', "should've", 
              'very', 've', 'them', "aren't", "shouldn't", 'where', 'while', 'our', "mustn't", 'yourself', 'by', 
              'now', 'be', 'do', 'y', 'should', 'about', 'for', 'her', 'then', 'from', "isn't", "he'd", 'whom', 
              'herself', 'before', "he's", "you'd", 'm', "didn't", "we've", 'no', "hasn't", "needn't", 'off', 's', 
              'some', 'are', 'all', "weren't", "i'm", 'theirs', 'wasn', 'during', 'didn', "haven't", 'own', 'at', 
              'hasn', 'his', 'shan', "it's", 'just', 'ourselves', 'between', 'its', 'again', "she'd", 're', 'that', 
              'few', 'more', 'hadn', 'is', 'nor', "you'll", 'having', 'than', "i'd", 'only', 'aren', "you're", "couldn't",
              "won't", 'each', 'if', 'am', 'hers', 'what', 'weren', 'will', 'an', 'it', "they've", "shan't", "that'll", 'll', 
              'he', 'how', "we'd", 'did', 'isn', 'in', 'ma', 'o', 'themselves', "don't", 'shouldn', 'those', 'why', 'who',
              "we're", "it'd", 'wouldn', 'don', 'until', 'up', 'out', 'couldn', 'yours', "it'll", 'because', 'there', 
              'a', 'same', 'which', 'being', 'himself', 'under', 'through', 'when', "mightn't", 'ours', 'above', 'were', 
              'd', 'does', 'such', 'doesn', 'itself', 'this', 'i', 'their', 'with', 'him'}

#constructing the stopwork function
def remove_stopwords(text):
    return " ".join([word for word in str(text).lower().split() if word not in stop_words])

#saving the loaded model for convience in a variable 
classifier = load_classifier()

#writing the text content in the page
st.markdown('<div class="main_title">📂 News Classifier </div>',unsafe_allow_html=True)
st.markdown("""<div class="feature_text">In this page the user can upload a CSV file which contains a content 
        column which includes text, then the model will use text pre-processing (anlysis step for more details) 
        and then label the data based on 5 categories,</div>""",unsafe_allow_html=True)
st.markdown("""
<div class="feature_text">
<ul>
    <li>Business</li>
    <li>Opinions</li>
    <li>Political_gossip</li>
    <li>Sports</li>
    <li>Worlds_news</li>
</ul>
</div>
""", unsafe_allow_html=True)

#dividing the page
st.divider()

#uploading the file 
st.markdown(
    '<div class="feature_text">Upload your CSV file:</div>',
    unsafe_allow_html=True
)

#saving the csv file in a variable
uploaded_file = st.file_uploader("", type=["csv"])

#loop to carry out the transformation
if uploaded_file is not None:
    try:
        uploaded_content = pd.read_csv(uploaded_file)
        #detect if a content coloumn is present
        if "content" not in uploaded_content.columns:
            st.error("The uploaded CSV must contain a column named `content`.")
        else:
            #removing empty strings
            uploaded_content["content"] = uploaded_content["content"].fillna("").astype(str)
            #saving the pre-processed datapoints as lowercase 
            uploaded_content["pre-processed"] = uploaded_content["content"].str.lower()
            #showing the loading message to user 
            with st.spinner("Classifying records..."):

                #processing the data
                #removing punctuations
                def remove_punctuation(text):
                    return text.translate(str.maketrans('', '', string.punctuation))
                uploaded_content["pre-processed"] = uploaded_content["pre-processed"].apply(remove_punctuation)
                #removing stop words 
                uploaded_content["pre-processed"] = uploaded_content["pre-processed"].apply(remove_stopwords)

                #constructing the labels
                preds = classifier(uploaded_content["pre-processed"].tolist(), batch_size=8)

            uploaded_content["class"] = [pred["label"] for pred in preds]

            # save for other pages
            st.session_state["uploaded_content"] = uploaded_content
            st.session_state["uploaded_filename"] = uploaded_file.name

            st.success("Classification complete.")
            st.dataframe(uploaded_content.head())

            #converting to a csv
            output_csv = uploaded_content.to_csv(index=False).encode("utf-8")

            #download action
            st.download_button(
                label="Download output.csv",
                data=output_csv,
                file_name="output.csv",
                mime="text/csv"
            )
    #exception handling if errors happpen
    except Exception as e:
        st.error(f"Error reading or processing file: {e}")