Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from prophet import Prophet
|
5 |
+
import random
|
6 |
+
df = pd.read_csv('df60')
|
7 |
+
place_df = pd.read_csv('place.csv')
|
8 |
+
Station = pd.DataFrame(np.load('Station.npy',allow_pickle=True))
|
9 |
+
def findProvince(Province):
|
10 |
+
if Province == 'Bangkok':
|
11 |
+
name = list(Station[Station[6]=='กรุงเทพมหานคร'][0])[0]
|
12 |
+
return name
|
13 |
+
|
14 |
+
def get_weather_forecast(Province,Activity,Purpose,Year,Month,Date):
|
15 |
+
name = findProvince(Province)
|
16 |
+
p_df = df.rename(columns={'Date_time':'ds',str(name):'y'})
|
17 |
+
rain_df = p_df[p_df['mode']=='Rain'][['ds','y']]
|
18 |
+
rain_model = Prophet(interval_width=0.95)
|
19 |
+
rain_model.fit(rain_df)
|
20 |
+
date_time = pd.Series(pd.to_datetime(f'{Year}-{Month}-{Date}',format = '%Y-%m-%d'),name='ds')
|
21 |
+
date_time = pd.DataFrame(date_time)
|
22 |
+
forecast = rain_model.predict(date_time)
|
23 |
+
|
24 |
+
|
25 |
+
if float(forecast['yhat'][0]) >= 5:
|
26 |
+
WeatherCon = 'SUNNY'
|
27 |
+
txt = 'น้อย'
|
28 |
+
else:
|
29 |
+
WeatherCon = 'RAIN'
|
30 |
+
txt = 'สูง'
|
31 |
+
if Activity == 'Indoor':
|
32 |
+
IsIndoor = 'Y'
|
33 |
+
else:
|
34 |
+
IsIndoor = 'N'
|
35 |
+
|
36 |
+
place = place_df[place_df['Purpose']==Purpose]
|
37 |
+
place = place[place['IsIndoor']==IsIndoor]
|
38 |
+
place = place[place['Province']==Province]
|
39 |
+
if WeatherCon == 'SUNNY':
|
40 |
+
place = place[place['WeatherCon']==WeatherCon]
|
41 |
+
place = place['LocationName']
|
42 |
+
#random = random.randrange(len(place))
|
43 |
+
place = list(place)[0]
|
44 |
+
return f'มีโอกาสฝนตก{txt} สถานที่ที่แนะนำคือ: {place}'
|
45 |
+
|
46 |
+
|
47 |
+
iface = gr.Interface(
|
48 |
+
fn = get_weather_forecast,
|
49 |
+
inputs = [
|
50 |
+
|
51 |
+
gr.Dropdown(
|
52 |
+
["Bangkok"], label="Province", info="Will add more later!"
|
53 |
+
),
|
54 |
+
|
55 |
+
gr.Dropdown(
|
56 |
+
["Indoor","Outdoor"], label="Activity"
|
57 |
+
),
|
58 |
+
|
59 |
+
gr.Dropdown(
|
60 |
+
["Entertainment","Culture",'Shop','Science'], label="Purpose"
|
61 |
+
),
|
62 |
+
|
63 |
+
gr.Dropdown(
|
64 |
+
[2023], label="Year", info="Will add more later!"
|
65 |
+
),
|
66 |
+
|
67 |
+
gr.Dropdown(
|
68 |
+
[1,2,3,4,5,6,7,8,9,10,11,12], label="Month"
|
69 |
+
),
|
70 |
+
|
71 |
+
gr.Dropdown(
|
72 |
+
[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"
|
73 |
+
)
|
74 |
+
|
75 |
+
],
|
76 |
+
|
77 |
+
outputs=gr.outputs.Textbox(label="Prediction"),
|
78 |
+
live=True,
|
79 |
+
title="Weather Forecast",
|
80 |
+
description="Get the weather forecast for a city.",
|
81 |
+
theme="default",
|
82 |
+
)
|
83 |
+
|
84 |
+
iface.launch()
|