CoolFace
Apppublic

eparham1981/AdvancedPython_Project2

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py78 linesDownload Raw Back to root
1import os2import uuid3import joblib4import json5 6import gradio as gr7import pandas as pd8 9from huggingface_hub import CommitScheduler10from pathlib import Path11 12log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"13log_folder = log_file.parent14 15scheduler = CommitScheduler(16    repo_id="machine-failure-logs",17    repo_type="dataset",18    folder_path=log_folder,19    path_in_repo="data",20    every=221)22 23machine_failure_predictor = joblib.load('model.joblib')24 25air_temperature_input = gr.Number(label='Air temperature [K]')26process_temperature_input = gr.Number(label='Process temperature [K]')27rotational_speed_input = gr.Number(label='Rotational speed [rpm]')28torque_input = gr.Number(label='Torque [Nm]')29tool_wear_input = gr.Number(label='Tool wear [min]')30type_input = gr.Dropdown(31    ['L', 'M', 'H'],32    label='Type'33)34 35model_output = gr.Label(label="Machine failure")36 37def 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': type45    }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 66demo = 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=875)76 77demo.queue()78demo.launch(share=True,show_error=True)