Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import pickle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
|
| 7 |
-
model = pickle.load(model_file)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
|
| 13 |
# Prepare the input data
|
| 14 |
sample_input = {
|
|
@@ -30,6 +69,9 @@ def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
|
|
| 30 |
# Reorder columns to match the order in the training data
|
| 31 |
sample_df = sample_df[feature_columns]
|
| 32 |
|
|
|
|
|
|
|
|
|
|
| 33 |
# Make a prediction
|
| 34 |
prediction = model.predict(sample_df)[0]
|
| 35 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import pickle
|
| 4 |
+
from imblearn.over_sampling import SMOTE
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.linear_model import LogisticRegression
|
| 7 |
+
from sklearn.metrics import classification_report, confusion_matrix
|
| 8 |
+
from sklearn.preprocessing import StandardScaler
|
| 9 |
|
| 10 |
+
# Load your dataset
|
| 11 |
+
df = pd.read_csv('data.csv') # Replace with your actual data file path
|
|
|
|
| 12 |
|
| 13 |
+
# Prepare features and target variable
|
| 14 |
+
X = df.drop(columns=['status']) # Features
|
| 15 |
+
y = df['status'] # Target labels
|
| 16 |
|
| 17 |
+
# Split the data into training and testing sets
|
| 18 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
| 19 |
+
|
| 20 |
+
# Apply SMOTE to balance the training set
|
| 21 |
+
smote = SMOTE(random_state=42)
|
| 22 |
+
X_train_balanced, y_train_balanced = smote.fit_resample(X_train, y_train)
|
| 23 |
+
|
| 24 |
+
# Standardize features
|
| 25 |
+
scaler = StandardScaler()
|
| 26 |
+
X_train_balanced = scaler.fit_transform(X_train_balanced)
|
| 27 |
+
X_test = scaler.transform(X_test)
|
| 28 |
+
|
| 29 |
+
# Train the model on the balanced data
|
| 30 |
+
model = LogisticRegression()
|
| 31 |
+
model.fit(X_train_balanced, y_train_balanced)
|
| 32 |
+
|
| 33 |
+
# Make predictions on the test set
|
| 34 |
+
y_pred = model.predict(X_test)
|
| 35 |
+
|
| 36 |
+
# Evaluate the model
|
| 37 |
+
print("Confusion Matrix:")
|
| 38 |
+
print(confusion_matrix(y_test, y_pred))
|
| 39 |
+
print("\nClassification Report:")
|
| 40 |
+
print(classification_report(y_test, y_pred))
|
| 41 |
+
|
| 42 |
+
# Save the updated model and feature columns
|
| 43 |
+
with open('logistic_regression_model.pkl', 'wb') as model_file:
|
| 44 |
+
pickle.dump(model, model_file)
|
| 45 |
+
|
| 46 |
+
feature_columns = X.columns.tolist()
|
| 47 |
+
with open('feature_columns.pkl', 'wb') as col_file:
|
| 48 |
+
pickle.dump(feature_columns, col_file)
|
| 49 |
+
|
| 50 |
+
# Define prediction function for Gradio
|
| 51 |
def predict_status(air_temp, proc_temp, speed, torque, tool_wear):
|
| 52 |
# Prepare the input data
|
| 53 |
sample_input = {
|
|
|
|
| 69 |
# Reorder columns to match the order in the training data
|
| 70 |
sample_df = sample_df[feature_columns]
|
| 71 |
|
| 72 |
+
# Standardize the input features
|
| 73 |
+
sample_df = scaler.transform(sample_df)
|
| 74 |
+
|
| 75 |
# Make a prediction
|
| 76 |
prediction = model.predict(sample_df)[0]
|
| 77 |
|