Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,74 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import joblib
|
|
|
|
|
|
|
|
|
|
| 3 |
from models.optimizer import optimize_design
|
| 4 |
-
|
| 5 |
-
# Sidebar for user inputs
|
| 6 |
-
with st.sidebar:
|
| 7 |
-
tool_width = st.number_input("Tool Width (mm)", value=50)
|
| 8 |
-
tool_height = st.number_input("Tool Height (mm)", value=20)
|
| 9 |
-
|
| 10 |
-
# Display tool dimensions
|
| 11 |
-
st.write(f"Tool dimensions: {tool_width}mm x {tool_height}mm")
|
| 12 |
-
|
| 13 |
# Load pre-trained model
|
| 14 |
model = joblib.load('models/defect_model.pkl')
|
| 15 |
-
|
| 16 |
-
|
| 17 |
st.title("AI-Driven Press Tool Defect Predictor & Optimizer")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
from models.optimizer import optimize_design
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Load pre-trained model
|
| 8 |
model = joblib.load('models/defect_model.pkl')
|
| 9 |
+
# Page Title
|
| 10 |
+
19/112
|
| 11 |
st.title("AI-Driven Press Tool Defect Predictor & Optimizer")
|
| 12 |
+
# Sidebar: User Input Parameters
|
| 13 |
+
st.sidebar.header("Input Parameters")
|
| 14 |
+
tool_width = st.sidebar.slider("Tool Width (mm)", min_value=30, max_value=100,
|
| 15 |
+
value=50, step=5)
|
| 16 |
+
tool_height = st.sidebar.slider("Tool Height (mm)", min_value=10, max_value=50,
|
| 17 |
+
value=20, step=2)
|
| 18 |
+
material_strength = st.sidebar.slider("Material Strength (MPa)", min_value=200,
|
| 19 |
+
max_value=500, value=300, step=20)
|
| 20 |
+
press_force = st.sidebar.slider("Press Force (kN)", min_value=50, max_value=300,
|
| 21 |
+
value=100, step=10)
|
| 22 |
+
# Input Data Frame
|
| 23 |
+
input_data = pd.DataFrame({
|
| 24 |
+
'tool_width': [tool_width],
|
| 25 |
+
'tool_height': [tool_height],
|
| 26 |
+
'material_strength': [material_strength],
|
| 27 |
+
'press_force': [press_force]
|
| 28 |
+
})
|
| 29 |
+
# Predict Defect
|
| 30 |
+
defect = model.predict(input_data)[0]
|
| 31 |
+
probabilities = model.predict_proba(input_data)[0]
|
| 32 |
+
# Optimize Design
|
| 33 |
+
optimized_params = optimize_design(tool_width, tool_height, material_strength,
|
| 34 |
+
press_force)
|
| 35 |
+
# Main Section: Results
|
| 36 |
+
st.subheader("Defect Prediction and Optimization Results")
|
| 37 |
+
# Prediction Result
|
| 38 |
+
st.markdown(f"### Predicted Defect: **{defect}**")
|
| 39 |
+
# Visualize Prediction Probabilities
|
| 40 |
+
st.markdown("### Prediction Probabilities")
|
| 41 |
+
fig, ax = plt.subplots()
|
| 42 |
+
labels = model.classes_
|
| 43 |
+
ax.bar(labels, probabilities, color='skyblue')
|
| 44 |
+
ax.set_ylabel("Probability")
|
| 45 |
+
ax.set_title("Defect Prediction Probabilities")
|
| 46 |
+
20/112
|
| 47 |
+
Enhancements Added
|
| 48 |
+
1. Bar Chart for Defect Probabilities:
|
| 49 |
+
Displays the condence level for each defect prediction.
|
| 50 |
+
2. Real-Time Parameter Adjustment:
|
| 51 |
+
Adjust parameters like tool width, height, material strength, and press force using
|
| 52 |
+
sliders.
|
| 53 |
+
Instantly see how predictions and optimizations are aected.
|
| 54 |
+
3. Line Chart for Parameter Impact:
|
| 55 |
+
Visualizes the relationship between tool width and defect probabilities.
|
| 56 |
+
Similar visualizations can be added for other parameters.
|
| 57 |
+
st.pyplot(fig)
|
| 58 |
+
# Optimized Parameters
|
| 59 |
+
st.markdown("### Optimized Design Parameters")
|
| 60 |
+
st.json(optimized_params)
|
| 61 |
+
# Dynamic Visualization: Adjusting Parameters
|
| 62 |
+
st.markdown("### Parameter Impact on Predictions")
|
| 63 |
+
impact_chart_data = []
|
| 64 |
+
for w in range(30, 101, 10):
|
| 65 |
+
temp_data = pd.DataFrame({
|
| 66 |
+
'tool_width': [w],
|
| 67 |
+
'tool_height': [tool_height],
|
| 68 |
+
'material_strength': [material_strength],
|
| 69 |
+
'press_force': [press_force]
|
| 70 |
+
})
|
| 71 |
+
prob = model.predict_proba(temp_data)[0]
|
| 72 |
+
impact_chart_data.append((w, prob))
|
| 73 |
+
impact_chart_df = pd.DataFrame(impact_chart_data, columns=["Tool Width", *labels])
|
| 74 |
+
st.line_chart(impact_chart_df.set_index("Tool Width"))
|