PyNaiD / app.py
Pattr's picture
Update app.py
a2bb28b verified
raw
history blame
2.71 kB
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()