|
|
|
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() |
|
|