Lrosado commited on
Commit
6c11612
·
verified ·
1 Parent(s): 62f66e7

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +78 -0
  2. model.joblib +3 -0
  3. requirements.txt +2 -0
  4. train.py +76 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
13
+ log_folder = log_file.parent
14
+
15
+ scheduler = CommitScheduler(
16
+ repo_id="machine-failure-logs",
17
+ repo_type="dataset",
18
+ folder_path=log_folder,
19
+ path_in_repo="data",
20
+ every=2
21
+ )
22
+
23
+ machine_failure_predictor = joblib.load('model.joblib')
24
+
25
+ air_temperature_input = gr.Number(label='Air temperature [K]')
26
+ process_temperature_input = gr.Number(label='Process temperature [K]')
27
+ rotational_speed_input = gr.Number(label='Rotational speed [rpm]')
28
+ torque_input = gr.Number(label='Torque [Nm]')
29
+ tool_wear_input = gr.Number(label='Tool wear [min]')
30
+ type_input = gr.Dropdown(
31
+ ['L', 'M', 'H'],
32
+ label='Type'
33
+ )
34
+
35
+ model_output = gr.Label(label="Machine failure")
36
+
37
+ def predict_machine_failure(air_temperature, process_temperature, rotational_speed, torque, tool_wear, type):
38
+ sample = {
39
+ 'Air temperature [K]': air_temperature,
40
+ 'Process temperature [K]': process_temperature,
41
+ 'Rotational speed [rpm]': rotational_speed,
42
+ 'Torque [Nm]': torque,
43
+ 'Tool wear [min]': tool_wear,
44
+ 'Type': type
45
+ }
46
+ data_point = pd.DataFrame([sample])
47
+ prediction = machine_failure_predictor.predict(data_point).tolist()
48
+
49
+ with scheduler.lock:
50
+ with log_file.open("a") as f:
51
+ f.write(json.dumps(
52
+ {
53
+ 'Air temperature [K]': air_temperature,
54
+ 'Process temperature [K]': process_temperature,
55
+ 'Rotational speed [rpm]': rotational_speed,
56
+ 'Torque [Nm]': torque,
57
+ 'Tool wear [min]': tool_wear,
58
+ 'Type': type,
59
+ 'prediction': prediction[0]
60
+ }
61
+ ))
62
+ f.write("\n")
63
+
64
+ return prediction[0]
65
+
66
+ demo = gr.Interface(
67
+ fn=predict_machine_failure,
68
+ inputs=[air_temperature_input, process_temperature_input, rotational_speed_input,
69
+ torque_input, tool_wear_input, type_input],
70
+ outputs=model_output,
71
+ title="Machine Failure Predictor",
72
+ description="This API allows you to predict the machine failure status of an equipment",
73
+ allow_flagging="auto",
74
+ concurrency_limit=8
75
+ )
76
+
77
+ demo.queue()
78
+ demo.launch(share=False)
model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:151394af3d0090b2de56fabb5ad43b6ec37d5e0cd6ecd42d83ec85d8b9dc79e6
3
+ size 3969
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ scikit-learn==1.6.0
2
+ numpy==1.26.4
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)