Spaces:
Sleeping
Sleeping
nn
commited on
Upload 4 files
Browse files- app.py +114 -0
- requirements.txt +10 -0
- scaler_f.pkl +3 -0
- transformer_pile.h5 +3 -0
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from keras.models import load_model
|
| 4 |
+
from sklearn.preprocessing import StandardScaler
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import pickle
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import io
|
| 9 |
+
import requests
|
| 10 |
+
|
| 11 |
+
# Load the model and scaler
|
| 12 |
+
transformer = load_model('transformer_pile.h5')
|
| 13 |
+
sc_f = pickle.load(open('scaler_f.pkl', 'rb'))
|
| 14 |
+
|
| 15 |
+
# URL to the default Excel file (replace with your actual URL)
|
| 16 |
+
DEFAULT_EXCEL_URL = "data_pile.xlsx"
|
| 17 |
+
|
| 18 |
+
def download_default_excel():
|
| 19 |
+
response = requests.get(DEFAULT_EXCEL_URL)
|
| 20 |
+
return io.BytesIO(response.content)
|
| 21 |
+
|
| 22 |
+
def process_excel(file):
|
| 23 |
+
if isinstance(file, str) and file == "default":
|
| 24 |
+
file = download_default_excel()
|
| 25 |
+
|
| 26 |
+
df = pd.read_excel(file, sheet_name='soil')
|
| 27 |
+
df_y = pd.read_excel(file, sheet_name='pile')
|
| 28 |
+
df_p = pd.read_excel(file, sheet_name='pile_length')
|
| 29 |
+
|
| 30 |
+
data = np.array(df)
|
| 31 |
+
data_y = np.array(df_y)
|
| 32 |
+
data_pile = np.array(df_p)[:, 1:61]
|
| 33 |
+
x_feature = data_y[:, 0:4]
|
| 34 |
+
bh = data[:, 1:61]
|
| 35 |
+
bh2 = data[:, 61:122] / 2
|
| 36 |
+
|
| 37 |
+
x_train = bh / 50
|
| 38 |
+
x_feature = sc_f.transform(x_feature)
|
| 39 |
+
soil_data = np.stack([x_train, bh2, data_pile], axis=2)
|
| 40 |
+
return soil_data
|
| 41 |
+
|
| 42 |
+
def predict_pile(file_choice, uploaded_file, pile_length, section_width, section_length, pile_type):
|
| 43 |
+
if file_choice == "default":
|
| 44 |
+
file = "default"
|
| 45 |
+
else:
|
| 46 |
+
if uploaded_file is None:
|
| 47 |
+
return "Please upload an Excel file or choose the default option."
|
| 48 |
+
file = uploaded_file
|
| 49 |
+
|
| 50 |
+
X = process_excel(file)
|
| 51 |
+
|
| 52 |
+
# Convert pile type to numerical value
|
| 53 |
+
pile_type_num = 1 if pile_type == "Circular" else 2
|
| 54 |
+
|
| 55 |
+
# Create feature array
|
| 56 |
+
feature = np.array([pile_length, section_width, section_length, pile_type_num])
|
| 57 |
+
feature = np.reshape(feature, (1, -1))
|
| 58 |
+
fd = sc_f.transform(feature)
|
| 59 |
+
x_feature = fd
|
| 60 |
+
|
| 61 |
+
# Use the first sample for demonstration
|
| 62 |
+
Xd = X[0:1]
|
| 63 |
+
|
| 64 |
+
X_train_CNN = np.zeros((Xd.shape[0], Xd.shape[1], x_feature.shape[1] + 3))
|
| 65 |
+
X_train_CNN[:, :, 0:3] = Xd
|
| 66 |
+
for i in range(Xd.shape[0]):
|
| 67 |
+
X_train_CNN[i, :, 3] = x_feature[i, 1]
|
| 68 |
+
X_train_CNN[i, :, 4] = x_feature[i, 2]
|
| 69 |
+
X_train_CNN[i, :, 5] = x_feature[i, 3]
|
| 70 |
+
X_train_CNN[i, :, 6] = x_feature[i, 3]
|
| 71 |
+
|
| 72 |
+
XT = X_train_CNN
|
| 73 |
+
print(XT.shape)
|
| 74 |
+
y_ini = np.zeros((1, 40))
|
| 75 |
+
y_ini[0, 0] = 0
|
| 76 |
+
|
| 77 |
+
for step in range(39):
|
| 78 |
+
y = transformer.predict([XT, y_ini, fd])
|
| 79 |
+
y_ini[0, step+1] = y
|
| 80 |
+
|
| 81 |
+
y_pred = y_ini * 40000
|
| 82 |
+
|
| 83 |
+
plt.figure(figsize=(10, 6))
|
| 84 |
+
ydist = range(1, 41)
|
| 85 |
+
plt.plot(ydist, y_pred[0], color='blue', label='predict')
|
| 86 |
+
plt.legend()
|
| 87 |
+
plt.xlabel("Deformation")
|
| 88 |
+
plt.ylabel("Load")
|
| 89 |
+
plt.title(f"Pile Prediction (Length: {pile_length}m, {pile_type})")
|
| 90 |
+
|
| 91 |
+
return plt
|
| 92 |
+
|
| 93 |
+
iface = gr.Interface(
|
| 94 |
+
fn=predict_pile,
|
| 95 |
+
inputs=[
|
| 96 |
+
gr.Radio(["default", "upload"], label="Choose Excel File Source", value="default"),
|
| 97 |
+
gr.File(label="Upload Excel File", type="binary", visible=False),
|
| 98 |
+
gr.Number(label="Pile Length (m)", value=30),
|
| 99 |
+
gr.Number(label="Section Width (m)", value=1),
|
| 100 |
+
gr.Number(label="Section Length (m)", value=1),
|
| 101 |
+
gr.Radio(["Circular", "Barrette"], label="Pile Type", value="Circular")
|
| 102 |
+
],
|
| 103 |
+
outputs="plot",
|
| 104 |
+
title="Pile Prediction Model",
|
| 105 |
+
description="Choose the default Excel file or upload your own, then enter pile characteristics to predict pile behavior.",
|
| 106 |
+
live=False
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
def update_file_input(choice):
|
| 110 |
+
return gr.update(visible=choice == "upload")
|
| 111 |
+
|
| 112 |
+
iface.inputs[0].change(update_file_input, inputs=[iface.inputs[0]], outputs=[iface.inputs[1]])
|
| 113 |
+
|
| 114 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
pandas
|
| 3 |
+
keras
|
| 4 |
+
scikit-learn
|
| 5 |
+
matplotlib
|
| 6 |
+
gradio
|
| 7 |
+
openpyxl
|
| 8 |
+
requests
|
| 9 |
+
tensorflow
|
| 10 |
+
keras
|
scaler_f.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4816bd84cf7af78609fbe1b16c487d39abec8b02670dccc2de5b7db5d748f82a
|
| 3 |
+
size 640
|
transformer_pile.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9aae751caaa685d61c5fe04379987b38f8b212eed51cee6a3d6d56b4313ce1cf
|
| 3 |
+
size 7422544
|