Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +10 -29
streamlit_app.py
CHANGED
|
@@ -33,59 +33,40 @@ st.write("Upload your CSV dataset or use a generated sample dataset.")
|
|
| 33 |
# Option to use generated sample dataset
|
| 34 |
if st.button("Use Sample Dataset (sample_dataset.csv)"):
|
| 35 |
# Path to the sample_dataset.csv relative to streamlit_app.py
|
| 36 |
-
# Assumes sample_dataset.csv is in the 'data' folder at the root of the project
|
| 37 |
sample_csv_path = os.path.join(os.path.dirname(__file__), 'data', 'sample_dataset.csv')
|
| 38 |
|
| 39 |
if os.path.exists(sample_csv_path):
|
| 40 |
with open(sample_csv_path, 'rb') as f:
|
| 41 |
csv_content = f.read()
|
| 42 |
-
|
| 43 |
-
# Prepare the file for upload using 'files' parameter for multipart/form-data
|
| 44 |
-
# 'file' is the name of the input field Flask expects (request.files['file'])
|
| 45 |
-
# 'sample_dataset.csv' is the filename
|
| 46 |
-
# csv_content is the actual binary content of the file
|
| 47 |
-
# 'text/csv' is the content type
|
| 48 |
files = {'file': ('sample_dataset.csv', csv_content, 'text/csv')}
|
| 49 |
-
|
| 50 |
try:
|
| 51 |
-
# Send the file to Flask backend
|
| 52 |
response = requests.post(f"{FLASK_API_URL}/preprocess/upload", files=files)
|
| 53 |
-
response.raise_for_status()
|
| 54 |
processed_data_json = response.json()
|
| 55 |
-
|
| 56 |
-
# Update Streamlit session state with processed data and columns
|
| 57 |
st.session_state.processed_data = processed_data_json['data']
|
| 58 |
st.session_state.processed_columns = processed_data_json['columns']
|
| 59 |
st.success("Sample dataset loaded and preprocessed successfully!")
|
| 60 |
-
|
| 61 |
-
# Optional: Display the columns or a snippet of data for confirmation
|
| 62 |
st.json(processed_data_json['columns'])
|
| 63 |
-
|
| 64 |
except requests.exceptions.ConnectionError:
|
| 65 |
st.error(f"Could not connect to Flask API at {FLASK_API_URL}. Please ensure the backend is running.")
|
| 66 |
-
except requests.exceptions.HTTPError as http_err:
|
| 67 |
st.error(f"HTTP error occurred: {http_err} - Server response: {http_err.response.text}")
|
| 68 |
except Exception as e:
|
| 69 |
st.error(f"An unexpected error occurred: {e}")
|
|
|
|
| 70 |
else:
|
| 71 |
st.error(f"Sample dataset not found at {sample_csv_path}. Please ensure it exists in your 'data' folder.")
|
| 72 |
-
|
| 73 |
-
if response.status_code == 200:
|
| 74 |
-
result = response.json()
|
| 75 |
-
st.session_state.processed_data = result['data']
|
| 76 |
-
st.session_state.processed_columns = result['columns']
|
| 77 |
-
st.success("Sample dataset preprocessed successfully!")
|
| 78 |
-
st.dataframe(pd.DataFrame(st.session_state.processed_data).head()) # Display first few rows
|
| 79 |
-
else:
|
| 80 |
-
st.error(f"Error preprocessing sample dataset: {response.json().get('detail', 'Unknown error')}")
|
| 81 |
-
except Exception as e:
|
| 82 |
-
st.error(f"Could not load or process sample dataset: {e}")
|
| 83 |
-
|
| 84 |
|
|
|
|
| 85 |
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 86 |
if uploaded_file is not None:
|
| 87 |
st.info("Uploading and preprocessing data...")
|
| 88 |
files = {'file': (uploaded_file.name, uploaded_file.getvalue(), 'text/csv')}
|
|
|
|
| 89 |
try:
|
| 90 |
response = requests.post(f"{FLASK_API_URL}/preprocess/upload", files=files)
|
| 91 |
if response.status_code == 200:
|
|
@@ -93,7 +74,7 @@ if uploaded_file is not None:
|
|
| 93 |
st.session_state.processed_data = result['data']
|
| 94 |
st.session_state.processed_columns = result['columns']
|
| 95 |
st.success("File preprocessed successfully!")
|
| 96 |
-
st.dataframe(pd.DataFrame(st.session_state.processed_data).head())
|
| 97 |
else:
|
| 98 |
st.error(f"Error during preprocessing: {response.json().get('detail', 'Unknown error')}")
|
| 99 |
except requests.exceptions.ConnectionError:
|
|
|
|
| 33 |
# Option to use generated sample dataset
|
| 34 |
if st.button("Use Sample Dataset (sample_dataset.csv)"):
|
| 35 |
# Path to the sample_dataset.csv relative to streamlit_app.py
|
|
|
|
| 36 |
sample_csv_path = os.path.join(os.path.dirname(__file__), 'data', 'sample_dataset.csv')
|
| 37 |
|
| 38 |
if os.path.exists(sample_csv_path):
|
| 39 |
with open(sample_csv_path, 'rb') as f:
|
| 40 |
csv_content = f.read()
|
| 41 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
files = {'file': ('sample_dataset.csv', csv_content, 'text/csv')}
|
| 43 |
+
|
| 44 |
try:
|
|
|
|
| 45 |
response = requests.post(f"{FLASK_API_URL}/preprocess/upload", files=files)
|
| 46 |
+
response.raise_for_status()
|
| 47 |
processed_data_json = response.json()
|
| 48 |
+
|
|
|
|
| 49 |
st.session_state.processed_data = processed_data_json['data']
|
| 50 |
st.session_state.processed_columns = processed_data_json['columns']
|
| 51 |
st.success("Sample dataset loaded and preprocessed successfully!")
|
|
|
|
|
|
|
| 52 |
st.json(processed_data_json['columns'])
|
| 53 |
+
|
| 54 |
except requests.exceptions.ConnectionError:
|
| 55 |
st.error(f"Could not connect to Flask API at {FLASK_API_URL}. Please ensure the backend is running.")
|
| 56 |
+
except requests.exceptions.HTTPError as http_err:
|
| 57 |
st.error(f"HTTP error occurred: {http_err} - Server response: {http_err.response.text}")
|
| 58 |
except Exception as e:
|
| 59 |
st.error(f"An unexpected error occurred: {e}")
|
| 60 |
+
|
| 61 |
else:
|
| 62 |
st.error(f"Sample dataset not found at {sample_csv_path}. Please ensure it exists in your 'data' folder.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# Option to upload your own CSV
|
| 65 |
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 66 |
if uploaded_file is not None:
|
| 67 |
st.info("Uploading and preprocessing data...")
|
| 68 |
files = {'file': (uploaded_file.name, uploaded_file.getvalue(), 'text/csv')}
|
| 69 |
+
|
| 70 |
try:
|
| 71 |
response = requests.post(f"{FLASK_API_URL}/preprocess/upload", files=files)
|
| 72 |
if response.status_code == 200:
|
|
|
|
| 74 |
st.session_state.processed_data = result['data']
|
| 75 |
st.session_state.processed_columns = result['columns']
|
| 76 |
st.success("File preprocessed successfully!")
|
| 77 |
+
st.dataframe(pd.DataFrame(st.session_state.processed_data).head())
|
| 78 |
else:
|
| 79 |
st.error(f"Error during preprocessing: {response.json().get('detail', 'Unknown error')}")
|
| 80 |
except requests.exceptions.ConnectionError:
|