Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,19 @@ 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,11 +28,13 @@ def plot_and_predict(zip, start_year, start_month, prediction_months):
|
|
15 |
df.columns = ['Date', 'Price']
|
16 |
df['Date'] = pd.to_datetime(df['Date'])
|
17 |
|
18 |
-
#
|
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 |
|
|
|
|
|
|
|
|
|
23 |
# Train linear regression model
|
24 |
df['MonthsSinceStart'] = np.arange(len(df))
|
25 |
X = df['MonthsSinceStart'].values.reshape(-1, 1)
|
@@ -60,22 +75,12 @@ def plot_and_predict(zip, start_year, start_month, prediction_months):
|
|
60 |
|
61 |
return fig
|
62 |
|
63 |
-
#
|
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"
|
78 |
-
gr.Textbox(label="Start Year", placeholder="e.g., 2020"
|
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 |
],
|
|
|
7 |
import datetime
|
8 |
|
9 |
def plot_and_predict(zip, start_year, start_month, prediction_months):
|
10 |
+
# Input validation for ZIP code
|
11 |
+
if not zip.isdigit() or len(zip) != 5:
|
12 |
+
return "Error: Please enter a valid 5-digit ZIP code."
|
13 |
+
|
14 |
+
# Input validation for start year
|
15 |
+
current_year = datetime.datetime.now().year
|
16 |
+
if not start_year.isdigit() or not (2000 <= int(start_year) <= current_year):
|
17 |
+
return f"Error: Please enter a valid year between 2000 and {current_year}."
|
18 |
+
|
19 |
+
# Combine year and month into a start date
|
20 |
+
start_date = f"{start_year}-{start_month:02d}-01"
|
21 |
+
start_date = pd.to_datetime(start_date)
|
22 |
+
|
23 |
# Read and process the real estate data from Zillow
|
24 |
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')
|
25 |
df = df[df['RegionName'] == int(zip)]
|
|
|
28 |
df.columns = ['Date', 'Price']
|
29 |
df['Date'] = pd.to_datetime(df['Date'])
|
30 |
|
31 |
+
# Filter data based on start date
|
|
|
|
|
32 |
df = df[df['Date'] >= start_date]
|
33 |
|
34 |
+
# Check for empty dataframe
|
35 |
+
if df.empty:
|
36 |
+
return "Error: No data found for the provided ZIP code or start year/month. Please check your inputs."
|
37 |
+
|
38 |
# Train linear regression model
|
39 |
df['MonthsSinceStart'] = np.arange(len(df))
|
40 |
X = df['MonthsSinceStart'].values.reshape(-1, 1)
|
|
|
75 |
|
76 |
return fig
|
77 |
|
78 |
+
# Gradio interface with updated inputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
interface = gr.Interface(
|
80 |
fn=plot_and_predict,
|
81 |
inputs=[
|
82 |
+
gr.Textbox(label="ZIP Code", placeholder="e.g., 90210"),
|
83 |
+
gr.Textbox(label="Start Year", placeholder="e.g., 2020"),
|
84 |
gr.Dropdown(label="Start Month", choices=[str(i) for i in range(1, 13)], placeholder="Select Month"),
|
85 |
gr.Slider(minimum=1, maximum=60, step=1, label="Prediction Months", default=12),
|
86 |
],
|