testModel / inference.py
akdiwahar's picture
Update inference.py
93d8fce verified
"""
Obtain Predictions for Machine Failure Predictor Model using Gradio Client
======================================================================
This script connects to a deployed machine failure predictor model using Gradio Client,
fetches the dataset, preprocesses the data, and generates predictions for a
sample of test data using the deployed model. The resulting predictions are
stored in a list. A time delay of one second is added after each prediction
submission to avoid overloading the model server.
"""
import time
from gradio_client import Client
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
client = Client("akdiwahar/testModel")
dataset = fetch_openml(data_id=42890, as_frame=True, parser="auto")
data_df = dataset.data
target = 'Machine failure'
numeric_features = [
'Air temperature [K]',
'Process temperature [K]',
'Rotational speed [rpm]',
'Torque [Nm]',
'Tool wear [min]'
]
categorical_features = ['Type']
X = data_df[numeric_features + categorical_features]
y = data_df[target]
Xtrain, Xtest, ytrain, ytest = train_test_split(
X, y,
test_size=0.2,
random_state=42
)
Xtest_sample = Xtest.sample(100)
Xtest_sample_rows = list(Xtest_sample.itertuples(index=False, name=None))
batch_predictions = []
for row in Xtest_sample_rows:
try:
job = client.submit(
air_temperature=row[0],
process_temperature=row[1],
rotational_speed=row[2],
torque=row[3],
tool_wear=row[4],
type=row[5],
api_name="/predict"
)
batch_predictions.append(job.result())
time.sleep(1)
except Exception as e:
print(e)