Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +8 -13
- app.py +59 -0
- requirements.txt +3 -3
Dockerfile
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
curl \
|
| 8 |
-
software-properties-common \
|
| 9 |
-
git \
|
| 10 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
-
|
| 12 |
-
COPY requirements.txt ./
|
| 13 |
-
COPY src/ ./src/
|
| 14 |
|
|
|
|
| 15 |
RUN pip3 install -r requirements.txt
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
|
| 20 |
|
| 21 |
-
|
|
|
|
| 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,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Streamlit UI
|
| 7 |
+
st.title('Boston Housing Price Predictor 🏠')
|
| 8 |
+
st.write('Enter the details of the area to predict the median home value.')
|
| 9 |
+
|
| 10 |
+
# Create input fields for each feature
|
| 11 |
+
crim = st.number_input('Per capita crime rate (CRIM)', value=0.0)
|
| 12 |
+
zn = st.number_input('Proportion of residential land zoned for lots over 25,000 sq.ft. (ZN)', value=0.0)
|
| 13 |
+
indus = st.number_input('Proportion of non-retail business acres per town (INDUS)', value=0.0)
|
| 14 |
+
chas = st.selectbox('Tract bounds Charles River? (CHAS)', options=[0, 1], format_func=lambda x: 'Yes' if x == 1 else 'No')
|
| 15 |
+
nox = st.number_input('Nitric oxides concentration (NOX)', value=0.0)
|
| 16 |
+
rm = st.number_input('Average number of rooms per dwelling (RM)', value=0.0)
|
| 17 |
+
age = st.number_input('Proportion of owner-occupied units built prior to 1940 (AGE)', value=0.0)
|
| 18 |
+
dis = st.number_input('Weighted distances to five Boston employment centers (DIS)', value=0.0)
|
| 19 |
+
rad = st.number_input('Index of accessibility to radial highways (RAD)', value=0.0)
|
| 20 |
+
tax = st.number_input('Full-value property-tax rate per $10,000 (TAX)', value=0.0)
|
| 21 |
+
ptratio = st.number_input('Pupil-teacher ratio by town (PTRATIO)', value=0.0)
|
| 22 |
+
lstat = st.number_input('% lower status of the population (LSTAT)', value=0.0)
|
| 23 |
+
|
| 24 |
+
# Create a dictionary of the input features
|
| 25 |
+
input_data = {
|
| 26 |
+
'CRIM': crim,
|
| 27 |
+
'ZN': zn,
|
| 28 |
+
'INDUS': indus,
|
| 29 |
+
'CHAS': chas,
|
| 30 |
+
'NOX': nox,
|
| 31 |
+
'RM': rm,
|
| 32 |
+
'AGE': age,
|
| 33 |
+
'DIS': dis,
|
| 34 |
+
'RAD': rad,
|
| 35 |
+
'TAX': tax,
|
| 36 |
+
'PTRATIO': ptratio,
|
| 37 |
+
'LSTAT': lstat
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Button to make a prediction
|
| 41 |
+
if st.button('Predict Median Home Value'):
|
| 42 |
+
# Define the backend API URL (replace with your deployed URL)
|
| 43 |
+
API_URL = "http://localhost:5000/predict" # For local testing
|
| 44 |
+
# API_URL = "https://<your-space-name>.hf.space/predict" # For Hugging Face
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
response = requests.post(API_URL, json=input_data)
|
| 48 |
+
if response.status_code == 200:
|
| 49 |
+
prediction = response.json()['prediction']
|
| 50 |
+
st.success(f'The predicted median home value is: ${prediction:.2f} (in thousands)')
|
| 51 |
+
st.markdown(f'**Predicted Value**: ${prediction * 1000:,.2f}')
|
| 52 |
+
else:
|
| 53 |
+
st.error(f"Error from API: {response.text}")
|
| 54 |
+
except requests.exceptions.ConnectionError:
|
| 55 |
+
st.error("Connection error. Please ensure the backend is running.")
|
| 56 |
+
|
| 57 |
+
st.markdown("---")
|
| 58 |
+
st.write("This application uses a machine learning model to predict the median value of homes in Boston suburbs.")
|
| 59 |
+
st.markdown("")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
| 1 |
+
streamlit==1.36.0
|
| 2 |
+
requests==2.32.3
|
| 3 |
+
pandas==2.2.2
|