Adityaganesh commited on
Commit
de79f8d
Β·
verified Β·
1 Parent(s): bbb6e4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -41
app.py CHANGED
@@ -18,13 +18,13 @@ nltk.download('wordnet')
18
  import tensorflow as tf
19
  import keras
20
  from keras.preprocessing.sequence import pad_sequences
21
-
22
  import pickle
23
 
24
- # Set Streamlit page configuration
25
- st.set_page_config(page_title="News Category Classifier", page_icon="πŸ“°", layout="centered")
 
26
 
27
- # Function to set background image correctly
28
  def set_background(image_path):
29
  if not os.path.exists(image_path):
30
  st.error(f"❌ Background image not found: {image_path}")
@@ -36,37 +36,42 @@ def set_background(image_path):
36
  bg_image_style = f"""
37
  <style>
38
  .stApp {{
39
- background: url("data:image/jpg;base64,{encoded_img}") no-repeat center center fixed;
40
  background-size: cover;
 
 
 
41
  }}
42
  </style>
43
  """
44
  st.markdown(bg_image_style, unsafe_allow_html=True)
45
 
46
- # Set background image
47
- set_background("Images/News image.jpg")
48
 
49
 
50
- # Initialize stopwords and lemmatizer
51
  stop_words = set(stopwords.words('english')).union({"pm"})
52
  lemmatizer = WordNetLemmatizer()
53
 
54
- # Preprocessing function
55
- def pre_process(x):
56
- x = x.lower()
57
- x = re.sub("<.*?>", "", x) # Remove HTML tags
58
- x = re.sub(r"http[s]?://\S+", "", x) # Remove URLs
59
- x = re.sub(r"[@#]\S+", "", x) # Remove mentions and hashtags
60
- x = re.sub(r"\_+", " ", x) # Replace underscores with space
61
- x = emoji.demojize(x) # Convert emojis to text
62
- x = re.sub(r":.*?:", "", x) # Remove emoji text
63
- x = re.sub(r"[^a-zA-Z0-9\s_]", "", x) # Remove special characters
64
- words = word_tokenize(x)
 
65
  words = [word for word in words if word not in stop_words]
66
- x = " ".join([lemmatizer.lemmatize(word) for word in words])
67
- return x
 
68
 
69
- # Cache model loading to improve performance
70
  @st.cache_resource
71
  def load_model():
72
  model_path = "news_model.keras"
@@ -75,16 +80,18 @@ def load_model():
75
 
76
  model = keras.models.load_model(model_path)
77
  vectorizer = keras.models.load_model(vectorizer_path)
78
-
79
  with open(label_encoder_path, 'rb') as file:
80
  label_encoder = pickle.load(file)
81
 
82
  return model, vectorizer, label_encoder
83
 
84
- # Load the models
 
85
  model, vectorizer, label_encoder = load_model()
86
 
87
- # Prediction function
 
88
  def predict_category(text):
89
  processed_text = [pre_process(text)]
90
  text_vectorized = pad_sequences(vectorizer(processed_text).numpy().tolist(), padding='pre', maxlen=82)
@@ -92,34 +99,35 @@ def predict_category(text):
92
  category_idx = np.argmax(prediction, axis=1)[0]
93
  return label_encoder.inverse_transform([category_idx])[0]
94
 
95
- # Streamlit UI
 
