SebastianoMeneghin commited on
Commit
afa19d2
·
1 Parent(s): 3f4a85c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -55
app.py CHANGED
@@ -1,76 +1,80 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import hopsworks
 
4
  import os
5
 
6
- # Get data from online source
7
- hopsworks_api_key = os.environ['HOPSWORKS_API_KEY']
8
- project = hopsworks.login(api_key_value = hopsworks_api_key)
9
- dataset_api = project.get_dataset_api()
10
- today_path = dataset_api.download("Resources/today_timetable_prediction/today_timetable_prediction.csv", overwrite=True)
11
- tomorrow_path = dataset_api.download("Resources/tomorrow_timetable_prediction/tomorrow_timetable_prediction.csv", overwrite=True)
12
-
13
-
14
  def download_data(path):
15
  os.path.abspath(path)
16
  df = pd.read_csv(path)
 
 
17
 
18
- wanted_df = pd.DataFrame()
 
 
 
 
 
 
 
 
 
19
 
20
- headers=["FlightNumber", "Destination", "ScheduleDep", "ActualDep"]
21
- columns = ['airport', 'flight_number', 'ontime', 'delayed']
22
 
23
- wanted_df[headers] = df[columns]
24
- return wanted_df
 
 
 
25
 
26
- def get_num_rows(path):
27
- os.path.abspath(path)
28
- df = pd.read_csv(path)
29
- return df.shape[0]
30
 
31
- io1 = gr.Interface(
32
-
33
- [
34
- gr.Dataframe(
35
- headers = ["FlightNumber", "Destination", "ScheduleDep", "ActualDep"],
36
- datatype = ["str", "str", "str", "str"],
37
- row_count = get_num_rows(today_path),
38
- col_count = (4,"fixed"),
39
- interactive = False,
40
- column_widths = '20%',
41
- show_label = True,
42
- label = 'Today Flight Delays Prediction',
43
- values = download_data(today_path)
44
- ),
45
- ],
46
- "dataframe",
47
- description="Here you can see how much delayed your flight will be!",
48
- )
49
 
 
 
 
50
 
51
- io2 = gr.Interface(
 
 
 
 
 
 
 
 
52
 
 
 
 
 
 
 
53
  [
54
- gr.Dataframe(
55
- headers = ["FlightNumber", "Destination", "ScheduleDep", "ActualDep"],
56
- datatype = ["str", "str", "str", "str"],
57
- row_count = get_num_rows(tomorrow_path),
58
- col_count = (4,"fixed"),
59
- interactive = False,
60
- column_widths = '20%',
61
- show_label = True,
62
- label = 'Tomorrow Flight Delays Prediction',
63
- values = download_data(tomorrow_path)
64
- ),
65
  ],
66
  "dataframe",
67
- description="Here you can see how much delayed your flight will be!",
68
  )
69
 
70
-
71
-
72
- io2 = gr.Interface()
73
-
74
- gr.TabbedInterface(
75
- [io1, io2], {"Today's Flight", "Tomorrow's Flight"}
76
- ).launch(share = True)
 
1
  import gradio as gr
2
  import pandas as pd
3
  import hopsworks
4
+ import math
5
  import os
6
 
 
 
 
 
 
 
 
 
7
  def download_data(path):
8
  os.path.abspath(path)
9
  df = pd.read_csv(path)
10
+ wanted_df = df[['airport', 'flight_number', 'ontime', 'delayed']].copy()
11
+ return wanted_df
12
 
13
+ def access_online_dataframe():
14
+ # Get data from online source
15
+ hopsworks_api_key = os.environ['HOPSWORKS_API_KEY']
16
+ project = hopsworks.login(api_key_value = hopsworks_api_key)
17
+ dataset_api = project.get_dataset_api()
18
+ today_path = dataset_api.download("Resources/today_timetable_prediction/today_timetable_prediction.csv", overwrite=True)
19
+ tomorrow_path = dataset_api.download("Resources/tomorrow_timetable_prediction/tomorrow_timetable_prediction.csv", overwrite=True)
20
+ today_df = download_data(today_path)
21
+ tomorrow_df = download_data(tomorrow_path)
22
+ return today_df, tomorrow_df
23
 
 
 
