File size: 1,739 Bytes
92045c0 de7dad7 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 |
"""
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) |