{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"mount_file_id":"101_T8xMCWHcohzgIs8NvdK3wYK7h7lwO","authorship_tag":"ABX9TyPqWv0VJNnuZXYPzsYolcuK"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","source":["!pip install gradio"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"oL1tjvMuzso2","executionInfo":{"status":"ok","timestamp":1745179514537,"user_tz":-180,"elapsed":11973,"user":{"displayName":"مالك المصنف","userId":"01645315346138749579"}},"outputId":"69f9431f-742c-42ce-e0de-a81909f4d23b"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting gradio\n"," Downloading gradio-5.25.2-py3-none-any.whl.metadata (16 kB)\n","Collecting aiofiles<25.0,>=22.0 (from gradio)\n"," Downloading aiofiles-24.1.0-py3-none-any.whl.metadata (10 kB)\n","Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (4.9.0)\n","Collecting fastapi<1.0,>=0.115.2 (from gradio)\n"," Downloading fastapi-0.115.12-py3-none-any.whl.metadata (27 kB)\n","Collecting ffmpy (from gradio)\n"," Downloading ffmpy-0.5.0-py3-none-any.whl.metadata (3.0 kB)\n","Collecting gradio-client==1.8.0 (from gradio)\n"," Downloading gradio_client-1.8.0-py3-none-any.whl.metadata (7.1 kB)\n","Collecting groovy~=0.1 (from gradio)\n"," Downloading groovy-0.1.2-py3-none-any.whl.metadata (6.1 kB)\n","Requirement already satisfied: httpx>=0.24.1 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.28.1)\n","Requirement already satisfied: huggingface-hub>=0.28.1 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.30.2)\n","Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (3.1.6)\n","Requirement already satisfied: markupsafe<4.0,>=2.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (3.0.2)\n","Requirement already satisfied: numpy<3.0,>=1.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (2.0.2)\n","Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (3.10.16)\n","Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from gradio) (24.2)\n","Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (2.2.2)\n","Requirement already satisfied: pillow<12.0,>=8.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (11.1.0)\n","Requirement already satisfied: pydantic<2.12,>=2.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (2.11.3)\n","Collecting pydub (from gradio)\n"," Downloading pydub-0.25.1-py2.py3-none-any.whl.metadata (1.4 kB)\n","Collecting python-multipart>=0.0.18 (from gradio)\n"," Downloading python_multipart-0.0.20-py3-none-any.whl.metadata (1.8 kB)\n","Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (6.0.2)\n","Collecting ruff>=0.9.3 (from gradio)\n"," Downloading ruff-0.11.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (25 kB)\n","Collecting safehttpx<0.2.0,>=0.1.6 (from gradio)\n"," Downloading safehttpx-0.1.6-py3-none-any.whl.metadata (4.2 kB)\n","Collecting semantic-version~=2.0 (from gradio)\n"," Downloading semantic_version-2.10.0-py2.py3-none-any.whl.metadata (9.7 kB)\n","Collecting starlette<1.0,>=0.40.0 (from gradio)\n"," Downloading starlette-0.46.2-py3-none-any.whl.metadata (6.2 kB)\n","Collecting tomlkit<0.14.0,>=0.12.0 (from gradio)\n"," Downloading tomlkit-0.13.2-py3-none-any.whl.metadata (2.7 kB)\n","Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.15.2)\n","Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (4.13.2)\n","Collecting uvicorn>=0.14.0 (from gradio)\n"," Downloading uvicorn-0.34.2-py3-none-any.whl.metadata (6.5 kB)\n","Requirement already satisfied: fsspec in /usr/local/lib/python3.11/dist-packages (from gradio-client==1.8.0->gradio) (2025.3.2)\n","Requirement already satisfied: websockets<16.0,>=10.0 in /usr/local/lib/python3.11/dist-packages (from gradio-client==1.8.0->gradio) (15.0.1)\n","Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/dist-packages (from anyio<5.0,>=3.0->gradio) (3.10)\n","Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.11/dist-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)\n","Requirement already satisfied: certifi in /usr/local/lib/python3.11/dist-packages (from httpx>=0.24.1->gradio) (2025.1.31)\n","Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx>=0.24.1->gradio) (1.0.8)\n","Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n","Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from huggingface-hub>=0.28.1->gradio) (3.18.0)\n","Requirement already satisfied: requests in /usr/local/lib/python3.11/dist-packages (from huggingface-hub>=0.28.1->gradio) (2.32.3)\n","Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.11/dist-packages (from huggingface-hub>=0.28.1->gradio) (4.67.1)\n","Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.11/dist-packages (from pandas<3.0,>=1.0->gradio) (2.8.2)\n","Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.11/dist-packages (from pandas<3.0,>=1.0->gradio) (2025.2)\n","Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.11/dist-packages (from pandas<3.0,>=1.0->gradio) (2025.2)\n","Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<2.12,>=2.0->gradio) (0.7.0)\n","Requirement already satisfied: pydantic-core==2.33.1 in /usr/local/lib/python3.11/dist-packages (from pydantic<2.12,>=2.0->gradio) (2.33.1)\n","Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<2.12,>=2.0->gradio) (0.4.0)\n","Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.11/dist-packages (from typer<1.0,>=0.12->gradio) (8.1.8)\n","Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.11/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n","Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.11/dist-packages (from typer<1.0,>=0.12->gradio) (13.9.4)\n","Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.11/dist-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio) (1.17.0)\n","Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.11/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n","Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.11/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.18.0)\n","Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests->huggingface-hub>=0.28.1->gradio) (3.4.1)\n","Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests->huggingface-hub>=0.28.1->gradio) (2.3.0)\n","Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.11/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n","Downloading gradio-5.25.2-py3-none-any.whl (46.9 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.9/46.9 MB\u001b[0m \u001b[31m23.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading gradio_client-1.8.0-py3-none-any.whl (322 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m322.2/322.2 kB\u001b[0m \u001b[31m23.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading aiofiles-24.1.0-py3-none-any.whl (15 kB)\n","Downloading fastapi-0.115.12-py3-none-any.whl (95 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m95.2/95.2 kB\u001b[0m \u001b[31m7.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading groovy-0.1.2-py3-none-any.whl (14 kB)\n","Downloading python_multipart-0.0.20-py3-none-any.whl (24 kB)\n","Downloading ruff-0.11.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.5 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m11.5/11.5 MB\u001b[0m \u001b[31m88.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading safehttpx-0.1.6-py3-none-any.whl (8.7 kB)\n","Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n","Downloading starlette-0.46.2-py3-none-any.whl (72 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m72.0/72.0 kB\u001b[0m \u001b[31m5.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading tomlkit-0.13.2-py3-none-any.whl (37 kB)\n","Downloading uvicorn-0.34.2-py3-none-any.whl (62 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.5/62.5 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hDownloading ffmpy-0.5.0-py3-none-any.whl (6.0 kB)\n","Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n","Installing collected packages: pydub, uvicorn, tomlkit, semantic-version, ruff, python-multipart, groovy, ffmpy, aiofiles, starlette, safehttpx, gradio-client, fastapi, gradio\n","Successfully installed aiofiles-24.1.0 fastapi-0.115.12 ffmpy-0.5.0 gradio-5.25.2 gradio-client-1.8.0 groovy-0.1.2 pydub-0.25.1 python-multipart-0.0.20 ruff-0.11.6 safehttpx-0.1.6 semantic-version-2.10.0 starlette-0.46.2 tomlkit-0.13.2 uvicorn-0.34.2\n"]}]},{"cell_type":"code","source":["\n","import gradio as gr\n","import pandas as pd\n","import pickle\n","import numpy as np\n","from tensorflow.keras.models import load_model\n","\n","# Load models and scalers\n","with open(\"/content/drive/MyDrive/Analaysis for Time Fauiler/Models/Feature_Scaler.pkl\", \"rb\") as f:\n"," scaler = pickle.load(f)\n","with open(\"/content/drive/MyDrive/Analaysis for Time Fauiler/Models/target_scaler.pkl\", \"rb\") as f:\n"," target_scaler = pickle.load(f)\n","with open(\"/content/drive/MyDrive/Analaysis for Time Fauiler/Models/LR_model.pkl\", \"rb\") as f:\n"," linear_model = pickle.load(f)\n","with open(\"/content/drive/MyDrive/Analaysis for Time Fauiler/Models/DT_model.pkl\", \"rb\") as f:\n"," dt_model = pickle.load(f)\n","with open(\"/content/drive/MyDrive/Analaysis for Time Fauiler/Models/RF_model.pkl\", \"rb\") as f:\n"," rf_model = pickle.load(f)\n","lstm_model = load_model(\"/content/drive/MyDrive/Analaysis for Time Fauiler/Models/best_model.h5\")\n","\n","# Create sequences for LSTM\n","def create_sequences(data, window_size=11):\n"," sequences = []\n"," for i in range(len(data) - window_size + 1):\n"," seq = data[i:i+window_size]\n"," sequences.append(seq)\n"," return np.array(sequences).astype('float32')\n","\n","# Data processing and alert function\n","def process_and_alert(file):\n"," try:\n"," df = pd.read_csv(file.name)\n"," df[\"timestamp\"] = pd.to_datetime(df[\"timestamp\"])\n"," df = df.sort_values(\"timestamp\").reset_index(drop=True)\n"," df[\"fault_flag\"] = df[\"status\"].apply(lambda x: 1 if x == \"fault\" else 0)\n"," fault_indices = df[df[\"fault_flag\"] == 1].index.tolist()\n"," time_to_failure = []\n"," for i in range(len(df)):\n"," next_faults = [j for j in fault_indices if j >= i]\n"," if next_faults:\n"," seconds = (df.loc[next_faults[0], \"timestamp\"] - df.loc[i, \"timestamp\"]).total_seconds()\n"," else:\n"," seconds = None\n"," time_to_failure.append(seconds)\n"," df[\"time_to_failure\"] = time_to_failure\n"," df.dropna(inplace=True)\n"," X = df.drop(columns=['time_to_failure', 'fault_flag', 'status', 'timestamp'])\n"," X_scaled = scaler.transform(X)\n","\n"," # LSTM processing\n"," window_size = 11\n"," if len(X_scaled) < window_size:\n"," raise ValueError(f\"Model requires at least {window_size} samples!\")\n"," X_seq = create_sequences(X_scaled, window_size)\n","\n"," # Predictions\n"," pred_linear = linear_model.predict(X_scaled)\n"," pred_dt = dt_model.predict(X_scaled)\n"," pred_rf = rf_model.predict(X_scaled)\n"," pred_lstm = lstm_model.predict(X_seq)\n","\n"," # Align lengths\n"," min_length = min(len(pred_linear), len(pred_dt), len(pred_rf), len(pred_lstm))\n"," pred_linear = pred_linear[:min_length]\n"," pred_dt = pred_dt[:min_length]\n"," pred_rf = pred_rf[:min_length]\n"," pred_lstm = pred_lstm[:min_length]\n","\n"," # Inverse transform\n"," pred_lstm = target_scaler.inverse_transform(pred_lstm.reshape(-1, 1))\n"," pred_linear = target_scaler.inverse_transform(pred_linear.reshape(-1, 1))\n"," pred_dt = target_scaler.inverse_transform(pred_dt.reshape(-1, 1))\n"," pred_rf = target_scaler.inverse_transform(pred_rf.reshape(-1, 1))\n","\n"," def format_value(val):\n"," return f'{val:.2f} (Fault)' if val < 0 else f'{val:.2f}'\n","\n"," html_rows = \"\"\n"," for i in range(min_length):\n"," html_rows += \"
| Linear Regression | \n","Decision Tree | \n","Random Forest | \n","LSTM | \n","
|---|
Model: {best_model}
\n","Estimated time to failure: {msg}
\n"," {\"⚠️ Imminent failure!
\" if time_left < 60 else \"\"}\n","{str(e)}
Estimated time to failure: {msg}
\n"," {\"⚠️ Imminent failure!
\" if pred_original < 60 else \"\"}\n","❌ {str(e)}
\"\n","\n"," btn = gr.Button(\"🔍 Predict Now\")\n"," btn.click(predict_and_alert_ready, inputs=[f1, f2, f3], outputs=[result_output, alert_output_ready])\n","\n","interface.launch(share=True)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":628},"id":"MKfTdIejfZIG","executionInfo":{"status":"ok","timestamp":1745180746931,"user_tz":-180,"elapsed":2519,"user":{"displayName":"مالك المصنف","userId":"01645315346138749579"}},"outputId":"d9d2d2e3-7ec3-4fba-e2e3-4d78d3fafafa"},"execution_count":7,"outputs":[{"output_type":"stream","name":"stderr","text":["WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.\n"]},{"output_type":"stream","name":"stdout","text":["Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n","* Running on public URL: https://0d8f523b48e5ed5d1f.gradio.live\n","\n","This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"]},{"output_type":"display_data","data":{"text/plain":["