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