Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,46 @@
|
|
| 1 |
import os
|
| 2 |
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
-
from sklearn.model_selection import train_test_split
|
| 7 |
-
from sklearn.metrics import accuracy_score
|
| 8 |
-
import gradio as gr
|
| 9 |
import joblib
|
| 10 |
-
|
| 11 |
-
# Step 1: Generate or Load Sample Data
|
| 12 |
-
np.random.seed(42)
|
| 13 |
-
data = pd.DataFrame({
|
| 14 |
-
"Pressure": np.random.randint(50, 200, 200),
|
| 15 |
-
"Temperature": np.random.randint(300, 700, 200),
|
| 16 |
-
"Material_Type": np.random.randint(1, 5, 200),
|
| 17 |
-
"Defect_Type": np.random.choice([0, 1, 2], 200, p=[0.6, 0.3, 0.1])
|
| 18 |
-
})
|
| 19 |
-
|
| 20 |
-
X = data[["Pressure", "Temperature", "Material_Type"]]
|
| 21 |
-
y = data["Defect_Type"]
|
| 22 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 23 |
-
|
| 24 |
-
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 25 |
-
model.fit(X_train, y_train)
|
| 26 |
-
joblib.dump(model, "defect_model.pkl")
|
| 27 |
-
y_pred = model.predict(X_test)
|
| 28 |
-
print(f"Model Accuracy: {accuracy_score(y_test, y_pred):.2f}")
|
| 29 |
|
| 30 |
# Visualization Function
|
| 31 |
def plot_simulation(pressure, temperature, material_type):
|
| 32 |
if material_type <= 0 or temperature <= 0:
|
| 33 |
-
raise ValueError("
|
| 34 |
stress = np.linspace(pressure * 0.5, pressure * 1.5, 100)
|
| 35 |
strain = stress / (temperature * 0.01 * material_type)
|
| 36 |
-
plt.figure(figsize=(
|
| 37 |
plt.plot(stress, strain, label="Stress-Strain Curve")
|
| 38 |
plt.xlabel("Stress (MPa)")
|
| 39 |
plt.ylabel("Strain (%)")
|
| 40 |
plt.title("Stress-Strain Simulation")
|
| 41 |
plt.legend()
|
| 42 |
plt.grid(True)
|
| 43 |
-
|
| 44 |
-
plt.savefig(
|
| 45 |
plt.close()
|
| 46 |
-
return
|
| 47 |
|
| 48 |
# Prediction Function
|
| 49 |
def predict_and_visualize(pressure, temperature, material_type):
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
input_data = np.array([[pressure, temperature, material_type]])
|
| 55 |
-
prediction = loaded_model.predict(input_data)[0]
|
| 56 |
-
defect_map = {0: "No Defect", 1: "Crack", 2: "Wrinkle"}
|
| 57 |
-
defect_prediction = defect_map[prediction]
|
| 58 |
-
plot_path = plot_simulation(pressure, temperature, material_type)
|
| 59 |
-
return f"Predicted Defect: {defect_prediction}", plot_path
|
| 60 |
|
| 61 |
# Gradio Interface
|
| 62 |
interface = gr.Interface(
|
|
@@ -71,7 +55,7 @@ interface = gr.Interface(
|
|
| 71 |
gr.Image(label="Stress-Strain Visualization")
|
| 72 |
],
|
| 73 |
title="Defect Prediction & Simulation",
|
| 74 |
-
description="Predict defect type and visualize stress-strain simulation
|
| 75 |
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import numpy as np
|
|
|
|
| 3 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import joblib
|
| 5 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Visualization Function
|
| 8 |
def plot_simulation(pressure, temperature, material_type):
|
| 9 |
if material_type <= 0 or temperature <= 0:
|
| 10 |
+
raise ValueError("Temperature and Material Type must be positive.")
|
| 11 |
stress = np.linspace(pressure * 0.5, pressure * 1.5, 100)
|
| 12 |
strain = stress / (temperature * 0.01 * material_type)
|
| 13 |
+
plt.figure(figsize=(6, 4))
|
| 14 |
plt.plot(stress, strain, label="Stress-Strain Curve")
|
| 15 |
plt.xlabel("Stress (MPa)")
|
| 16 |
plt.ylabel("Strain (%)")
|
| 17 |
plt.title("Stress-Strain Simulation")
|
| 18 |
plt.legend()
|
| 19 |
plt.grid(True)
|
| 20 |
+
output_path = "simulation_plot.png"
|
| 21 |
+
plt.savefig(output_path)
|
| 22 |
plt.close()
|
| 23 |
+
return output_path
|
| 24 |
|
| 25 |
# Prediction Function
|
| 26 |
def predict_and_visualize(pressure, temperature, material_type):
|
| 27 |
+
model_path = "defect_model.pkl"
|
| 28 |
+
if not os.path.exists(model_path):
|
| 29 |
+
return "Error: Model file not found.", None
|
| 30 |
+
try:
|
| 31 |
+
model = joblib.load(model_path)
|
| 32 |
+
prediction = model.predict([[pressure, temperature, material_type]])[0]
|
| 33 |
+
defect_map = {0: "No Defect", 1: "Defect"}
|
| 34 |
+
defect_prediction = defect_map.get(prediction, "Unknown")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Prediction Error: {str(e)}", None
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
plot_path = plot_simulation(pressure, temperature, material_type)
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return defect_prediction, f"Visualization Error: {str(e)}"
|
| 42 |
|
| 43 |
+
return defect_prediction, plot_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Gradio Interface
|
| 46 |
interface = gr.Interface(
|
|
|
|
| 55 |
gr.Image(label="Stress-Strain Visualization")
|
| 56 |
],
|
| 57 |
title="Defect Prediction & Simulation",
|
| 58 |
+
description="Predict defect type and visualize stress-strain simulation."
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|