dibend commited on
Commit
bf8aa5a
1 Parent(s): ae905a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -8
app.py CHANGED
@@ -4,8 +4,9 @@ import plotly.graph_objects as go
4
  from sklearn.linear_model import LinearRegression
5
  import numpy as np
6
  from pandas.tseries.offsets import MonthEnd
 
7
 
8
- def plot_and_predict(zip, start_date, prediction_months):
9
  # Read and process the real estate data from Zillow
10
  df = pd.read_csv('https://files.zillowstatic.com/research/public_csvs/zhvi/Zip_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv')
11
  df = df[df['RegionName'] == int(zip)]
@@ -14,7 +15,8 @@ def plot_and_predict(zip, start_date, prediction_months):
14
  df.columns = ['Date', 'Price']
15
  df['Date'] = pd.to_datetime(df['Date'])
16
 
17
- # Filter data based on start date
 
18
  start_date = pd.to_datetime(start_date)
19
  df = df[df['Date'] >= start_date]
20
 
@@ -58,16 +60,30 @@ def plot_and_predict(zip, start_date, prediction_months):
58
 
59
  return fig
60
 
61
- # Gradio interface
 
 
 
 
 
 
 
 
 
 
62
  interface = gr.Interface(
63
  fn=plot_and_predict,
64
  inputs=[
65
- gr.Textbox(label="ZIP Code"),
66
- gr.Textbox(label="Start Date (YYYY-MM-DD)", placeholder="YYYY-MM-DD"),
67
- gr.Slider(minimum=1, maximum=60, step=1, label="Prediction Months"),
 
68
  ],
69
- outputs="plot"
 
 
 
70
  )
71
 
72
  # Launch the app
73
- interface.launch()
 
4
  from sklearn.linear_model import LinearRegression
5
  import numpy as np
6
  from pandas.tseries.offsets import MonthEnd
7
+ import datetime
8
 
9
+ def plot_and_predict(zip, start_year, start_month, prediction_months):
10
  # Read and process the real estate data from Zillow
11
  df = pd.read_csv('https://files.zillowstatic.com/research/public_csvs/zhvi/Zip_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv')
12
  df = df[df['RegionName'] == int(zip)]
 
15
  df.columns = ['Date', 'Price']
16
  df['Date'] = pd.to_datetime(df['Date'])
17
 
18
+ # Combine year and month into a start date and filter data based on it
19
+ start_date = f"{start_year}-{start_month:02d}-01"
20
  start_date = pd.to_datetime(start_date)
21
  df = df[df['Date'] >= start_date]
22
 
 
60
 
61
  return fig
62
 
63
+ # Custom validation functions
64
+ def validate_zip(zip_code):
65
+ if not zip_code.isdigit() or len(zip_code) != 5:
66
+ return "Please enter a valid 5-digit ZIP code."
67
+
68
+ def validate_year(year):
69
+ current_year = datetime.datetime.now().year
70
+ if not year.isdigit() or not (2000 <= int(year) <= current_year):
71
+ return f"Please enter a valid year between 2000 and {current_year}."
72
+
73
+ # Gradio interface with updated inputs and validations
74
  interface = gr.Interface(
75
  fn=plot_and_predict,
76
  inputs=[
77
+ gr.Textbox(label="ZIP Code", placeholder="e.g., 90210", validation=validate_zip),
78
+ gr.Textbox(label="Start Year", placeholder="e.g., 2020", validation=validate_year),
79
+ gr.Dropdown(label="Start Month", choices=[str(i) for i in range(1, 13)], placeholder="Select Month"),
80
+ gr.Slider(minimum=1, maximum=60, step=1, label="Prediction Months", default=12),
81
  ],
82
+ outputs="plot",
83
+ layout="vertical",
84
+ title="Real Estate Price Predictor",
85
+ description="Enter a ZIP code, start year, start month, and the number of months for price prediction."
86
  )
87
 
88
  # Launch the app
89
+ interface.launch(debug=True)