Mpavan45 commited on
Commit
5b49530
·
verified ·
1 Parent(s): 702cb25

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ import pickle
4
+ import numpy as np
5
+ import pandas as pd
6
+
7
+ # Load Model
8
+ model = tf.keras.models.load_model("news_classification_rnn.h5")
9
+
10
+ # Load Preprocessing Function
11
+ with open("preprocessing.pkl", "rb") as f:
12
+ clean_text = pickle.load(f)
13
+
14
+ # Load TF-IDF Vectorizer
15
+ with open("tfidf_vectorizer.pkl", "rb") as f:
16
+ vectorizer = pickle.load(f)import streamlit as st
17
+ import tensorflow as tf
18
+ import pickle
19
+ import numpy as np
20
+ import pandas as pd
21
+
22
+ # Load Model
23
+ model = tf.keras.models.load_model("news_classification_rnn.h5")
24
+
25
+ # Load Preprocessing Function
26
+ with open("preprocessing.pkl", "rb") as f:
27
+ clean_text = pickle.load(f)
28
+
29
+ # Load TF-IDF Vectorizer
30
+ with open("vectorizer.pkl", "rb") as f:
31
+ vectorizer = pickle.load(f)
32
+
33
+ # Define News Categories
34
+ news_categories = ["World", "Sports", "Business", "Sci/Tech"]
35
+
36
+ # Streamlit UI
37
+ st.title("📰 News Classification with Simple RNN + TF-IDF")
38
+ st.write("Enter a news headline to predict its category.")
39
+
40
+ user_input = st.text_area("Enter News Text:", "")
41
+
42
+ if st.button("Classify"):
43
+ if user_input:
44
+ # Preprocess Input
45
+ processed_text = clean_text(user_input)
46
+
47
+ # Convert text to vector
48
+ text_vector = vectorizer.transform([processed_text]).toarray()
49
+
50
+ # Prediction
51
+ prediction = model.predict(text_vector)
52
+ category = np.argmax(prediction)
53
+
54
+ st.success(f"Predicted Category: {news_categories[category]}")
55
+ else:
56
+ st.warning("Please enter a news headline.")
57
+
58
+
59
+ # Define News Categories
60
+ news_categories = ["World", "Sports", "Business", "Sci/Tech"]
61
+
62
+ # Streamlit UI
63
+ st.title("📰 News Classification with Simple RNN + TF-IDF")
64
+ st.write("Enter a news headline to predict its category.")
65
+
66
+ user_input = st.text_area("Enter News Text:", "")
67
+
68
+ if st.button("Classify"):
69
+ if user_input:
70
+ # Preprocess Input
71
+ processed_text = clean_text(user_input)
72
+
73
+ # Convert text to vector
74
+ text_vector = vectorizer.transform([processed_text]).toarray()
75
+
76
+ # Prediction
77
+ prediction = model.predict(text_vector)
78
+ category = np.argmax(prediction)
79
+
80
+ st.success(f"Predicted Category: {news_categories[category]}")
81
+ else:
82
+ st.warning("Please enter a news headline.")