{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "5f81d089",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"import gradio as gr\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from pathlib import Path\n",
"from sklearn.preprocessing import MinMaxScaler, PowerTransformer\n",
"\n",
"pd.options.mode.chained_assignment = 'warn'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a4a28281",
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d4ec3046",
"metadata": {},
"outputs": [],
"source": [
"modelPath = Path('trainedModel/')\n",
"\n",
"model = tf.keras.models.load_model(modelPath)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ef122845",
"metadata": {},
"outputs": [],
"source": [
"col_names = [ 'setting1'\n",
" , 'T30', 'T50','P2', 'P15', 'P30', 'Nf', 'Nc', 'Ps30'\n",
" , 'phi', 'NRf', 'NRc', 'BPR','htBleed',\n",
" 'W31', 'W32']"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "950efda4",
"metadata": {},
"outputs": [],
"source": [
"testData1 = pd.read_csv('data/testData.csv')\n",
"testData1.drop(columns='Unnamed: 0', inplace=True)\n",
"testData1.columns = col_names"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9600ddda",
"metadata": {},
"outputs": [],
"source": [
"testDataNonNormal = pd.read_csv('data/unnormalizedTestData.csv')\n",
"testDataNonNormal.drop(columns='Unnamed: 0', inplace=True)\n",
"testDataNonNormal.columns = col_names"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3ca99247",
"metadata": {},
"outputs": [],
"source": [
"col_names = ['id', 'cycle', 'setting1', 'setting2', 'setting3', 'T2', 'T24'\n",
" , 'T30', 'T50','P2', 'P15', 'P30', 'Nf', 'Nc', 'epr', 'Ps30'\n",
" , 'phi', 'NRf', 'NRc', 'BPR','farB', 'htBleed', 'Nf_dmd',\n",
" 'PCNfR_dmd','W31', 'W32', 's22', 's23']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "715ff95e",
"metadata": {},
"outputs": [],
"source": [
"testData = pd.read_csv(\"data/test.txt\", sep=' ', names=col_names)\n",
"testDataNoDrop = pd.read_csv(\"data/test.txt\", sep=' ', names=col_names)\n",
"testData = testData.drop(['id', 'cycle', 'setting2', 'setting3', 'T2',\n",
" 'T24', 'epr', 'farB', 'Nf_dmd', 'PCNfR_dmd', 's22', 's23'], axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "403d5e69",
"metadata": {},
"outputs": [],
"source": [
"gen = MinMaxScaler(feature_range=(0,1))\n",
"pt = PowerTransformer()"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0e635ec3",
"metadata": {},
"outputs": [],
"source": [
"def predict(engineId, NRc, T30, P30):\n",
" # W31 is index 15, T30 is index 1, P30 is index 5\n",
" engineIdx = testDataNoDrop.index[testDataNoDrop['id'] == engineId].tolist()\n",
" engineIdx = engineIdx[int(len(engineIdx)/2)]\n",
" \n",
" testData.loc[engineIdx,'NRc'] = NRc\n",
" testData.loc[engineIdx,'T30'] = T30\n",
" testData.loc[engineIdx,'P30'] = P30\n",
" \n",
" testDf = gen.fit_transform(testData)\n",
" testDf = pd.DataFrame(testDf)\n",
" testDf = np.nan_to_num(testDf)\n",
" testDf = pt.fit_transform(testDf)\n",
" testDf = np.array(testDf)\n",
" # truncData = np.array(testData.iloc[engineIdx[0],:])\n",
" # truncData.W31 = W31\n",
" # truncData.T30 = T30\n",
" # truncData.P30 = P30\n",
" # truncData[1] = T30\n",
" # truncData[5] = P30\n",
" # truncData[15] = W31\n",
" # print(truncData)\n",
" \n",
" # truncData = gen.fit_transform(np.array(truncData).reshape(-1,1))\n",
" # print(truncData)\n",
" # truncData = pt.fit_transform(truncData)\n",
" # print(truncData)\n",
" data = testDf[engineIdx]\n",
" data = data.reshape(1, 16)\n",
" # print(data)\n",
" pred = int(model.predict(data))\n",
" \n",
" if pred > 30:\n",
" maintReq = 'No '\n",
" \n",
" return pred\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "94a919e7",
"metadata": {},
"outputs": [],
"source": [
"defaultNrc = int(max(testData['NRc']) - (max(testData['NRc'])-8075)/2)\n",
"defaultT = int(max(testData['T30']) - (max(testData['T30'])-1580)/2)\n",
"defaultP = int(max(testData['P30']) - (max(testData['P30'])-550)/2)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "67a89db6",
"metadata": {},
"outputs": [],
"source": [
"input = [gr.inputs.Slider(1, 100, step=1, label='Engine ID'),\n",
" gr.inputs.Slider(8075, max(testData['NRc']), default=defaultNrc, step=0.1, label='Corrected Engine Core Speed (rpm)'),\n",
" gr.inputs.Slider(1580, max(testData['T30']), default=defaultT, label='Total Temperature at HPC Outlet (\\N{DEGREE SIGN}R)'),\n",
" gr.inputs.Slider(550, max(testData['P30']), default=defaultP, label='Total Pressure at HPC Outlet (psi)')]\n",
"\n",
"output = [gr.outputs.Textbox(type='number', label=\"Remaining Engine Cycles\")]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "56acae73",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7860/\n",
"Running on public URL: https://46768.gradio.app\n",
"\n",
"This share link expires in 72 hours. For free permanent hosting, check out Spaces (https://huggingface.co/spaces)\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" "
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(,\n",
" 'http://127.0.0.1:7860/',\n",
" 'https://46768.gradio.app')"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iface = gr.Interface(fn=predict, inputs=input, outputs=output, live=True, theme=\"dark-peach\")\n",
"iface.launch(debug=False, share=True)"
]
}
],
"metadata": {
"interpreter": {
"hash": "9cf77d9e31fba3236aefb4748d140888e596bc65dcef8da4aa710fb6056a88b0"
},
"kernelspec": {
"display_name": "ML",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}