Mpavan45 commited on
Commit
3a6e7b6
·
verified ·
1 Parent(s): 908e7fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -214
app.py CHANGED
@@ -1,270 +1,192 @@
1
- import streamlit as st
2
  import numpy as np
3
  import re
4
  import emoji
5
- from textblob import TextBlob
6
- import spacy
7
  import nltk
 
8
  from nltk.corpus import stopwords
 
9
  import tensorflow as tf
10
  import keras
11
  from keras.utils import pad_sequences
12
  import pickle
13
- # Page Config
14
- st.set_page_config(page_title="Newsense AI", page_icon="📰", layout="wide")
15
 
16
- # Custom HTML and CSS for center alignment
17
- st.markdown(
18
- """
19
- <style>
20
- .center-title {
21
- display: flex;
22
- justify-content: center;
23
- align-items: center;
24
- gap: 10px;
25
- font-size: 50px;
26
- font-weight: bold;
27
- color: white;
28
- background: linear-gradient(135deg, #FF6B6B, #6B7EFF, #6BFF95, #FFDE59);
29
- padding: 20px;
30
- border-radius: 20px;
31
- box-shadow: 0 10px 35px rgba(255, 107, 107, 0.7),
32
- 0 5px 20px rgba(107, 126, 255, 0.7);
33
- animation: fadeSlide 1.5s ease-out forwards;
34
- width: fit-content;
35
- margin: 30px auto;
36
- }
37
 
38
- @keyframes fadeSlide {
39
- from {
40
- opacity: 0;
41
- transform: translateY(-50px);
42
- }
43
- to {
44
- opacity: 1;
45
- transform: translateY(0);
46
- }
47
- }
 
 
 
 
 
 
48
  </style>
49
- <div class="center-title">📰 Newsense AI</div>
50
- """,
51
- unsafe_allow_html=True
52
- )
53
-
54
-
55
- # # Download necessary resources
56
- # # nltk.download('stopwords')
57
-
58
- # # Load SpaCy model
59
- # nlp = spacy.load("en_core_web_sm")
60
-
61
- # # Stopwords
62
- # stop_words = set(stopwords.words('english')).union({"pm"})
63
-
64
- # # Pre-processing function (without parentheses extraction)
65
- # def pre_process(x):
66
- # # Convert to lowercase
67
- # x = x.lower()
68
-
69
- # # Remove HTML tags
70
- # x = re.sub(r"<.*?>", "", x)
71
-
72
- # # Remove URLs
73
- # x = re.sub(r"http[s]?://\S+", "", x)
74
-
75
- # # Remove mentions (@, #)
76
- # x = re.sub(r"[@#]\S+", "", x)
77
-
78
- # # Remove emojis
79
- # x = emoji.replace_emoji(x, replace="")
80
-
81
- # # Remove special characters (-, ., :, \, ,)
82
- # x = re.sub(r"[-.:,\\]", " ", x)
83
-
84
- # # Remove single and double quotes
85
- # x = re.sub(r"['\"](.*?)['\"]", r'\1', x)
86
-
87
- # # Remove content inside parentheses
88
- # x = re.sub(r"\(.*?\)", "", x)
89
-
90
- # # Remove extra spaces
91
- # x = re.sub(r"\s+", " ", x).strip()
92
-
93
- # # Spell checking
94
- # x = str(TextBlob(x).correct())
95
-
96
- # # Lemmatization using SpaCy
97
- # x = " ".join([token.lemma_ for token in nlp(x)])
98
-
99
- # return " ".join(x)
100
-
101
- # @st.cache_resource
102
- # def load_model():
103
- # model = keras.models.load_model("model_m3_new.keras")
104
- # with open("label_encoder_m5.pkl", 'rb') as file:
105
- # label_encoder = pickle.load(file)
106
- # return model, label_encoder
107
-
108
- # model, label_encoder = load_model()
109
-
110
- # def predict_category(text):
111
- # cleaned_text = pre_process(text)
112
-
113
- # vectorizer = keras.models.load_model("vec_text_m3_new.keras")
114
-
115
- # # Vectorizing the pre-processed text
116
- # text_vectorized = pad_sequences(vectorizer.predict(np.array([cleaned_text])).numpy(), padding='pre', maxlen=128)
117
-
118
- # # Model prediction
119
- # prediction = model.predict(text_vectorized)
120
- # category_idx = np.argmax(prediction, axis=1)[0]
121
-
122
- # return label_encoder.inverse_transform([category_idx])[0], cleaned_text
123
-
124
  st.markdown(
125
  """
126
  <style>
127
  body {
128
- background-image: url('https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/WwOWG8MBGYxHnIeM2Dowo.webp');
129
  background-size: cover;
130
  background-repeat: no-repeat;
131
  background-attachment: fixed;
132
  }
133
-
134
- /* Title Styling */
 
135
  .title {
136
  font-size: 60px;
137
  font-weight: bold;
138
  color: white;
139
- background: linear-gradient(135deg, #FF6B6B, #6B7EFF, #6BFF95, #FFDE59);
140
  padding: 20px;
141
  border-radius: 20px;
142
- box-shadow: 0 10px 35px rgba(255, 107, 107, 0.7),
143
- 0 5px 20px rgba(107, 126, 255, 0.7);
 
144
  display: inline-block;
145
- margin-bottom: 30px;
146
- text-align: center;
147
- animation: fadeSlide 1.5s ease-out forwards;
148
  }
149
-
150
- /* Input Box Styling */
151
- .input-box {
152
- display: flex;
153
- flex-direction: column;
154
- align-items: center;
155
- gap: 20px;
156
- margin: 0 auto;
157
- width: 80%;
158
- }
159
-
160
- .input-prompt {
161
- font-size: 24px;
162
  font-weight: bold;
163
- color: #ffffff;
164
  text-align: center;
165
- opacity: 0.9;
166
- text-shadow: 0 0 8px #6BFF95;
 
 
 
 
 
 
 
167
  }
168
 
169
- /* Text Area Styling */
170
- div.stTextArea textarea {
171
- width: 100%;
172
- height: 200px;
173
- padding: 20px;
174
- border-radius: 15px;
175
- background: rgba(0, 0, 0, 0.8);
176
- color: #FFFFFF;
177
- font-size: 18px;
178
- outline: none;
179
- box-shadow: 0 8px 25px rgba(107, 126, 255, 0.5);
180
- transition: all 0.5s ease;
181
  }
182
 
183
- div.stTextArea textarea:hover {
184
- transform: scale(1.05);
185
- box-shadow: 0 12px 40px rgba(107, 126, 255, 0.8);
 
186
  }
187
 
188
- /* Button Styling */
189
  .analyze-button {
190
- width: 220px;
191
- height: 65px;
192
- border-radius: 35px;
193
- background: linear-gradient(45deg, #FF6B6B, #6B7EFF, #6BFF95, #FFDE59);
194
- font-size: 22px;
195
  font-weight: bold;
196
- color: #000000;
197
  border: none;
 
 
 
 
198
  cursor: pointer;
199
- transition: all 0.4s ease;
200
- box-shadow: 0 8px 25px rgba(255, 107, 107, 0.7);
201
  }
202
-
203
  .analyze-button:hover {
204
  transform: scale(1.1);
205
- box-shadow: 0 14px 45px rgba(255, 222, 89, 0.8);
 
 
 
206
  }
207
-
208
- /* Result Box Styling */
209
  .result-box {
210
  text-align: center;
211
- font-size: 30px;
212
  font-weight: bold;
213
- color: #ffffff;
214
- background: linear-gradient(135deg, #6B7EFF, #6BFF95, #FFDE59, #FF6B6B);
215
- padding: 35px;
216
- border-radius: 25px;
217
- box-shadow: 0 10px 30px rgba(107, 255, 107, 0.6);
218
- margin-top: 40px;
219
- animation: fadeIn 1.2s ease-in;
220
- }
221
-
222
- /* Animations */
223
- @keyframes fadeSlide {
224
- from {
225
- opacity: 0;
226
- transform: translateY(-50px);
227
- }
228
- to {
229
- opacity: 1;
230
- transform: translateY(0);
231
- }
232
- }
233
-
234
- @keyframes fadeIn {
235
- from {
236
- opacity: 0;
237
- }
238
- to {
239
- opacity: 1;
240
- }
241
  }
242
-
243
  </style>
244
  """,
245
  unsafe_allow_html=True
246
  )
