adomfosugit commited on
Commit
fe34bda
·
verified ·
1 Parent(s): d14bd32

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import pandas as pd
4
+ import pickle
5
+ from copy import deepcopy as dc
6
+ from tensorflow.keras.models import load_model
7
+
8
+ st.title('Estimate BHP')
9
+ st.subheader("Upload your CSV file here")
10
+
11
+ # Required columns for file validation
12
+ required_columns = ['PRODUCTION DATE', 'Qliquid', 'GOR', 'Pwh', 'THT', 'WCT']
13
+
14
+ # File uploader
15
+ uploaded_file = st.file_uploader("Choose a file")
16
+
17
+ def prepare_dataframe_for_lstm(df, n_steps):
18
+ df = dc(df)
19
+ df.set_index('PRODUCTION DATE', inplace=True)
20
+
21
+ for i in range(1, n_steps + 1):
22
+ df[f'Qliquid(t-{i})'] = df['Qliquid'].shift(i)
23
+ df[f'GOR(t-{i})'] = df['GOR'].shift(i)
24
+ df[f'Pwh(t-{i})'] = df['Pwh'].shift(i)
25
+ df[f'THT(t-{i})'] = df['THT'].shift(i)
26
+ df[f'WCT(t-{i})'] = df['WCT'].shift(i)
27
+
28
+ df.dropna(inplace=True)
29
+ return df
30
+
31
+ # File processing and validation
32
+ if uploaded_file is not None:
33
+ try:
34
+ dataframe = pd.read_csv(uploaded_file)
35
+ missing_columns = [col for col in required_columns if col not in dataframe.columns]
36
+
37
+ if missing_columns:
38
+ st.error(f"The uploaded file is missing the following required columns: {', '.join(missing_columns)}")
39
+ st.image("description.jpg", caption="Please check that the uploaded file has this structure")
40
+ else:
41
+ original_dataframe = dataframe.copy()
42
+ processed_dataframe = prepare_dataframe_for_lstm(dataframe, 2)
43
+ st.success("File successfully uploaded and verified!")
44
+ st.write("Processed Data Preview:", processed_dataframe)
45
+ st.session_state['processed_dataframe'] = processed_dataframe
46
+ st.session_state['original_dataframe'] = original_dataframe
47
+ except Exception as e:
48
+ st.error(f"An error occurred while processing the file: {e}")
49
+
50
+ # Sidebar for model selection
51
+ model_files = {
52
+ 'modelJ05': 'modelXAMATR_MODELTRAINFILEBETASF.pkl',
53
+ 'modelJ57': 'model57 (2).pkl',
54
+ 'modelJ61': 'modelJ61.pkl',
55
+ 'modelJ68': 'modelJ68.pkl'
56
+ }
57
+
58
+ selected_model = st.sidebar.selectbox("Select Model", list(model_files.keys()))
59
+
60
+ # Sidebar for trend selection
61
+ if 'original_dataframe' in st.session_state:
62
+ available_columns = [col for col in st.session_state['original_dataframe'].columns
63
+ if col not in ['PRODUCTION DATE', 'MBHFP']]
64
+ selected_trends = st.sidebar.multiselect("Select Trends to Display", available_columns)
65
+
66
+ # Load the saved model and scaler
67
+ def load_model_m(model_file):
68
+ with open(model_file, 'rb') as file:
69
+ data = pickle.load(file)
70
+ return data
71
+
72
+ data = load_model_m(model_files[selected_model])
73
+ model = data['model']
74
+ scaler = data['scaler']
75
+
76
+
77
+ def start_prediction():
78
+ if 'processed_dataframe' in st.session_state:
79
+ df = st.session_state['processed_dataframe']
80
+
81
+ # Columns to scale
82
+ columns_to_scale = ['MBHFP', 'Qliquid', 'GOR', 'Pwh', 'THT', 'WCT',
83
+ 'Qliquid(t-1)', 'GOR(t-1)', 'Pwh(t-1)', 'THT(t-1)', 'WCT(t-1)',
84
+ 'Qliquid(t-2)', 'GOR(t-2)', 'Pwh(t-2)', 'THT(t-2)', 'WCT(t-2)']
85
+
86
+ scaled_columns = [col for col in columns_to_scale if col in df.columns]
87
+ data_predicted = scaler.transform(df[scaled_columns])
88
+ scaled_df = pd.DataFrame(data_predicted, columns=scaled_columns)
89
+
90
+ X = scaled_df[['Qliquid', 'GOR', 'Pwh','WCT', 'THT','Qliquid(t-1)','GOR(t-1)','Pwh(t-1)','WCT(t-1)','THT(t-1)','Qliquid(t-2)','GOR(t-2)','Pwh(t-2)','WCT(t-1)','THT(t-2)']]
91
+ X = X.values.reshape((scaled_df.shape[0], 5, 3))
92
+ y_pred = model.predict(X).reshape(1,-1)
93
+ data_predicted[:, 0] = y_pred
94
+ unscaled_data = scaler.inverse_transform(data_predicted)
95
+ unscaled_df = pd.DataFrame(unscaled_data, columns=scaled_columns)
96
+ st.session_state['prediction_result'] = unscaled_df['MBHFP'].values
97
+ else:
98
+ st.error("Please upload a file and preprocess it first.")
99
+
100
+ if st.button("Predict"):
101
+ start_prediction()
102
+
103
+ if 'prediction_result' in st.session_state:
104
+ st.subheader("Prediction Result:")
105
+
106
+ # Show predictions as a table
107
+ predictions_table = pd.DataFrame({
108
+ 'Prediction Index': range(len(st.session_state['prediction_result'])),
109
+ 'Predicted BHP': st.session_state['prediction_result']
110
+ })
111
+ st.write(predictions_table)
112
+
113
+ # Create simple index-based DataFrame for plotting
114
+ plot_df = pd.DataFrame({
115
+ 'Predicted BHP': st.session_state['prediction_result']
116
+ })
117
+
118
+ # Add selected trends, skipping first 2 elements
119
+ if 'original_dataframe' in st.session_state and selected_trends:
120
+ original_df = st.session_state['original_dataframe']
121
+ start_idx = 2 # Skip first 2 elements
122
+ end_idx = start_idx + len(plot_df)
123
+
124
+ for trend in selected_trends:
125
+ plot_df[trend] = original_df[trend].iloc[start_idx:end_idx].values
126
+
127
+ # Plot with simple numeric index
128
+ st.subheader("Visualization")
129
+ st.line_chart(plot_df)
130
+
131
+ if st.checkbox("Normalize data for better comparison"):
132
+ normalized_df = (plot_df - plot_df.mean()) / plot_df.std()
133
+ st.line_chart(normalized_df)