Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import pandas as pd | |
| # Backend URL | |
| BACKEND_URL = "https://omm7-boston-backend.hf.space" | |
| # Check backend health | |
| try: | |
| health_resp = requests.get(f"{BACKEND_URL}/health", timeout=5) | |
| if health_resp.status_code == 200 and health_resp.json().get("status") == "ok": | |
| st.toast("β Connected to backend", icon="β ") | |
| else: | |
| st.toast("β οΈ Backend connection failed", icon="β οΈ") | |
| except Exception: | |
| st.toast("β Backend not reachable", icon="β") | |
| st.title('Boston Housing Price Predictor π ') | |
| st.write('Enter the details of the area to predict the median home value.') | |
| # Input fields | |
| crim = st.number_input('Per capita crime rate (CRIM)', value=0.0) | |
| zn = st.number_input('Proportion of residential land zoned for lots over 25,000 sq.ft. (ZN)', value=0.0) | |
| indus = st.number_input('Proportion of non-retail business acres per town (INDUS)', value=0.0) | |
| chas = st.selectbox('Tract bounds Charles River? (CHAS)', options=[0, 1], format_func=lambda x: 'Yes' if x == 1 else 'No') | |
| nox = st.number_input('Nitric oxides concentration (NOX)', value=0.0) | |
| rm = st.number_input('Average number of rooms per dwelling (RM)', value=0.0) | |
| age = st.number_input('Proportion of owner-occupied units built prior to 1940 (AGE)', value=0.0) | |
| dis = st.number_input('Weighted distances to five Boston employment centers (DIS)', value=0.0) | |
| rad = st.number_input('Index of accessibility to radial highways (RAD)', value=0.0) | |
| tax = st.number_input('Full-value property-tax rate per $10,000 (TAX)', value=0.0) | |
| ptratio = st.number_input('Pupil-teacher ratio by town (PTRATIO)', value=0.0) | |
| lstat = st.number_input('% lower status of the population (LSTAT)', value=0.0) | |
| input_data = { | |
| 'CRIM': crim, | |
| 'ZN': zn, | |
| 'INDUS': indus, | |
| 'CHAS': chas, | |
| 'NOX': nox, | |
| 'RM': rm, | |
| 'AGE': age, | |
| 'DIS': dis, | |
| 'RAD': rad, | |
| 'TAX': tax, | |
| 'PTRATIO': ptratio, | |
| 'LSTAT': lstat | |
| } | |
| if st.button('Predict Median Home Value'): | |
| status_placeholder = st.empty() | |
| status_placeholder.info("π‘ Sending request to backend...") | |
| try: | |
| response = requests.post(f"{BACKEND_URL}/predict", json=input_data) | |
| if response.status_code == 200: | |
| data = response.json() | |
| # Show step-by-step status | |
| for step in data.get("steps", []): | |
| status_placeholder.info(step) | |
| prediction = data['prediction'] | |
| st.success(f'The predicted median home value is: ${prediction:.2f} (in thousands)') | |
| st.markdown(f'**Predicted Value**: ${prediction * 1000:,.2f}') | |
| else: | |
| st.error(f"Error from API: {response.text}") | |
| except requests.exceptions.ConnectionError: | |
| st.error("β Connection error. Please ensure the backend is running.") | |
| st.markdown("---") | |
| st.write("This application uses a machine learning model to predict the median value of homes in Boston suburbs.") | |