File size: 2,405 Bytes
e2328ed |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
# Load your saved model and tokenizer
def load_model_and_tokenizer():
# Assuming the model is saved as 'spam_ham_model.h5' and tokenizer saved as 'tokenizer.pickle'
model = tf.keras.models.load_model('model.h5')
# You need to have a way to load the tokenizer that you used
import pickle
with open('tokenizer.pkl', 'rb') as handle:
tokenizer = pickle.load(handle)
return model, tokenizer
# Preprocessing function for the user input
def preprocess_input(texts, tokenizer, maxlen=50):
sequences = tokenizer.texts_to_sequences(texts)
return pad_sequences(sequences, maxlen=maxlen, padding='post')
# Prediction function
def predict_text(model, tokenizer, sample_texts, maxlen=50):
X_predict = preprocess_input(sample_texts, tokenizer, maxlen)
predictions = model.predict(X_predict)
results = []
for text, pred in zip(sample_texts, predictions):
label = "spam" if pred[0] > 0.5 else "ham"
results.append({
"Text": text,
"Predicted Label": label,
"Prediction Confidence": f"{pred[0]:.4f}"
})
return results
# Streamlit App Interface
def main():
st.title('Spam vs Ham Text Classifier')
st.markdown("""
This is a simple Streamlit app that predicts whether a given text is **Spam** or **Ham** using a pre-trained model.
""")
# Load model and tokenizer
model, tokenizer = load_model_and_tokenizer()
# Text input
text_input = st.text_area("Enter the text you want to classify:")
# Button to predict
if st.button("Predict"):
if text_input:
# Get the prediction
prediction_results = predict_text(model, tokenizer, [text_input])
# Display the result
for result in prediction_results:
st.write(f"**Text**: {result['Text']}")
st.write(f"**Predicted Label**: {result['Predicted Label']}")
st.write(f"**Prediction Confidence**: {result['Prediction Confidence']}")
else:
st.error("Please enter some text to classify.")
# Run the app
if __name__ == "__main__":
main()
|