Adityaganesh commited on
Commit
4bb4749
·
verified ·
1 Parent(s): 9a7c910

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import tensorflow as tf
4
+ import numpy as np
5
+ import re
6
+ import emoji
7
+ import nltk
8
+ from nltk.tokenize import word_tokenize
9
+ from nltk.stem import WordNetLemmatizer
10
+ from nltk.corpus import stopwords
11
+
12
+ # Ensure necessary downloads
13
+ nltk.download("punkt")
14
+ nltk.download("wordnet")
15
+ nltk.download('stopwords')
16
+
17
+ lemmatizer = WordNetLemmatizer()
18
+ stop_words = set(stopwords.words('english'))
19
+
20
+ def pre_process(x):
21
+ x = x.lower()
22
+ x = re.sub("<.*?>", "", x)
23
+ x = re.sub("http[s]?://.+?\\S+", "", x)
24
+ x = re.sub("[@#].+?\\S", "", x)
25
+ x = re.sub(r"\\_+", " ", x)
26
+ x = re.sub("^[A-Za-z.].*\\s-\\s", "", x)
27
+ x = emoji.demojize(x)
28
+ x = re.sub(":.*?:", "", x)
29
+ x = re.sub("[^a-zA-Z0-9\\s_]", "", x)
30
+ words = word_tokenize(x)
31
+ words = [word for word in words if word not in stop_words]
32
+ x = " ".join([lemmatizer.lemmatize(word) for word in words])
33
+ return x
34
+
35
+ # Load the label encoder
36
+ with open("label_encoder.pkl", "rb") as f:
37
+ label_encoder = pickle.load(f)
38
+
39
+ # Load the text vectorization model
40
+ text_vectorizer = tf.keras.models.load_model("news_tv_model.keras")
41
+
42
+ # Load the news classification model
43
+ news_model = tf.keras.models.load_model("news_model.keras")
44
+
45
+ def predict_category(text):
46
+ # Preprocess the input text
47
+ processed_text = [pre_process(text[0])]
48
+ vectorized_text = text_vectorizer(processed_text)
49
+ # Predict category
50
+ prediction = news_model.predict(vectorized_text)
51
+ predicted_label_index = np.argmax(prediction, axis=1)[0]
52
+ predicted_label = label_encoder.inverse_transform([predicted_label_index])[0]
53
+ return predicted_label
54
+
55
+ # Streamlit UI
56
+ st.title("News Classification App")
57
+
58
+ # User input
59
+ user_text = st.text_area("Enter news text:")
60
+
61
+ if st.button("Predict Category"):
62
+ if user_text.strip():
63
+ category = predict_category([user_text])
64
+ st.success(f"Predicted Category: {category}")
65
+ else:
66
+ st.warning("Please enter some text to classify.")