Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
from autogluon.multimodal import MultiModalPredictor | |
from autogluon.tabular import TabularPredictor | |
# Define icons | |
seller_icon = "🏡" | |
buyer_icon = "🔍" | |
submit_icon = "📝" | |
predict_icon = "🔮" | |
# Initialize df as a global variable | |
df = None | |
def predict_price(): | |
global df # Declare df as a global variable | |
# Set the title and subheader | |
st.title("Real Estate Price Prediction") | |
st.subheader("Choose your role and provide property details") | |
# User role selection | |
option = st.selectbox("Who are you?", ['Seller', 'Buyer'], index=0) | |
if option == "Seller": | |
st.subheader(f"{seller_icon} Seller Information") | |
with st.spinner("Loading model..."): | |
predictor = MultiModalPredictor.load("C:/Users/duong/OneDrive/Desktop/mm-nlp-image-transformer") | |
st.success("Done") | |
description = st.text_area("Property Description", help="Describe your property") | |
title = st.text_input("Property Title", help="Enter a title for your property") | |
else: | |
st.subheader(f"{buyer_icon} Buyer Information") | |
with st.spinner("Loading model..."): | |
predictor = TabularPredictor.load("C:/Users/duong/OneDrive/Desktop/tabular", require_py_version_match=False) | |
st.success("Done") | |
# Property details input | |
area = st.number_input("Property Area (square meters)", min_value=1) | |
location = st.text_input("Property Location", help="Enter the location of the property") | |
city_code = st.text_input("City Code", help="Enter the city code") | |
district = st.text_input("District", help="Enter the district name") | |
bedroom = st.slider("Number of Bedrooms", min_value=1, max_value=10, value=5, step=1) | |
bathroom = st.slider("Number of Bathrooms", min_value=1, max_value=10, value=2, step=1) | |
# Submit button to create the DataFrame | |
submitted = st.button(f"{submit_icon} Submit") | |
# Create a DataFrame from user inputs | |
if submitted: | |
if area and location and city_code and district and bedroom and bathroom: | |
if option == "Seller": | |
if (not description or not title): | |
st.error("Please fill in both Description and Title fields for Sellers.") | |
else: | |
data = { | |
"Price": np.nan, | |
"Area": [area], | |
"Location": [location], | |
"Time stamp": np.nan, | |
"Certification status": np.nan, | |
"Direction": np.nan, | |
"Bedrooms": [bedroom], | |
"Bathrooms": [bathroom], | |
"Front width": np.nan, | |
"Floor": np.nan, | |
"Image URL": np.nan, | |
"Road width": np.nan, | |
"City_code": [city_code], | |
"DistrictId": [district], | |
"Balcony_Direction": np.nan, | |
"Longitude": np.nan, | |
"Lattitude": np.nan, | |
"Description": [description], | |
"Title": [title] | |
} | |
df = pd.DataFrame(data) | |
st.write(f"{seller_icon} Input Data:") | |
st.dataframe(df) | |
elif option == "Buyer": | |
data = { | |
"Price": np.nan, | |
"Area": [area], | |
"Location": [location], | |
"Time stamp": np.nan, | |
"Certification status": np.nan, | |
"Direction": np.nan, | |
"Bedrooms": [bedroom], | |
"Bathrooms": [bathroom], | |
"Front width": np.nan, | |
"Floor": np.nan, | |
"Image URL": np.nan, | |
"Road width": np.nan, | |
"City_code": [city_code], | |
"DistrictId": [district], | |
"Balcony_Direction": np.nan, | |
"Longitude": np.nan, | |
"Lattitude": np.nan | |
} | |
df = pd.DataFrame(data) | |
st.write(f"{buyer_icon} Input Data:") | |
st.dataframe(df) | |
else: | |
st.error("Please fill in all fields to have a better prediction!") | |
# Prediction button (enabled only when data has been submitted) | |
if st.button(f"{predict_icon} Predict"): | |
with st.spinner("Loading..."): | |
# Perform predictions and calculations here | |
predictions = predictor.predict(df.drop(columns="Price")) | |
st.success(f"Predicted Price: {predictions[0]:,.0f} VND") | |
scores = predictor.evaluate( | |
df, | |
metrics=[ | |
"mean_squared_error", | |
"r2", | |
], | |
) | |
st.subheader("Model Evaluation Metrics:") | |
for metric, score in scores.items(): | |
st.write(f"{metric}: {score:.2f}") | |