File size: 2,734 Bytes
92045c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68c7727
92045c0
 
 
 
 
 
7f0bcc7
92045c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import uuid
import joblib
import json

import gradio as gr
import pandas as pd

from huggingface_hub import CommitScheduler
from pathlib import Path

# Run the training script in the same directory

os.system("python train.py")

# Load the freshly trained model

machine_failure_predictor = joblib.load('model.joblib')

# Prepare the logging functionality

log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
log_folder = log_file.parent

scheduler = CommitScheduler(
    repo_id="machine-failure-logs",
    repo_type="dataset",
    folder_path=log_folder,
    path_in_repo="data",
    every=2
)


# Set up UI components for input and output

air_temperature_input = gr.Number(label='Air temperature [K]')
process_temperature_input = gr.Number(label='Process temperature [K]')
rotational_speed_input = gr.Number(label='Rotational speed [rpm]')
torque_input = gr.Number(label='Torque [Nm]')
tool_wear_input = gr.Number(label='Tool wear [min]')
type_input = gr.Dropdown(
    ['L', 'M', 'H'],
    label='Type'
)

model_output = gr.Label(label="Machine failure")

# Define the predict function that runs when 'Submit' is clicked or when a API request is made
def predict_machine_failure(air_temperature, process_temperature, rotational_speed, torque, tool_wear, type):
    sample = {
        'Air temperature [K]': air_temperature,
        'Process temperature [K]': process_temperature,
        'Rotational speed [rpm]': rotational_speed,
        'Torque [Nm]': torque,
        'Tool wear [min]': tool_wear,
        'Type': type
    }
    data_point = pd.DataFrame([sample])
    prediction = machine_failure_predictor.predict(data_point).tolist()

    with scheduler.lock:
        with log_file.open("a") as f:
            f.write(json.dumps(
                {
                    'Air temperature [K]': air_temperature,
                    'Process temperature [K]': process_temperature,
                    'Rotational speed [rpm]': rotational_speed,
                    'Torque [Nm]': torque,
                    'Tool wear [min]': tool_wear,
                    'Type': type,
                    'prediction': prediction[0]
                }
            ))
            f.write("\n")
            
    return prediction[0]

# Create the interface
demo = gr.Interface(
    fn=predict_machine_failure,
    inputs=[air_temperature_input, process_temperature_input, rotational_speed_input, 
            torque_input, tool_wear_input, type_input],
    outputs=model_output,
    title="Machine Failure Predictor",
    description="This API allows you to predict the machine failure status of an equipment",
    allow_flagging="auto",
    concurrency_limit=8
)

# Launch with a load balancer
demo.queue()
demo.launch(share=False)