Jurk06 commited on
Commit
9f2db9b
·
verified ·
1 Parent(s): a2e0e2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -27
app.py CHANGED
@@ -1,19 +1,17 @@
1
- # prompt: make a web app for house price using Deep Learning ang and dashbording using Gradio
2
- #!pip install scikit-learn --upgrade
3
  import pandas as pd
4
  from sklearn.model_selection import train_test_split
5
  from sklearn.preprocessing import StandardScaler
6
  from sklearn.neural_network import MLPRegressor
7
  from sklearn.metrics import mean_squared_error
8
  import gradio as gr
 
9
 
10
- # ... (Rest of your code)
11
  # Load the dataset
12
  df = pd.read_csv('california_housing_train.csv')
13
 
14
  # Select features and target
15
  features = df[['longitude', 'latitude', 'housing_median_age', 'total_rooms',
16
- 'total_bedrooms', 'population', 'households', 'median_income']]
17
  target = df['median_house_value']
18
 
19
  # Split the data
@@ -33,29 +31,56 @@ predictions = model.predict(X_test_scaled)
33
  mse = mean_squared_error(y_test, predictions)
34
  print(f'Mean Squared Error: {mse}')
35
 
36
- # Create Gradio interface
37
  def predict_house_price(longitude, latitude, housing_median_age, total_rooms,
38
- total_bedrooms, population, households, median_income):
39
- input_data = scaler.transform([[longitude, latitude, housing_median_age, total_rooms,
40
- total_bedrooms, population, households, median_income]])
41
- prediction = model.predict(input_data)[0]
42
- return prediction
43
-
44
- iface = gr.Interface(
45
- fn=predict_house_price,
46
- inputs=[
47
- gr.Number(label="Longitude"), # Use gr.Number directly
48
- gr.Number(label="Latitude"),
49
- gr.Number(label="Housing Median Age"),
50
- gr.Number(label="Total Rooms"),
51
- gr.Number(label="Total Bedrooms"),
52
- gr.Number(label="Population"),
53
- gr.Number(label="Households"),
54
- gr.Number(label="Median Income"),
55
- ],
56
- outputs="text",
57
- title="House Price Prediction",
58
- description="Enter the features to get the predicted house price."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  )
60
 
61
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import pandas as pd
2
  from sklearn.model_selection import train_test_split
3
  from sklearn.preprocessing import StandardScaler
4
  from sklearn.neural_network import MLPRegressor
5
  from sklearn.metrics import mean_squared_error
6
  import gradio as gr
7
+ import plotly.express as px
8
 
 
9
  # Load the dataset
10
  df = pd.read_csv('california_housing_train.csv')
11
 
12
  # Select features and target
13
  features = df[['longitude', 'latitude', 'housing_median_age', 'total_rooms',
14
+ 'total_bedrooms', 'population', 'households', 'median_income']]
15
  target = df['median_house_value']
16
 
17
  # Split the data
 
31
  mse = mean_squared_error(y_test, predictions)
32
  print(f'Mean Squared Error: {mse}')
33
 
34
+ # Create prediction function
35
  def predict_house_price(longitude, latitude, housing_median_age, total_rooms,
36
+ total_bedrooms, population, households, median_income):
37
+ input_data = scaler.transform([[longitude, latitude, housing_median_age, total_rooms,
38
+ total_bedrooms, population, households, median_income]])
39
+ prediction = model.predict(input_data)[0]
40
+ return prediction
41
+
42
+ # Create dashboard function
43
+ def create_dashboard():
44
+ fig1 = px.scatter(df, x='longitude', y='latitude', color='median_house_value',
45
+ title="House Prices by Location",
46
+ labels={'longitude': 'Longitude', 'latitude': 'Latitude', 'median_house_value': 'House Value'})
47
+
48
+ fig2 = px.histogram(df, x='median_income', nbins=30, title="Distribution of Median Income",
49
+ labels={'median_income': 'Median Income'})
50
+
51
+ fig3 = px.histogram(df, x='housing_median_age', nbins=30, title="Distribution of Housing Median Age",
52
+ labels={'housing_median_age': 'Housing Median Age'})
53
+
54
+ return fig1, fig2, fig3
55
+
56
+ # Gradio interface for prediction
57
+ iface_predict = gr.Interface(
58
+ fn=predict_house_price,
59
+ inputs=[
60
+ gr.Number(label="Longitude"), # Use gr.Number directly
61
+ gr.Number(label="Latitude"),
62
+ gr.Number(label="Housing Median Age"),
63
+ gr.Number(label="Total Rooms"),
64
+ gr.Number(label="Total Bedrooms"),
65
+ gr.Number(label="Population"),
66
+ gr.Number(label="Households"),
67
+ gr.Number(label="Median Income"),
68
+ ],
69
+ outputs="text",
70
+ title="House Price Prediction",
71
+ description="Enter the features to get the predicted house price."
72
  )
73
 
74
+ # Gradio interface for dashboard
75
+ iface_dashboard = gr.Interface(
76
+ fn=create_dashboard,
77
+ inputs=[],
78
+ outputs=[gr.Plot(), gr.Plot(), gr.Plot()],
79
+ title="House Price Dashboard",
80
+ description="Visualizations of the housing dataset."
81
+ )
82
+
83
+ # Launch both interfaces
84
+ iface = gr.TabbedInterface([iface_predict, iface_dashboard], ["Prediction", "Dashboard"])
85
+
86
+ iface.launch()