saba000farahani commited on
Commit
b011d91
Β·
verified Β·
1 Parent(s): 5beb40a

Delete App_Env&CL.ipynb

Browse files
Files changed (1) hide show
  1. App_Env&CL.ipynb +0 -309
App_Env&CL.ipynb DELETED
@@ -1,309 +0,0 @@
1
- {
2
- "nbformat": 4,
3
- "nbformat_minor": 0,
4
- "metadata": {
5
- "colab": {
6
- "provenance": []
7
- },
8
- "kernelspec": {
9
- "name": "python3",
10
- "display_name": "Python 3"
11
- },
12
- "language_info": {
13
- "name": "python"
14
- }
15
- },
16
- "cells": [
17
- {
18
- "cell_type": "code",
19
- "source": [
20
- "pip install gradio"
21
- ],
22
- "metadata": {
23
- "colab": {
24
- "base_uri": "https://localhost:8080/"
25
- },
26
- "id": "bk0XfjBP09aJ",
27
- "outputId": "4134f0d7-5fe6-4cac-c092-04d128a9fb17"
28
- },
29
- "execution_count": 4,
30
- "outputs": [
31
- {
32
- "output_type": "stream",
33
- "name": "stdout",
34
- "text": [
35
- "Collecting gradio\n",
36
- " Downloading gradio-4.38.1-py3-none-any.whl (12.4 MB)\n",
37
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.4/12.4 MB\u001b[0m \u001b[31m51.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
38
- "\u001b[?25hCollecting aiofiles<24.0,>=22.0 (from gradio)\n",
39
- " Downloading aiofiles-23.2.1-py3-none-any.whl (15 kB)\n",
40
- "Collecting altair<6.0,>=5.0 (from gradio)\n",
41
- " Downloading altair-5.3.0-py3-none-any.whl (857 kB)\n",
42
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m857.8/857.8 kB\u001b[0m \u001b[31m57.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
43
- "\u001b[?25hCollecting fastapi (from gradio)\n",
44
- " Downloading fastapi-0.111.1-py3-none-any.whl (92 kB)\n",
45
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m92.2/92.2 kB\u001b[0m \u001b[31m11.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
46
- "\u001b[?25hCollecting ffmpy (from gradio)\n",
47
- " Downloading ffmpy-0.3.2.tar.gz (5.5 kB)\n",
48
- " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
49
- "Collecting gradio-client==1.1.0 (from gradio)\n",
50
- " Downloading gradio_client-1.1.0-py3-none-any.whl (318 kB)\n",
51
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m318.1/318.1 kB\u001b[0m \u001b[31m33.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
52
- "\u001b[?25hCollecting httpx>=0.24.1 (from gradio)\n",
53
- " Downloading httpx-0.27.0-py3-none-any.whl (75 kB)\n",
54
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m8.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
55
- "\u001b[?25hRequirement already satisfied: huggingface-hub>=0.19.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.23.5)\n",
56
- "Requirement already satisfied: importlib-resources<7.0,>=1.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.4.0)\n",
57
- "Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.4)\n",
58
- "Requirement already satisfied: markupsafe~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.5)\n",
59
- "Requirement already satisfied: matplotlib~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n",
60
- "Requirement already satisfied: numpy<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.25.2)\n",
61
- "Collecting orjson~=3.0 (from gradio)\n",
62
- " Downloading orjson-3.10.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (141 kB)\n",
63
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m141.1/141.1 kB\u001b[0m \u001b[31m15.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
64
- "\u001b[?25hRequirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio) (24.1)\n",
65
- "Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.0.3)\n",
66
- "Requirement already satisfied: pillow<11.0,>=8.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (9.4.0)\n",
67
- "Requirement already satisfied: pydantic>=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.8.2)\n",
68
- "Collecting pydub (from gradio)\n",
69
- " Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n",
70
- "Collecting python-multipart>=0.0.9 (from gradio)\n",
71
- " Downloading python_multipart-0.0.9-py3-none-any.whl (22 kB)\n",
72
- "Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.1)\n",
73
- "Collecting ruff>=0.2.2 (from gradio)\n",
74
- " Downloading ruff-0.5.3-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (10.1 MB)\n",
75
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.1/10.1 MB\u001b[0m \u001b[31m55.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
76
- "\u001b[?25hCollecting semantic-version~=2.0 (from gradio)\n",
77
- " Downloading semantic_version-2.10.0-py2.py3-none-any.whl (15 kB)\n",
78
- "Collecting tomlkit==0.12.0 (from gradio)\n",
79
- " Downloading tomlkit-0.12.0-py3-none-any.whl (37 kB)\n",
80
- "Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.12.3)\n",
81
- "Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.12.2)\n",
82
- "Requirement already satisfied: urllib3~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.0.7)\n",
83
- "Collecting uvicorn>=0.14.0 (from gradio)\n",
84
- " Downloading uvicorn-0.30.1-py3-none-any.whl (62 kB)\n",
85
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.4/62.4 kB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
86
- "\u001b[?25hRequirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.1.0->gradio) (2023.6.0)\n",
87
- "Collecting websockets<12.0,>=10.0 (from gradio-client==1.1.0->gradio)\n",
88
- " Downloading websockets-11.0.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (129 kB)\n",
89
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m129.9/129.9 kB\u001b[0m \u001b[31m15.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
90
- "\u001b[?25hRequirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=5.0->gradio) (4.19.2)\n",
91
- "Requirement already satisfied: toolz in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=5.0->gradio) (0.12.1)\n",
92
- "Requirement already satisfied: anyio in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (3.7.1)\n",
93
- "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (2024.7.4)\n",
94
- "Collecting httpcore==1.* (from httpx>=0.24.1->gradio)\n",
95
- " Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)\n",
96
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m9.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
97
- "\u001b[?25hRequirement already satisfied: idna in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (3.7)\n",
98
- "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (1.3.1)\n",
99
- "Collecting h11<0.15,>=0.13 (from httpcore==1.*->httpx>=0.24.1->gradio)\n",
100
- " Downloading h11-0.14.0-py3-none-any.whl (58 kB)\n",
101
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
102
- "\u001b[?25hRequirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (3.15.4)\n",
103
- "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (2.31.0)\n",
104
- "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (4.66.4)\n",
105
- "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.2.1)\n",
106
- "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (0.12.1)\n",
107
- "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (4.53.1)\n",
108
- "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.4.5)\n",
109
- "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (3.1.2)\n",
110
- "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (2.8.2)\n",
111
- "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2023.4)\n",
112
- "Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n",
113
- "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (0.7.0)\n",
114
- "Requirement already satisfied: pydantic-core==2.20.1 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (2.20.1)\n",
115
- "Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n",
116
- "Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
117
- "Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (13.7.1)\n",
118
- "Collecting starlette<0.38.0,>=0.37.2 (from fastapi->gradio)\n",
119
- " Downloading starlette-0.37.2-py3-none-any.whl (71 kB)\n",
120
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.9/71.9 kB\u001b[0m \u001b[31m9.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
121
- "\u001b[?25hCollecting fastapi-cli>=0.0.2 (from fastapi->gradio)\n",
122
- " Downloading fastapi_cli-0.0.4-py3-none-any.whl (9.5 kB)\n",
123
- "Collecting email_validator>=2.0.0 (from fastapi->gradio)\n",
124
- " Downloading email_validator-2.2.0-py3-none-any.whl (33 kB)\n",
125
- "Collecting dnspython>=2.0.0 (from email_validator>=2.0.0->fastapi->gradio)\n",
126
- " Downloading dnspython-2.6.1-py3-none-any.whl (307 kB)\n",
127
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m307.7/307.7 kB\u001b[0m \u001b[31m32.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
128
- "\u001b[?25hRequirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=5.0->gradio) (23.2.0)\n",
129
- "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=5.0->gradio) (2023.12.1)\n",
130
- "Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=5.0->gradio) (0.35.1)\n",
131
- "Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=5.0->gradio) (0.19.0)\n",
132
- "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n",
133
- "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n",
134
- "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.16.1)\n",
135
- "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio->httpx>=0.24.1->gradio) (1.2.2)\n",
136
- "Collecting httptools>=0.5.0 (from uvicorn>=0.14.0->gradio)\n",
137
- " Downloading httptools-0.6.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (341 kB)\n",
138
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m341.4/341.4 kB\u001b[0m \u001b[31m33.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
139
- "\u001b[?25hCollecting python-dotenv>=0.13 (from uvicorn>=0.14.0->gradio)\n",
140
- " Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)\n",
141
- "Collecting uvloop!=0.15.0,!=0.15.1,>=0.14.0 (from uvicorn>=0.14.0->gradio)\n",
142
- " Downloading uvloop-0.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB)\n",
143
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m90.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
144
- "\u001b[?25hCollecting watchfiles>=0.13 (from uvicorn>=0.14.0->gradio)\n",
145
- " Downloading watchfiles-0.22.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)\n",
146
- "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m67.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
147
- "\u001b[?25hRequirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.3->gradio) (3.3.2)\n",
148
- "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n",
149
- "Building wheels for collected packages: ffmpy\n",
150
- " Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
151
- " Created wheel for ffmpy: filename=ffmpy-0.3.2-py3-none-any.whl size=5584 sha256=f3f29dcbec7250e84a6a6df6492b6bd75d1cd20c812b8be3a090af573d31e368\n",
152
- " Stored in directory: /root/.cache/pip/wheels/bd/65/9a/671fc6dcde07d4418df0c592f8df512b26d7a0029c2a23dd81\n",
153
- "Successfully built ffmpy\n",
154
- "Installing collected packages: pydub, ffmpy, websockets, uvloop, tomlkit, semantic-version, ruff, python-multipart, python-dotenv, orjson, httptools, h11, dnspython, aiofiles, watchfiles, uvicorn, starlette, httpcore, email_validator, httpx, gradio-client, fastapi-cli, altair, fastapi, gradio\n",
155
- " Attempting uninstall: altair\n",
156
- " Found existing installation: altair 4.2.2\n",
157
- " Uninstalling altair-4.2.2:\n",
158
- " Successfully uninstalled altair-4.2.2\n",
159
- "Successfully installed aiofiles-23.2.1 altair-5.3.0 dnspython-2.6.1 email_validator-2.2.0 fastapi-0.111.1 fastapi-cli-0.0.4 ffmpy-0.3.2 gradio-4.38.1 gradio-client-1.1.0 h11-0.14.0 httpcore-1.0.5 httptools-0.6.1 httpx-0.27.0 orjson-3.10.6 pydub-0.25.1 python-dotenv-1.0.1 python-multipart-0.0.9 ruff-0.5.3 semantic-version-2.10.0 starlette-0.37.2 tomlkit-0.12.0 uvicorn-0.30.1 uvloop-0.19.0 watchfiles-0.22.0 websockets-11.0.3\n"
160
- ]
161
- }
162
- ]
163
- },
164
- {
165
- "cell_type": "code",
166
- "execution_count": null,
167
- "metadata": {
168
- "colab": {
169
- "base_uri": "https://localhost:8080/"
170
- },
171
- "id": "P3FcIUqkzVTW",
172
- "outputId": "58ce4065-4295-4648-f111-42b02261c6b4"
173
- },
174
- "outputs": [
175
- {
176
- "output_type": "stream",
177
- "name": "stdout",
178
- "text": [
179
- "Downloading scaler_X.pkl...\n",
180
- "Downloaded scaler_X.pkl.\n",
181
- "Downloading rf_model.pkl...\n",
182
- "Downloaded rf_model.pkl.\n",
183
- "Downloading mlp_model.h5...\n",
184
- "Downloaded mlp_model.h5.\n",
185
- "Downloading meta_model.pkl...\n",
186
- "Downloaded meta_model.pkl.\n",
187
- "Error loading models or scaler: 60\n"
188
- ]
189
- }
190
- ],
191
- "source": [
192
- "\n",
193
- "import os\n",
194
- "import requests\n",
195
- "import gradio as gr\n",
196
- "import numpy as np\n",
197
- "import joblib\n",
198
- "from tensorflow.keras.models import load_model\n",
199
- "\n",
200
- "# URLs to your saved models and scaler\n",
201
- "scaler_url = 'https://huggingface.co/spaces/saba000farahani/Env_ContaminationLevel/blob/main/scaler_X.pkl'\n",
202
- "rf_model_url = 'https://huggingface.co/spaces/saba000farahani/Env_ContaminationLevel/blob/main/rf_model.pkl'\n",
203
- "mlp_model_url = 'https://huggingface.co/spaces/saba000farahani/Env_ContaminationLevel/blob/main/mlp_model.h5'\n",
204
- "meta_model_url = 'https://huggingface.co/spaces/saba000farahani/Env_ContaminationLevel/blob/main/meta_model.pkl'\n",
205
- "\n",
206
- "# Paths to save the downloaded models\n",
207
- "scaler_path = 'scaler_X.pkl'\n",
208
- "rf_model_path = 'rf_model.pkl'\n",
209
- "mlp_model_path = 'mlp_model.h5'\n",
210
- "meta_model_path = 'meta_model.pkl'\n",
211
- "\n",
212
- "def download_model(url, path):\n",
213
- " if not os.path.exists(path):\n",
214
- " print(f\"Downloading {path}...\")\n",
215
- " response = requests.get(url)\n",
216
- " with open(path, 'wb') as f:\n",
217
- " f.write(response.content)\n",
218
- " print(f\"Downloaded {path}.\")\n",
219
- " else:\n",
220
- " print(f\"{path} already exists.\")\n",
221
- "\n",
222
- "# Download the models and scaler if they don't exist locally\n",
223
- "download_model(scaler_url, scaler_path)\n",
224
- "download_model(rf_model_url, rf_model_path)\n",
225
- "download_model(mlp_model_url, mlp_model_path)\n",
226
- "download_model(meta_model_url, meta_model_path)\n",
227
- "\n",
228
- "try:\n",
229
- " # Load the scaler and models\n",
230
- " scaler_X = joblib.load(scaler_path)\n",
231
- " loaded_rf_model = joblib.load(rf_model_path)\n",
232
- " loaded_mlp_model = load_model(mlp_model_path)\n",
233
- " loaded_meta_model = joblib.load(meta_model_path)\n",
234
- " print(\"Models and scaler loaded successfully.\")\n",
235
- "except Exception as e:\n",
236
- " print(f\"Error loading models or scaler: {e}\")\n",
237
- "\n",
238
- "def predict_new_values(new_input_data):\n",
239
- " try:\n",
240
- " # Ensure the new input data is in the correct format\n",
241
- " print(f\"Raw Input Data: {new_input_data}\")\n",
242
- " new_input_data = np.array(new_input_data).reshape(1, -1)\n",
243
- "\n",
244
- " # Scale the new input data\n",
245
- " new_input_scaled = scaler_X.transform(new_input_data)\n",
246
- " print(f\"Scaled Input Data: {new_input_scaled}\")\n",
247
- "\n",
248
- " # Make predictions with both base models\n",
249
- " mlp_predictions_new = loaded_mlp_model.predict(new_input_scaled)\n",
250
- " rf_predictions_new = loaded_rf_model.predict(new_input_scaled)\n",
251
- "\n",
252
- " # Combine the predictions\n",
253
- " combined_features_new = np.concatenate([mlp_predictions_new, rf_predictions_new], axis=1)\n",
254
- " print(f\"Combined Features: {combined_features_new}\")\n",
255
- "\n",
256
- " # Use the loaded meta model to make predictions on the new data\n",
257
- " loaded_meta_predictions_new = loaded_meta_model.predict(combined_features_new)\n",
258
- " print(f\"Meta Model Predictions: {loaded_meta_predictions_new}\")\n",
259
- "\n",
260
- " return loaded_meta_predictions_new[0]\n",
261
- " except Exception as e:\n",
262
- " print(f\"Error in prediction: {e}\")\n",
263
- " return [\"Error\", \"Error\", \"Error\", \"Error\", \"Error\", \"Error\"]\n",
264
- "\n",
265
- "def gradio_interface(velocity, temperature, precipitation, humidity):\n",
266
- " try:\n",
267
- " input_data = [velocity, temperature, precipitation, humidity]\n",
268
- " print(f\"Input Data: {input_data}\")\n",
269
- " predictions = predict_new_values(input_data)\n",
270
- " print(f\"Predictions: {predictions}\")\n",
271
- " return [\n",
272
- " f\"{predictions[0] * 100:.2f}%\" if predictions[0] != \"Error\" else \"Error\",\n",
273
- " f\"{predictions[1] * 100:.2f}%\" if predictions[1] != \"Error\" else \"Error\",\n",
274
- " f\"{predictions[2] * 100:.2f}%\" if predictions[2] != \"Error\" else \"Error\",\n",
275
- " f\"{predictions[3] * 100:.2f}%\" if predictions[3] != \"Error\" else \"Error\",\n",
276
- " f\"{predictions[4] * 100:.2f}%\" if predictions[4] != \"Error\" else \"Error\",\n",
277
- " f\"{predictions[5] * 100:.2f}%\" if predictions[5] != \"Error\" else \"Error\"\n",
278
- " ]\n",
279
- " except Exception as e:\n",
280
- " print(f\"Error in Gradio interface: {e}\")\n",
281
- " return [\"Error\", \"Error\", \"Error\", \"Error\", \"Error\", \"Error\"]\n",
282
- "\n",
283
- "inputs = [\n",
284
- " gr.Slider(minimum=0, maximum=100, value=50,step=0.5, label=\"Velocity (mph)\"),\n",
285
- " gr.Slider(minimum=-30, maximum=50, value=0,step=0.5, label=\"Temperature (Β°C)\"),\n",
286
- " gr.Slider(minimum=0, maximum=10, value=0,step=0.01, label=\"Precipitation (inch)\"),\n",
287
- " gr.Slider(minimum=0, maximum=100, value=50, label=\"Humidity (%)\")\n",
288
- "]\n",
289
- "\n",
290
- "outputs = [\n",
291
- " gr.Textbox(label=\"Front Left\"),\n",
292
- " gr.Textbox(label=\"Front Right\"),\n",
293
- " gr.Textbox(label=\"Left\"),\n",
294
- " gr.Textbox(label=\"Right\"),\n",
295
- " gr.Textbox(label=\"Roof\"),\n",
296
- " gr.Textbox(label=\"Rear\")\n",
297
- "]\n",
298
- "\n",
299
- "gr.Interface(\n",
300
- " fn=gradio_interface,\n",
301
- " inputs=inputs,\n",
302
- " outputs=outputs,\n",
303
- " title=\"Environmental Factor-Based Contamination Level Prediction\",\n",
304
- " description=\"Enter the environmental factors to get the contamination levels for Front Left, Front Right, Left, Right, Roof, and Rear LiDARs.\"\n",
305
- ").launch()\n"
306
- ]
307
- }
308
- ]
309
- }