|
import gradio as gr |
|
import pandas as pd |
|
import plotly.graph_objects as go |
|
from sklearn.linear_model import LinearRegression |
|
from datetime import timedelta |
|
|
|
def plot_and_predict(zip, prediction_days): |
|
|
|
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') |
|
df = df[df['RegionName'] == int(zip)] |
|
df = df.loc[:, '2000-01-31':] |
|
df = df.T.reset_index() |
|
df.columns = ['Date', 'Price'] |
|
df['Date'] = pd.to_datetime(df['Date']) |
|
|
|
|
|
df['Timestamp'] = (df['Date'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1D') |
|
X = df['Timestamp'].values.reshape(-1, 1) |
|
y = df['Price'].values |
|
model = LinearRegression() |
|
model.fit(X, y) |
|
|
|
|
|
last_timestamp = df['Timestamp'].iloc[-1] |
|
future_timestamps = [last_timestamp + i for i in range(1, prediction_days + 1)] |
|
predicted_prices = model.predict(pd.np.array(future_timestamps).reshape(-1, 1)) |
|
|
|
|
|
historical_prices_trace = go.Scatter( |
|
x=df['Date'], |
|
y=df['Price'], |
|
mode="lines", |
|
name="Historical Prices" |
|
) |
|
future_dates = [df['Date'].iloc[-1] + timedelta(days=i) for i in range(1, prediction_days + 1)] |
|
predicted_prices_trace = go.Scatter( |
|
x=future_dates, |
|
y=predicted_prices, |
|
mode="lines", |
|
name="Predicted Prices" |
|
) |
|
|
|
|
|
fig = go.Figure() |
|
fig.add_trace(historical_prices_trace) |
|
fig.add_trace(predicted_prices_trace) |
|
fig.update_layout( |
|
title=f"Real Estate Price Prediction for Zip Code {zip}", |
|
xaxis_title="Date", |
|
yaxis_title="Price", |
|
legend_title_text="Data" |
|
) |
|
|
|
return fig |
|
|
|
|
|
interface = gr.Interface( |
|
fn=plot_and_predict, |
|
inputs=[ |
|
gr.Textbox(label="ZIP Code"), |
|
gr.Slider(minimum=1, maximum=365, step=1, label="Prediction Days"), |
|
], |
|
outputs="plot" |
|
) |
|
|
|
|
|
interface.launch(debug=True) |