Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +9 -13
- app.py +70 -0
- requirements.txt +3 -3
Dockerfile
CHANGED
|
@@ -1,20 +1,16 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
curl \
|
| 8 |
-
git \
|
| 9 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
-
|
| 11 |
-
COPY requirements.txt ./
|
| 12 |
-
COPY src/ ./src/
|
| 13 |
|
|
|
|
| 14 |
RUN pip3 install -r requirements.txt
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
|
| 19 |
|
| 20 |
-
|
|
|
|
| 1 |
+
# Use a minimal base image with Python 3.9 installed
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
|
| 4 |
+
# Set the working directory inside the container to /app
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
+
# Copy all files from the current directory on the host to the container's /app directory
|
| 8 |
+
COPY . .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Install Python dependencies listed in requirements.txt
|
| 11 |
RUN pip3 install -r requirements.txt
|
| 12 |
|
| 13 |
+
# Define the command to run the Streamlit app on port 8501 and make it accessible externally
|
| 14 |
+
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
|
|
|
|
| 15 |
|
| 16 |
+
# NOTE: Disable XSRF protection for easier external access in order to make batch predictions
|
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
# Load the trained regression model
|
| 6 |
+
def load_model():
|
| 7 |
+
return joblib.load("boston_housing_model_v1_0.joblib")
|
| 8 |
+
|
| 9 |
+
model = load_model()
|
| 10 |
+
|
| 11 |
+
# Streamlit UI for Boston Housing Price Prediction
|
| 12 |
+
st.title("Boston Housing Price Prediction App")
|
| 13 |
+
st.write("This app predicts the median value of owner-occupied homes (`MEDV`) in $1000s based on Boston housing dataset features.")
|
| 14 |
+
st.write("Move the sliders below to adjust values and get a prediction.")
|
| 15 |
+
|
| 16 |
+
# Collect user input using sliders
|
| 17 |
+
CRIM = st.slider("Per capita crime rate by town (CRIM)", 0.0, 100.0, 0.2, 0.1)
|
| 18 |
+
ZN = st.slider("Proportion of residential land zoned for lots over 25,000 sq.ft. (ZN)", 0.0, 100.0, 12.0, 1.0)
|
| 19 |
+
INDUS = st.slider("Proportion of non-retail business acres per town (INDUS)", 0.0, 30.0, 11.0, 0.5)
|
| 20 |
+
NX = st.slider("Nitric oxides concentration (NX)", 0.0, 1.0, 0.55, 0.01)
|
| 21 |
+
RM = st.slider("Average number of rooms per dwelling (RM)", 3.0, 9.0, 6.3, 0.1)
|
| 22 |
+
AGE = st.slider("Proportion of owner-occupied units built prior to 1940 (AGE)", 0.0, 100.0, 65.0, 1.0)
|
| 23 |
+
DIS = st.slider("Weighted distances to employment centers (DIS)", 1.0, 12.0, 4.0, 0.1)
|
| 24 |
+
RAD = st.slider("Index of accessibility to radial highways (RAD)", 1, 24, 4, 1)
|
| 25 |
+
TAX = st.slider("Full-value property tax rate per $10,000 (TAX)", 100, 700, 300, 1)
|
| 26 |
+
PTRATIO = st.slider("Pupil-teacher ratio by town (PTRATIO)", 10.0, 25.0, 19.0, 0.1)
|
| 27 |
+
LSTAT = st.slider("% lower status of the population (LSTAT)", 0.0, 40.0, 12.0, 0.1)
|
| 28 |
+
|
| 29 |
+
# Categorical feature
|
| 30 |
+
CHAS = st.selectbox("Charles River dummy variable (CHAS)", ["0 (No)", "1 (Yes)"])
|
| 31 |
+
CHAS_value = 1 if CHAS.startswith("1") else 0
|
| 32 |
+
|
| 33 |
+
# Create input DataFrame
|
| 34 |
+
input_data = pd.DataFrame([{
|
| 35 |
+
'CRIM': CRIM,
|
| 36 |
+
'ZN': ZN,
|
| 37 |
+
'INDUS': INDUS,
|
| 38 |
+
'NX': NX,
|
| 39 |
+
'RM': RM,
|
| 40 |
+
'AGE': AGE,
|
| 41 |
+
'DIS': DIS,
|
| 42 |
+
'RAD': RAD,
|
| 43 |
+
'TAX': TAX,
|
| 44 |
+
'PTRATIO': PTRATIO,
|
| 45 |
+
'LSTAT': LSTAT,
|
| 46 |
+
'CHAS': CHAS_value
|
| 47 |
+
}])
|
| 48 |
+
|
| 49 |
+
if st.button("Predict", type='primary'):
|
| 50 |
+
response = requests.post("https://<user_name>-<space_name>.hf.space/v1/customer", json=input_data) # enter user name and space name before running the cell
|
| 51 |
+
if response.status_code == 200:
|
| 52 |
+
result = response.json()
|
| 53 |
+
predicted_price = result["Predicted_MEDV"]
|
| 54 |
+
st.success(f"🏡 Predicted Median House Value: **${predicted_price * 1000:.2f}**")
|
| 55 |
+
else:
|
| 56 |
+
st.error("Error in API request")
|
| 57 |
+
|
| 58 |
+
# Batch Prediction
|
| 59 |
+
st.subheader("Batch Prediction")
|
| 60 |
+
|
| 61 |
+
file = st.file_uploader("Upload CSV file", type=["csv"])
|
| 62 |
+
if file is not None:
|
| 63 |
+
if st.button("Predict for Batch", type='primary'):
|
| 64 |
+
response = requests.post("https://<user_name>-<space_name>.hf.space/v1/customerbatch", files={"file": file}) # enter user name and space name before running the cell
|
| 65 |
+
if response.status_code == 200:
|
| 66 |
+
result = response.json()
|
| 67 |
+
st.header("Batch Prediction Results")
|
| 68 |
+
st.write(result)
|
| 69 |
+
else:
|
| 70 |
+
st.error("Error in API request")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
streamlit
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
requests==2.28.1
|
| 3 |
+
streamlit==1.43.2
|