Spaces:
Sleeping
Sleeping
first push
Browse files- src/streamlit_app.py +13 -17
src/streamlit_app.py
CHANGED
|
@@ -12,7 +12,7 @@ from sklearn.metrics import accuracy_score, r2_score
|
|
| 12 |
st.sidebar.title("Navigation")
|
| 13 |
page = st.sidebar.selectbox("Go to", ["Upload File", "Preprocess Data", "Train & Select Model", "Create App"])
|
| 14 |
|
| 15 |
-
BASE_DIR = "/tmp"
|
| 16 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
| 17 |
PROCESSED_DIR = os.path.join(BASE_DIR, "processed")
|
| 18 |
PKL_DIR = os.path.join(BASE_DIR, "pkl")
|
|
@@ -23,13 +23,12 @@ os.makedirs(PROCESSED_DIR, exist_ok=True)
|
|
| 23 |
os.makedirs(PKL_DIR, exist_ok=True)
|
| 24 |
os.makedirs(ENCODER_DIR, exist_ok=True)
|
| 25 |
|
| 26 |
-
|
| 27 |
if page == "Upload File":
|
| 28 |
st.title("Upload CSV or SQL File")
|
| 29 |
uploaded_file = st.file_uploader("Upload a CSV or SQL file", type=["csv", "sql"])
|
| 30 |
|
| 31 |
if uploaded_file is not None:
|
| 32 |
-
save_path = os.path.join(
|
| 33 |
with open(save_path, "wb") as f:
|
| 34 |
f.write(uploaded_file.getbuffer())
|
| 35 |
st.success(f"File saved to {save_path}")
|
|
@@ -44,11 +43,11 @@ if page == "Upload File":
|
|
| 44 |
|
| 45 |
elif page == "Preprocess Data":
|
| 46 |
st.title("Preprocess Uploaded CSV")
|
| 47 |
-
files = [f for f in os.listdir(
|
| 48 |
|
| 49 |
if files:
|
| 50 |
selected_file = st.selectbox("Select a CSV file to preprocess", files)
|
| 51 |
-
file_path = os.path.join(
|
| 52 |
|
| 53 |
if "df" not in st.session_state or st.session_state.file_path != file_path:
|
| 54 |
st.session_state.df = pd.read_csv(file_path)
|
|
@@ -84,16 +83,15 @@ elif page == "Preprocess Data":
|
|
| 84 |
st.session_state.df[col] = le.fit_transform(st.session_state.df[col].astype(str))
|
| 85 |
label_encoders[col] = le
|
| 86 |
|
| 87 |
-
# Save each encoder as pickle
|
| 88 |
for col, le in label_encoders.items():
|
| 89 |
-
with open(f"
|
| 90 |
pickle.dump(le, f)
|
| 91 |
|
| 92 |
st.success("All object columns label-encoded and encoders saved!")
|
| 93 |
st.dataframe(st.session_state.df)
|
| 94 |
|
| 95 |
if st.button("Save Preprocessed File"):
|
| 96 |
-
processed_path = os.path.join(
|
| 97 |
st.session_state.df.to_csv(processed_path, index=False)
|
| 98 |
st.success(f"Preprocessed file saved to: {processed_path}")
|
| 99 |
else:
|
|
@@ -102,11 +100,11 @@ elif page == "Preprocess Data":
|
|
| 102 |
elif page == "Train & Select Model":
|
| 103 |
st.title("Train Models and Select the Best One")
|
| 104 |
|
| 105 |
-
processed_files = [f for f in os.listdir(
|
| 106 |
|
| 107 |
if processed_files:
|
| 108 |
selected_file = st.selectbox("Select a processed CSV", processed_files)
|
| 109 |
-
file_path = os.path.join(
|
| 110 |
|
| 111 |
df = pd.read_csv(file_path)
|
| 112 |
st.dataframe(df.head())
|
|
@@ -159,7 +157,7 @@ elif page == "Train & Select Model":
|
|
| 159 |
selected_model_name = st.selectbox("Select model to save", list(results.keys()))
|
| 160 |
if st.button("Save Selected Model"):
|
| 161 |
final_model = models[selected_model_name]
|
| 162 |
-
pkl_path = os.path.join(
|
| 163 |
with open(pkl_path, "wb") as f:
|
| 164 |
pickle.dump(final_model, f)
|
| 165 |
st.success(f"Model saved as {pkl_path}")
|
|
@@ -169,10 +167,10 @@ elif page == "Train & Select Model":
|
|
| 169 |
elif page == "Create App":
|
| 170 |
st.title("Use Model for Prediction")
|
| 171 |
|
| 172 |
-
processed_files = [f for f in os.listdir(
|
| 173 |
if processed_files:
|
| 174 |
selected_csv = st.selectbox("Select a processed CSV file", processed_files)
|
| 175 |
-
df = pd.read_csv(os.path.join(
|
| 176 |
target_col = st.selectbox("Select target column", df.columns)
|
| 177 |
|
| 178 |
if target_col:
|
|
@@ -188,17 +186,15 @@ elif page == "Create App":
|
|
| 188 |
|
| 189 |
model.fit(X, y)
|
| 190 |
|
| 191 |
-
# Load encoders for features if available
|
| 192 |
encoders = {}
|
| 193 |
for col in X.columns:
|
| 194 |
-
encoder_path = f"
|
| 195 |
if os.path.exists(encoder_path):
|
| 196 |
with open(encoder_path, "rb") as f:
|
| 197 |
encoders[col] = pickle.load(f)
|
| 198 |
|
| 199 |
-
# Load encoder for target if available (for inverse transform of prediction)
|
| 200 |
target_encoder = None
|
| 201 |
-
target_encoder_path = f"
|
| 202 |
if os.path.exists(target_encoder_path):
|
| 203 |
with open(target_encoder_path, "rb") as f:
|
| 204 |
target_encoder = pickle.load(f)
|
|
|
|
| 12 |
st.sidebar.title("Navigation")
|
| 13 |
page = st.sidebar.selectbox("Go to", ["Upload File", "Preprocess Data", "Train & Select Model", "Create App"])
|
| 14 |
|
| 15 |
+
BASE_DIR = "/tmp"
|
| 16 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
| 17 |
PROCESSED_DIR = os.path.join(BASE_DIR, "processed")
|
| 18 |
PKL_DIR = os.path.join(BASE_DIR, "pkl")
|
|
|
|
| 23 |
os.makedirs(PKL_DIR, exist_ok=True)
|
| 24 |
os.makedirs(ENCODER_DIR, exist_ok=True)
|
| 25 |
|
|
|
|
| 26 |
if page == "Upload File":
|
| 27 |
st.title("Upload CSV or SQL File")
|
| 28 |
uploaded_file = st.file_uploader("Upload a CSV or SQL file", type=["csv", "sql"])
|
| 29 |
|
| 30 |
if uploaded_file is not None:
|
| 31 |
+
save_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
|
| 32 |
with open(save_path, "wb") as f:
|
| 33 |
f.write(uploaded_file.getbuffer())
|
| 34 |
st.success(f"File saved to {save_path}")
|
|
|
|
| 43 |
|
| 44 |
elif page == "Preprocess Data":
|
| 45 |
st.title("Preprocess Uploaded CSV")
|
| 46 |
+
files = [f for f in os.listdir(UPLOAD_DIR) if f.endswith(".csv")]
|
| 47 |
|
| 48 |
if files:
|
| 49 |
selected_file = st.selectbox("Select a CSV file to preprocess", files)
|
| 50 |
+
file_path = os.path.join(UPLOAD_DIR, selected_file)
|
| 51 |
|
| 52 |
if "df" not in st.session_state or st.session_state.file_path != file_path:
|
| 53 |
st.session_state.df = pd.read_csv(file_path)
|
|
|
|
| 83 |
st.session_state.df[col] = le.fit_transform(st.session_state.df[col].astype(str))
|
| 84 |
label_encoders[col] = le
|
| 85 |
|
|
|
|
| 86 |
for col, le in label_encoders.items():
|
| 87 |
+
with open(os.path.join(ENCODER_DIR, f"{selected_file}_{col}_encoder.pkl"), "wb") as f:
|
| 88 |
pickle.dump(le, f)
|
| 89 |
|
| 90 |
st.success("All object columns label-encoded and encoders saved!")
|
| 91 |
st.dataframe(st.session_state.df)
|
| 92 |
|
| 93 |
if st.button("Save Preprocessed File"):
|
| 94 |
+
processed_path = os.path.join(PROCESSED_DIR, selected_file)
|
| 95 |
st.session_state.df.to_csv(processed_path, index=False)
|
| 96 |
st.success(f"Preprocessed file saved to: {processed_path}")
|
| 97 |
else:
|
|
|
|
| 100 |
elif page == "Train & Select Model":
|
| 101 |
st.title("Train Models and Select the Best One")
|
| 102 |
|
| 103 |
+
processed_files = [f for f in os.listdir(PROCESSED_DIR) if f.endswith(".csv")]
|
| 104 |
|
| 105 |
if processed_files:
|
| 106 |
selected_file = st.selectbox("Select a processed CSV", processed_files)
|
| 107 |
+
file_path = os.path.join(PROCESSED_DIR, selected_file)
|
| 108 |
|
| 109 |
df = pd.read_csv(file_path)
|
| 110 |
st.dataframe(df.head())
|
|
|
|
| 157 |
selected_model_name = st.selectbox("Select model to save", list(results.keys()))
|
| 158 |
if st.button("Save Selected Model"):
|
| 159 |
final_model = models[selected_model_name]
|
| 160 |
+
pkl_path = os.path.join(PKL_DIR, f"{selected_model_name}.pkl")
|
| 161 |
with open(pkl_path, "wb") as f:
|
| 162 |
pickle.dump(final_model, f)
|
| 163 |
st.success(f"Model saved as {pkl_path}")
|
|
|
|
| 167 |
elif page == "Create App":
|
| 168 |
st.title("Use Model for Prediction")
|
| 169 |
|
| 170 |
+
processed_files = [f for f in os.listdir(PROCESSED_DIR) if f.endswith(".csv")]
|
| 171 |
if processed_files:
|
| 172 |
selected_csv = st.selectbox("Select a processed CSV file", processed_files)
|
| 173 |
+
df = pd.read_csv(os.path.join(PROCESSED_DIR, selected_csv))
|
| 174 |
target_col = st.selectbox("Select target column", df.columns)
|
| 175 |
|
| 176 |
if target_col:
|
|
|
|
| 186 |
|
| 187 |
model.fit(X, y)
|
| 188 |
|
|
|
|
| 189 |
encoders = {}
|
| 190 |
for col in X.columns:
|
| 191 |
+
encoder_path = os.path.join(ENCODER_DIR, f"{selected_csv}_{col}_encoder.pkl")
|
| 192 |
if os.path.exists(encoder_path):
|
| 193 |
with open(encoder_path, "rb") as f:
|
| 194 |
encoders[col] = pickle.load(f)
|
| 195 |
|
|
|
|
| 196 |
target_encoder = None
|
| 197 |
+
target_encoder_path = os.path.join(ENCODER_DIR, f"{selected_csv}_{target_col}_encoder.pkl")
|
| 198 |
if os.path.exists(target_encoder_path):
|
| 199 |
with open(target_encoder_path, "rb") as f:
|
| 200 |
target_encoder = pickle.load(f)
|