Upload 2 files
Browse files- app-5.py +143 -0
- requirements-4.txt +7 -0
app-5.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import joblib # For loading the model
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import io
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# Load the scaler and model
|
| 10 |
+
scaler_path = "scaler.pkl"
|
| 11 |
+
model_path = "ebm_model.pkl"
|
| 12 |
+
|
| 13 |
+
# Load the pre-trained scaler and model
|
| 14 |
+
scaler = joblib.load(scaler_path)
|
| 15 |
+
ebm = joblib.load(model_path)
|
| 16 |
+
print("Loaded saved model and scaler.")
|
| 17 |
+
|
| 18 |
+
# Global variable to store the most recent input data for explanation
|
| 19 |
+
last_input_data_scaled = None
|
| 20 |
+
|
| 21 |
+
# Define prediction function
|
| 22 |
+
def predict_strength(cement, blast_furnace_slag, fly_ash, water, superplasticizer,
|
| 23 |
+
coarse_aggregate, fine_aggregate, age):
|
| 24 |
+
global last_input_data_scaled
|
| 25 |
+
input_data = pd.DataFrame({
|
| 26 |
+
'cement': [cement], 'blast_furnace_slag': [blast_furnace_slag],
|
| 27 |
+
'fly_ash': [fly_ash], 'water': [water],
|
| 28 |
+
'superplasticizer': [superplasticizer],
|
| 29 |
+
'coarse_aggregate': [coarse_aggregate],
|
| 30 |
+
'fine_aggregate': [fine_aggregate], 'age': [age]
|
| 31 |
+
})
|
| 32 |
+
last_input_data_scaled = scaler.transform(input_data)
|
| 33 |
+
prediction = ebm.predict(last_input_data_scaled)
|
| 34 |
+
return prediction[0]
|
| 35 |
+
|
| 36 |
+
# Explanation function for dynamic detailed report and enhanced visual
|
| 37 |
+
def show_local_explanation():
|
| 38 |
+
if last_input_data_scaled is not None:
|
| 39 |
+
local_exp = ebm.explain_local(last_input_data_scaled)
|
| 40 |
+
contributions = local_exp.data(0)['scores']
|
| 41 |
+
names = local_exp.data(0)['names']
|
| 42 |
+
|
| 43 |
+
# Generate dynamic, user-friendly text report based on contributions
|
| 44 |
+
report_lines = []
|
| 45 |
+
for name, contribution in zip(names, contributions):
|
| 46 |
+
abs_contribution = abs(contribution)
|
| 47 |
+
if contribution > 0:
|
| 48 |
+
if abs_contribution > 10:
|
| 49 |
+
report_lines.append(f"- {name.capitalize()} strongly contributes to concrete strength (+{contribution:.2f}).")
|
| 50 |
+
else:
|
| 51 |
+
report_lines.append(f"- {name.capitalize()} has a minor positive effect on concrete strength (+{contribution:.2f}).")
|
| 52 |
+
elif contribution < 0:
|
| 53 |
+
if abs_contribution > 10:
|
| 54 |
+
report_lines.append(f"- {name.capitalize()} significantly reduces concrete strength ({contribution:.2f}). Consider adjusting this component.")
|
| 55 |
+
else:
|
| 56 |
+
report_lines.append(f"- {name.capitalize()} has a slight negative effect on concrete strength ({contribution:.2f}).")
|
| 57 |
+
|
| 58 |
+
# Join lines into a single report
|
| 59 |
+
report = "\n".join(report_lines)
|
| 60 |
+
|
| 61 |
+
# Enhanced Plotting
|
| 62 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 63 |
+
colors = ['red' if x < 0 else 'green' for x in contributions]
|
| 64 |
+
ax.barh(names, contributions, color=colors)
|
| 65 |
+
ax.set_xlabel('Contribution to Prediction')
|
| 66 |
+
ax.set_title('Local Explanation for the Most Recent Prediction')
|
| 67 |
+
|
| 68 |
+
# Save plot to a buffer
|
| 69 |
+
buf = io.BytesIO()
|
| 70 |
+
plt.savefig(buf, format='png', bbox_inches='tight')
|
| 71 |
+
buf.seek(0)
|
| 72 |
+
plt.close()
|
| 73 |
+
|
| 74 |
+
# Load image for display
|
| 75 |
+
img = Image.open(buf)
|
| 76 |
+
img_array = np.array(img)
|
| 77 |
+
return img_array, report
|
| 78 |
+
else:
|
| 79 |
+
return None, "No prediction has been made yet."
|
| 80 |
+
|
| 81 |
+
# Function to provide the PDF guide
|
| 82 |
+
def download_guide():
|
| 83 |
+
pdf_path = "Guide.pdf" # Path to the uploaded guide
|
| 84 |
+
return pdf_path
|
| 85 |
+
|
| 86 |
+
# Gradio interface setup with introduction and instructions
|
| 87 |
+
with gr.Blocks() as app:
|
| 88 |
+
gr.Markdown("## Concrete Strength Prediction App")
|
| 89 |
+
gr.Markdown("""
|
| 90 |
+
This app predicts the compressive strength of concrete based on its composition using the Explainable Boosting Machine (EBM).
|
| 91 |
+
EBM is a transparent, interpretable machine learning model that combines the power of boosting techniques with interpretable models,
|
| 92 |
+
making it easier to explain prediction outcomes.
|
| 93 |
+
""")
|
| 94 |
+
gr.Markdown("### Instructions")
|
| 95 |
+
gr.Markdown("""
|
| 96 |
+
- Enter the composition of the concrete in the input fields.
|
| 97 |
+
- Click 'Predict Concrete Strength' to see the predicted strength.
|
| 98 |
+
- Click 'Show Detailed Report' to see a breakdown of each feature's impact on the prediction.
|
| 99 |
+
- Click 'Download Guide' to learn about the concrete mix components and input guidelines.
|
| 100 |
+
""")
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
cement = gr.Number(label="Cement")
|
| 104 |
+
slag = gr.Number(label="Blast Furnace Slag")
|
| 105 |
+
fly_ash = gr.Number(label="Fly Ash")
|
| 106 |
+
water = gr.Number(label="Water")
|
| 107 |
+
superplasticizer = gr.Number(label="Superplasticizer")
|
| 108 |
+
coarse_agg = gr.Number(label="Coarse Aggregate")
|
| 109 |
+
fine_agg = gr.Number(label="Fine Aggregate")
|
| 110 |
+
age = gr.Number(label="Age")
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
download_btn = gr.Button("Download Guide")
|
| 114 |
+
predict_btn = gr.Button("Predict Concrete Strength")
|
| 115 |
+
explanation_btn = gr.Button("Show Detailed Report")
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
guide_file = gr.File(label="Guide Download")
|
| 119 |
+
|
| 120 |
+
result = gr.Textbox(label="Predicted Concrete Strength")
|
| 121 |
+
local_image = gr.Image(label="Local Explanation", type="numpy")
|
| 122 |
+
explanation_text = gr.Textbox(label="Feature Impact Report")
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
download_btn.click(
|
| 126 |
+
fn=download_guide,
|
| 127 |
+
inputs=[],
|
| 128 |
+
outputs=guide_file
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
predict_btn.click(
|
| 132 |
+
fn=predict_strength,
|
| 133 |
+
inputs=[cement, slag, fly_ash, water, superplasticizer, coarse_agg, fine_agg, age],
|
| 134 |
+
outputs=result
|
| 135 |
+
)
|
| 136 |
+
explanation_btn.click(
|
| 137 |
+
fn=show_local_explanation,
|
| 138 |
+
inputs=[],
|
| 139 |
+
outputs=[local_image, explanation_text]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
app.launch()
|
requirements-4.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
matplotlib
|
| 5 |
+
Pillow
|
| 6 |
+
interpret
|
| 7 |
+
joblib
|