File size: 9,609 Bytes
3a6e7b6
5b49530
db9518c
 
 
3a6e7b6
db9518c
3a6e7b6
db9518c
 
 
 
d15065c
db9518c
3a6e7b6
 
316ef0d
 
 
 
 
 
 
a68967b
316ef0d
 
 
 
 
 
 
 
 
 
 
 
da0932e
 
 
 
 
 
a68967b
da0932e
83d3123
0f50349
da0932e
0f50349
da0932e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f50349
da0932e
0f50349
 
 
d15065c
db9518c
 
 
3a6e7b6
 
 
d15065c
db9518c
 
 
 
3a6e7b6
db9518c
 
3a6e7b6
 
 
db9518c
3a6e7b6
 
db9518c
3a6e7b6
 
 
db9518c
3a6e7b6
db9518c
3a6e7b6
 
 
 
 
 
 
 
 
db9518c
d6eeb4a
3a6e7b6
 
 
 
 
db9518c
d6eeb4a
3a6e7b6
 
 
 
db9518c
d6eeb4a
db9518c
3a6e7b6
 
 
 
 
db9518c
3a6e7b6
db9518c
3a6e7b6
 
 
 
db9518c
3a6e7b6
db9518c
3a6e7b6
db9518c
 
3a6e7b6
 
 
 
db9518c
3a6e7b6
db9518c
ddf15e1
db9518c
ddf15e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6eeb4a
 
 
 
 
d15065c
 
316ef0d
 
d6eeb4a
d4c35a0
 
 
 
d15065c
 
 
 
 
d6c6562
b0a4e3e
d15065c
 
 
 
 
 
 
 
 
 
b0a4e3e
d15065c
 
 
 
 
b0a4e3e
 
d15065c
 
b0a4e3e
 
 
b23de27
b0a4e3e
 
d15065c
 
b0a4e3e
d15065c
 
b0a4e3e
 
d15065c
 
b0a4e3e
 
d15065c
 
94e9e1c
d15065c
 
 
 
b0a4e3e
 
ddf15e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0a4e3e
3a6e7b6
5b49530
3a6e7b6
 
 
 
db9518c
83d3123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import streamlit as st 
import numpy as np
import re
import emoji
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import tensorflow as tf
import keras
from keras.utils import pad_sequences
import pickle
import os

# Streamlit UI
st.set_page_config(page_title="PressGuard", page_icon="🛡️")
# Radium color effect for the title
st.markdown("""
    <style>
    .radium {
        font-size: 60px;
        font-weight: bold;
        color: #f4ff81;  /* Radium-like light greenish-yellow color */
        text-shadow: 0 0 5px #f4ff81, 0 0 10px #f4ff81, 0 0 20px #f4ff81, 0 0 30px #e8ff94;
        text-align: center;
    }
    .tagline {
        font-size: 20px;
        color: #ffffff;
        text-align: center;
        margin-bottom: 30px;
    }
    </style>
    <div class='radium'>🛡️ PressGuard</div>
""", unsafe_allow_html=True)

# st.markdown(
#     """
#     <img src="https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/ut9wBSlRR2CCpw95V5ej8.jpeg" width="100%" />
#     """, 
#     unsafe_allow_html=True
# )

# Apply custom CSS for the background image and overlay
background_image_url="https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/yiXBcm5bq8gcMoaMRSYEv.webp"
st.markdown(
    f"""
    <style>
        .stApp {{
            background-image: url("{background_image_url}");
            background-size: auto;  /* Ensure the image width is 100% of the screen, and the height scales proportionally */
            background-repeat: repeat;  /* Repeat only vertically */
            background-position: top center;  /* Start repeating from the top center */
            background-attachment: fixed;  /* Keeps the background fixed as you scroll */
            height: 100%;
        }}
        
        /* Semi-transparent overlay */
        .stApp::before {{
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.4);  /* Adjust transparency here (0.4 for 40% transparency) */
            z-index: -1;
        }}
        
        /* Container to center elements and limit width */
        .content-container {{
            max-width: 70%;  /* Limit content width to 70% */
            margin: 0 auto;  /* Center the container */
            padding: 50px;  /* Add some padding for spacing */
        }}
        /* Styling the markdown content */
        .stMarkdown {{
            color: white;  /* White text to ensure visibility */
            font-size: 100px;  /* Adjust font size for readability */
        }}
    </style>
    """, 
    unsafe_allow_html=True
)

