nn commited on
Commit
c4d38f5
·
verified ·
1 Parent(s): 42393d4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -31
app.py CHANGED
@@ -12,28 +12,25 @@ import requests
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)
@@ -57,10 +54,9 @@ def predict_pile(file_choice, uploaded_file, pile_length, section_width, section
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]):
@@ -71,44 +67,58 @@ def predict_pile(file_choice, uploaded_file, pile_length, section_width, section
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()
 
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
16
+ DEFAULT_EXCEL_URL = "https://huggingface.co/spaces/neng123/Pile_deform/resolve/main/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 file == "default":
24
  file = download_default_excel()
 
25
  df = pd.read_excel(file, sheet_name='soil')
26
  df_y = pd.read_excel(file, sheet_name='pile')
27
  df_p = pd.read_excel(file, sheet_name='pile_length')
 
28
  data = np.array(df)
29
  data_y = np.array(df_y)
30
  data_pile = np.array(df_p)[:, 1:61]
31
  x_feature = data_y[:, 0:4]
32
  bh = data[:, 1:61]
33
  bh2 = data[:, 61:122] / 2
 
34
  x_train = bh / 50
35
  x_feature = sc_f.transform(x_feature)
36
  soil_data = np.stack([x_train, bh2, data_pile], axis=2)
 
54
  feature = np.reshape(feature, (1, -1))
55
  fd = sc_f.transform(feature)
56
  x_feature = fd
57
+
58
  # Use the first sample for demonstration
59
  Xd = X[0:1]
 
60
  X_train_CNN = np.zeros((Xd.shape[0], Xd.shape[1], x_feature.shape[1] + 3))
61
  X_train_CNN[:, :, 0:3] = Xd
62
  for i in range(Xd.shape[0]):
 
67
 
68
  XT = X_train_CNN
69
  print(XT.shape)
70
+
71
  y_ini = np.zeros((1, 40))
72
  y_ini[0, 0] = 0
 
73
  for step in range(39):
74
  y = transformer.predict([XT, y_ini, fd])
75
  y_ini[0, step+1] = y
76
+
77
  y_pred = y_ini * 40000
78
+
79
  plt.figure(figsize=(10, 6))
80
  ydist = range(1, 41)
81
  plt.plot(ydist, y_pred[0], color='blue', label='predict')
82
  plt.legend()
83
+ plt.xlabel("Deformation (mm)")
84
+ plt.ylabel("Load (kN)")
85
  plt.title(f"Pile Prediction (Length: {pile_length}m, {pile_type})")
 
86
  return plt
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  def update_file_input(choice):
89
  return gr.update(visible=choice == "upload")
90
 
91
+ with gr.Blocks() as iface:
92
+ gr.Markdown("# Pile Prediction Model")
93
+ gr.Markdown("Choose the default Excel file or upload your own, then enter pile characteristics to predict pile behavior.")
94
+
95
+ with gr.Row():
96
+ file_choice = gr.Radio(["default", "upload"], label="Choose Excel File Source", value="default")
97
+ uploaded_file = gr.File(label="Upload Excel File", type="binary", visible=False)
98
+
99
+ with gr.Row():
100
+ pile_length = gr.Number(label="Pile Length (m) please change data in excel file together", value=30)
101
+ section_width = gr.Number(label="Section Width (m)", value=1)
102
+ section_length = gr.Number(label="Section Length (m)", value=1)
103
+ pile_type = gr.Radio(["Circular", "Barrette"], label="Pile Type", value="Circular")
104
+
105
+ output = gr.Plot()
106
+
107
+ submit_btn = gr.Button("Predict")
108
+
109
+ # Add a download link for the default Excel file
110
+ gr.Markdown(f"[Download Default Excel File]({DEFAULT_EXCEL_URL})")
111
+
112
+ file_choice.change(
113
+ fn=update_file_input,
114
+ inputs=[file_choice],
115
+ outputs=[uploaded_file]
116
+ )
117
+
118
+ submit_btn.click(
119
+ fn=predict_pile,
120
+ inputs=[file_choice, uploaded_file, pile_length, section_width, section_length, pile_type],
121
+ outputs=[output]
122
+ )
123
 
124
  iface.launch()