Spaces:
Runtime error
Runtime error
pgurazada1
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Run the training script placed in the same directory as app.py
|
13 |
+
# The training script will train and persist a logistic regression
|
14 |
+
# model with the filename 'model.joblib'
|
15 |
+
|
16 |
+
os.system("python train.py")
|
17 |
+
|
18 |
+
# Load the freshly trained model from disk
|
19 |
+
|
20 |
+
machine_failure_predictor = joblib.load('model.joblib')
|
21 |
+
|
22 |
+
# Prepare the logging functionality
|
23 |
+
|
24 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
25 |
+
log_folder = log_file.parent
|
26 |
+
|
27 |
+
scheduler = CommitScheduler(
|
28 |
+
repo_id="machine-failure-mlops-demo-logs",
|
29 |
+
repo_type="dataset",
|
30 |
+
folder_path=log_folder,
|
31 |
+
path_in_repo="data",
|
32 |
+
every=2
|
33 |
+
)
|
34 |
+
|
35 |
+
# Define the predict function that runs when 'Submit' is clicked or when a API request is made
|
36 |
+
def predict_machine_failure(air_temperature, process_temperature, rotational_speed, torque, tool_wear, type):
|
37 |
+
sample = {
|
38 |
+
'Air temperature [K]': air_temperature,
|
39 |
+
'Process temperature [K]': process_temperature,
|
40 |
+
'Rotational speed [rpm]': rotational_speed,
|
41 |
+
'Torque [Nm]': torque,
|
42 |
+
'Tool wear [min]': tool_wear,
|
43 |
+
'Type': type
|
44 |
+
}
|
45 |
+
|
46 |
+
data_point = pd.DataFrame([sample])
|
47 |
+
prediction = machine_failure_predictor.predict(data_point).tolist()
|
48 |
+
|
49 |
+
# While the prediction is made, log both the inputs and outputs to a local log file
|
50 |
+
# While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
|
51 |
+
# access
|
52 |
+
|
53 |
+
with scheduler.lock:
|
54 |
+
with log_file.open("a") as f:
|
55 |
+
f.write(json.dumps(
|
56 |
+
{
|
57 |
+
'Air temperature [K]': air_temperature,
|
58 |
+
'Process temperature [K]': process_temperature,
|
59 |
+
'Rotational speed [rpm]': rotational_speed,
|
60 |
+
'Torque [Nm]': torque,
|
61 |
+
'Tool wear [min]': tool_wear,
|
62 |
+
'Type': type,
|
63 |
+
'prediction': prediction[0]
|
64 |
+
}
|
65 |
+
))
|
66 |
+
f.write("\n")
|
67 |
+
|
68 |
+
return prediction[0]
|
69 |
+
|
70 |
+
# Set up UI components for input and output
|
71 |
+
|
72 |
+
air_temperature_input = gr.Number(label='Air temperature [K]')
|
73 |
+
process_temperature_input = gr.Number(label='Process temperature [K]')
|
74 |
+
rotational_speed_input = gr.Number(label='Rotational speed [rpm]')
|
75 |
+
torque_input = gr.Number(label='Torque [Nm]')
|
76 |
+
tool_wear_input = gr.Number(label='Tool wear [min]')
|
77 |
+
type_input = gr.Dropdown(
|
78 |
+
['L', 'M', 'H'],
|
79 |
+
label='Type'
|
80 |
+
)
|
81 |
+
|
82 |
+
model_output = gr.Label(label="Machine failure")
|
83 |
+
|
84 |
+
# Create the interface
|
85 |
+
demo = gr.Interface(
|
86 |
+
fn=predict_machine_failure,
|
87 |
+
inputs=[air_temperature_input, process_temperature_input, rotational_speed_input,
|
88 |
+
torque_input, tool_wear_input, type_input],
|
89 |
+
outputs=model_output,
|
90 |
+
title="Machine Failure Predictor",
|
91 |
+
description="This API allows you to predict the machine failure status of an equipment",
|
92 |
+
examples=[[300.8, 310.3, 1538, 36.1, 198, 'L'],
|
93 |
+
[296.3, 307.3, 1368, 49.5, 10, 'M'],
|
94 |
+
[298.6, 309.1, 1339, 51.1, 34, 'M'],
|
95 |
+
[302.4, 311.1, 1634, 34.2, 184, 'L'],
|
96 |
+
[297.9, 307.7, 1546, 37.6, 72, 'L']],
|
97 |
+
concurrency_limit=16
|
98 |
+
)
|
99 |
+
|
100 |
+
# Launch with a load balancer
|
101 |
+
demo.queue()
|
102 |
+
demo.launch(share=False)
|