dibend commited on
Commit
b5c06f5
1 Parent(s): efdd0c3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import plotly.graph_objects as go
4
+ from sklearn.linear_model import LinearRegression
5
+ from datetime import timedelta
6
+
7
+ def plot_and_predict(zip, prediction_days):
8
+ # Read and process the real estate data from Zillow
9
+ 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')
10
+ df = df[df['RegionName'] == int(zip)]
11
+ df = df.loc[:, '2000-01-31':]
12
+ df = df.T.reset_index()
13
+ df.columns = ['Date', 'Price']
14
+ df['Date'] = pd.to_datetime(df['Date'])
15
+
16
+ # Train linear regression model
17
+ df['Timestamp'] = (df['Date'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1D')
18
+ X = df['Timestamp'].values.reshape(-1, 1)
19
+ y = df['Price'].values
20
+ model = LinearRegression()
21
+ model.fit(X, y)
22
+
23
+ # Predict future prices
24
+ last_timestamp = df['Timestamp'].iloc[-1]
25
+ future_timestamps = [last_timestamp + i for i in range(1, prediction_days + 1)]
26
+ predicted_prices = model.predict(pd.np.array(future_timestamps).reshape(-1, 1))
27
+
28
+ # Prepare data for plotting
29
+ historical_prices_trace = go.Scatter(
30
+ x=df['Date'],
31
+ y=df['Price'],
32
+ mode="lines",
33
+ name="Historical Prices"
34
+ )
35
+ future_dates = [df['Date'].iloc[-1] + timedelta(days=i) for i in range(1, prediction_days + 1)]
36
+ predicted_prices_trace = go.Scatter(
37
+ x=future_dates,
38
+ y=predicted_prices,
39
+ mode="lines",
40
+ name="Predicted Prices"
41
+ )
42
+
43
+ # Plot data
44
+ fig = go.Figure()
45
+ fig.add_trace(historical_prices_trace)
46
+ fig.add_trace(predicted_prices_trace)
47
+ fig.update_layout(
48
+ title=f"Real Estate Price Prediction for Zip Code {zip}",
49
+ xaxis_title="Date",
50
+ yaxis_title="Price",
51
+ legend_title_text="Data"
52
+ )
53
+
54
+ return fig
55
+
56
+ # Gradio interface
57
+ interface = gr.Interface(
58
+ fn=plot_and_predict,
59
+ inputs=[
60
+ gr.Textbox(label="ZIP Code"),
61
+ gr.Slider(minimum=1, maximum=365, step=1, label="Prediction Days"),
62
+ ],
63
+ outputs="plot"
64
+ )
65
+
66
+ # Launch the app
67
+ interface.launch(debug=True)