Spaces:
Sleeping
Sleeping
first push
Browse files- src/streamlit_app.py +15 -13
src/streamlit_app.py
CHANGED
|
@@ -6,23 +6,24 @@ from sklearn.preprocessing import LabelEncoder
|
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.linear_model import LinearRegression, LogisticRegression
|
| 8 |
from sklearn.neighbors import KNeighborsClassifier
|
| 9 |
-
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
from sklearn.metrics import accuracy_score, r2_score
|
| 11 |
|
|
|
|
| 12 |
st.sidebar.title("Navigation")
|
| 13 |
page = st.sidebar.selectbox("Go to", ["Upload File", "Preprocess Data", "Train & Select Model", "Create App"])
|
| 14 |
|
| 15 |
-
|
|
|
|
| 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")
|
| 19 |
ENCODER_DIR = os.path.join(BASE_DIR, "encoders")
|
| 20 |
|
| 21 |
-
|
| 22 |
-
os.makedirs(
|
| 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"])
|
|
@@ -34,13 +35,15 @@ if page == "Upload File":
|
|
| 34 |
st.success(f"File saved to {save_path}")
|
| 35 |
|
| 36 |
if uploaded_file.name.endswith(".csv"):
|
| 37 |
-
|
|
|
|
| 38 |
st.dataframe(df)
|
| 39 |
elif uploaded_file.name.endswith(".sql"):
|
| 40 |
-
|
| 41 |
-
|
| 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")]
|
|
@@ -66,9 +69,6 @@ elif page == "Preprocess Data":
|
|
| 66 |
st.session_state.df = st.session_state.df.replace(missing_placeholders, pd.NA)
|
| 67 |
st.session_state.df = st.session_state.df.dropna()
|
| 68 |
st.success("All placeholder and missing values dropped!")
|
| 69 |
-
missing_after = st.session_state.df.isnull().sum()
|
| 70 |
-
st.write("Missing values per column AFTER drop:")
|
| 71 |
-
st.dataframe(missing_after.rename("Missing Count"))
|
| 72 |
st.dataframe(st.session_state.df)
|
| 73 |
|
| 74 |
col_to_drop = st.multiselect("Select columns to drop", st.session_state.df.columns)
|
|
@@ -97,6 +97,7 @@ 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 |
|
|
@@ -142,7 +143,7 @@ elif page == "Train & Select Model":
|
|
| 142 |
else:
|
| 143 |
models = {
|
| 144 |
"Linear Regression": LinearRegression(),
|
| 145 |
-
"Decision Tree":
|
| 146 |
}
|
| 147 |
for name, model in models.items():
|
| 148 |
model.fit(X_train, y_train)
|
|
@@ -164,6 +165,7 @@ 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 |
|
|
|
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.linear_model import LinearRegression, LogisticRegression
|
| 8 |
from sklearn.neighbors import KNeighborsClassifier
|
| 9 |
+
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
|
| 10 |
from sklearn.metrics import accuracy_score, r2_score
|
| 11 |
|
| 12 |
+
# Sidebar navigation
|
| 13 |
st.sidebar.title("Navigation")
|
| 14 |
page = st.sidebar.selectbox("Go to", ["Upload File", "Preprocess Data", "Train & Select Model", "Create App"])
|
| 15 |
|
| 16 |
+
# Writable directories for Hugging Face / Streamlit Cloud
|
| 17 |
+
BASE_DIR = "/tmp" # only /tmp is writable in HF Spaces
|
| 18 |
UPLOAD_DIR = os.path.join(BASE_DIR, "uploads")
|
| 19 |
PROCESSED_DIR = os.path.join(BASE_DIR, "processed")
|
| 20 |
PKL_DIR = os.path.join(BASE_DIR, "pkl")
|
| 21 |
ENCODER_DIR = os.path.join(BASE_DIR, "encoders")
|
| 22 |
|
| 23 |
+
for d in [UPLOAD_DIR, PROCESSED_DIR, PKL_DIR, ENCODER_DIR]:
|
| 24 |
+
os.makedirs(d, exist_ok=True)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# -------------------- UPLOAD FILE PAGE --------------------
|
| 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"])
|
|
|
|
| 35 |
st.success(f"File saved to {save_path}")
|
| 36 |
|
| 37 |
if uploaded_file.name.endswith(".csv"):
|
| 38 |
+
uploaded_file.seek(0)
|
| 39 |
+
df = pd.read_csv(uploaded_file)
|
| 40 |
st.dataframe(df)
|
| 41 |
elif uploaded_file.name.endswith(".sql"):
|
| 42 |
+
uploaded_file.seek(0)
|
| 43 |
+
sql_content = uploaded_file.read().decode("utf-8", errors="ignore")
|
| 44 |
st.text_area("SQL Content", sql_content, height=300)
|
| 45 |
|
| 46 |
+
# -------------------- PREPROCESS DATA PAGE --------------------
|
| 47 |
elif page == "Preprocess Data":
|
| 48 |
st.title("Preprocess Uploaded CSV")
|
| 49 |
files = [f for f in os.listdir(UPLOAD_DIR) if f.endswith(".csv")]
|
|
|
|
| 69 |
st.session_state.df = st.session_state.df.replace(missing_placeholders, pd.NA)
|
| 70 |
st.session_state.df = st.session_state.df.dropna()
|
| 71 |
st.success("All placeholder and missing values dropped!")
|
|
|
|
|
|
|
|
|
|
| 72 |
st.dataframe(st.session_state.df)
|
| 73 |
|
| 74 |
col_to_drop = st.multiselect("Select columns to drop", st.session_state.df.columns)
|
|
|
|
| 97 |
else:
|
| 98 |
st.warning("No CSV files available. Please upload a CSV first.")
|
| 99 |
|
| 100 |
+
# -------------------- TRAIN & SELECT MODEL PAGE --------------------
|
| 101 |
elif page == "Train & Select Model":
|
| 102 |
st.title("Train Models and Select the Best One")
|
| 103 |
|
|
|
|
| 143 |
else:
|
| 144 |
models = {
|
| 145 |
"Linear Regression": LinearRegression(),
|
| 146 |
+
"Decision Tree Regressor": DecisionTreeRegressor()
|
| 147 |
}
|
| 148 |
for name, model in models.items():
|
| 149 |
model.fit(X_train, y_train)
|
|
|
|
| 165 |
else:
|
| 166 |
st.warning("No processed files found. Please preprocess a file first.")
|
| 167 |
|
| 168 |
+
# -------------------- CREATE APP PAGE --------------------
|
| 169 |
elif page == "Create App":
|
| 170 |
st.title("Use Model for Prediction")
|
| 171 |
|