24
 
25
+ def get_possible_destinations():
26
+ today_df, tomorrow_df = access_online_dataframe()
27
+ total_df = pd.DataFrame({'airport': pd.concat([today_df['airport'], tomorrow_df['airport']]).drop_duplicates().reset_index(drop=True)})
28
+ total_dest = (total_df['airport']).tolist()
29
+ return total_dest
30
 
 
 
 
 
31
 
32
+ def get_specific_flights(day, max_delay, departure_hour, ampm, weather, destinations, yes):
33
+ today_df, tomorrow_df = access_online_dataframe()
34
+ df = pd.DataFrame()
35
+ if(day == 'today'):
36
+ df = today_df
37
+ else:
38
+ df = tomorrow_df
39
+
40
+ # Remove unwanted destinations
41
+ destinations = [dest for dest in destinations if dest not in ["That's a reason why I travel alone...", "I prefer not to say"]]
42
+
43
+ # Select only flight during the same departure hour
44
+ df['departure_hour'] = df['departure_time'].str.split(':').str[0].astype(int)
45
+ df = df[df['departure_hour'] == departure_hour].drop(columns=['departure_hour'])
 
 
 
 
46
 
47
+ # Convert time columns to datetime objects
48
+ df['ontime'] = pd.to_datetime(df['ontime'], format='%H:%M')
49
+ df['delayed'] = pd.to_datetime(df['delayed'], format='%H:%M')
50
 
51
+ # Get flight with less delay than the given and from the destinations selected, of the right day
52
+ df['delay'] = (df['delayed'] - df['ontime']).dt.total_seconds() / 60
53
+ filtered_df = df.loc[(df['delay'] < max_delay) & (df['airport'].isin(destinations)), ['airport', 'flight_number', 'ontime', 'delayed']]
54
+
55
+ # Convert the string to datetime, then the datetime column to HH:MM
56
+ filtered_df['ontime'] = pd.to_datetime(filtered_df['ontime'])
57
+ filtered_df['ontime'] = filtered_df['ontime'].dt.strftime('%H:%M')
58
+ filtered_df['delayed'] = pd.to_datetime(filtered_df['delayed'])
59
+ filtered_df['delayed'] = filtered_df['delayed'].dt.strftime('%H:%M')
60
 
61
+ return filtered_df
62
+
63
+
64
+
65
+ flights = gr.Interface(
66
+ get_specific_flights,
67
  [
68
+ gr.Radio(["today", "tomorrow"], type="value", label="Day", info="When do you have the plane?"),
69
+ gr.Slider(0, 50, value=20, label="Possible Delay", info="How unfortunate do you wanna be?"),
70
+ gr.Row(gr.Number(precision=0, minimum=0, maximum=23, label="Departure Time"), gr.Radio(["am", "pm"], type="index", info="It's the same, no worries!")),
71
+ gr.CheckboxGroup(["Yes, it's cloudy", "I am not in Stockholm"], label="Weather", info="Is it a typical Stockholm day?"),
72
+ gr.Dropdown(get_possible_destinations() + ["That's a reason why I travel alone...", "I prefer not to say"],
73
+ type = "value", multiselect=True, label="Destination", value=["That's a reason why I travel alone..."],
74
+ info="Are you just curious or you are actually going somewhere? Where? With who?"),
75
+ gr.Radio(["Yes", "Yes", "Yes"], type="index", label="Let's guess?", info="We know that you'll say yes!"),
 
 
 
76
  ],
77
  "dataframe",
 
78
  )
79
 
80
+ flights.launch()