File size: 3,835 Bytes
b35d74a
82c9f5e
 
 
b35d74a
 
 
 
1be00cd
7128315
b4a0f73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52fc559
 
 
 
 
 
 
 
 
 
5123ae9
9f3eed6
 
52fc559
 
 
b4a0f73
b5b79e5
9f3eed6
a2bb28b
 
 
 
 
 
 
1be00cd
82aff7d
52fc559
578ba67
 
52fc559
82aff7d
a2bb28b
1be00cd
a2bb28b
 
c4a845b
a2bb28b
82c9f5e
a2bb28b
 
 
52fc559
a2bb28b
b5b79e5
 
9f3eed6
52fc559
a2bb28b
 
b4a0f73
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

import gradio as gr
import pandas as pd
import numpy as np
from prophet import Prophet
from mysql import connector
import json
import random
from prophet.serialize import model_from_json

def get_data():
    _conn = connector.connect(
        host='110.238.111.32',
        user = 'outsider',
        password='Hack2024',
        database = 'TheSimp'
    )
    _cursor = _conn.cursor()
    _query = "SELECT name , province ,a_type ,genre ,close ,hour ,link FROM places"
    _cursor.execute(_query)
    _data = _cursor.fetchall()
    _cursor.close()
    _conn.close()
    _df = pd.DataFrame(_data,columns=['name','province','a_type','genre','close','hour','link'])
    return _df
def get_dataset():
    _conn = connector.connect(
        host='110.238.111.32',
        user = 'outsider',
        password='Hack2024',
        database = 'TheSimp'
    )
    _cursor = _conn.cursor()
    _query = "SELECT id , ds ,a_temp ,m_temp ,n_temp ,y ,a_pres ,m_pres ,a_ws ,m_ws ,a_humi ,m_humi ,n_humi ,a_vis flat,m_vis ,n_vis  FROM weather_new"
    _cursor.execute(_query)
    _data = _cursor.fetchall()
    _cursor.close()
    _conn.close()
    _df = pd.DataFrame(_data,columns=['id','ds','a_temp','m_temp','n_temp','y','a_pres','m_pres','a_ws','m_ws','a_humi','m_humi','n_humi' ,'a_vis','m_vis' ,'n_vis']).drop('id',axis=1)
    return _df
    



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 = get_data()
    places.replace({'indoor ': 'indoor', 'outdoor ': 'outdoor'}, inplace=True)
    places = places[(places['province'] == province) & (places['a_type'] == activity) & (places['genre'] == purpose.lower())]
    
    
    random_idx = np.random.randint(0, len(places))
    place_name = places.iloc[random_idx]['name']
    close_day = places.iloc[random_idx]['close']
    open_hour = places.iloc[random_idx]['hour']
    
    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()