ArashMehdipour commited on
Commit
c56f55e
1 Parent(s): 688f433

Upload CapstoneDeploymentTest.ipynb

Browse files
Files changed (1) hide show
  1. CapstoneDeploymentTest.ipynb +272 -0
CapstoneDeploymentTest.ipynb ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 2,
6
+ "id": "5f81d089",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "import pandas as pd\n",
11
+ "import numpy as np\n",
12
+ "import tensorflow as tf\n",
13
+ "import gradio as gr\n",
14
+ "import matplotlib.pyplot as plt\n",
15
+ "\n",
16
+ "from pathlib import Path\n",
17
+ "from sklearn.preprocessing import MinMaxScaler, PowerTransformer\n",
18
+ "\n",
19
+ "pd.options.mode.chained_assignment = 'warn'"
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": 3,
25
+ "id": "a4a28281",
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "import warnings\n",
30
+ "warnings.filterwarnings(\"ignore\")"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 4,
36
+ "id": "d4ec3046",
37
+ "metadata": {},
38
+ "outputs": [],
39
+ "source": [
40
+ "modelPath = Path('trainedModel/')\n",
41
+ "\n",
42
+ "model = tf.keras.models.load_model(modelPath)"
43
+ ]
44
+ },
45
+ {
46
+ "cell_type": "code",
47
+ "execution_count": 5,
48
+ "id": "ef122845",
49
+ "metadata": {},
50
+ "outputs": [],
51
+ "source": [
52
+ "col_names = [ 'setting1'\n",
53
+ " , 'T30', 'T50','P2', 'P15', 'P30', 'Nf', 'Nc', 'Ps30'\n",
54
+ " , 'phi', 'NRf', 'NRc', 'BPR','htBleed',\n",
55
+ " 'W31', 'W32']"
56
+ ]
57
+ },
58
+ {
59
+ "cell_type": "code",
60
+ "execution_count": 6,
61
+ "id": "950efda4",
62
+ "metadata": {},
63
+ "outputs": [],
64
+ "source": [
65
+ "testData1 = pd.read_csv('data/testData.csv')\n",
66
+ "testData1.drop(columns='Unnamed: 0', inplace=True)\n",
67
+ "testData1.columns = col_names"
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "execution_count": 7,
73
+ "id": "9600ddda",
74
+ "metadata": {},
75
+ "outputs": [],
76
+ "source": [
77
+ "testDataNonNormal = pd.read_csv('data/unnormalizedTestData.csv')\n",
78
+ "testDataNonNormal.drop(columns='Unnamed: 0', inplace=True)\n",
79
+ "testDataNonNormal.columns = col_names"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "code",
84
+ "execution_count": 8,
85
+ "id": "3ca99247",
86
+ "metadata": {},
87
+ "outputs": [],
88
+ "source": [
89
+ "col_names = ['id', 'cycle', 'setting1', 'setting2', 'setting3', 'T2', 'T24'\n",
90
+ " , 'T30', 'T50','P2', 'P15', 'P30', 'Nf', 'Nc', 'epr', 'Ps30'\n",
91
+ " , 'phi', 'NRf', 'NRc', 'BPR','farB', 'htBleed', 'Nf_dmd',\n",
92
+ " 'PCNfR_dmd','W31', 'W32', 's22', 's23']"
93
+ ]
94
+ },
95
+ {
96
+ "cell_type": "code",
97
+ "execution_count": 9,
98
+ "id": "715ff95e",
99
+ "metadata": {},
100
+ "outputs": [],
101
+ "source": [
102
+ "testData = pd.read_csv(\"data/test.txt\", sep=' ', names=col_names)\n",
103
+ "testDataNoDrop = pd.read_csv(\"data/test.txt\", sep=' ', names=col_names)\n",
104
+ "testData = testData.drop(['id', 'cycle', 'setting2', 'setting3', 'T2',\n",
105
+ " 'T24', 'epr', 'farB', 'Nf_dmd', 'PCNfR_dmd', 's22', 's23'], axis=1)"
106
+ ]
107
+ },
108
+ {
109
+ "cell_type": "code",
110
+ "execution_count": 10,
111
+ "id": "403d5e69",
112
+ "metadata": {},
113
+ "outputs": [],
114
+ "source": [
115
+ "gen = MinMaxScaler(feature_range=(0,1))\n",
116
+ "pt = PowerTransformer()"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "code",
121
+ "execution_count": 11,
122
+ "id": "0e635ec3",
123
+ "metadata": {},
124
+ "outputs": [],
125
+ "source": [
126
+ "def predict(engineId, NRc, T30, P30):\n",
127
+ " # W31 is index 15, T30 is index 1, P30 is index 5\n",
128
+ " engineIdx = testDataNoDrop.index[testDataNoDrop['id'] == engineId].tolist()\n",
129
+ " engineIdx = engineIdx[int(len(engineIdx)/2)]\n",
130
+ " \n",
131
+ " testData.loc[engineIdx,'NRc'] = NRc\n",
132
+ " testData.loc[engineIdx,'T30'] = T30\n",
133
+ " testData.loc[engineIdx,'P30'] = P30\n",
134
+ " \n",
135
+ " testDf = gen.fit_transform(testData)\n",
136
+ " testDf = pd.DataFrame(testDf)\n",
137
+ " testDf = np.nan_to_num(testDf)\n",
138
+ " testDf = pt.fit_transform(testDf)\n",
139
+ " testDf = np.array(testDf)\n",
140
+ " # truncData = np.array(testData.iloc[engineIdx[0],:])\n",
141
+ " # truncData.W31 = W31\n",
142
+ " # truncData.T30 = T30\n",
143
+ " # truncData.P30 = P30\n",
144
+ " # truncData[1] = T30\n",
145
+ " # truncData[5] = P30\n",
146
+ " # truncData[15] = W31\n",
147
+ " # print(truncData)\n",
148
+ " \n",
149
+ " # truncData = gen.fit_transform(np.array(truncData).reshape(-1,1))\n",
150
+ " # print(truncData)\n",
151
+ " # truncData = pt.fit_transform(truncData)\n",
152
+ " # print(truncData)\n",
153
+ " data = testDf[engineIdx]\n",
154
+ " data = data.reshape(1, 16)\n",
155
+ " # print(data)\n",
156
+ " pred = int(model.predict(data))\n",
157
+ " \n",
158
+ " if pred > 30:\n",
159
+ " maintReq = 'No '\n",
160
+ " \n",
161
+ " return pred\n",
162
+ " "
163
+ ]
164
+ },
165
+ {
166
+ "cell_type": "code",
167
+ "execution_count": 12,
168
+ "id": "94a919e7",
169
+ "metadata": {},
170
+ "outputs": [],
171
+ "source": [
172
+ "defaultNrc = int(max(testData['NRc']) - (max(testData['NRc'])-8075)/2)\n",
173
+ "defaultT = int(max(testData['T30']) - (max(testData['T30'])-1580)/2)\n",
174
+ "defaultP = int(max(testData['P30']) - (max(testData['P30'])-550)/2)"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "code",
179
+ "execution_count": 13,
180
+ "id": "67a89db6",
181
+ "metadata": {},
182
+ "outputs": [],
183
+ "source": [
184
+ "input = [gr.inputs.Slider(1, 100, step=1, label='Engine ID'),\n",
185
+ " gr.inputs.Slider(8075, max(testData['NRc']), default=defaultNrc, step=0.1, label='Corrected Engine Core Speed (rpm)'),\n",
186
+ " gr.inputs.Slider(1580, max(testData['T30']), default=defaultT, label='Total Temperature at HPC Outlet (\\N{DEGREE SIGN}R)'),\n",
187
+ " gr.inputs.Slider(550, max(testData['P30']), default=defaultP, label='Total Pressure at HPC Outlet (psi)')]\n",
188
+ "\n",
189
+ "output = [gr.outputs.Textbox(type='number', label=\"Remaining Engine Cycles\")]"
190
+ ]
191
+ },
192
+ {
193
+ "cell_type": "code",
194
+ "execution_count": 14,
195
+ "id": "56acae73",
196
+ "metadata": {},
197
+ "outputs": [
198
+ {
199
+ "name": "stdout",
200
+ "output_type": "stream",
201
+ "text": [
202
+ "Running on local URL: http://127.0.0.1:7860/\n",
203
+ "Running on public URL: https://46768.gradio.app\n",
204
+ "\n",
205
+ "This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"
206
+ ]
207
+ },
208
+ {
209
+ "data": {
210
+ "text/html": [
211
+ "\n",
212
+ " <iframe\n",
213
+ " width=\"900\"\n",
214
+ " height=\"500\"\n",
215
+ " src=\"https://46768.gradio.app\"\n",
216
+ " frameborder=\"0\"\n",
217
+ " allowfullscreen\n",
218
+ " \n",
219
+ " ></iframe>\n",
220
+ " "
221
+ ],
222
+ "text/plain": [
223
+ "<IPython.lib.display.IFrame at 0x200411ef220>"
224
+ ]
225
+ },
226
+ "metadata": {},
227
+ "output_type": "display_data"
228
+ },
229
+ {
230
+ "data": {
231
+ "text/plain": [
232
+ "(<fastapi.applications.FastAPI at 0x20033b30fd0>,\n",
233
+ " 'http://127.0.0.1:7860/',\n",
234
+ " 'https://46768.gradio.app')"
235
+ ]
236
+ },
237
+ "execution_count": 14,
238
+ "metadata": {},
239
+ "output_type": "execute_result"
240
+ }
241
+ ],
242
+ "source": [
243
+ "iface = gr.Interface(fn=predict, inputs=input, outputs=output, live=True, theme=\"dark-peach\")\n",
244
+ "iface.launch(debug=False, share=True)"
245
+ ]
246
+ }
247
+ ],
248
+ "metadata": {
249
+ "interpreter": {
250
+ "hash": "9cf77d9e31fba3236aefb4748d140888e596bc65dcef8da4aa710fb6056a88b0"
251
+ },
252
+ "kernelspec": {
253
+ "display_name": "ML",
254
+ "language": "python",
255
+ "name": "python3"
256
+ },
257
+ "language_info": {
258
+ "codemirror_mode": {
259
+ "name": "ipython",
260
+ "version": 3
261
+ },
262
+ "file_extension": ".py",
263
+ "mimetype": "text/x-python",
264
+ "name": "python",
265
+ "nbconvert_exporter": "python",
266
+ "pygments_lexer": "ipython3",
267
+ "version": "3.9.10"
268
+ }
269
+ },
270
+ "nbformat": 4,
271
+ "nbformat_minor": 5
272
+ }