247
 
 
 
248
 
249
- st.markdown(
250
- <div class="title">📰 Newsense AI - News Classification</div>,
251
- unsafe_allow_html=True
252
- )
253
-
254
-
255
- # Input and button section
256
- st.markdown('<div class="input-box">', unsafe_allow_html=True)
257
- user_input = st.text_area("Enter your news article:", height=200)
258
 
259
- # Predict button
260
- if st.button("Classify", key="analyze-button"):
261
- if user_input:
262
- category, cleaned_text = predict_category(user_input)
263
-
264
- # Display the prediction and cleaned text
265
- st.markdown(f'<div class="result-box">Prediction: {category}</div>', unsafe_allow_html=True)
266
- st.markdown(f'<div class="result-box">Cleaned Text: {cleaned_text}</div>', unsafe_allow_html=True)
267
  else:
268
- st.warning("Please enter some text to classify!")
269
-
270
- st.markdown('</div>', unsafe_allow_html=True)
 
1
+ import streamlit as st
2
  import numpy as np
3
  import re
4
  import emoji
 
 
5
  import nltk
6
+ from nltk.tokenize import word_tokenize
7
  from nltk.corpus import stopwords
8
+ from nltk.stem import WordNetLemmatizer
9
  import tensorflow as tf
10
  import keras
