PraneshJs commited on
Commit
d44713d
·
verified ·
1 Parent(s): bc07442

Added app.py file

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import re
3
+ import gradio as gr
4
+
5
+ def load_timetable_data(files):
6
+ """Load all timetable CSV files into a single DataFrame"""
7
+ all_data = []
8
+ for file in files:
9
+ df = pd.read_csv(file)
10
+ all_data.append(df)
11
+
12
+ return pd.concat(all_data, ignore_index=True)
13
+
14
+ def parse_time_input(time_str):
15
+ """Convert time input like '8:00' to minutes since midnight"""
16
+ try:
17
+ if ':' in time_str:
18
+ hours, minutes = map(int, time_str.split(':'))
19
+ else:
20
+ hours = int(time_str)
21
+ minutes = 0
22
+ return hours * 60 + minutes
23
+ except:
24
+ return None
25
+
26
+ def parse_slot_time(slot_str):
27
+ """Convert slot string like 'S2.05pm - 2.55pm' to time range in minutes"""
28
+ match = re.search(r'S?(\d+\.\d+)(am|pm)?\s*-\s*(\d+\.\d+)(am|pm)?', slot_str, re.IGNORECASE)
29
+ if not match:
30
+ return None, None
31
+
32
+ start_time_str, start_period, end_time_str, end_period = match.groups()
33
+
34
+ def convert_to_minutes(time_str, period):
35
+ time_str = time_str.replace('.', ':')
36
+ if ':' in time_str:
37
+ hours, minutes = map(int, time_str.split(':'))
38
+ else:
39
+ hours = int(time_str)
40
+ minutes = 0
41
+
42
+ if period and period.lower() == 'pm' and 1 <= hours <= 11:
43
+ period = 'am'
44
+ if period and period.lower() == 'pm' and hours != 12:
45
+ hours += 12
46
+ elif period and period.lower() == 'am' and hours == 12:
47
+ hours = 0
48
+ return hours * 60 + minutes
49
+
50
+ start_minutes = convert_to_minutes(start_time_str, start_period)
51
+ end_minutes = convert_to_minutes(end_time_str, end_period)
52
+
53
+ return start_minutes, end_minutes
54
+
55
+ def find_free_classrooms(day, time_str, df):
56
+ """Find free classrooms for a given day and time"""
57
+ time_minutes = parse_time_input(time_str)
58
+ if time_minutes is None:
59
+ return "Invalid time format. Please use HH:MM format.", None
60
+
61
+ day_data = df[df['Day'].str.lower() == day.lower()]
62
+ if day_data.empty:
63
+ return f"No data available for {day}.", None
64
+
65
+ free_classrooms = []
66
+ occupied_classrooms = []
67
+
68
+ for _, row in day_data.iterrows():
69
+ start_minutes, end_minutes = parse_slot_time(row['Slot'])
70
+ if start_minutes is None or end_minutes is None:
71
+ continue
72
+ if start_minutes <= time_minutes < end_minutes:
73
+ subject = str(row['Subject'])
74
+ if (pd.isna(subject) or subject.strip() == '' or subject == 'nan' or
75
+ subject.lower() == 'break' or subject.lower() == 'mentor hour'):
76
+ free_classrooms.append(f"{row['Department']} - {row['Block']} - {row['Classroom']} ({row['Slot']})")
77
+ else:
78
+ occupied_classrooms.append(f"{row['Department']} - {row['Block']} - {row['Classroom']}: {subject} ({row['Slot']})")
79
+
80
+ return free_classrooms, occupied_classrooms
81
+
82
+ # Load all timetable data once
83
+ csv_files = ['civil_timetable_neat.csv', 'datascience.csv', 'eee.csv']
84
+ df = load_timetable_data(csv_files)
85
+
86
+ def gradio_interface(day, time_str):
87
+ free_classrooms, occupied_classrooms = find_free_classrooms(day, time_str, df)
88
+ if isinstance(free_classrooms, str):
89
+ return free_classrooms, ""
90
+ free_text = "\n".join(free_classrooms) if free_classrooms else "No free classrooms"
91
+ occupied_text = "\n".join(occupied_classrooms) if occupied_classrooms else "No occupied classrooms"
92
+ return free_text, occupied_text
93
+
94
+ # Gradio UI
95
+ iface = gr.Interface(
96
+ fn=gradio_interface,
97
+ inputs=[
98
+ gr.Dropdown(choices=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"], label="Day"),
99
+ gr.Textbox(label="Enter time (e.g., 8:00, 14:45)")
100
+ ],
101
+ outputs=[
102
+ gr.Textbox(label="Free Classrooms"),
103
+ gr.Textbox(label="Occupied Classrooms")
104
+ ],
105
+ title="Classroom Availability Finder",
106
+ description="Select a day and enter a time (HH:MM) to check free and occupied classrooms."
107
+ )
108
+
109
+ if __name__ == "__main__":
110
+ iface.launch(server_name=" 0.0.0.0",server_port=7860,pwa=True,debug=True)