-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
50 lines (35 loc) · 1.41 KB
/
streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import streamlit as st
from keras.models import load_model
import numpy as np
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
model = load_model("C:/Users/user/Downloads/News/news_classification")
def preprocess_input(input):
input = input.lower()
input = re.sub(r"[^\w\s]",input)
input = nltk.word_tokenize(input)
stop_words = set(stopwords.words("english"))
input = [word for word in input if word not in stop_words]
lemmatizer = WordNetLemmatizer;
input = " ".join([lemmatizer.lemmatize(word) for word in input])
tokenizer = Tokenizer()
tokenizer.fit_on_texts(input)
sequences = tokenizer.texts_to_sequences(input)
features = pad_sequences(sequences)
def predict_category(input):
processed_input = preprocess_input(input)
prediction = model.predict(np.array([processed_input]))
categories = ["WELLNESS", "ENTERTAINMENT", "POLITICS", "TRAVEL", "STYLE & BEAUTY"]
return categories[np.argmax(prediction)]
def main():
st.title('News Category Predictor')
user_input = st.text_input("Enter a news headline:")
if st.button('Predict'):
prediction = predict_category(user_input)
st.write(f'The predicted category is: {prediction}')
if __name__ == "__main__":
main()