# Background Image and Enhanced Styling
st.markdown(
    """
    <style>
        .centered-container { 
            text-align: center; 
        }
        
        .title {
            font-size: 60px; 
            font-weight: bold; 
            color: white;
            background: linear-gradient(60deg, #880E4F, #4A235A, #311B92, #000000);
            padding: 20px; 
            border-radius: 20px;
            box-shadow: 0 8px 25px rgba(136, 14, 79, 0.5),
                        0 4px 15px rgba(74, 35, 90, 0.6),
                        inset 0 2px 10px rgba(49, 27, 146, 0.4);
            display: inline-block;
            margin-bottom: 20px;
            animation: elegantFadeSlide 1.5s ease-out forwards;
        }
        
        .prompt-box {
            font-size: 22px;
            font-weight: bold;
            color: white;
            text-align: center;
            background: linear-gradient(135deg, #33ccff, #ff99cc, #33ff99, #ffcc00);
            background-size: 400% 400%;
            animation: gradientAnimation 8s ease infinite;
            padding: 15px;
            border-radius: 15px;
            box-shadow: 0 0 15px rgba(255, 255, 255, 0.7),
                        0 0 25px rgba(136, 14, 79, 0.7),
                        0 0 35px rgba(49, 27, 146, 0.7);
            transition: all 0.4s ease-in-out;
        }

        .prompt-box:hover {
            transform: scale(1.05) rotate(1deg);
            box-shadow: 0 0 25px rgba(255, 255, 255, 0.9),
                        0 0 35px rgba(136, 14, 79, 0.9),
                        0 0 45px rgba(49, 27, 146, 0.9);
        }

        @keyframes gradientAnimation {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }

        .analyze-button {
            width: 180px;
            height: 60px;
            border-radius: 50px;
            background: linear-gradient(45deg, #880E4F, #4A235A, #311B92, #000000);
            font-size: 20px; 
            font-weight: bold; 
            color: white; 
            border: none;
            box-shadow: 0 8px 25px rgba(136, 14, 79, 0.5),
                        0 4px 15px rgba(74, 35, 90, 0.6),
                        0 2px 10px rgba(49, 27, 146, 0.7),
                        inset 0 1px 5px rgba(0, 0, 0, 0.4);
            cursor: pointer;
            transition: all 0.4s ease-in-out;
        }
        
        .analyze-button:hover {
            transform: scale(1.1);
            background: linear-gradient(225deg, #880E4F, #4A235A, #311B92, #000000);
            box-shadow: 0 12px 35px rgba(49, 27, 146, 0.8),
                        0 8px 25px rgba(74, 35, 90, 0.7),
                        0 4px 15px rgba(136, 14, 79, 0.6);
        }
        
        .result-box {
            font-size: 22px;
            font-weight: bold;
            color: white;
            text-align: center;
            background: linear-gradient(135deg, #6a11cb, #2575fc, #ff6a00, #ffcc00);
            background-size: 400% 400%;
            animation: gradientAnimation 6s ease infinite;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.5),
                        0 0 25px rgba(255, 105, 180, 0.7),
                        0 0 35px rgba(0, 191, 255, 0.7);
            transition: all 0.4s ease-in-out;
        }
        .result-box:hover {
            transform: scale(1.08) rotate(1deg);
            box-shadow: 0 0 25px rgba(0, 0, 0, 0.7),
                        0 0 35px rgba(255, 105, 180, 0.9),
                        0 0 45px rgba(0, 191, 255, 0.9);
        }
        
        @keyframes gradientAnimation {
            0% {
                background-position: 0% 50%;
            }
            50% {
                background-position: 100% 50%;
            }
            100% {
                background-position: 0% 50%;
            }
        }
    </style>
    """,
    unsafe_allow_html=True
)

# Title and Prompt

st.markdown("<div class='prompt-box'>Paste the article content below to analyze its category with PressGuard🛡️</div>", unsafe_allow_html=True)

# Download necessary resources
nltk.download('punkt_tab')
nltk.download('stopwords')
nltk.download('wordnet')

# Initialize stopwords and lemmatizer
stop_words = set(stopwords.words('english')).union({"pm"})
lemmatizer = WordNetLemmatizer()


# ✅ Preprocessing Function
def pre_process(x):
    x = x.lower()
    x = re.sub("<.*?>", "", x)
    x = re.sub("http[s]?://.+?\\S+", "", x)
    x = re.sub("[@#].+?\\S", "", x)
    x = re.sub(r"\\_+", " ", x)
    x = re.sub("^[A-Za-z.].*\\s-\\s", "", x)
    x = emoji.demojize(x)
    x = re.sub(":.*?:", "", x)
    x = re.sub("[^a-zA-Z0-9\\s_]", "", x)
    
    words = word_tokenize(x)
    words = [word for word in words if word not in stop_words]
    x = " ".join([lemmatizer.lemmatize(word) for word in words])
    return x


# ✅ Load Model and Vectorizer
@st.cache_resource
def load_model():
    # Load the model
    model = tf.keras.models.load_model("model_m3_new.keras")

    vectorizer = keras.models.load_model("vec_text_m3_new.keras")

    # Load label encoder
    with open("label_encoder_m5.pkl", 'rb') as file:
        label_encoder = pickle.load(file)

    return model, vectorizer, label_encoder


# Load models
model, vectorizer, label_encoder = load_model()


# ✅ Prediction Function
def predict_category(text):
    processed_text = [pre_process(text)]
    text_vectorized = pad_sequences(vectorizer(processed_text).numpy().tolist(), padding='pre', maxlen=128)
    prediction = model.predict(text_vectorized)
    category_idx = np.argmax(prediction, axis=1)[0]
    return label_encoder.inverse_transform([category_idx])[0]


# ✅ Streamlit UI
st.markdown("""
    <style>
    .title-box {
        font-size: 48px;
        font-weight: bold;
        text-align: center;
        background: linear-gradient(135deg, #ff7e5f, #feb47b, #86a8e7, #91eac9);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        animation: gradientAnimation 6s ease infinite;
        margin-bottom: 30px;
    }
    
    @keyframes gradientAnimation {
        0% { background-position: 0% 50%; }
        50% { background-position: 100% 50%; }
        100% { background-position: 0% 50%; }
    }
    </style>
    
    <div class="title-box">AI-Powered News Categorization</div>
""", unsafe_allow_html=True)


input_text = st.text_area("Enter News Article:", height=200)

if st.button("Analyze", key="analyze-btn", help="Click to classify the news article"):
    if input_text:
        category = predict_category(input_text)
        st.markdown(f"<div class='result-box'>Predicted Category: {category}</div>", unsafe_allow_html=True)
    else:
        st.warning("Please enter some text to analyze.")