SuchithTissera commited on
Commit
183e8ed
·
verified ·
1 Parent(s): 7b6c2cc

Update pages/1_Classification.py

Browse files
Files changed (1) hide show
  1. pages/1_Classification.py +48 -10
pages/1_Classification.py CHANGED
@@ -1,8 +1,14 @@
 
 
1
  import streamlit as st
2
  import pandas as pd
3
- import matplotlib.pyplot as plt
4
  from transformers import pipeline
 
 
 
 
5
 
 
6
  @st.cache_resource
7
  def load_classifier():
8
  return pipeline(
@@ -11,31 +17,63 @@ def load_classifier():
11
  truncation=True
12
  )
13
 
 
14
  classifier = load_classifier()
15
 
 
16
  st.title("News Classifier")
 
 
 
 
 
 
 
 
 
 
 
17
  uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
18
 
 
19
  if uploaded_file is not None:
20
  try:
21
- df = pd.read_csv(uploaded_file)
22
-
23
- if "content" not in df.columns:
24
  st.error("The uploaded CSV must contain a column named `content`.")
25
  else:
26
- df["content"] = df["content"].fillna("").astype(str)
27
-
 
 
28
  with st.spinner("Classifying records..."):
29
- preds = classifier(df["content"].tolist(), batch_size=8)
30
 
31
- df["class"] = [pred["label"] for pred in preds]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  # save for other pages
34
- st.session_state["df"] = df
35
  st.session_state["uploaded_filename"] = uploaded_file.name
36
 
37
  st.success("Classification complete.")
38
- st.dataframe(df.head())
39
 
40
  except Exception as e:
41
  st.error(f"Error reading or processing file: {e}")
 
1
+ #importing the libraries needed
2
+ #for the classification
3
  import streamlit as st
4
  import pandas as pd
 
5
  from transformers import pipeline
6
+ #for the pre-processing of uploaded data
7
+ import string
8
+ import nltk
9
+ nltk.download("stopwords")
10
 
11
+ #calling and loading the fine tuned model from the hugging face account
12
  @st.cache_resource
13
  def load_classifier():
14
  return pipeline(
 
17
  truncation=True
18
  )
19
 
20
+ #saving the loaded model for convience in a variable
21
  classifier = load_classifier()
22
 
23
+ #writing the text content in the page
24
  st.title("News Classifier")
25
+ st.write("""In this page the user can upload a CSV file which contains a content
26
+ column which includes a text, then the model will use text pre-processing (anlysis step for more details)
27
+ and then label the data based on 5 categories,
28
+ - Business
29
+ - Opinions
30
+ - Political_gossip
31
+ - Sports
32
+ - Worlds_news
33
+ """)
34
+
35
+ #uploading the file
36
  uploaded_file = st.file_uploader("Upload CSV", type=["csv"])
37
 
38
+ #loop to carry out the transformation
39
  if uploaded_file is not None:
40
  try:
41
+ uploaded_content = pd.read_csv(uploaded_file)
42
+ #detect if a content coloumn is present
43
+ if "content" not in uploaded_content.columns:
44
  st.error("The uploaded CSV must contain a column named `content`.")
45
  else:
46
+ #removing empty strings
47
+ uploaded_content["content"] = uploaded_content["content"].fillna("").astype(str)
48
+ uploaded_content["pre-processed"] = uploaded_content["content"]
49
+ #showing the loading message to user
50
  with st.spinner("Classifying records..."):
 
51
 
52
+ #processing the data
53
+ #removing punctuations
54
+ string.punctuation
55
+ punct_to_remove = string.punctuation.replace("'",'').replace("-",'')
56
+ def remove_punctuation(text):
57
+ return text.translate(str.maketrans('', '', string.punctuation))
58
+ uploaded_content["pre-processed"] = uploaded_content["pre-processed"].apply(remove_punctuation)
59
+
60
+ #removing stop words
61
+ stop_words = set(stopwords.words('english'))
62
+ def remove_stopwords(text):
63
+ return " ".join([word for word in str(text).split() if word not in stop_words])
64
+ uploaded_content["pre-processed"] = uploaded_content["pre-processed"].apply(lambda text: remove_stopwords(text))
65
+
66
+ #constructing the labels
67
+ preds = classifier(uploaded_content["pre-processed"].tolist(), batch_size=8)
68
+
69
+ uploaded_content["class"] = [pred["label"] for pred in preds]
70
 
71
  # save for other pages
72
+ st.session_state["uploaded_content"] = uploaded_content
73
  st.session_state["uploaded_filename"] = uploaded_file.name
74
 
75
  st.success("Classification complete.")
76
+ st.dataframe(uploaded_content.head())
77
 
78
  except Exception as e:
79
  st.error(f"Error reading or processing file: {e}")