szili2011 commited on
Commit
dd24533
·
verified ·
1 Parent(s): a1bc73a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -27
app.py CHANGED
@@ -1,39 +1,29 @@
1
- # app.py - FINAL, GUARANTEED VERSION
2
- # This script explicitly downloads the model files from the Hub before loading them.
3
- # This removes all guesswork about file paths.
4
 
5
  import gradio as gr
6
  import joblib
7
  import pandas as pd
8
  import os
9
- import subprocess # We need this to run system commands like 'wget'
10
 
11
- # --- STEP 1: Download the Model Files from the Hub ---
12
- # This happens only ONCE when the Space boots up.
13
-
14
- # The direct download URLs for your files on the Hub
15
  MODEL_URL = "https://huggingface.co/szili2011/ai-house-price-predictor/resolve/main/housing_model.joblib"
16
  COLUMNS_URL = "https://huggingface.co/szili2011/ai-house-price-predictor/resolve/main/model_columns.joblib"
17
-
18
- # The names we will save the files as locally in our Space
19
  MODEL_LOCAL_PATH = "housing_model.joblib"
20
  COLUMNS_LOCAL_PATH = "model_columns.joblib"
21
 
22
- # Use the 'wget' command to download the files. This is a robust method.
23
- print("--- Downloading Model Files ---")
24
- subprocess.run(["wget", "-O", MODEL_LOCAL_PATH, MODEL_URL])
25
- subprocess.run(["wget", "-O", COLUMNS_LOCAL_PATH, COLUMNS_URL])
26
- print("--- Download Complete ---")
27
-
28
 
29
- # --- STEP 2: Load the Now-Local Model and Columns ---
30
  try:
31
  print(f"Loading model from local path: '{MODEL_LOCAL_PATH}'")
32
  model = joblib.load(MODEL_LOCAL_PATH)
33
-
34
  print(f"Loading columns from local path: '{COLUMNS_LOCAL_PATH}'")
35
  model_columns = joblib.load(COLUMNS_LOCAL_PATH)
36
-
37
  print("✅ Model and columns loaded successfully.")
38
  model_loaded_successfully = True
39
  except Exception as e:
@@ -41,11 +31,26 @@ except Exception as e:
41
  model_loaded_successfully = False
42
 
43
 
44
- # --- Core prediction function ---
45
  def predict_price(sqft, bedrooms, house_age, condition, year_sold, interest_rate, region, sub_type, style, has_garage, has_pool):
46
  if not model_loaded_successfully:
