Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pickle | |
| import tensorflow as tf | |
| from PIL import Image | |
| import numpy as np | |
| import cv2 | |
| import pandas as pd | |
| from tensorflow.keras.datasets import imdb | |
| from tensorflow.keras.preprocessing.text import Tokenizer | |
| from tensorflow.keras.models import save_model, load_model | |
| from BackPropogation import BackPropogation | |
| from sklearn.model_selection import train_test_split | |
| def cnn_tumor(img): | |
| img=Image.open(img) | |
| img=np.array(img) | |
| img=cv2.cvtColor(img,cv2.COLOR_RGB2BGR) | |
| img_array = cv2.medianBlur(img, 5) | |
| img = Image.fromarray(cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB)) | |
| img=img.resize((128,128)) | |
| input_img = np.expand_dims(img, axis=0) | |
| st.image(input_img, caption='Image Processing', use_column_width=True) | |
| loaded_model = tf.keras.models.load_model('cnn_model.h5') | |
| predictions = loaded_model.predict(input_img) | |
| if predictions: | |
| st.write("Tumor Detected") | |
| else: | |
| st.write("No Tumor") | |
| def perceptron(): | |
| with open('perceptron_model.pkl', 'rb') as file: | |
| loaded_model = pickle.load(file) | |
| top_words = 5000 | |
| (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words) | |
| word_index = imdb.get_word_index() | |
| reverse_word_index = dict([(value, key) for (key, value) in word_index.items()]) | |
| user_input = st.text_input("Enter your text:") | |
| if st.button("Predict"): | |
| if user_input: | |
| st.write("Review:", user_input) | |
| user_input_sequence = [word_index.get(word, 0) for word in user_input.split()] | |
| processed_input = tf.keras.preprocessing.sequence.pad_sequences([user_input_sequence], maxlen=500, padding='post', truncating='post') | |
| prediction = loaded_model.predict(processed_input) | |
| sentiment = 'Positive' if prediction[0] > 0.5 else 'Negative' | |
| st.write("Predicted Sentiment:", sentiment) | |
| else: | |
| st.warning("Please enter a text.") | |
| def backprop(): | |
| with open('back-propagation_model.pkl', 'rb') as file: | |
| loaded_model = pickle.load(file) | |
| top_words = 5000 | |
| (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words) | |
| word_index = imdb.get_word_index() | |
| reverse_word_index = dict([(value, key) for (key, value) in word_index.items()]) | |
| user_input = st.text_input("Enter your text:") | |
| if st.button("Predict"): | |
| if user_input: | |
| st.write("Review:", user_input) | |
| user_input_sequence = [word_index.get(word, 0) for word in user_input.split()] | |
| processed_input = tf.keras.preprocessing.sequence.pad_sequences([user_input_sequence], maxlen=500, padding='post', truncating='post') | |
| prediction = loaded_model.predict(processed_input) | |
| sentiment = 'Positive' if prediction[0] > 0.5 else 'Negative' | |
| st.write("Predicted Sentiment:", sentiment) | |
| else: | |
| st.warning("Please enter a text.") | |
| def dnn_model(): | |
| user_input = st.text_input('Enter a sentence:') | |
| tokenizer = Tokenizer(num_words=10000, oov_token='<OOV>') | |
| tokenizer.fit_on_texts([user_input]) | |
| sequences = tokenizer.texts_to_sequences([user_input]) | |
| padded_sequence = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=500, padding='post', truncating='post') | |
| loaded_model = tf.keras.models.load_model('dnn_model.h5') | |
| if st.button('Predict'): | |
| prediction = loaded_model.predict(np.array(padded_sequence)) | |
| sentiment = 'Positive' if prediction > 0.5 else 'Negative' | |
| st.success(f'Sentiment: {sentiment}, Confidence: {prediction[0][0]:.4f}') | |
| def rnn_model(): | |
| loaded_model = tf.keras.models.load_model('rnn_model.h5') | |
| user_input_sequence = st.text_area("Enter your text message:") | |
| if st.button('Predict'): | |
| if user_input_sequence: | |
| tokenizer = Tokenizer(num_words=5000) | |
| tokenizer.fit_on_texts([user_input_sequence]) | |
| sequences = tokenizer.texts_to_sequences([user_input_sequence]) | |
| processed_input = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=10, padding='post', truncating='post') | |
| prediction = loaded_model.predict(np.array(processed_input)) | |
| is_spam = 'Spam' if prediction[0] > 0.5 else 'Not Spam' | |
| st.write("Predicted Label:", is_spam) | |
| else: | |
| st.warning("Please enter a text message.") | |
| def lstm_model(): | |
| user_input = st.text_input('Enter a sentence:') | |
| tokenizer = Tokenizer(num_words=10000, oov_token='<OOV>') | |
| tokenizer.fit_on_texts([user_input]) | |
| sequences = tokenizer.texts_to_sequences([user_input]) | |
| padded_sequence = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=500, padding='post', truncating='post') | |
| loaded_model = tf.keras.models.load_model('lstm_model.h5') | |
| if st.button('Predict Sentiment'): | |
| prediction = loaded_model.predict(np.array(padded_sequence)) | |
| sentiment = 'Positive' if prediction > 0.5 else 'Negative' | |
| st.success(f'Sentiment: {sentiment}, Confidence: {prediction[0][0]:.4f}') | |
| st.title('Prediction Model') | |
| option = st.selectbox("Select task",['Tumor Detection','Sentiment Classification']) | |
| if option=='Tumor Detection': | |
| st.title('CNN Tumor Detection Model') | |
| img=st.file_uploader("Upload your file here.....", type=['png', 'jpeg', 'jpg']) | |
| cnn_tumor(img) | |
| else: | |
| opt=st.radio("Select your model for sentiment classification",key="visibility",options=["Perceptron",'Backpropogation','DNN','RNN','LSTM']) | |
| if opt=="Perceptron": | |
| st.title('Perceptron Model') | |
| perceptron() | |
| elif opt=="Backpropogation": | |
| st.title('Backpropogation Model') | |
| backprop() | |
| elif opt=='RNN': | |
| st.title('RNN Spam Detection') | |
| rnn_model() | |
| elif opt=='DNN': | |
| st.title('DNN Text Classification') | |
| dnn_model() | |
| else: | |
| st.title('LSTM Sentiment Analysis') | |
| lstm_model() |