{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "AqkyIqoQhORh", "outputId": "8de02391-27d0-42aa-887b-578b2d6b79dc" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m313.4/313.4 kB\u001b[0m \u001b[31m4.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m4.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m129.9/129.9 kB\u001b[0m \u001b[31m5.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m5.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m5.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h" ] } ], "source": [ "!pip install -q gradio_client" ] }, { "cell_type": "code", "source": [ "import time\n", "\n", "from gradio_client import Client\n", "\n", "from sklearn.datasets import fetch_openml\n", "from sklearn.model_selection import train_test_split\n", "\n", "from tqdm import tqdm" ], "metadata": { "id": "_6hqa3y5hWQ2" }, "execution_count": 30, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Test Data" ], "metadata": { "id": "nHr-lJsMiAr3" } }, { "cell_type": "code", "source": [ "dataset = fetch_openml(data_id=42890, as_frame=True, parser=\"auto\")\n", "\n", "data_df = dataset.data\n", "\n", "target = 'Machine failure'\n", "numeric_features = [\n", " 'Air temperature [K]',\n", " 'Process temperature [K]',\n", " 'Rotational speed [rpm]',\n", " 'Torque [Nm]',\n", " 'Tool wear [min]'\n", "]\n", "categorical_features = ['Type']\n", "\n", "X = data_df[numeric_features + categorical_features]\n", "y = data_df[target]\n", "\n", "Xtrain, Xtest, ytrain, ytest = train_test_split(\n", " X, y,\n", " test_size=0.2,\n", " random_state=42\n", ")" ], "metadata": { "id": "UkgdHHg2hzRg" }, "execution_count": 3, "outputs": [] }, { "cell_type": "code", "source": [ "Xtest.shape" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8polBKfli8zm", "outputId": "b8933ac6-2117-4bab-a801-c05d9e1ea1e4" }, "execution_count": 17, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(2000, 6)" ] }, "metadata": {}, "execution_count": 17 } ] }, { "cell_type": "code", "source": [ "Xtest.head(3)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "2u29J1KRiZfz", "outputId": "0bf3a999-fb58-48fe-aaee-035ea5a54f74" }, "execution_count": 15, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " Air temperature [K] Process temperature [K] Rotational speed [rpm] \\\n", "6252 300.8 310.3 1538 \n", "4684 303.6 311.8 1421 \n", "1731 298.3 307.9 1485 \n", "\n", " Torque [Nm] Tool wear [min] Type \n", "6252 36.1 198 L \n", "4684 44.8 101 M \n", "1731 42.0 117 M " ], "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Air temperature [K]Process temperature [K]Rotational speed [rpm]Torque [Nm]Tool wear [min]Type
6252300.8310.3153836.1198L
4684303.6311.8142144.8101M
1731298.3307.9148542.0117M
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "Xtest", "summary": "{\n \"name\": \"Xtest\",\n \"rows\": 2000,\n \"fields\": [\n {\n \"column\": \"Air temperature [K]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.992750366832799,\n \"min\": 295.4,\n \"max\": 304.4,\n \"num_unique_values\": 90,\n \"samples\": [\n 303.4,\n 301.7,\n 302.1\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Process temperature [K]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 1.4730505885808056,\n \"min\": 305.9,\n \"max\": 313.7,\n \"num_unique_values\": 76,\n \"samples\": [\n 310.4,\n 309.0,\n 309.2\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Rotational speed [rpm]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 173,\n \"min\": 1168,\n \"max\": 2861,\n \"num_unique_values\": 597,\n \"samples\": [\n 1336,\n 2099,\n 1750\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Torque [Nm]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 9.929988726859552,\n \"min\": 4.6,\n \"max\": 76.6,\n \"num_unique_values\": 441,\n \"samples\": [\n 45.8,\n 20.9,\n 35.0\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Tool wear [min]\",\n \"properties\": {\n \"dtype\": \"number\",\n \"std\": 65,\n \"min\": 0,\n \"max\": 246,\n \"num_unique_values\": 236,\n \"samples\": [\n 176,\n 188,\n 105\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n },\n {\n \"column\": \"Type\",\n \"properties\": {\n \"dtype\": \"category\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"L\",\n \"M\",\n \"H\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" } }, "metadata": {}, "execution_count": 15 } ] }, { "cell_type": "code", "source": [ "Xtest.loc[6252].tolist()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8Lqv_mP3igAN", "outputId": "a159e22e-f082-445b-baa3-76293dde003b" }, "execution_count": 16, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[300.8, 310.3, 1538, 36.1, 198, 'L']" ] }, "metadata": {}, "execution_count": 16 } ] }, { "cell_type": "code", "source": [ "Xtest_sample = Xtest.sample(100)" ], "metadata": { "id": "J7GEWMhwjCdC" }, "execution_count": 18, "outputs": [] }, { "cell_type": "code", "source": [ "Xtest_sample_rows = list(Xtest_sample.itertuples(index=False, name=None))" ], "metadata": { "id": "UsC7wJJPjGhu" }, "execution_count": 20, "outputs": [] }, { "cell_type": "code", "source": [ "Xtest_sample_rows[0]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4pgM6Xgtj3zj", "outputId": "1192bdbe-c955-4dcb-c33c-b3c947952f3e" }, "execution_count": 35, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(300.5, 310.5, 1695, 26.7, 28, 'L')" ] }, "metadata": {}, "execution_count": 35 } ] }, { "cell_type": "markdown", "source": [ "# Inference" ], "metadata": { "id": "R76BjqoEiQZp" } }, { "cell_type": "code", "source": [ "client = Client(\"pgurazada1/machine-failure-predictor\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DmLUATIshX8x", "outputId": "2985eb39-0cbd-4095-8c3b-5924696a3c9a" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:88: UserWarning: \n", "The secret `HF_TOKEN` does not exist in your Colab secrets.\n", "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n", "You will be able to reuse this secret in all of your notebooks.\n", "Please note that authentication is recommended but still optional to access public models or datasets.\n", " warnings.warn(\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Loaded as API: https://pgurazada1-machine-failure-predictor.hf.space ✔\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Online/Real time" ], "metadata": { "id": "LlNyuobOk-Nz" } }, { "cell_type": "code", "source": [ "job = client.submit(\n", " air_temperature=300.5,\n", " process_temperature=310.5,\n", " rotational_speed=1695,\n", " torque=26.7,\n", " tool_wear=28,\n", " type=\"L\",\n", " api_name=\"/predict\"\n", ")" ], "metadata": { "id": "zwCtvdcqkIG2" }, "execution_count": 28, "outputs": [] }, { "cell_type": "code", "source": [ "print(job.result())" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "iXPyA3WYkvII", "outputId": "5cf471e0-c615-481a-865a-911b4659b0fe" }, "execution_count": 29, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{'label': '0', 'confidences': None}\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Batch" ], "metadata": { "id": "iDORkOkBlNTf" } }, { "cell_type": "code", "source": [ "batch_predictions = []" ], "metadata": { "id": "0xGAU0EelVnV" }, "execution_count": 36, "outputs": [] }, { "cell_type": "code", "source": [ "for row in tqdm(Xtest_sample_rows):\n", " try:\n", " job = client.submit(\n", " air_temperature=row[0],\n", " process_temperature=row[1],\n", " rotational_speed=row[2],\n", " torque=row[3],\n", " tool_wear=row[4],\n", " type=row[5],\n", " api_name=\"/predict\"\n", " )\n", "\n", " batch_predictions.append(job.result())\n", "\n", " time.sleep(1)\n", "\n", " except Exception as e:\n", " print(e)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0kYU2lspj_h2", "outputId": "022608f6-90d2-4210-b5a7-bdd14dd26649" }, "execution_count": 37, "outputs": [ { "output_type": "stream", "name": "stderr", "text": [ "100%|██████████| 100/100 [03:11<00:00, 1.92s/it]\n" ] } ] }, { "cell_type": "code", "source": [ "batch_predictions[0:5]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rZu_UaAznHS2", "outputId": "f90ad4b0-652f-416d-a833-042a0207fc1f" }, "execution_count": 39, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[{'label': '0', 'confidences': None},\n", " {'label': '0', 'confidences': None},\n", " {'label': '0', 'confidences': None},\n", " {'label': '0', 'confidences': None},\n", " {'label': '0', 'confidences': None}]" ] }, "metadata": {}, "execution_count": 39 } ] } ] }