karthsanth's picture
Update app.py
1b48f5a verified
import numpy as np
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
RANDOM_STATE = 55
# Import data
data = pd.read_csv("predictive_maintenance.csv")
# Data preprocessing
data['Tool wear [min]'] = data['Tool wear [min]'].astype('float64')
data['Rotational speed [rpm]'] = data['Rotational speed [rpm]'].astype('float64')
data.rename(mapper={
'Air temperature [K]': 'Air temperature',
'Process temperature [K]': 'Process temperature',
'Rotational speed [rpm]': 'Rotational speed',
'Torque [Nm]': 'Torque',
'Tool wear [min]': 'Tool wear'}, axis=1, inplace=True)
data['Product ID'] = data['Product ID'].apply(lambda x: x[1:])
data['Product ID'] = pd.to_numeric(data['Product ID'])
# Drop ID columns
df = data.copy()
df.drop(columns=['UDI', 'Product ID'], inplace=True)
# Data cleaning
idx_RNF = df.loc[df['Failure Type'] == 'Random Failures'].index
df.drop(index=idx_RNF, inplace=True)
idx_ambiguous = df.loc[(df['Target'] == 1) & (df['Failure Type'] == 'No Failure')].index
df.drop(index=idx_ambiguous, inplace=True)
df.reset_index(drop=True, inplace=True)
# Data resampling using SMOTENC
from imblearn.over_sampling import SMOTENC
n_working = df['Failure Type'].value_counts()['No Failure']
desired_length = round(n_working / 0.8)
spc = round((desired_length - n_working) / 4)
balance_cause = {
'No Failure': n_working,
'Overstrain Failure': spc,
'Heat Dissipation Failure': spc,
'Power Failure': spc,
'Tool Wear Failure': spc
}
sm = SMOTENC(categorical_features=[0, 7], sampling_strategy=balance_cause, random_state=0)
df_res, y_res = sm.fit_resample(df, df['Failure Type'])
# Prepare the data for modeling
type_dict = {'L': 0, 'M': 1, 'H': 2}
cause_dict = {
'No Failure': 0,
'Power Failure': 1,
'Overstrain Failure': 2,
'Heat Dissipation Failure': 3,
'Tool Wear Failure': 4
}
df_pre = df_res.copy()
df_pre['Type'].replace(to_replace=type_dict, inplace=True)
df_pre['Failure Type'].replace(to_replace=cause_dict, inplace=True)
features = [col for col in df_pre.columns if df_pre[col].dtype == 'float64' or col == 'Type']
target = 'Target'
X, y = df_pre[features], df_pre[[target]]
X_train, X_val, y_train, y_val = train_test_split(X, y, train_size=0.8, random_state=RANDOM_STATE)
# Random Forest model
random_forest_model = RandomForestClassifier(n_estimators=100, max_depth=16, min_samples_split=10, random_state=RANDOM_STATE).fit(X_train, y_train)
# Make predictions on validation data
predictions_random_forest = random_forest_model.predict(X_val)
# Create a submission file
X_val['ID'] = range(1, len(X_val) + 1)
submission_forest = X_val[["ID"]].copy()
submission_forest["predictions"] = predictions_random_forest
submission_forest.to_csv("submission_forest.csv", index=None)
import gradio as gr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import precision_score, recall_score, accuracy_score
from sklearn.ensemble import RandomForestClassifier
import seaborn as sns
# Load the data and model
x_train = pd.read_csv("X_train.csv")
x_val = pd.read_csv("X_val.csv")
y_train = pd.read_csv("y_train.csv")
y_val = pd.read_csv("y_val.csv")
RANDOM_STATE = 55
random_forest_model = RandomForestClassifier(
n_estimators=100,
max_depth=16,
min_samples_split=10,
random_state=RANDOM_STATE
).fit(x_train, y_train)
# Make predictions
y_pred = random_forest_model.predict(x_val.drop(columns=['ID']))
# Calculate metrics
precision = precision_score(y_val, y_pred, average='weighted')
recall = recall_score(y_val, y_pred, average='weighted')
accuracy = accuracy_score(y_val, y_pred)
# Calculate metrics
precision = precision_score(y_val, y_pred, average='weighted')
recall = recall_score(y_val, y_pred, average='weighted')
accuracy = accuracy_score(y_val, y_pred)
def plt_to_base64(fig):
import io
import base64
buf = io.BytesIO()
fig.savefig(buf, format='png', bbox_inches='tight')
buf.seek(0)
plt.close(fig)
return base64.b64encode(buf.getvalue()).decode('utf-8')
def generate_pie_chart():
failure_counts = y_val['Target'].value_counts().sort_index()
labels = ['Non-Failure', 'Failure']
colors = ['#4CAF50', '#F44336'] # Green for non-failure, red for failure
fig, ax = plt.subplots(figsize=(6, 6))
ax.pie(
failure_counts,
labels=labels,
autopct='%1.1f%%',
startangle=90,
colors=colors,
textprops={'color': 'white'}
)
ax.set_title('Failure Type Distribution (Green: Non-Failure, Red: Failure)', color='white')
return fig
def generate_report():
# Generate metrics HTML
metrics_html = f"""
<div class="metrics">
<div>
<h3>Precision</h3>
<p>{precision:.2%}</p>
</div>
<div>
<h3>Accuracy</h3>
<p>{accuracy:.2%}</p>
</div>
<div>
<h3>Recall</h3>
<p>{recall:.2%}</p>
</div>
</div>
"""
# Generate pie chart and convert to base64
pie_chart = generate_pie_chart()
pie_chart_base64 = plt_to_base64(pie_chart)
# Load additional images (Replace the URLs with your local image paths or URLs)
image1_url = "https://i.postimg.cc/13tFG3DB/graph1.png"
image2_url = "https://i.postimg.cc/BZptmGSV/grpah-2.png"
images_html = f"""
<div class="images-container">
<img src="{image1_url}" alt="Image 1">
<img src="{image2_url}" alt="Image 2">
</div>
"""
# Generate sample table
sample_data = x_val.copy()
sample_data['Target'] = y_val['Target']
sample_data = sample_data.head(10)
table_html = sample_data.to_html(index=False, classes='data-table')
# Combine all elements
report_html = f"""
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Predictive Maintenance Report</title>
<style>
body {{
font-family: Arial, sans-serif;
margin: 20px;
padding: 0;
background-color: #333333;
color: #FFFFFF;
}}
h1 {{
text-align: center;
color: #FFFFFF;
margin-bottom: 40px;
}}
.metrics {{
display: flex;
justify-content: space-around;
margin-bottom: 40px;
}}
.metrics div {{
text-align: center;
padding: 20px;
border-radius: 8px;
background-color: #FFA500;
width: 25%;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}}
.metrics h3 {{
margin-bottom: 10px;
font-size: 24px;
color: #FFFFFF;
}}
.metrics p {{
font-size: 20px;
margin: 0;
color: #FFFFFF;
}}
.chart-container {{
display: flex;
justify-content: center;
margin-left: px;
padding-left: 50px;
border: 2px solid #555555;
border-radius: 8px;
background-color: #444444;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}}
.chart-container img {{
max-width: 100%;
height: auto;
}}
.images-container {{
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 40px;
}}
.images-container img {{
width: 80%;
margin-bottom: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
border: 2px solid #555555;
}}
.data-table {{
width: 100%;
border-collapse: collapse;
margin-top: 20px;
color: #FFFFFF;
}}
.data-table th, .data-table td {{
border: 1px solid #555555;
padding: 10px;
text-align: center;
}}
.data-table th {{
background-color: #555555;
}}
.data-table tr:nth-child(even) {{
background-color: #444444;
}}
.data-table tr:nth-child(odd) {{
background-color: #555555;
}}
</style>
</head>
<body>
<h1>Predictive Maintenance Report</h1>
{metrics_html}
<div class="chart-container">
<img src="data:image/png;base64,{pie_chart_base64}" alt="Failure Type Distribution">
</div>
{images_html}
{table_html}
</body>
</html>
"""
return report_html
iface = gr.Interface(
fn=generate_report,
inputs=[],
outputs=gr.HTML(),
title="Predictive Maintenance Report Generator",
description="Click to generate a report based on the predictive maintenance model."
)
iface.launch()
'gradio deploy'