CoolFace
Apppublic

Amittha/dl_app

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
chck.py81 linesDownload Raw Back to root
1import streamlit as st2import numpy as np3from PIL import Image4from tensorflow.keras.models import load_model5from tensorflow.keras.preprocessing.text import Tokenizer6from tensorflow.keras.preprocessing.sequence import pad_sequences7from tensorflow.keras.applications.inception_v3 import preprocess_input8import tensorflow as tf9import joblib10 11# Load saved models12image_model = load_model('tumor_detection_model.h5')13dnn_model = load_model('sms_spam_detection_dnnmodel.h5')14rnn_model = load_model('spam_detection_rnn_model.h5')15perceptron_model = joblib.load('imdb_perceptron_model.pkl')16backprop_model = joblib.load('backprop_model.pkl')17LSTM_model = load_model('imdb_LSTM.h5')18 19# Streamlit app20st.title("Classification")21 22# Sidebar23task = st.sidebar.selectbox("Select Task", ["Tumor Detection", "Sentiment Classification"])24 25def preprocess_message_dnn(message, tokeniser, max_length):26    encoded_message = tokeniser.texts_to_sequences([message])27    padded_message = pad_sequences(encoded_message, maxlen=max_length, padding='post')28    return padded_message29 30def predict_dnnspam(message, tokeniser, max_length):31    processed_message = preprocess_message_dnn(message, tokeniser, max_length)32    prediction = dnn_model.predict(processed_message)33    return "Spam" if prediction >= 0.5 else "Ham"34 35# Other prediction functions for sentiment analysis can follow a similar pattern36 37# Function for CNN prediction38def preprocess_image(image):39    image = image.resize((299, 299))40    image_array = np.array(image)41    preprocessed_image = preprocess_input(image_array)42    return preprocessed_image43 44def make_prediction_cnn(image, model):45    img = image.resize((128, 128))46    img_array = np.array(img)47    img_array = img_array.reshape((1, img_array.shape[0], img_array.shape[1], img_array.shape[2]))48    preprocessed_image = preprocess_input(img_array)49    prediction = model.predict(preprocessed_image)50    return "Tumor Detected" if prediction > 0.5 else "No Tumor"51 52if task == "Sentiment Classification":53    st.subheader("Choose Model")54    model_choice = st.radio("Select Model", ["DNN", "RNN", "Perceptron", "Backpropagation", "LSTM"])55 56    st.subheader("Text Input")57    text_input = st.text_area("Enter Text")58 59    if st.button("Predict"):60        if model_choice == "DNN":61            # You need to define tokeniser and max_length for DNN model62            prediction_result = predict_dnnspam(text_input, tokeniser, max_length)63            st.write(f"The message is classified as: {prediction_result}")64        # Other model choices should call respective prediction functions similarly65 66else:67    st.subheader("Choose Model")68    model_choice = st.radio("Select Model", ["CNN"])69 70    st.subheader("Image Input")71    image_input = st.file_uploader("Choose an image...", type="jpg")72 73    if image_input is not None:74        image = Image.open(image_input)75        st.image(image, caption="Uploaded Image.", use_column_width=True)76 77        if st.button("Predict"):78            if model_choice == "CNN":79                prediction_result = make_prediction_cnn(image, image_model)80                st.write(prediction_result)81