File size: 2,706 Bytes
82c9f5e
 
 
1be00cd
7128315
a2bb28b
 
 
 
 
 
 
1be00cd
82aff7d
c4a845b
a2bb28b
82aff7d
a2bb28b
1be00cd
a2bb28b
 
c4a845b
a2bb28b
82c9f5e
a2bb28b
 
 
82c9f5e
a2bb28b
c4a845b
a2bb28b
 
 
 
 
 
 
 
 
 
 
 
82c9f5e
 
a2bb28b
 
 
 
 
 
 
 
82c9f5e
c4a845b
a2bb28b
 
 
 
 
82c9f5e
a2bb28b
c4a845b
a2bb28b
82c9f5e
 
a2bb28b
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
63
64
65
66
67
import gradio as gr
import pandas as pd
import numpy as np
from prophet.serialize import model_from_json

province_mapping = {
    'Bangkok': 'กรุงเทพฯ',
    'Nakohn Pathom': 'นครปฐม',
    'Pathum Thani': 'ปทุมธานี',
    'Nakohn Nayok': 'นครนายก',
    'Nonthaburi': 'นนทบุรี',
    'Samut Songkhram': 'สมุทรสงคราม'
}

with open('prophet_model.json', 'r') as fin:
    prophet_model = model_from_json(json.load(fin))

def will_rain(year, month, date):
    _date = pd.to_datetime(f'{year}-{month}-{date}')
    _df = pd.DataFrame({'ds': [_date]})
    _prediction = prophet_model.predict(_df)
    _prediction = float(_prediction['yhat'])
    return _prediction >= 0.5

def get_advice(province, activity, purpose, year, month, date):
    is_rain = will_rain(year, month, date)
    activity = 'indoor' if is_rain else activity.lower()
    
    province = province_mapping[province]
    
    places = pd.read_csv('Places.csv')
    places.replace({'indoor ': 'indoor', 'outdoor ': 'outdoor'}, inplace=True)
    places = places[(places['จังหวัด'] == province) & (places['indoor/outdoor'] == activity) & (places['หมวดหมู่'] == purpose.lower())]
    
    random_idx = np.random.randint(0, len(places))
    place_name = places.iloc[random_idx]['ที่เที่ยว']
    close_day = places.iloc[random_idx]['ปิดวัน']
    open_hour = places.iloc[random_idx]['เวลา']
    
    advice = f"It might rain on {year}-{month}-{date}, so we suggest you to go indoor place such as {place_name}." if is_rain else f"It might not rain on {year}-{month}-{date}, so we suggest you to go to place such as {place_name}."
    
    return advice, place_name, close_day, open_hour

iface = gr.Interface(
    fn=get_advice,
    inputs=[
        gr.Dropdown(["Bangkok", 'Nakohn Pathom', 'Pathum Thani', 'Nakohn Nayok', 'Nonthaburi', 'Samut Songkhram'], label="Province",),
        gr.Dropdown(["indoor", "outdoor"], label="Activity"),
        gr.Dropdown(["shopping", "relax", 'education', 'culture', 'nature'], label="Purpose"),
        gr.Dropdown([2024], label="Year"),
        gr.Dropdown([i for i in range(1, 13)], label="Month"),
        gr.Dropdown([i for i in range(1, 32)], label="Date"),
    ],
    outputs=[
        gr.components.Textbox(label='Advice'),
        gr.components.Textbox(label="Place Name"),
        gr.components.Textbox(label="Close day"),
        gr.components.Textbox(label="Open hour"),
    ],
    live=True,
    title="Right place, Right day",
    description="Get the weather forecast for a place you would like to go.",
    theme="default"
)

iface.launch()