47
- raise gr.Error("Model failed to load. Please check the Space logs for errors.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
 
49
  input_data = {
50
  'SquareFootage': sqft, 'Bedrooms': bedrooms, 'HouseAge': house_age,
51
  'PropertyCondition': condition, 'HasGarage': has_garage, 'HasPool': has_pool,
@@ -61,16 +66,17 @@ def predict_price(sqft, bedrooms, house_age, condition, year_sold, interest_rate
61
 
62
  return f"${predicted_price:,.0f}"
63
 
64
- # --- Create the Gradio Interface ---
65
  demo = gr.Interface(
66
  fn=predict_price,
67
  inputs=[
68
- gr.Number(label="Square Footage", value=2500),
69
- gr.Number(label="Bedrooms", value=4),
70
- gr.Number(label="House Age (years)", value=15),
 
71
  gr.Slider(label="Property Condition", minimum=1, maximum=10, step=1, value=8),
72
- gr.Number(label="Year Sold", value=2024),
73
- gr.Number(label="Interest Rate (%)", value=5.5),
74
  gr.Radio(['Sunbelt', 'Pacific Northwest', 'Rust Belt', 'New England', 'Mountain West'], label="Region", value="Sunbelt"),
75
  gr.Radio(['Urban', 'Suburban', 'Rural', 'Historic District'], label="Sub-Type", value="Suburban"),
76
  gr.Radio(['Modern', 'Ranch', 'Colonial', 'Craftsman', 'Victorian'], label="Architectural Style", value="Colonial"),
 
1
+ # app.py - FINAL PROFESSIONAL version with input validation and guardrails
 
 
2
 
3
  import gradio as gr
4
  import joblib
5
  import pandas as pd
6
  import os
7
+ import subprocess
8
 
9
+ # --- STEP 1: Download the Model Files from the Hub (same as before) ---
 
 
 
10
  MODEL_URL = "https://huggingface.co/szili2011/ai-house-price-predictor/resolve/main/housing_model.joblib"
11
  COLUMNS_URL = "https://huggingface.co/szili2011/ai-house-price-predictor/resolve/main/model_columns.joblib"
 
 
12
  MODEL_LOCAL_PATH = "housing_model.joblib"
13
  COLUMNS_LOCAL_PATH = "model_columns.joblib"
14
 
15
+ if not os.path.exists(MODEL_LOCAL_PATH):
16
+ print("--- Downloading Model Files ---")
17
+ subprocess.run(["wget", "-O", MODEL_LOCAL_PATH, MODEL_URL])
18
+ subprocess.run(["wget", "-O", COLUMNS_LOCAL_PATH, COLUMNS_URL])
19
+ print("--- Download Complete ---")
 
20
 
21
+ # --- STEP 2: Load the Now-Local Model and Columns (same as before) ---
22
  try:
23
  print(f"Loading model from local path: '{MODEL_LOCAL_PATH}'")
24
  model = joblib.load(MODEL_LOCAL_PATH)
 
25
  print(f"Loading columns from local path: '{COLUMNS_LOCAL_PATH}'")
26
  model_columns = joblib.load(COLUMNS_LOCAL_PATH)
 
27
  print("✅ Model and columns loaded successfully.")
28
  model_loaded_successfully = True
29
  except Exception as e:
 
31
  model_loaded_successfully = False
32
 
33
 
34
+ # --- This is the core prediction function with the new validation logic ---
35
  def predict_price(sqft, bedrooms, house_age, condition, year_sold, interest_rate, region, sub_type, style, has_garage, has_pool):
36
  if not model_loaded_successfully:
37
+ raise gr.Error("Model is not loaded. Please check the Space logs for errors.")
38
+
39
+ # --- NEW: Input Validation Guardrails ---
40
+ # Before we do anything, we check if the user's inputs are reasonable.
41
+ # If not, we stop and send back a helpful error message.
42
+ if not (300 <= sqft <= 30000):
43
+ raise gr.Error(f"Input Error: Square Footage must be between 300 and 30,000. You entered {sqft}.")
44
+ if not (1 <= bedrooms <= 20):
45
+ raise gr.Error(f"Input Error: Number of Bedrooms must be between 1 and 20. You entered {bedrooms}.")
46
+ if not (0 <= house_age <= 200):
47
+ raise gr.Error(f"Input Error: House Age must be between 0 and 200. You entered {house_age}.")
48
+ if not (2000 <= year_sold <= 2030):
49
+ raise gr.Error(f"Input Error: Year Sold must be between 2000 and 2030. You entered {year_sold}.")
50
+ if not (0 <= interest_rate <= 25):
51
+ raise gr.Error(f"Input Error: Interest Rate must be between 0 and 25. You entered {interest_rate}.")
52
 
53
+ # If all checks pass, we proceed with the prediction.
54
  input_data = {
55
  'SquareFootage': sqft, 'Bedrooms': bedrooms, 'HouseAge': house_age,
56
  'PropertyCondition': condition, 'HasGarage': has_garage, 'HasPool': has_pool,
 
66
 
67
  return f"${predicted_price:,.0f}"
68
 
69
+ # --- Create the Gradio Interface with better UI limits ---
70
  demo = gr.Interface(
71
  fn=predict_price,
72
  inputs=[
73
+ # --- NEW: Added minimum and maximum values to the UI components ---
74
+ gr.Number(label="Square Footage", value=2500, minimum=300, maximum=30000),
75
+ gr.Number(label="Bedrooms", value=4, minimum=1, maximum=20),
76
+ gr.Number(label="House Age (years)", value=15, minimum=0, maximum=200),
77
  gr.Slider(label="Property Condition", minimum=1, maximum=10, step=1, value=8),
78
+ gr.Number(label="Year Sold", value=2024, minimum=2000, maximum=2030),
79
+ gr.Number(label="Interest Rate (%)", value=5.5, minimum=0, maximum=25),
80
  gr.Radio(['Sunbelt', 'Pacific Northwest', 'Rust Belt', 'New England', 'Mountain West'], label="Region", value="Sunbelt"),
81
  gr.Radio(['Urban', 'Suburban', 'Rural', 'Historic District'], label="Sub-Type", value="Suburban"),
82
  gr.Radio(['Modern', 'Ranch', 'Colonial', 'Craftsman', 'Victorian'], label="Architectural Style", value="Colonial"),