Sahil0605 commited on
Commit
0f41a5d
·
1 Parent(s): 000fb26

first push

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +23 -17
src/streamlit_app.py CHANGED
@@ -9,17 +9,16 @@ from sklearn.neighbors import KNeighborsClassifier
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"])
@@ -50,7 +49,10 @@ if page == "Upload File":
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")]
 
 
 
54
 
55
  if files:
56
  selected_file = st.selectbox("Select a CSV file to preprocess", files)
@@ -90,7 +92,6 @@ elif page == "Preprocess Data":
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)
@@ -111,7 +112,10 @@ elif page == "Preprocess Data":
111
  elif page == "Train & Select Model":
112
  st.title("Train Models and Select the Best One")
113
 
114
- processed_files = [f for f in os.listdir(PROCESSED_DIR) if f.endswith(".csv")]
 
 
 
115
 
116
  if processed_files:
117
  selected_file = st.selectbox("Select a processed CSV", processed_files)
@@ -181,7 +185,11 @@ elif page == "Train & Select Model":
181
  elif page == "Create App":
182
  st.title("Use Model for Prediction")
183
 
184
- processed_files = [f for f in os.listdir(PROCESSED_DIR) if f.endswith(".csv")]
 
 
 
 
185
  if processed_files:
186
  selected_csv = st.selectbox("Select a processed CSV file", processed_files)
187
  df = pd.read_csv(os.path.join(PROCESSED_DIR, selected_csv))
@@ -200,7 +208,6 @@ elif page == "Create App":
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"
@@ -208,7 +215,6 @@ elif page == "Create App":
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):
 
9
  from sklearn.tree import DecisionTreeClassifier
10
  from sklearn.metrics import accuracy_score, r2_score
11
 
12
+ # Define existing directories (must already be present in environment)
13
+ UPLOAD_DIR = "uploads"
14
+ PROCESSED_DIR = "processed"
15
+ PKL_DIR = "pkl"
16
+ ENCODERS_DIR = "encoders"
17
+
18
+ # Warn if directories don't exist
19
+ for folder in [UPLOAD_DIR, PROCESSED_DIR, PKL_DIR, ENCODERS_DIR]:
20
+ if not os.path.exists(folder):
21
+ st.warning(f"Directory not found: {folder}. Please create it before using this app.")
 
22
 
23
  st.sidebar.title("Navigation")
24
  page = st.sidebar.selectbox("Go to", ["Upload File", "Preprocess Data", "Train & Select Model", "Create App"])
 
49
  # ========================
50
  elif page == "Preprocess Data":
51
  st.title("Preprocess Uploaded CSV")
52
+ if os.path.exists(UPLOAD_DIR):
53
+ files = [f for f in os.listdir(UPLOAD_DIR) if f.endswith(".csv")]
54
+ else:
55
+ files = []
56
 
57
  if files:
58
  selected_file = st.selectbox("Select a CSV file to preprocess", files)
 
92
  st.session_state.df[col] = le.fit_transform(st.session_state.df[col].astype(str))
93
  label_encoders[col] = le
94
 
 
95
  for col, le in label_encoders.items():
96
  with open(f"{ENCODERS_DIR}/{selected_file}_{col}_encoder.pkl", "wb") as f:
97
  pickle.dump(le, f)
 
112
  elif page == "Train & Select Model":
113
  st.title("Train Models and Select the Best One")
114
 
115
+ if os.path.exists(PROCESSED_DIR):
116
+ processed_files = [f for f in os.listdir(PROCESSED_DIR) if f.endswith(".csv")]
117
+ else:
118
+ processed_files = []
119
 
120
  if processed_files:
121
  selected_file = st.selectbox("Select a processed CSV", processed_files)
 
185
  elif page == "Create App":
186
  st.title("Use Model for Prediction")
187
 
188
+ if os.path.exists(PROCESSED_DIR):
189
+ processed_files = [f for f in os.listdir(PROCESSED_DIR) if f.endswith(".csv")]
190
+ else:
191
+ processed_files = []
192
+
193
  if processed_files:
194
  selected_csv = st.selectbox("Select a processed CSV file", processed_files)
195
  df = pd.read_csv(os.path.join(PROCESSED_DIR, selected_csv))
 
208
 
209
  model.fit(X, y)
210
 
 
211
  encoders = {}
212
  for col in X.columns:
213
  encoder_path = f"{ENCODERS_DIR}/{selected_csv}_{col}_encoder.pkl"
 
215
  with open(encoder_path, "rb") as f:
216
  encoders[col] = pickle.load(f)
217
 
 
218
  target_encoder = None
219
  target_encoder_path = f"{ENCODERS_DIR}/{selected_csv}_{target_col}_encoder.pkl"
220
  if os.path.exists(target_encoder_path):