Spaces:
Sleeping
Sleeping
File size: 5,117 Bytes
63d4885 c4d38f5 63d4885 c4d38f5 63d4885 42bca3e 63d4885 42bca3e 63d4885 42bca3e 63d4885 c4d38f5 63d4885 c4d38f5 63d4885 c4d38f5 63d4885 c4d38f5 63d4885 c4d38f5 63d4885 42bca3e 63d4885 c4d38f5 42bca3e c4d38f5 c173890 c4d38f5 42bca3e c4d38f5 42bca3e c4d38f5 63d4885 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
import numpy as np
import pandas as pd
from keras.models import load_model
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import pickle
import gradio as gr
import io
import requests
# Load the model and scaler
transformer = load_model('transformer_pile.h5')
sc_f = pickle.load(open('scaler_f.pkl', 'rb'))
# URL to the default Excel file
DEFAULT_EXCEL_URL = "https://huggingface.co/spaces/neng123/Pile_deform/resolve/main/data_pile.xlsx"
def download_default_excel():
response = requests.get(DEFAULT_EXCEL_URL)
return io.BytesIO(response.content)
def process_excel(file):
if file == "default":
file = download_default_excel()
df = pd.read_excel(file, sheet_name='soil')
df_y = pd.read_excel(file, sheet_name='pile')
df_p = pd.read_excel(file, sheet_name='pile_length')
data = np.array(df)
data_y = np.array(df_y)
data_pile = np.array(df_p)[:, 1:61]
x_feature = data_y[:, 0:4]
bh = data[:, 1:61]
bh2 = data[:, 61:122] / 2
x_train = bh / 50
x_feature = sc_f.transform(x_feature)
soil_data = np.stack([x_train, bh2, data_pile], axis=2)
return soil_data
def validate_inputs(pile_length, section_width, section_length, pile_type):
if pile_length < 39 or pile_length > 60:
return False, "Pile length must be between 39 and 60 meters."
if pile_type == "Circular":
if section_width < 0.8 or section_width > 1.8 or section_length < 0.8 or section_length > 1.8:
return False, "For circular piles, section width and length must be between 0.8 and 1.8 meters."
elif pile_type == "Barrette":
if section_width < 1 or section_width > 4 or section_length < 1 or section_length > 4:
return False, "For barrette piles, section width and length must be between 1 and 4 meters."
return True, ""
def predict_pile(file_choice, uploaded_file, pile_length, section_width, section_length, pile_type):
# Validate inputs
is_valid, error_message = validate_inputs(pile_length, section_width, section_length, pile_type)
if not is_valid:
return None, error_message
if file_choice == "default":
file = "default"
else:
if uploaded_file is None:
return None, "Please upload an Excel file or choose the default option."
file = uploaded_file
X = process_excel(file)
# Convert pile type to numerical value
pile_type_num = 1 if pile_type == "Circular" else 2
# Create feature array
feature = np.array([pile_length, section_width, section_length, pile_type_num])
feature = np.reshape(feature, (1, -1))
fd = sc_f.transform(feature)
x_feature = fd
# Use the first sample for demonstration
Xd = X[0:1]
X_train_CNN = np.zeros((Xd.shape[0], Xd.shape[1], x_feature.shape[1] + 3))
X_train_CNN[:, :, 0:3] = Xd
for i in range(Xd.shape[0]):
X_train_CNN[i, :, 3] = x_feature[i, 1]
X_train_CNN[i, :, 4] = x_feature[i, 2]
X_train_CNN[i, :, 5] = x_feature[i, 3]
X_train_CNN[i, :, 6] = x_feature[i, 3]
XT = X_train_CNN
print(XT.shape)
y_ini = np.zeros((1, 40))
y_ini[0, 0] = 0
for step in range(39):
y = transformer.predict([XT, y_ini, fd])
y_ini[0, step+1] = y
y_pred = y_ini * 40000
plt.figure(figsize=(10, 6))
ydist = range(1, 41)
plt.plot(ydist, y_pred[0], color='blue', label='predict')
plt.legend()
plt.xlabel("Deformation (mm)")
plt.ylabel("Load (kN)")
plt.title(f"Pile Prediction (Length: {pile_length}m, {pile_type})")
return plt, ""
def update_file_input(choice):
return gr.update(visible=choice == "upload")
with gr.Blocks() as iface:
gr.Markdown("# Pile Prediction Model in Bangkok Subsoil")
gr.Markdown("Choose the default Excel file or upload your own, then enter pile characteristics to predict pile behavior.")
with gr.Row():
file_choice = gr.Radio(["default", "upload"], label="Choose Excel File Source", value="default")
uploaded_file = gr.File(label="Upload Excel File", type="binary", visible=False)
with gr.Row():
pile_length = gr.Number(label="Pile Length (m) please change data in excel file together", value=42)
section_width = gr.Number(label="Section Width (m)", value=1)
section_length = gr.Number(label="Section Length (m)", value=1)
pile_type = gr.Radio(["Circular", "Barrette"], label="Pile Type", value="Circular")
output = gr.Plot()
error_output = gr.Textbox(label="Error", visible=True)
submit_btn = gr.Button("Predict")
# Add a download link for the default Excel file
gr.Markdown(f"[Download Default Excel File]({DEFAULT_EXCEL_URL})")
file_choice.change(
fn=update_file_input,
inputs=[file_choice],
outputs=[uploaded_file]
)
submit_btn.click(
fn=predict_pile,
inputs=[file_choice, uploaded_file, pile_length, section_width, section_length, pile_type],
outputs=[output, error_output]
)
iface.launch() |