Delete app.py
Browse files
app.py
DELETED
|
@@ -1,69 +0,0 @@
|
|
| 1 |
-
import re
|
| 2 |
-
import emoji
|
| 3 |
-
import nltk
|
| 4 |
-
import numpy as np
|
| 5 |
-
import streamlit as st
|
| 6 |
-
import pickle # To load the tokenizer
|
| 7 |
-
|
| 8 |
-
from nltk.tokenize import word_tokenize
|
| 9 |
-
from nltk.stem import WordNetLemmatizer
|
| 10 |
-
from tensorflow.keras.models import load_model
|
| 11 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 12 |
-
|
| 13 |
-
# Ensure necessary downloads
|
| 14 |
-
nltk.download("punkt")
|
| 15 |
-
nltk.download("wordnet")
|
| 16 |
-
nltk.download("omw-1.4")
|
| 17 |
-
nltk.download("averaged_perceptron_tagger")
|
| 18 |
-
|
| 19 |
-
lemmatizer = WordNetLemmatizer()
|
| 20 |
-
|
| 21 |
-
# Function to preprocess text
|
| 22 |
-
def pre_process(x):
|
| 23 |
-
x = x.lower()
|
| 24 |
-
x = re.sub("<.*?>", "", x)
|
| 25 |
-
x = re.sub("http[s]?://.+?\\S+", "", x)
|
| 26 |
-
x = re.sub("[@#].+?\\S", "", x)
|
| 27 |
-
x = re.sub(r"\\_+", " ", x)
|
| 28 |
-
x = re.sub("^[A-Za-z.].*\\s-\\s", "", x)
|
| 29 |
-
x = emoji.demojize(x)
|
| 30 |
-
x = re.sub(":.*?:", "", x)
|
| 31 |
-
x = re.sub("[^a-zA-Z0-9\\s_]", "", x)
|
| 32 |
-
words = word_tokenize(x)
|
| 33 |
-
x = " ".join([lemmatizer.lemmatize(word) for word in words])
|
| 34 |
-
return x
|
| 35 |
-
|
| 36 |
-
# Load trained model
|
| 37 |
-
model = load_model("best_rnn_model.h5")
|
| 38 |
-
|
| 39 |
-
# Load the same tokenizer used during training
|
| 40 |
-
with open("tokenizer.pickle", "rb") as handle:
|
| 41 |
-
tokenizer = pickle.load(handle)
|
| 42 |
-
|
| 43 |
-
# Maximum length (must match training settings)
|
| 44 |
-
MAX_LENGTH = 100
|
| 45 |
-
|
| 46 |
-
# Class labels
|
| 47 |
-
class_labels = ['Sports', 'Business', 'SciTech', 'World']
|
| 48 |
-
|
| 49 |
-
# Function to predict category
|
| 50 |
-
def predict_category(text):
|
| 51 |
-
processed_text = pre_process(text)
|
| 52 |
-
seq = tokenizer.texts_to_sequences([processed_text])
|
| 53 |
-
padded_seq = pad_sequences(seq, maxlen=MAX_LENGTH, padding='post')
|
| 54 |
-
prediction = model.predict(padded_seq)
|
| 55 |
-
predicted_label = class_labels[np.argmax(prediction)]
|
| 56 |
-
return predicted_label
|
| 57 |
-
|
| 58 |
-
# Streamlit UI
|
| 59 |
-
st.title("π° News Category Classifier")
|
| 60 |
-
st.write("Enter a news headline or article snippet, and the model will predict its category.")
|
| 61 |
-
|
| 62 |
-
user_input = st.text_area("β Enter text here:")
|
| 63 |
-
|
| 64 |
-
if st.button("π Predict"):
|
| 65 |
-
if user_input.strip():
|
| 66 |
-
prediction = predict_category(user_input)
|
| 67 |
-
st.success(f"π Predicted Category: **{prediction}**")
|
| 68 |
-
else:
|
| 69 |
-
st.warning("β οΈ Please enter some text to classify.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|