Spaces:
Sleeping
Sleeping
first push
Browse files- src/streamlit_app.py +29 -13
src/streamlit_app.py
CHANGED
|
@@ -9,20 +9,24 @@ from sklearn.neighbors import KNeighborsClassifier
|
|
| 9 |
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
from sklearn.metrics import accuracy_score, r2_score
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
st.sidebar.title("Navigation")
|
| 24 |
page = st.sidebar.selectbox("Go to", ["Upload File", "Preprocess Data", "Train & Select Model", "Create App"])
|
| 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"])
|
|
@@ -41,6 +45,9 @@ if page == "Upload File":
|
|
| 41 |
sql_content = f.read()
|
| 42 |
st.text_area("SQL Content", sql_content, height=300)
|
| 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")]
|
|
@@ -83,8 +90,9 @@ elif page == "Preprocess Data":
|
|
| 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(
|
| 88 |
pickle.dump(le, f)
|
| 89 |
|
| 90 |
st.success("All object columns label-encoded and encoders saved!")
|
|
@@ -97,6 +105,9 @@ elif page == "Preprocess Data":
|
|
| 97 |
else:
|
| 98 |
st.warning("No CSV files available. Please upload a CSV first.")
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
elif page == "Train & Select Model":
|
| 101 |
st.title("Train Models and Select the Best One")
|
| 102 |
|
|
@@ -164,6 +175,9 @@ elif page == "Train & Select Model":
|
|
| 164 |
else:
|
| 165 |
st.warning("No processed files found. Please preprocess a file first.")
|
| 166 |
|
|
|
|
|
|
|
|
|
|
| 167 |
elif page == "Create App":
|
| 168 |
st.title("Use Model for Prediction")
|
| 169 |
|
|
@@ -186,15 +200,17 @@ elif page == "Create App":
|
|
| 186 |
|
| 187 |
model.fit(X, y)
|
| 188 |
|
|
|
|
| 189 |
encoders = {}
|
| 190 |
for col in X.columns:
|
| 191 |
-
encoder_path =
|
| 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 =
|
| 198 |
if os.path.exists(target_encoder_path):
|
| 199 |
with open(target_encoder_path, "rb") as f:
|
| 200 |
target_encoder = pickle.load(f)
|
|
|
|
| 9 |
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
from sklearn.metrics import accuracy_score, r2_score
|
| 11 |
|
| 12 |
+
# Define writable directories in /tmp
|
| 13 |
+
UPLOAD_DIR = "/tmp/uploads"
|
| 14 |
+
PROCESSED_DIR = "/tmp/processed"
|
| 15 |
+
PKL_DIR = "/tmp/pkl"
|
| 16 |
+
ENCODERS_DIR = "/tmp/encoders"
|
| 17 |
+
|
| 18 |
+
# Ensure directories exist
|
| 19 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 20 |
+
os.makedirs(PROCESSED_DIR, exist_ok=True)
|
| 21 |
+
os.makedirs(PKL_DIR, exist_ok=True)
|
| 22 |
+
os.makedirs(ENCODERS_DIR, exist_ok=True)
|
| 23 |
|
| 24 |
st.sidebar.title("Navigation")
|
| 25 |
page = st.sidebar.selectbox("Go to", ["Upload File", "Preprocess Data", "Train & Select Model", "Create App"])
|
| 26 |
|
| 27 |
+
# ========================
|
| 28 |
+
# Upload File Page
|
| 29 |
+
# ========================
|
| 30 |
if page == "Upload File":
|
| 31 |
st.title("Upload CSV or SQL File")
|
| 32 |
uploaded_file = st.file_uploader("Upload a CSV or SQL file", type=["csv", "sql"])
|
|
|
|
| 45 |
sql_content = f.read()
|
| 46 |
st.text_area("SQL Content", sql_content, height=300)
|
| 47 |
|
| 48 |
+
# ========================
|
| 49 |
+
# Preprocess Data Page
|
| 50 |
+
# ========================
|
| 51 |
elif page == "Preprocess Data":
|
| 52 |
st.title("Preprocess Uploaded CSV")
|
| 53 |
files = [f for f in os.listdir(UPLOAD_DIR) if f.endswith(".csv")]
|
|
|
|
| 90 |
st.session_state.df[col] = le.fit_transform(st.session_state.df[col].astype(str))
|
| 91 |
label_encoders[col] = le
|
| 92 |
|
| 93 |
+
# Save each encoder as pickle
|
| 94 |
for col, le in label_encoders.items():
|
| 95 |
+
with open(f"{ENCODERS_DIR}/{selected_file}_{col}_encoder.pkl", "wb") as f:
|
| 96 |
pickle.dump(le, f)
|
| 97 |
|
| 98 |
st.success("All object columns label-encoded and encoders saved!")
|
|
|
|
| 105 |
else:
|
| 106 |
st.warning("No CSV files available. Please upload a CSV first.")
|
| 107 |
|
| 108 |
+
# ========================
|
| 109 |
+
# Train & Select Model Page
|
| 110 |
+
# ========================
|
| 111 |
elif page == "Train & Select Model":
|
| 112 |
st.title("Train Models and Select the Best One")
|
| 113 |
|
|
|
|
| 175 |
else:
|
| 176 |
st.warning("No processed files found. Please preprocess a file first.")
|
| 177 |
|
| 178 |
+
# ========================
|
| 179 |
+
# Create App Page
|
| 180 |
+
# ========================
|
| 181 |
elif page == "Create App":
|
| 182 |
st.title("Use Model for Prediction")
|
| 183 |
|
|
|
|
| 200 |
|
| 201 |
model.fit(X, y)
|
| 202 |
|
| 203 |
+
# Load encoders for features if available
|
| 204 |
encoders = {}
|
| 205 |
for col in X.columns:
|
| 206 |
+
encoder_path = f"{ENCODERS_DIR}/{selected_csv}_{col}_encoder.pkl"
|
| 207 |
if os.path.exists(encoder_path):
|
| 208 |
with open(encoder_path, "rb") as f:
|
| 209 |
encoders[col] = pickle.load(f)
|
| 210 |
|
| 211 |
+
# Load encoder for target if available
|
| 212 |
target_encoder = None
|
| 213 |
+
target_encoder_path = f"{ENCODERS_DIR}/{selected_csv}_{target_col}_encoder.pkl"
|
| 214 |
if os.path.exists(target_encoder_path):
|
| 215 |
with open(target_encoder_path, "rb") as f:
|
| 216 |
target_encoder = pickle.load(f)
|