News_Classifier / app.py
Mpavan45's picture
Update app.py
f9b212d verified
raw
history blame
7.58 kB
import streamlit as st
import numpy as np
import re
import emoji
from textblob import TextBlob
import spacy
import nltk
from nltk.corpus import stopwords
import tensorflow as tf
import keras
from keras.utils import pad_sequences
import pickle
# Page Config
st.set_page_config(page_title="Newsense AI", page_icon="📰", layout="wide")
# Custom HTML and CSS for center alignment
st.markdown(
"""
<style>
.center-title {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
font-size: 50px;
font-weight: bold;
color: white;
background: linear-gradient(135deg, #FF6B6B, #6B7EFF, #6BFF95, #FFDE59);
padding: 20px;
border-radius: 20px;
box-shadow: 0 10px 35px rgba(255, 107, 107, 0.7),
0 5px 20px rgba(107, 126, 255, 0.7);
animation: fadeSlide 1.5s ease-out forwards;
width: fit-content;
margin: 30px auto;
}
@keyframes fadeSlide {
from {
opacity: 0;
transform: translateY(-50px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>
<div class="center-title">📰 Newsense AI</div>
""",
unsafe_allow_html=True
)
# # Download necessary resources
# # nltk.download('stopwords')
# # Load SpaCy model
# nlp = spacy.load("en_core_web_sm")
# # Stopwords
# stop_words = set(stopwords.words('english')).union({"pm"})
# # Pre-processing function (without parentheses extraction)
# def pre_process(x):
# # Convert to lowercase
# x = x.lower()
# # Remove HTML tags
# x = re.sub(r"<.*?>", "", x)
# # Remove URLs
# x = re.sub(r"http[s]?://\S+", "", x)
# # Remove mentions (@, #)
# x = re.sub(r"[@#]\S+", "", x)
# # Remove emojis
# x = emoji.replace_emoji(x, replace="")
# # Remove special characters (-, ., :, \, ,)
# x = re.sub(r"[-.:,\\]", " ", x)
# # Remove single and double quotes
# x = re.sub(r"['\"](.*?)['\"]", r'\1', x)
# # Remove content inside parentheses
# x = re.sub(r"\(.*?\)", "", x)
# # Remove extra spaces
# x = re.sub(r"\s+", " ", x).strip()
# # Spell checking
# x = str(TextBlob(x).correct())
# # Lemmatization using SpaCy
# x = " ".join([token.lemma_ for token in nlp(x)])
# return " ".join(x)
# @st.cache_resource
# def load_model():
# model = keras.models.load_model("model_m3_new.keras")
# with open("label_encoder_m5.pkl", 'rb') as file:
# label_encoder = pickle.load(file)
# return model, label_encoder
# model, label_encoder = load_model()
# def predict_category(text):
# cleaned_text = pre_process(text)
# vectorizer = keras.models.load_model("vec_text_m3_new.keras")
# # Vectorizing the pre-processed text
# text_vectorized = pad_sequences(vectorizer.predict(np.array([cleaned_text])).numpy(), padding='pre', maxlen=128)
# # Model prediction
# prediction = model.predict(text_vectorized)
# category_idx = np.argmax(prediction, axis=1)[0]
# return label_encoder.inverse_transform([category_idx])[0], cleaned_text
st.markdown(
"""
<style>
body {
background-image: url('https://cdn-uploads.huggingface.co/production/uploads/675fab3a2d0851e23d23cad3/WwOWG8MBGYxHnIeM2Dowo.webp');
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
/* Title Styling */
.title {
font-size: 60px;
font-weight: bold;
color: white;
background: linear-gradient(135deg, #FF6B6B, #6B7EFF, #6BFF95, #FFDE59);
padding: 20px;
border-radius: 20px;
box-shadow: 0 10px 35px rgba(255, 107, 107, 0.7),
0 5px 20px rgba(107, 126, 255, 0.7);
display: inline-block;
margin-bottom: 30px;
text-align: center;
animation: fadeSlide 1.5s ease-out forwards;
}
/* Input Box Styling */
.input-box {
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
margin: 0 auto;
width: 80%;
}
.input-prompt {
font-size: 24px;
font-weight: bold;
color: #ffffff;
text-align: center;
opacity: 0.9;
text-shadow: 0 0 8px #6BFF95;
}
/* Text Area Styling */
div.stTextArea textarea {
width: 100%;
height: 200px;
padding: 20px;
border-radius: 15px;
background: rgba(0, 0, 0, 0.8);
color: #FFFFFF;
font-size: 18px;
outline: none;
box-shadow: 0 8px 25px rgba(107, 126, 255, 0.5);
transition: all 0.5s ease;
}
div.stTextArea textarea:hover {
transform: scale(1.05);
box-shadow: 0 12px 40px rgba(107, 126, 255, 0.8);
}
/* Button Styling */
.analyze-button {
width: 220px;
height: 65px;
border-radius: 35px;
background: linear-gradient(45deg, #FF6B6B, #6B7EFF, #6BFF95, #FFDE59);
font-size: 22px;
font-weight: bold;
color: #000000;
border: none;
cursor: pointer;
transition: all 0.4s ease;
box-shadow: 0 8px 25px rgba(255, 107, 107, 0.7);
}
.analyze-button:hover {
transform: scale(1.1);
box-shadow: 0 14px 45px rgba(255, 222, 89, 0.8);
}
/* Result Box Styling */
.result-box {
text-align: center;
font-size: 30px;
font-weight: bold;
color: #ffffff;
background: linear-gradient(135deg, #6B7EFF, #6BFF95, #FFDE59, #FF6B6B);
padding: 35px;
border-radius: 25px;
box-shadow: 0 10px 30px rgba(107, 255, 107, 0.6);
margin-top: 40px;
animation: fadeIn 1.2s ease-in;
}
/* Animations */
@keyframes fadeSlide {
from {
opacity: 0;
transform: translateY(-50px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
</style>
""",
unsafe_allow_html=True
)
st.markdown(
<div class="title">📰 Newsense AI - News Classification</div>,
unsafe_allow_html=True
)
# Input and button section
st.markdown('<div class="input-box">', unsafe_allow_html=True)
user_input = st.text_area("Enter your news article:", height=200)
# Predict button
if st.button("Classify", key="analyze-button"):
if user_input:
category, cleaned_text = predict_category(user_input)
# Display the prediction and cleaned text
st.markdown(f'<div class="result-box">Prediction: {category}</div>', unsafe_allow_html=True)
st.markdown(f'<div class="result-box">Cleaned Text: {cleaned_text}</div>', unsafe_allow_html=True)
else:
st.warning("Please enter some text to classify!")
st.markdown('</div>', unsafe_allow_html=True)