Spaces:
Sleeping
Sleeping
File size: 2,081 Bytes
c1aff9b 4065124 c1aff9b 4065124 c1aff9b 4065124 c1aff9b 53d1f3c c1aff9b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import os
import pandas as pd
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from joblib import dump, load
# Function to train models
def train_models():
# Synthetic dataset
data = {
"Thickness": [10, 15, 20, 25],
"Hole_Diameter": [5, 10, 15, 20],
"Force": [5000, 7000, 10000, 12000],
"Max_Stress": [300, 250, 200, 150],
"Max_Deformation": [0.5, 0.4, 0.3, 0.2],
"Pass_Fail": [1, 1, 0, 0]
}
df = pd.DataFrame(data)
X = df[["Thickness", "Hole_Diameter", "Force"]]
# Train regression models
stress_model = RandomForestRegressor().fit(X, df["Max_Stress"])
deformation_model = RandomForestRegressor().fit(X, df["Max_Deformation"])
# Train classification model
pass_fail_model = RandomForestClassifier().fit(X, df["Pass_Fail"])
# Save models
dump(stress_model, "stress_model.pkl")
dump(deformation_model, "deformation_model.pkl")
dump(pass_fail_model, "pass_fail_model.pkl")
print("Models have been trained and saved.")
# Function to load models with error handling
def load_models():
try:
# Check if model files exist
if not os.path.exists("stress_model.pkl") or not os.path.exists("deformation_model.pkl") or not os.path.exists("pass_fail_model.pkl"):
print("Model files not found. Training models...")
train_models()
# Load models
stress_model = load("stress_model.pkl")
deformation_model = load("deformation_model.pkl")
pass_fail_model = load("pass_fail_model.pkl")
print("Models have been loaded.")
return stress_model, deformation_model, pass_fail_model
except Exception as e:
print(f"Error loading models: {e}")
return None, None, None
# Main program
if __name__ == "__main__":
stress_model, deformation_model, pass_fail_model = load_models()
if stress_model and deformation_model and pass_fail_model:
print("Models are ready to use!")
else:
print("Failed to load models.")
|