#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('
📂 News Classifier
',unsafe_allow_html=True) st.markdown("""
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,
""",unsafe_allow_html=True) st.markdown("""
""", unsafe_allow_html=True) #dividing the page st.divider() #uploading the file st.markdown( '
Upload your CSV file:
', 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}")