Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from EnYa32/FloodPrediction: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/spaces/EnYa32/FloodPrediction/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/EnYa32/FloodPrediction/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/EnYa32/FloodPrediction/resolve/main/src/streamlit_app.py
2.03 kB
| import numpy as np | |
| import pandas as pd | |
| import streamlit as st | |
| import joblib | |
| from pathlib import Path | |
| st.set_page_config( | |
| page_title='Flood Probability Predictor (Linear Regression)', | |
| page_icon='๐', | |
| layout='centered' | |
| ) | |
| BASE_DIR = Path(__file__).resolve().parent | |
| MODEL_PATH = BASE_DIR / 'linear_regression_model.pkl' | |
| FEATURES_PATH = BASE_DIR / 'feature_names.pkl' | |
| st.title('๐ Flood Probability Predictor') | |
| st.write('Predicts **FloodProbability** using a trained **Linear Regression** model.') | |
| def load_assets(): | |
| if not MODEL_PATH.exists(): | |
| raise FileNotFoundError( | |
| f'Model not found: {MODEL_PATH.name}. Put it in the repo root (same folder as app.py).' | |
| ) | |
| if not FEATURES_PATH.exists(): | |
| raise FileNotFoundError( | |
| f'Feature list not found: {FEATURES_PATH.name}. Put it in the repo root (same folder as app.py).' | |
| ) | |
| model = joblib.load(MODEL_PATH) | |
| feature_names = joblib.load(FEATURES_PATH) | |
| return model, feature_names | |
| model, feature_names = load_assets() | |
| st.subheader('Enter feature values') | |
| with st.form('input_form'): | |
| cols = st.columns(2) | |
| inputs = {} | |
| for i, feat in enumerate(feature_names): | |
| col = cols[i % 2] | |
| # Most features in your dataset are small integers (0-19) | |
| inputs[feat] = col.number_input( | |
| feat, | |
| min_value=0.0, | |
| max_value=30.0, | |
| value=5.0, | |
| step=1.0 | |
| ) | |
| submitted = st.form_submit_button('Predict') | |
| if submitted: | |
| x_row = pd.DataFrame([inputs], columns=feature_names) | |
| pred = float(model.predict(x_row)[0]) | |
| # Optional safety clip (not required by Kaggle, but safe for probabilities) | |
| pred_clipped = float(np.clip(pred, 0.0, 1.0)) | |
| st.subheader('Prediction') | |
| st.metric('FloodProbability', f'{pred_clipped:.4f}') | |
| with st.expander('Show raw prediction (before clipping)'): | |
| st.write(pred) | |
| with st.expander('Show input dataframe'): | |
| st.dataframe(x_row) |