Adityaganesh commited on
Commit
77fcbcc
Β·
verified Β·
1 Parent(s): d6bea13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -48
app.py CHANGED
@@ -4,7 +4,6 @@ import numpy as np
4
  import re
5
  import emoji
6
  import os
7
-
8
  import nltk
9
  from nltk.tokenize import word_tokenize
10
  from nltk.corpus import stopwords
@@ -21,8 +20,7 @@ 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="Intelligent News Classifier", page_icon="🧠 ", layout="wide")
25
-
26
 
27
  # βœ… Function to set background image
28
  def set_background(image_path):
@@ -46,15 +44,13 @@ def set_background(image_path):
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/picture.png")
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()
@@ -70,7 +66,6 @@ def pre_process(text):
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():
@@ -86,11 +81,9 @@ def load_model():
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)]
@@ -99,10 +92,18 @@ def predict_category(text):
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;
@@ -121,51 +122,17 @@ st.markdown(
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;
134
- transition: 0.3s;
135
- }
136
- .classify-button:hover {
137
- background-color: #2980b9;
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;
151
- transition: transform 0.3s ease-in-out, box-shadow 0.3s ease-in-out;
152
- }
153
- .result-box:hover {
154
- transform: scale(1.05);
155
- box-shadow: 0px 10px 30px rgba(98, 132, 255, 0.8),
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);
163
- animation: fadeIn 0.8s ease-in-out;
164
  }
165
  </style>
166
- """,
167
- unsafe_allow_html=True
168
- )
169
 
170
  # βœ… Page Title
171
  st.markdown("<div class='title'>🧠 Intelligent News Classifier</div>", unsafe_allow_html=True)
@@ -175,9 +142,17 @@ st.markdown("<div class='subtitle'>Find out what type of news you're reading!.</
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)
181
- st.markdown(f"<div class='result-box'><span class='result-text'>πŸ—‚οΈ Predicted Category: <strong>{category}</strong></span></div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
182
  else:
183
  st.warning("⚠️ Please enter some text to analyze.")
 
4
  import re
5
  import emoji
6
  import os
 
7
  import nltk
8
  from nltk.tokenize import word_tokenize
9
  from nltk.corpus import stopwords
 
20
  import pickle
21
 
22
  # βœ… Enable full-width mode for Hugging Face
23
+ st.set_page_config(page_title="Intelligent News Classifier", page_icon="🧠", layout="wide")
 
24
 
25
  # βœ… Function to set background image
26
  def set_background(image_path):
 
44
  """
45
  st.markdown(bg_image_style, unsafe_allow_html=True)
46
 
47
+ # βœ… Set background image
48
  set_background("Images/picture.png")
49
 
 
50
  # βœ… Initialize stopwords and lemmatizer
51
  stop_words = set(stopwords.words('english')).union({"pm"})
52
  lemmatizer = WordNetLemmatizer()
53
 
 
54
  # βœ… Text Preprocessing Function
55
  def pre_process(text):
56
  text = text.lower()
 
66
  text = " ".join([lemmatizer.lemmatize(word) for word in words])
67
  return text
68
 
 
69
  # βœ… Cache Model Loading for Performance
70
  @st.cache_resource
71
  def load_model():
 
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)]
 
92
  category_idx = np.argmax(prediction, axis=1)[0]
93
  return label_encoder.inverse_transform([category_idx])[0]
94
 
95
+ # βœ… Category Color Mapping
96
+ category_colors = {
97
+ "Sports": "#27ae60", # Green
98
+ "Politics": "#2980b9", # Blue
99
+ "Entertainment": "#8e44ad", # Purple
100
+ "Technology": "#e67e22", # Orange
101
+ "Business": "#c0392b", # Red
102
+ "Default": "#ffffff" # White
103
+ }
104
 
105
  # βœ… Streamlit UI Design
106
+ st.markdown("""
 
107
  <style>
108
  .title {
109
  color: #ffffff;
 
122
  text-shadow: 1px 1px 6px rgba(0, 0, 0, 1.0);
123
  padding: 10px;
124
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  .result-box {
 
126
  padding: 25px;
127
  border-radius: 12px;
128
  text-align: center;
129
  margin-top: 30px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  font-size: 2em;
 
131
  font-weight: 900;
132
  text-shadow: 3px 3px 10px rgba(0, 0, 0, 0.5);
 
133
  }
134
  </style>
135
+ """, unsafe_allow_html=True)
 
 
136
 
137
  # βœ… Page Title
138
  st.markdown("<div class='title'>🧠 Intelligent News Classifier</div>", unsafe_allow_html=True)
 
142
  user_input = st.text_area("Enter text here:", height=150, placeholder="Type your news text here...")
143
 
144
  # βœ… Analyze Button
145
+ if st.button("Analyze 🧐"):
146
  if user_input.strip():
147
  category = predict_category(user_input)
148
+ color = category_colors.get(category, category_colors["Default"])
149
+ st.markdown(
150
+ f"""
151
+ <div class='result-box' style='color: {color};'>
152
+ πŸ—‚οΈ Predicted Category: <strong>{category}</strong>
153
+ </div>
154
+ """,
155
+ unsafe_allow_html=True
156
+ )
157
  else:
158
  st.warning("⚠️ Please enter some text to analyze.")