Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 17 |
-
st.
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
</style>
|
| 49 |
-
<div class=
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
#
|
| 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/
|
| 129 |
background-size: cover;
|
| 130 |
background-repeat: no-repeat;
|
| 131 |
background-attachment: fixed;
|
| 132 |
}
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
.title {
|
| 136 |
font-size: 60px;
|
| 137 |
font-weight: bold;
|
| 138 |
color: white;
|
| 139 |
-
background: linear-gradient(
|
| 140 |
padding: 20px;
|
| 141 |
border-radius: 20px;
|
| 142 |
-
box-shadow: 0
|
| 143 |
-
0
|
|
|
|
| 144 |
display: inline-block;
|
| 145 |
-
margin-bottom:
|
| 146 |
-
|
| 147 |
-
animation: fadeSlide 1.5s ease-out forwards;
|
| 148 |
}
|
| 149 |
-
|
| 150 |
-
/*
|
| 151 |
-
.
|
| 152 |
-
|
| 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:
|
| 164 |
text-align: center;
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
}
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 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 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
| 186 |
}
|
| 187 |
|
| 188 |
-
/* Button Styling */
|
| 189 |
.analyze-button {
|
| 190 |
-
width:
|
| 191 |
-
height:
|
| 192 |
-
border-radius:
|
| 193 |
-
background: linear-gradient(45deg, #
|
| 194 |
-
font-size:
|
| 195 |
font-weight: bold;
|
| 196 |
-
color:
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 206 |
}
|
| 207 |
-
|
| 208 |
-
/* Result Box Styling */
|
| 209 |
.result-box {
|
| 210 |
text-align: center;
|
| 211 |
-
font-size:
|
| 212 |
font-weight: bold;
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
padding:
|
| 216 |
-
border-radius:
|
| 217 |
-
|
| 218 |
-
margin-top:
|
| 219 |
-
|
| 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 |
-
|
| 250 |
-
|
| 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 |
-
|
| 260 |
-
if
|
| 261 |
-
|
| 262 |
-
category,
|
| 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
|
| 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.")
|
|
|
|
|
|