96
  st.markdown(
97
  """
98
  <style>
99
  .title {
100
  color: #ffffff;
101
- font-size: 2.4em;
102
  text-align: center;
103
  font-weight: 700;
104
  text-transform: uppercase;
105
  text-shadow: 2px 2px 8px rgba(0, 0, 0, 1.0);
106
- padding: 10px;
107
  }
108
  .subtitle {
109
- color: #ffff;
110
- font-size: 1.3em;
111
  text-align: center;
112
  font-weight: 600;
113
  text-shadow: 1px 1px 6px rgba(0, 0, 0, 1.0);
114
- padding: 5px;
115
  }
116
  .classify-button {
117
  background-color: #3498db;
118
  color: white;
119
- font-size: 1.2em;
120
- padding: 12px 24px;
121
  border: none;
122
- border-radius: 8px;
123
  cursor: pointer;
124
  display: block;
125
  margin: 20px auto;
@@ -130,13 +138,13 @@ st.markdown(
130
  }
131
  .result-box {
132
  background: linear-gradient(135deg, #6284FF 30%, #FF0000 70%);
133
- padding: 20px;
134
- border-radius: 10px;
135
  text-align: center;
136
  margin-top: 30px;
137
  position: relative;
138
  overflow: hidden;
139
- border: 2px solid transparent;
140
  background-clip: padding-box, border-box;
141
  border-image: linear-gradient(135deg, #6284FF 30%, #FF0000 70%);
142
  border-image-slice: 0;
@@ -148,7 +156,7 @@ st.markdown(
148
  0px 10px 30px rgba(255, 0, 0, 0.8);
149
  }
150
  .result-text {
151
- font-size: 1.8em;
152
  color: #ffffff;
153
  font-weight: 900;
154
  text-shadow: 3px 3px 10px rgba(0, 0, 0, 0.5);
@@ -159,14 +167,14 @@ st.markdown(
159
  unsafe_allow_html=True
160
  )
161
 
162
- # Page title
163
  st.markdown("<div class='title'>πŸ“° News Classifier</div>", unsafe_allow_html=True)
164
  st.markdown("<div class='subtitle'>Enter a news headline or article snippet to analyze its category.</div>", unsafe_allow_html=True)
165
 
166
- # User input
167
  user_input = st.text_area("Enter text here:", height=150, placeholder="Type your news text here...")
168
 
169
- # Button to analyze
170
  if st.button("Analyze 🍿", key="analyze_button"):
171
  if user_input.strip():
172
  category = predict_category(user_input)
 
18
  import tensorflow as tf
19
  import keras
20
  from keras.preprocessing.sequence import pad_sequences
 
21
  import pickle
22
 
23
+ # βœ… Enable full-width mode for Hugging Face
24
+ st.set_page_config(page_title="News Category Classifier", page_icon="πŸ“°", layout="wide")
25
+
26
 
27
+ # βœ… Function to set background image
28
  def set_background(image_path):
29
  if not os.path.exists(image_path):
30
  st.error(f"❌ Background image not found: {image_path}")
 
36
  bg_image_style = f"""
37
  <style>
38
  .stApp {{
39
+ background-image: url("data:image/jpg;base64,{encoded_img}");
40
  background-size: cover;
41
+ background-repeat: no-repeat;
42
+ background-position: center;
43
+ background-attachment: fixed;
44
  }}
45
  </style>
46
  """
47
  st.markdown(bg_image_style, unsafe_allow_html=True)
48
 
49
+ # βœ… Set background image (Make sure the image is in the same directory)
50
+ set_background("Images/News_image.jpg")
51
 
52
 
53
+ # βœ… Initialize stopwords and lemmatizer
54
  stop_words = set(stopwords.words('english')).union({"pm"})
55
  lemmatizer = WordNetLemmatizer()
56
 
57
+
58
+ # βœ… Text Preprocessing Function
59
+ def pre_process(text):
60
+ text = text.lower()
61
+ text = re.sub("<.*?>", "", text) # Remove HTML tags
62
+ text = re.sub("http[s]?://\\S+", "", text) # Remove URLs
63
+ text = re.sub("[@#]\\S+", "", text) # Remove mentions and hashtags
64
+ text = re.sub(r"\\_+", " ", text) # Replace underscores with spaces
65
+ text = emoji.demojize(text) # Convert emojis to text
66
+ text = re.sub(":.*?:", "", text) # Remove emoji text
67
+ text = re.sub("[^a-zA-Z0-9\\s_]", "", text) # Remove special characters
68
+ words = word_tokenize(text)
69
  words = [word for word in words if word not in stop_words]
70
+ text = " ".join([lemmatizer.lemmatize(word) for word in words])
71
+ return text
72
+
73
 
74
+ # βœ… Cache Model Loading for Performance
75
  @st.cache_resource
76
  def load_model():
77
  model_path = "news_model.keras"
 
80
 
81
  model = keras.models.load_model(model_path)
82
  vectorizer = keras.models.load_model(vectorizer_path)
83
+
84
  with open(label_encoder_path, 'rb') as file:
85
  label_encoder = pickle.load(file)
86
 
87
  return model, vectorizer, label_encoder
88
 
89
+
90
+ # βœ… Load the models
91
  model, vectorizer, label_encoder = load_model()
92
 
93
+
94
+ # βœ… Prediction Function
95
  def predict_category(text):
96
  processed_text = [pre_process(text)]
97
  text_vectorized = pad_sequences(vectorizer(processed_text).numpy().tolist(), padding='pre', maxlen=82)
 
99
  category_idx = np.argmax(prediction, axis=1)[0]
100
  return label_encoder.inverse_transform([category_idx])[0]
101
 
102
+
103
+ # βœ… Streamlit UI Design
104
  st.markdown(
105
  """
106
  <style>
107
  .title {
108
  color: #ffffff;
109
+ font-size: 2.8em;
110
  text-align: center;
111
  font-weight: 700;
112
  text-transform: uppercase;
113
  text-shadow: 2px 2px 8px rgba(0, 0, 0, 1.0);
114
+ padding: 15px;
115
  }
116
  .subtitle {
117
+ color: #ffffff;
118
+ font-size: 1.5em;
119
  text-align: center;
120
  font-weight: 600;
121
  text-shadow: 1px 1px 6px rgba(0, 0, 0, 1.0);
122
+ padding: 10px;
123
  }
124
  .classify-button {
125
  background-color: #3498db;
126
  color: white;
127
+ font-size: 1.3em;
128
+ padding: 14px 28px;
129
  border: none;
130
+ border-radius: 10px;
131
  cursor: pointer;
132
  display: block;
133
  margin: 20px auto;
 
138
  }
139
  .result-box {
140
  background: linear-gradient(135deg, #6284FF 30%, #FF0000 70%);
141
+ padding: 25px;
142
+ border-radius: 12px;
143
  text-align: center;
144
  margin-top: 30px;
145
  position: relative;
146
  overflow: hidden;
147
+ border: 3px solid transparent;
148
  background-clip: padding-box, border-box;
149
  border-image: linear-gradient(135deg, #6284FF 30%, #FF0000 70%);
150
  border-image-slice: 0;
 
156
  0px 10px 30px rgba(255, 0, 0, 0.8);
157
  }
158
  .result-text {
159
+ font-size: 2em;
160
  color: #ffffff;
161
  font-weight: 900;
162
  text-shadow: 3px 3px 10px rgba(0, 0, 0, 0.5);
 
167
  unsafe_allow_html=True
168
  )
169
 
170
+ # βœ… Page Title
171
  st.markdown("<div class='title'>πŸ“° News Classifier</div>", unsafe_allow_html=True)
172
  st.markdown("<div class='subtitle'>Enter a news headline or article snippet to analyze its category.</div>", unsafe_allow_html=True)
173
 
174
+ # βœ… User Input
175
  user_input = st.text_area("Enter text here:", height=150, placeholder="Type your news text here...")
176
 
177
+ # βœ… Analyze Button
178
  if st.button("Analyze 🍿", key="analyze_button"):
179
  if user_input.strip():
180
  category = predict_category(user_input)