File size: 2,338 Bytes
6b63915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# !pip install gradio ipywidgets
import pandas as pd
import gradio as gr
import joblib
import numpy as np

# "Artifacts"
pipeline = joblib.load("pipeline.joblib")
label_pipeline = joblib.load("label_pipeline.joblib")
cities = joblib.load("cities.joblib")
classes = joblib.load("classes.joblib")

def predict(city, location, area, bedrooms, baths):
    sample = dict()
    sample["city"] = city
    sample["location"] = location
    sample["area"] = area # Column names matching feature names
    sample["bedrooms"] = bedrooms
    sample["baths"] = baths

    sample = pd.DataFrame([sample])
    y_pred = pipeline.predict_proba(sample)[0]
    y_pred = dict(zip(classes, y_pred))
    return y_pred

# https://www.gradio.app/guides
with gr.Blocks() as demo: #value คือ ค่าเริ่มต้น
    city = gr.Dropdown(cities, value=cities[0], label="City")
    location = gr.Textbox(label="Location", placeholder="E.g. Bangkhen")
    area = gr.Number(label="Area", value=0.5, minimum=0.5, step=0.5)
    bedrooms = gr.Slider(value=1, label="Bedrooms", minimum=0, maximum=10, step=1)
    baths = gr.Slider(value=1, label="Baths", minimum=0, maximum=10, step=1)
   
    # with gr.Row():
    #     city_init = np.random.choice(cities)
    #     city = gr.Dropdown(cities, value=city_init, label="City")
        
    #     location = gr.Textbox(label="Location", placeholder="E.g. Bangken")
    
    # with gr.Row():
    #     area_init = np.random.choice(np.arange(0, 50, 0.5))
    #     area = gr.Number(label="Area", value=area_init, minimum=0.5, step=0.5)

    #     bedrooms_init = np.random.choice(np.arange(0, 10, 1))
    #     bedrooms = gr.Slider(value=bedrooms_init, label="Bedrooms", minimum=0, maximum=10, step=1)
        
    #     baths_init = np.random.choice(np.arange(0, 10, 1))
        # baths = gr.Slider(value=baths_init, label="Baths", minimum=0, maximum=10, step=1)
    
    predict_btn = gr.Button("Predict", variant="primary")
    price = gr.Label(label="Price")

    inputs = [city, location, area, bedrooms, baths]
    outputs = [price]
    
    predict_btn.click(predict, inputs=inputs, outputs=outputs)

if __name__ == "__main__":
    demo.launch() # Local machine only
    # demo.launch(server_name="0.0.0.0") # LAN access to local machine
    # demo.launch(share=True) # Public access to local machine