File size: 1,861 Bytes
b4d95ba
 
 
 
 
a3d8b0e
b4d95ba
 
 
 
 
 
 
 
 
 
 
 
 
555f215
b4d95ba
555f215
79aa5cd
71f866a
b4d95ba
71f866a
671b280
a1c4ba9
 
b4d95ba
 
555f215
b4d95ba
555f215
b4d95ba
b9fa5f7
 
b4d95ba
b9fa5f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import pickle
import tensorflow as tf
import gradio as gr
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler

data_heading = ['longitude', 'latitude', 'housing_median_age', 'total_rooms',
       'total_bedrooms', 'population', 'households', 'median_income',
       'median_house_value']

# Model and scaler loading
with open("./model/scaler_sklearn.pkl", "rb") as f:
    scaler = pickle.load(f)
loaded_model = tf.keras.saving.load_model('./model/house_value_model.keras')

def test_ml_model(longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, median_house_value):

    df_test = pd.DataFrame(data=[[longitude, latitude, housing_median_age,
                            total_rooms, total_bedrooms, population,
                            households, median_income, median_house_value]], columns=data_heading)
    df_test_norm = pd.DataFrame(scaler.fit_transform(df_test), columns=data_heading)
    features = {name:np.array(value) for name, value in df_test_norm.items()}
    
    result = loaded_model.predict(features)
    if result['dense'][0][0] > 0.5:
        return 'Prediction: Houses in this neighborhood are above average price.'
    return 'Prediction: Houses in this neighborhood are below average price.'

demo = gr.Interface(fn=test_ml_model,
                    inputs=[gr.Number(value=0.0), gr.Number(value=0.0), gr.Number(value=0.0),
                            gr.Number(value=0.0), gr.Number(value=0.0), gr.Number(value=0.0),
                            gr.Number(value=0.0), gr.Number(value=0.0), gr.Number(value=0.0)], 
                    outputs="text",
                    description="It will help to classifiy the houses in this neighborhood above a avaerage price or not.",
                    title='Classifier')
    
demo.launch()