vishalkryadav's picture
Update app.py
86018aa verified
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
import seaborn as sns
from sklearn.preprocessing import LabelEncoder
import streamlit as st
st.title("Rouge Component Prediction")
#Reading Dataset
df = pd.read_csv('identify_rogue_50K_ALL.csv')
print("Dataset Size:",df.shape)
st.sidebar.header('Enter the Components Details here')
# Dropping the SRU serial number
df.drop(['SRU serial number','Date of Manufacture','Last Maintenance Date','date of last failure'], axis = 1, inplace=True)
# DATA from user
def user_report():
manufacturer = st.sidebar.selectbox("Manufacturer",
("JKL Company", "GHI Company","AGS Company","ABC Company","XYZ Company" ))
if manufacturer=='JKL Company':
manufacturer=3
elif manufacturer=="GHI Company":
manufacturer=2
elif manufacturer=="AGS Company":
manufacturer=1
elif manufacturer=="ABC Company":
manufacturer =0
else:
manufacturer=4
component_age = st.sidebar.slider('Component Age (in hours)', 500,2000, 600 )
total_operating_hours = st.sidebar.slider('Total Operating Hours)', 50,2000, 500 )
usage_intensity = st.sidebar.slider('Usage Intensity hours/day', 0,9, 5 )
last_maintance_type = st.sidebar.selectbox('Last Mantainence Type', ("Preventive","Corrective") )
if last_maintance_type=="Preventive":
last_maintance_type=1
else:
last_maintance_type=0
previous_number_of_repairs = st.sidebar.number_input('Enter the Previous Number of Repairs Undergone 0 to 5 )',min_value=0,max_value=5,step=1)
operating_temperature = st.sidebar.slider('Operating Temperature', 10,25, 15 )
humidity = st.sidebar.slider('Humidity', 20,105, 25 )
Vibration_Level = st.sidebar.slider('Vibration Level', 2,7, 2 )
Pressure = st.sidebar.slider('Pressure', 200,550, 250 )
Power_Input_Voltage= st.sidebar.slider('Power Input Voltage (V)',100,133,115)
repair_type = st.sidebar.selectbox('Repair Type', ("Hardware","Software") )
if repair_type=='Hardware':
repair_type=0
else:
repair_type=1
number_of_inspection = st.sidebar.selectbox('Number of Inspections',('1','2'))
if number_of_inspection=='1':
number_of_inspection=1
else:
number_of_inspection=2
number_of_inspection_6months = st.sidebar.selectbox('Number of Inspections in last 6 Months',('0','1'))
if number_of_inspection_6months=='0':
number_of_inspection_6months=0
else:
number_of_inspection_6months=1
prior_maintainence = st.sidebar.selectbox('Prior Maintainence',("Regular","Irregular"))
if prior_maintainence =='Regular':
prior_maintainence=1
else:
prior_maintainence=0
user_report_data = {
'Manufacturer':manufacturer,
'Component_Age':component_age,
'Total Operating Hours':total_operating_hours,
'Usage Intensity (hours/day)':usage_intensity,
'Last Maintenance Type': last_maintance_type,
'Previous number of repairs':previous_number_of_repairs,
'Operating Temperature':operating_temperature,
'Humidity': humidity,
'Vibration Level':Vibration_Level,
'Pressure':Pressure,
'Power Input Voltage (V)':Power_Input_Voltage,
'repair type':repair_type ,
'total number of inspection':number_of_inspection,
'No. of Inspections in Last 6 Months':number_of_inspection_6months,
'Prior Maintenance':prior_maintainence
}
report_data = pd.DataFrame(user_report_data, index=[0])
return report_data
#Customer Data
user_data = user_report()
st.header("Component Details")
st.write(user_data)
def label_encoder(df):
le = LabelEncoder()
cat = df.select_dtypes(include='O').keys()
categ = list(cat)
df[categ] = df[categ].apply(le.fit_transform)
return df
def preprocess_dataset(X):
x = X.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
X_df = pd.DataFrame(x_scaled)
return X_df
def prediction(df):
#X = df.loc[:,df.columns!= "Rogue LRU/SRU (Target)"]
#y = df["Rogue LRU/SRU (Target)"]
#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)
#print(X_train.shape)
#print(X_test.shape)
X_test_encoded = label_encoder(df)
X_test_df = preprocess_dataset(X_test_encoded)
#x_model = loaded_model = tf.keras.models.load_model('my_model')
#x_model = tf.keras.models.load_model('my_model.h5')
try:
x_model = tf.keras.models.load_model('my_model.h5')
except (OSError, ValueError):
# Load the model as a TFSMLayer if .h5 loading fails
tfs_layer = tf.keras.layers.TFSMLayer('my_model', call_endpoint='serving_default')
# Create a new model using the TFSMLayer
inputs = tf.keras.Input(shape=(X_test_df.shape[1],))
outputs = tfs_layer(inputs)
x_model = tf.keras.Model(inputs=inputs, outputs=outputs)
y_pred = x_model.predict(X_test_df)
#predicition = []
#for i in list(y_pred):
if y_pred ==0:
return 'Component is Good'
else:
return 'Component is not Good'
#X_test['Actual_time_to_repair'] = y_test
#X_test['Predicted_time_to_repair'] = predicition
# X_test.to_csv(r'/content/drive/MyDrive/Colab Notebooks/HAL/rogue_test_data.csv')
#print(X_test.head())
y_pred = prediction(user_data)
if st.button("Predict"):
st.subheader(y_pred)