11
  from keras.utils import pad_sequences
12
  import pickle
 
 
13
 
14
+ # Streamlit UI
15
+ st.set_page_config(page_title="PressGuard", page_icon="🛡️")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Radium color effect for the title
18
+ st.markdown("""
19
+ <style>
20
+ .radium {
21
+ font-size: 60px;
22
+ font-weight: bold;
23
+ color: #f4ff81; /* Radium-like light greenish-yellow color */
24
+ text-shadow: 0 0 5px #f4ff81, 0 0 10px #f4ff81, 0 0 20px #f4ff81, 0 0 30px #f4ff81;
25
+ text-align: center;
26
+ }
27
+ .tagline {
28
+ font-size: 20px;
29
+ color: #ffffff;
30
+ text-align: center;
31
+ margin-bottom: 30px;
32
+ }
33
  </style>
34
+ <div class='radium'>🛡️ PressGuard</div>
35
+ <div class='tagline'>Classify and Filter Trustworthy News</div>
36
+ """, unsafe_allow_html=True)
37
+
38
+ # Download necessary resources
39
+ nltk.download('punkt')
40
+ nltk.download('stopwords')
41
+ nltk.download('wordnet')
42
+
43
+ # Initialize stopwords and lemmatizer
44
+ stop_words = set(stopwords.words('english')).union({"pm"})
45
+ lemmatizer = WordNetLemmatizer()
46
+
47
+ def pre_process(x):
48
+ x = x.lower()
49
+ x = re.sub("<.*?>", "", x)
50
+ x = re.sub("http[s]?://.+?\\S+", "", x)
51
+ x = re.sub("[@#].+?\\S", "", x)
52
+ x = re.sub(r"\\_+", " ", x)
53
+ x = re.sub("^[A-Za-z.].*\\s-\\s", "", x)
54
+ x = emoji.demojize(x)
55
+ x = re.sub(":.*?:", "", x)
56
+ x = re.sub("[^a-zA-Z0-9\\s_]", "", x)
57
+ words = word_tokenize(x)
58
+ words = [word for word in words if word not in stop_words]
59
+ x = " ".join([lemmatizer.lemmatize(word) for word in words])
60
+ return x
61
+
62
+ @st.cache_resource
63
+ def load_model():
64
+ model = keras.models.load_model("model_m3_new.keras")
65
+ vectorizer = keras.models.load_model("vec_text_m3_new.keras")
66
+ with open("label_encoder_m5.pkl", 'rb') as file:
67
+ label_encoder = pickle.load(file)
68
+ return model, vectorizer, label_encoder
69
+
70
+ model, vectorizer, label_encoder = load_model()
71
+
72
+ def predict_category(text):
73
+ processed_text = [pre_process(text)]
74
+ text_vectorized = pad_sequences(vectorizer(processed_text).numpy().tolist(), padding='pre', maxlen=128)
75
+ prediction = model.predict(text_vectorized)
76
+ category_idx = np.argmax(prediction, axis=1)[0]
77
+ return label_encoder.inverse_transform([category_idx])[0]
78
+
79
+ # Custom CSS with Radium Color Effect for the Prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  st.markdown(
81
  """
82
  <style>
83
  body {
84
+ background-image: url('https://cdn-uploads.huggingface.co/production/uploads/67441c51a784a9d15cb12871/4FFTjgkYjYUq6w-0gR15v.jpeg');
85
  background-size: cover;
86
  background-repeat: no-repeat;
87
  background-attachment: fixed;
88
  }
89
+ .centered-container {
90
+ text-align: center;
91
+ }
92
  .title {
93
  font-size: 60px;
94
  font-weight: bold;
95
  color: white;
96
+ background: linear-gradient(60deg, #880E4F, #4A235A, #311B92, #000000);
97
  padding: 20px;
98
  border-radius: 20px;
99
+ box-shadow: 0 8px 25px rgba(136, 14, 79, 0.5),
100
+ 0 4px 15px rgba(74, 35, 90, 0.6),
101
+ inset 0 2px 10px rgba(49, 27, 146, 0.4);
102
  display: inline-block;
103
+ margin-bottom: 20px;
104
+ animation: elegantFadeSlide 1.5s ease-out forwards;
 
105
  }
106
+
107
+ /* Radium Effect for the Prompt */
108
+ .prompt-box {
109
+ font-size: 22px;
 
 
 
 
 
 
 
 
 
110
  font-weight: bold;
111
+ color: white;
112
  text-align: center;
113
+ background: linear-gradient(135deg, #33ccff, #ff99cc, #33ff99, #ffcc00);
114
+ background-size: 400% 400%;
115
+ animation: gradientAnimation 8s ease infinite;
116
+ padding: 15px;
117
+ border-radius: 15px;
118
+ box-shadow: 0 0 15px rgba(255, 255, 255, 0.7),
119
+ 0 0 25px rgba(136, 14, 79, 0.7),
120
+ 0 0 35px rgba(49, 27, 146, 0.7);
121
+ transition: all 0.4s ease-in-out;
122
  }
123
 
124
+ .prompt-box:hover {
125
+ transform: scale(1.05) rotate(1deg);
126
+ box-shadow: 0 0 25px rgba(255, 255, 255, 0.9),
127
+ 0 0 35px rgba(136, 14, 79, 0.9),
128
+ 0 0 45px rgba(49, 27, 146, 0.9);
 
 
 
 
 
 
 
129
  }
130
 
131
+ @keyframes gradientAnimation {
132
+ 0% { background-position: 0% 50%; }
133
+ 50% { background-position: 100% 50%; }
134
+ 100% { background-position: 0% 50%; }
135
  }
136
 
 
137
  .analyze-button {
138
+ width: 180px;
139
+ height: 60px;
140
+ border-radius: 50px;
141
+ background: linear-gradient(45deg, #880E4F, #4A235A, #311B92, #000000);
142
+ font-size: 20px;
143
  font-weight: bold;
144
+ color: white;
145
  border: none;
146
+ box-shadow: 0 8px 25px rgba(136, 14, 79, 0.5),
147
+ 0 4px 15px rgba(74, 35, 90, 0.6),
148
+ 0 2px 10px rgba(49, 27, 146, 0.7),
149
+ inset 0 1px 5px rgba(0, 0, 0, 0.4);
150
  cursor: pointer;
151
+ transition: all 0.4s ease-in-out;
 
152
  }
153
+
154
  .analyze-button:hover {
155
  transform: scale(1.1);
156
+ background: linear-gradient(225deg, #880E4F, #4A235A, #311B92, #000000);
157
+ box-shadow: 0 12px 35px rgba(49, 27, 146, 0.8),
158
+ 0 8px 25px rgba(74, 35, 90, 0.7),
159
+ 0 4px 15px rgba(136, 14, 79, 0.6);
160
  }
161
+
 
162
  .result-box {
163
  text-align: center;
164
+ font-size: 28px;
165
  font-weight: bold;
166
+ background: linear-gradient(60deg, #880E4F, #4A235A, #311B92, #000000);
167
+ color: white;
168
+ padding: 30px;
169
+ border-radius: 20px;
170
+ display: inline-block;
171
+ margin-top: 30px;
172
+ box-shadow: 0 6px 20px rgba(74, 35, 90, 0.5),
173
+ 0 3px 15px rgba(136, 14, 79, 0.4),
174
+ inset 0 2px 10px rgba(49, 27, 146, 0.3);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  }
 
176
  </style>
177
  """,
178
  unsafe_allow_html=True
179
  )
180
 
181
+ st.markdown("<div class='centered-container'><h1 class='title'>"PressGuard</h1></div>", unsafe_allow_html=True)
182
+ st.markdown("<div class='prompt-box'>Paste the article content below to analyze its category with Newsense AI</div>", unsafe_allow_html=True)
183
 
184
+ # User input
185
+ input_text = st.text_area("Enter News Article:", height=200)
 
 
 
 
 
 
 
186
 
187
+ if st.button("Analyze", key="analyze-btn", help="Click to classify the news article"):
188
+ if input_text:
189
+ category = predict_category(input_text)
190
+ st.markdown(f"<div class='result-box'>Predicted Category: {category}</div>", unsafe_allow_html=True)
 
 
 
 
191
  else:
192
+ st.warning("Please enter some text to analyze.")