Update app.py
Browse files
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="π§
|
| 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
|
| 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
|
| 179 |
if user_input.strip():
|
| 180 |
category = predict_category(user_input)
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|