Update app.py
Browse files
app.py
CHANGED
|
@@ -18,13 +18,13 @@ nltk.download('wordnet')
|
|
| 18 |
import tensorflow as tf
|
| 19 |
import keras
|
| 20 |
from keras.preprocessing.sequence import pad_sequences
|
| 21 |
-
|
| 22 |
import pickle
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
st.set_page_config(page_title="News Category Classifier", page_icon="π°", layout="
|
|
|
|
| 26 |
|
| 27 |
-
# Function to set background image
|
| 28 |
def set_background(image_path):
|
| 29 |
if not os.path.exists(image_path):
|
| 30 |
st.error(f"β Background image not found: {image_path}")
|
|
@@ -36,37 +36,42 @@ def set_background(image_path):
|
|
| 36 |
bg_image_style = f"""
|
| 37 |
<style>
|
| 38 |
.stApp {{
|
| 39 |
-
background: url("data:image/jpg;base64,{encoded_img}")
|
| 40 |
background-size: cover;
|
|
|
|
|
|
|
|
|
|
| 41 |
}}
|
| 42 |
</style>
|
| 43 |
"""
|
| 44 |
st.markdown(bg_image_style, unsafe_allow_html=True)
|
| 45 |
|
| 46 |
-
# Set background image
|
| 47 |
-
set_background("Images/
|
| 48 |
|
| 49 |
|
| 50 |
-
# Initialize stopwords and lemmatizer
|
| 51 |
stop_words = set(stopwords.words('english')).union({"pm"})
|
| 52 |
lemmatizer = WordNetLemmatizer()
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
words = [word for word in words if word not in stop_words]
|
| 66 |
-
|
| 67 |
-
return
|
|
|
|
| 68 |
|
| 69 |
-
# Cache
|
| 70 |
@st.cache_resource
|
| 71 |
def load_model():
|
| 72 |
model_path = "news_model.keras"
|
|
@@ -75,16 +80,18 @@ def load_model():
|
|
| 75 |
|
| 76 |
model = keras.models.load_model(model_path)
|
| 77 |
vectorizer = keras.models.load_model(vectorizer_path)
|
| 78 |
-
|
| 79 |
with open(label_encoder_path, 'rb') as file:
|
| 80 |
label_encoder = pickle.load(file)
|
| 81 |
|
| 82 |
return model, vectorizer, label_encoder
|
| 83 |
|
| 84 |
-
|
|
|
|
| 85 |
model, vectorizer, label_encoder = load_model()
|
| 86 |
|
| 87 |
-
|
|
|
|
| 88 |
def predict_category(text):
|
| 89 |
processed_text = [pre_process(text)]
|
| 90 |
text_vectorized = pad_sequences(vectorizer(processed_text).numpy().tolist(), padding='pre', maxlen=82)
|
|
@@ -92,34 +99,35 @@ def predict_category(text):
|
|
| 92 |
category_idx = np.argmax(prediction, axis=1)[0]
|
| 93 |
return label_encoder.inverse_transform([category_idx])[0]
|
| 94 |
|
| 95 |
-
|
|
|
|
| 96 |
st.markdown(
|
| 97 |
"""
|
| 98 |
<style>
|
| 99 |
.title {
|
| 100 |
color: #ffffff;
|
| 101 |
-
font-size: 2.
|
| 102 |
text-align: center;
|
| 103 |
font-weight: 700;
|
| 104 |
text-transform: uppercase;
|
| 105 |
text-shadow: 2px 2px 8px rgba(0, 0, 0, 1.0);
|
| 106 |
-
padding:
|
| 107 |
}
|
| 108 |
.subtitle {
|
| 109 |
-
color: #
|
| 110 |
-
font-size: 1.
|
| 111 |
text-align: center;
|
| 112 |
font-weight: 600;
|
| 113 |
text-shadow: 1px 1px 6px rgba(0, 0, 0, 1.0);
|
| 114 |
-
padding:
|
| 115 |
}
|
| 116 |
.classify-button {
|
| 117 |
background-color: #3498db;
|
| 118 |
color: white;
|
| 119 |
-
font-size: 1.
|
| 120 |
-
padding:
|
| 121 |
border: none;
|
| 122 |
-
border-radius:
|
| 123 |
cursor: pointer;
|
| 124 |
display: block;
|
| 125 |
margin: 20px auto;
|
|
@@ -130,13 +138,13 @@ st.markdown(
|
|
| 130 |
}
|
| 131 |
.result-box {
|
| 132 |
background: linear-gradient(135deg, #6284FF 30%, #FF0000 70%);
|
| 133 |
-
padding:
|
| 134 |
-
border-radius:
|
| 135 |
text-align: center;
|
| 136 |
margin-top: 30px;
|
| 137 |
position: relative;
|
| 138 |
overflow: hidden;
|
| 139 |
-
border:
|
| 140 |
background-clip: padding-box, border-box;
|
| 141 |
border-image: linear-gradient(135deg, #6284FF 30%, #FF0000 70%);
|
| 142 |
border-image-slice: 0;
|
|
@@ -148,7 +156,7 @@ st.markdown(
|
|
| 148 |
0px 10px 30px rgba(255, 0, 0, 0.8);
|
| 149 |
}
|
| 150 |
.result-text {
|
| 151 |
-
font-size:
|
| 152 |
color: #ffffff;
|
| 153 |
font-weight: 900;
|
| 154 |
text-shadow: 3px 3px 10px rgba(0, 0, 0, 0.5);
|
|
@@ -159,14 +167,14 @@ st.markdown(
|
|
| 159 |
unsafe_allow_html=True
|
| 160 |
)
|
| 161 |
|
| 162 |
-
# Page
|
| 163 |
st.markdown("<div class='title'>π° News Classifier</div>", unsafe_allow_html=True)
|
| 164 |
st.markdown("<div class='subtitle'>Enter a news headline or article snippet to analyze its category.</div>", unsafe_allow_html=True)
|
| 165 |
|
| 166 |
-
# User
|
| 167 |
user_input = st.text_area("Enter text here:", height=150, placeholder="Type your news text here...")
|
| 168 |
|
| 169 |
-
#
|
| 170 |
if st.button("Analyze πΏ", key="analyze_button"):
|
| 171 |
if user_input.strip():
|
| 172 |
category = predict_category(user_input)
|
|
|
|
| 18 |
import tensorflow as tf
|
| 19 |
import keras
|
| 20 |
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="News Category Classifier", page_icon="π°", layout="wide")
|
| 25 |
+
|
| 26 |
|
| 27 |
+
# β
Function to set background image
|
| 28 |
def set_background(image_path):
|
| 29 |
if not os.path.exists(image_path):
|
| 30 |
st.error(f"β Background image not found: {image_path}")
|
|
|
|
| 36 |
bg_image_style = f"""
|
| 37 |
<style>
|
| 38 |
.stApp {{
|
| 39 |
+
background-image: url("data:image/jpg;base64,{encoded_img}");
|
| 40 |
background-size: cover;
|
| 41 |
+
background-repeat: no-repeat;
|
| 42 |
+
background-position: center;
|
| 43 |
+
background-attachment: fixed;
|
| 44 |
}}
|
| 45 |
</style>
|
| 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/News_image.jpg")
|
| 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()
|
| 61 |
+
text = re.sub("<.*?>", "", text) # Remove HTML tags
|
| 62 |
+
text = re.sub("http[s]?://\\S+", "", text) # Remove URLs
|
| 63 |
+
text = re.sub("[@#]\\S+", "", text) # Remove mentions and hashtags
|
| 64 |
+
text = re.sub(r"\\_+", " ", text) # Replace underscores with spaces
|
| 65 |
+
text = emoji.demojize(text) # Convert emojis to text
|
| 66 |
+
text = re.sub(":.*?:", "", text) # Remove emoji text
|
| 67 |
+
text = re.sub("[^a-zA-Z0-9\\s_]", "", text) # Remove special characters
|
| 68 |
+
words = word_tokenize(text)
|
| 69 |
words = [word for word in words if word not in stop_words]
|
| 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():
|
| 77 |
model_path = "news_model.keras"
|
|
|
|
| 80 |
|
| 81 |
model = keras.models.load_model(model_path)
|
| 82 |
vectorizer = keras.models.load_model(vectorizer_path)
|
| 83 |
+
|
| 84 |
with open(label_encoder_path, 'rb') as file:
|
| 85 |
label_encoder = pickle.load(file)
|
| 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)]
|
| 97 |
text_vectorized = pad_sequences(vectorizer(processed_text).numpy().tolist(), padding='pre', maxlen=82)
|
|
|
|
| 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;
|
| 109 |
+
font-size: 2.8em;
|
| 110 |
text-align: center;
|
| 111 |
font-weight: 700;
|
| 112 |
text-transform: uppercase;
|
| 113 |
text-shadow: 2px 2px 8px rgba(0, 0, 0, 1.0);
|
| 114 |
+
padding: 15px;
|
| 115 |
}
|
| 116 |
.subtitle {
|
| 117 |
+
color: #ffffff;
|
| 118 |
+
font-size: 1.5em;
|
| 119 |
text-align: center;
|
| 120 |
font-weight: 600;
|
| 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;
|
|
|
|
| 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;
|
|
|
|
| 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);
|
|
|
|
| 167 |
unsafe_allow_html=True
|
| 168 |
)
|
| 169 |
|
| 170 |
+
# β
Page Title
|
| 171 |
st.markdown("<div class='title'>π° News Classifier</div>", unsafe_allow_html=True)
|
| 172 |
st.markdown("<div class='subtitle'>Enter a news headline or article snippet to analyze its category.</div>", unsafe_allow_html=True)
|
| 173 |
|
| 174 |
+
# β
User Input
|
| 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)
|