Pattr commited on
Commit
a2bb28b
·
verified ·
1 Parent(s): 5e9e97c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -75
app.py CHANGED
@@ -1,97 +1,66 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
- from prophet import Prophet
5
- from mysql import connector
6
- import json
7
- import random
8
  from prophet.serialize import model_from_json
9
 
10
- province_dict = {
11
- "Bangkok":"กรุงเทพฯ",
12
- 'Nakohn Pathom':'นครปฐม',
13
- 'Pathum Thani':'ปทุมธานี',
14
- 'Nakohn Nayok':'นครนายก',
15
- 'Nonthaburi':'นนทบุรี',
16
- 'Samut Songkhram':'สมุทรสงคราม'
17
  }
18
 
19
  with open('prophet_model.json', 'r') as fin:
20
- _model = model_from_json(json.load(fin))
21
 
22
- def weather_forcast(year,month,date):
23
  _date = pd.to_datetime(f'{year}-{month}-{date}')
24
-
25
- _df = pd.DataFrame({'ds':[_date]})
26
-
27
- _prediction = _model.predict(_df)
28
  _prediction = float(_prediction['yhat'])
 
29
 
30
- _israin = False if(_prediction<0.5) else True
31
- return _israin
 
32
 
33
- def get_advice(province,activity,purpose,year,month,date):
34
- _province = province_dict[province]
35
 
36
- _purpose = str(purpose).lower()
37
- _day = pd.to_datetime(f'{year}-{month}-{date}').day_name()
38
- _activity = 'indoor' if(weather_forcast(year,month,date)) else activity.lower()
39
-
40
- _places = pd.read_csv('Places.csv')
41
- _places = _places[_places['จังหวัด']==_province]
42
- _places = _places[_places['indoor/outdoor']==_activity]
43
- _places = _places[_places['หมวดหมู่']==_purpose]
44
- _places = _places[_places['ปิดวัน']!=_day]
45
-
46
- random_idx = random.randrange(0,len(_places))
47
- _advice = f'It might rain on {year}-{month}-{date} so we suggest you to go indoor place such as...' if(weather_forcast(year,month,date)) else f'It might not rain on {year}-{month}-{date} so we suggest you to go to place such as...'
48
- _place_name = _places['ที่เที่ยว'][random_idx]
49
- _place_close = _places['ปิดวัน'][random_idx]
50
- _place_hour = _places['เวลา'][random_idx]
51
- return _advice,_place_name,_place_close,_place_hour
52
-
53
-
54
 
55
  iface = gr.Interface(
56
- fn = get_advice,
57
- inputs = [
58
-
59
- gr.Dropdown(
60
- ["Bangkok",'Nakohn Pathom','Pathum Thani','Nakohn Nayok','Nonthaburi','Samut Songkhram'], label="Province", info="Will add more later!"
61
- ),
62
-
63
- gr.Dropdown(
64
- ["Indoor","Outdoor"], label="Activity"
65
- ),
66
-
67
- gr.Dropdown(
68
- ["Shopping","Relax",'Education','Culture','Nature'], label="Purpose"
69
- ),
70
-
71
- gr.Dropdown(
72
- [2024], label="Year", info="Will add more later!"
73
- ),
74
-
75
- gr.Dropdown(
76
- [1,2,3,4,5,6,7,8,9,10,11,12], label="Month"
77
- ),
78
-
79
- gr.Dropdown(
80
- [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], label="Date"
81
- )
82
-
83
  ],
84
-
85
  outputs=[
86
- gr.components.Textbox(label='Advice :'),
87
- gr.components.Textbox(label="Place Name :"),
88
- gr.components.Textbox(label="Close day :"),
89
- gr.components.Textbox(label="Open hour :")
90
- ],
91
  live=True,
92
- title="Right place , Right day",
93
  description="Get the weather forecast for a place you would like to go.",
94
- theme="default",
95
  )
96
 
97
- iface.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
  import numpy as np
 
 
 
 
4
  from prophet.serialize import model_from_json
5
 
6
+ province_mapping = {
7
+ 'Bangkok': 'กรุงเทพฯ',
8
+ 'Nakohn Pathom': 'นครปฐม',
9
+ 'Pathum Thani': 'ปทุมธานี',
10
+ 'Nakohn Nayok': 'นครนายก',
11
+ 'Nonthaburi': 'นนทบุรี',
12
+ 'Samut Songkhram': 'สมุทรสงคราม'
13
  }
14
 
15
  with open('prophet_model.json', 'r') as fin:
16
+ prophet_model = model_from_json(json.load(fin))
17
 
18
+ def will_rain(year, month, date):
19
  _date = pd.to_datetime(f'{year}-{month}-{date}')
20
+ _df = pd.DataFrame({'ds': [_date]})
21
+ _prediction = prophet_model.predict(_df)
 
 
22
  _prediction = float(_prediction['yhat'])
23
+ return _prediction >= 0.5
24
 
25
+ def get_advice(province, activity, purpose, year, month, date):
26
+ is_rain = will_rain(year, month, date)
27
+ activity = 'indoor' if is_rain else activity.lower()
28
 
29
+ province = province_mapping[province]
 
30
 
31
+ places = pd.read_csv('Places.csv')
32
+ places.replace({'indoor ': 'indoor', 'outdoor ': 'outdoor'}, inplace=True)
33
+ places = places[(places['จังหวัด'] == province) & (places['indoor/outdoor'] == activity) & (places['หมวดหมู่'] == purpose.lower())]
34
+
35
+ random_idx = np.random.randint(0, len(places))
36
+ place_name = places.iloc[random_idx]['ที่เที่ยว']
37
+ close_day = places.iloc[random_idx]['ปิดวัน']
38
+ open_hour = places.iloc[random_idx]['เวลา']
39
+
40
+ 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}."
41
+
42
+ return advice, place_name, close_day, open_hour
 
 
 
 
 
 
43
 
44
  iface = gr.Interface(
45
+ fn=get_advice,
46
+ inputs=[
47
+ gr.Dropdown(["Bangkok", 'Nakohn Pathom', 'Pathum Thani', 'Nakohn Nayok', 'Nonthaburi', 'Samut Songkhram'], label="Province",),
48
+ gr.Dropdown(["indoor", "outdoor"], label="Activity"),
49
+ gr.Dropdown(["shopping", "relax", 'education', 'culture', 'nature'], label="Purpose"),
50
+ gr.Dropdown([2024], label="Year"),
51
+ gr.Dropdown([i for i in range(1, 13)], label="Month"),
52
+ gr.Dropdown([i for i in range(1, 32)], label="Date"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  ],
 
54
  outputs=[
55
+ gr.components.Textbox(label='Advice'),
56
+ gr.components.Textbox(label="Place Name"),
57
+ gr.components.Textbox(label="Close day"),
58
+ gr.components.Textbox(label="Open hour"),
59
+ ],
60
  live=True,
61
+ title="Right place, Right day",
62
  description="Get the weather forecast for a place you would like to go.",
63
+ theme="default"
64
  )
65
 
66
+ iface.launch()