mayankchugh-learning commited on
Commit
54e55a2
·
verified ·
1 Parent(s): 64ac37c

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +79 -0
  2. model.joblib +3 -0
  3. requirements.txt +3 -0
  4. train.py +76 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uuid
3
+ import joblib
4
+ import json
5
+
6
+ import gradio as gr
7
+ import pandas as pd
8
+
9
+ # from huggingface_hub import CommitScheduler
10
+ from pathlib import Path
11
+
12
+
13
+ # log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
14
+ # log_folder = log_file.parent
15
+
16
+ # scheduler = CommitScheduler(
17
+ # repo_id="machine-failure-logs",
18
+ # repo_type="dataset",
19
+ # folder_path=log_folder,
20
+ # path_in_repo="data",
21
+ # every=2
22
+ # )
23
+
24
+ machine_failure_predictor = joblib.load('model.joblib')
25
+
26
+ air_temperature_input = gr.Number(label='Air temperature [K]')
27
+ process_temperature_input = gr.Number(label='Process temperature [K]')
28
+ rotational_speed_input = gr.Number(label='Rotational speed [rpm]')
29
+ torque_input = gr.Number(label='Torque [Nm]')
30
+ tool_wear_input = gr.Number(label='Tool wear [min]')
31
+ type_input = gr.Dropdown(
32
+ ['L', 'M', 'H'],
33
+ label='Type'
34
+ )
35
+
36
+ model_output = gr.Label(label="Machine failure")
37
+
38
+ def predict_machine_failure(air_temperature, process_temperature, rotational_speed, torque, tool_wear, type):
39
+ sample = {
40
+ 'Air temperature [K]': air_temperature,
41
+ 'Process temperature [K]': process_temperature,
42
+ 'Rotational speed [rpm]': rotational_speed,
43
+ 'Torque [Nm]': torque,
44
+ 'Tool wear [min]': tool_wear,
45
+ 'Type': type
46
+ }
47
+ data_point = pd.DataFrame([sample])
48
+ prediction = machine_failure_predictor.predict(data_point).tolist()
49
+
50
+ # with scheduler.lock:
51
+ # with log_file.open("a") as f:
52
+ # f.write(json.dumps(
53
+ # {
54
+ # 'Air temperature [K]': air_temperature,
55
+ # 'Process temperature [K]': process_temperature,
56
+ # 'Rotational speed [rpm]': rotational_speed,
57
+ # 'Torque [Nm]': torque,
58
+ # 'Tool wear [min]': tool_wear,
59
+ # 'Type': type,
60
+ # 'prediction': prediction[0]
61
+ # }
62
+ # ))
63
+ # f.write("\n")
64
+
65
+ return prediction[0]
66
+
67
+ demo = gr.Interface(
68
+ fn=predict_machine_failure,
69
+ inputs=[air_temperature_input, process_temperature_input, rotational_speed_input,
70
+ torque_input, tool_wear_input, type_input],
71
+ outputs=model_output,
72
+ title="Machine Failure Predictor",
73
+ description="This API allows you to predict the machine failure status of an equipment",
74
+ allow_flagging="auto",
75
+ concurrency_limit=8
76
+ )
77
+
78
+ demo.queue()
79
+ demo.launch(share=False)
model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a0db284be28e1303ab3612a3a6e35076ff8e9e32c035dd4e2ffdf9635b940780
3
+ size 3838
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ scikit-learn==1.2.2
2
+ joblib
3
+ gradio
train.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import joblib
3
+
4
+ from sklearn.datasets import fetch_openml
5
+
6
+ from sklearn.preprocessing import StandardScaler, OneHotEncoder
7
+ from sklearn.compose import make_column_transformer
8
+
9
+ from sklearn.pipeline import make_pipeline
10
+
11
+ from sklearn.model_selection import train_test_split, RandomizedSearchCV
12
+
13
+ from sklearn.linear_model import LogisticRegression
14
+ from sklearn.metrics import accuracy_score, classification_report
15
+
16
+ dataset = fetch_openml(data_id=42890, as_frame=True, parser="auto")
17
+
18
+ data_df = dataset.data
19
+
20
+ target = 'Machine failure'
21
+ numeric_features = [
22
+ 'Air temperature [K]',
23
+ 'Process temperature [K]',
24
+ 'Rotational speed [rpm]',
25
+ 'Torque [Nm]',
26
+ 'Tool wear [min]'
27
+ ]
28
+ categorical_features = ['Type']
29
+
30
+ print("Creating data subsets")
31
+
32
+ X = data_df[numeric_features + categorical_features]
33
+ y = data_df[target]
34
+
35
+ Xtrain, Xtest, ytrain, ytest = train_test_split(
36
+ X, y,
37
+ test_size=0.2,
38
+ random_state=42
39
+ )
40
+
41
+ preprocessor = make_column_transformer(
42
+ (StandardScaler(), numeric_features),
43
+ (OneHotEncoder(handle_unknown='ignore'), categorical_features)
44
+ )
45
+
46
+ model_logistic_regression = LogisticRegression(n_jobs=-1)
47
+
48
+ print("Estimating Best Model Pipeline")
49
+
50
+ model_pipeline = make_pipeline(
51
+ preprocessor,
52
+ model_logistic_regression
53
+ )
54
+
55
+ param_distribution = {
56
+ "logisticregression__C": [0.001, 0.01, 0.1, 0.5, 1, 5, 10]
57
+ }
58
+
59
+ rand_search_cv = RandomizedSearchCV(
60
+ model_pipeline,
61
+ param_distribution,
62
+ n_iter=3,
63
+ cv=3,
64
+ random_state=42
65
+ )
66
+
67
+ rand_search_cv.fit(Xtrain, ytrain)
68
+
69
+ print("Logging Metrics")
70
+ print(f"Accuracy: {rand_search_cv.best_score_}")
71
+
72
+ print("Serializing Model")
73
+
74
+ saved_model_path = "model.joblib"
75
+
76
+ joblib.dump(rand_search_cv.best_estimator_, saved_model_path)