# app.py - FINAL PROFESSIONAL version with input validation and guardrails import gradio as gr import joblib import pandas as pd import os import subprocess # --- STEP 1: Download the Model Files from the Hub (same as before) --- MODEL_URL = "https://huggingface.co/szili2011/ai-house-price-predictor/resolve/main/housing_model.joblib" COLUMNS_URL = "https://huggingface.co/szili2011/ai-house-price-predictor/resolve/main/model_columns.joblib" MODEL_LOCAL_PATH = "housing_model.joblib" COLUMNS_LOCAL_PATH = "model_columns.joblib" if not os.path.exists(MODEL_LOCAL_PATH): print("--- Downloading Model Files ---") subprocess.run(["wget", "-O", MODEL_LOCAL_PATH, MODEL_URL]) subprocess.run(["wget", "-O", COLUMNS_LOCAL_PATH, COLUMNS_URL]) print("--- Download Complete ---") # --- STEP 2: Load the Now-Local Model and Columns (same as before) --- try: print(f"Loading model from local path: '{MODEL_LOCAL_PATH}'") model = joblib.load(MODEL_LOCAL_PATH) print(f"Loading columns from local path: '{COLUMNS_LOCAL_PATH}'") model_columns = joblib.load(COLUMNS_LOCAL_PATH) print("✅ Model and columns loaded successfully.") model_loaded_successfully = True except Exception as e: print(f"❌ CRITICAL ERROR during model loading: {e}") model_loaded_successfully = False # --- This is the core prediction function with the new validation logic --- def predict_price(sqft, bedrooms, house_age, condition, year_sold, interest_rate, region, sub_type, style, has_garage, has_pool): if not model_loaded_successfully: raise gr.Error("Model is not loaded. Please check the Space logs for errors.") # --- NEW: Input Validation Guardrails --- # Before we do anything, we check if the user's inputs are reasonable. # If not, we stop and send back a helpful error message. if not (300 <= sqft <= 30000): raise gr.Error(f"Input Error: Square Footage must be between 300 and 30,000. You entered {sqft}.") if not (1 <= bedrooms <= 20): raise gr.Error(f"Input Error: Number of Bedrooms must be between 1 and 20. You entered {bedrooms}.") if not (0 <= house_age <= 200): raise gr.Error(f"Input Error: House Age must be between 0 and 200. You entered {house_age}.") if not (2000 <= year_sold <= 2030): raise gr.Error(f"Input Error: Year Sold must be between 2000 and 2030. You entered {year_sold}.") if not (0 <= interest_rate <= 25): raise gr.Error(f"Input Error: Interest Rate must be between 0 and 25. You entered {interest_rate}.") # If all checks pass, we proceed with the prediction. input_data = { 'SquareFootage': sqft, 'Bedrooms': bedrooms, 'HouseAge': house_age, 'PropertyCondition': condition, 'HasGarage': has_garage, 'HasPool': has_pool, 'YearSold': year_sold, 'InterestRate': interest_rate, 'Region': region, 'SubType': sub_type, 'ArchitecturalStyle': style } input_df = pd.DataFrame([input_data]) input_processed = pd.get_dummies(input_df) final_input = input_processed.reindex(columns=model_columns, fill_value=0) predicted_price = model.predict(final_input)[0] return f"${predicted_price:,.0f}" # --- Create the Gradio Interface with better UI limits --- demo = gr.Interface( fn=predict_price, inputs=[ # --- NEW: Added minimum and maximum values to the UI components --- gr.Number(label="Square Footage", value=2500, minimum=300, maximum=30000), gr.Number(label="Bedrooms", value=4, minimum=1, maximum=20), gr.Number(label="House Age (years)", value=15, minimum=0, maximum=200), gr.Slider(label="Property Condition", minimum=1, maximum=10, step=1, value=8), gr.Number(label="Year Sold", value=2024, minimum=2000, maximum=2030), gr.Number(label="Interest Rate (%)", value=5.5, minimum=0, maximum=25), gr.Radio(['Sunbelt', 'Pacific Northwest', 'Rust Belt', 'New England', 'Mountain West'], label="Region", value="Sunbelt"), gr.Radio(['Urban', 'Suburban', 'Rural', 'Historic District'], label="Sub-Type", value="Suburban"), gr.Radio(['Modern', 'Ranch', 'Colonial', 'Craftsman', 'Victorian'], label="Architectural Style", value="Colonial"), gr.Checkbox(label="Has Garage?", value=True), gr.Checkbox(label="Has Pool?", value=False) ], outputs=gr.Textbox(label="Predicted Price"), title="AI House Price Predictor", description="Describe a property, and our AI will estimate its market value. This AI was trained on a 9.2GB simulated dataset and is hosted on Hugging Face." ) # Launch the app demo.launch()