Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
# Function to generate a
|
| 5 |
-
def generate_timetable(days, subjects, start_hour=8, slot_duration=1):
|
| 6 |
-
"""
|
| 7 |
-
days: list of days of week
|
| 8 |
-
subjects: list of subjects
|
| 9 |
-
start_hour: starting hour in 24h format
|
| 10 |
-
slot_duration: duration of each slot in hours
|
| 11 |
-
"""
|
| 12 |
-
import random
|
| 13 |
timetable = {}
|
| 14 |
num_slots = len(subjects)
|
| 15 |
-
|
| 16 |
for day in days:
|
| 17 |
-
random.shuffle(subjects) # shuffle subjects for uniqueness
|
| 18 |
day_schedule = []
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
slot_start = datetime.time(hour=start_hour + i*slot_duration)
|
| 21 |
slot_end_hour = start_hour + (i+1)*slot_duration
|
| 22 |
slot_end = datetime.time(hour=slot_end_hour)
|
| 23 |
-
day_schedule.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
timetable[day] = day_schedule
|
| 25 |
return timetable
|
| 26 |
|
|
@@ -30,31 +35,84 @@ def format_timetable(timetable_dict):
|
|
| 30 |
for day, slots in timetable_dict.items():
|
| 31 |
formatted += f"### {day}\n"
|
| 32 |
for slot in slots:
|
| 33 |
-
formatted += f"- {slot}\n"
|
| 34 |
formatted += "\n"
|
| 35 |
return formatted
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
days = [d.strip().capitalize() for d in days_input.split(",") if d.strip()]
|
| 40 |
subjects = [s.strip().capitalize() for s in subjects_input.split(",") if s.strip()]
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
# Gradio
|
| 47 |
with gr.Blocks(title="Automatic Time Table Generation Agent (Improved)") as demo:
|
| 48 |
gr.Markdown("# 🗓 Automatic Time Table Generation Agent (Improved)")
|
| 49 |
-
gr.Markdown("
|
| 50 |
|
| 51 |
-
days_input = gr.Textbox(label="Days (comma-separated
|
| 52 |
-
subjects_input = gr.Textbox(label="Subjects (comma-separated
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
generate_btn = gr.Button("Generate Timetable")
|
| 57 |
-
generate_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
demo.launch(server_name="0.0.0.0", share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import datetime
|
| 3 |
+
import random
|
| 4 |
+
from fpdf import FPDF
|
| 5 |
+
from docx import Document
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Function to generate a timetable
|
| 9 |
+
def generate_timetable(days, subjects, teachers, start_hour=8, slot_duration=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
timetable = {}
|
| 11 |
num_slots = len(subjects)
|
|
|
|
| 12 |
for day in days:
|
|
|
|
| 13 |
day_schedule = []
|
| 14 |
+
# shuffle subjects for uniqueness
|
| 15 |
+
subject_order = subjects.copy()
|
| 16 |
+
teacher_order = teachers.copy()
|
| 17 |
+
random.shuffle(subject_order)
|
| 18 |
+
random.shuffle(teacher_order)
|
| 19 |
+
for i, subject in enumerate(subject_order):
|
| 20 |
+
teacher = teacher_order[i % len(teachers)]
|
| 21 |
slot_start = datetime.time(hour=start_hour + i*slot_duration)
|
| 22 |
slot_end_hour = start_hour + (i+1)*slot_duration
|
| 23 |
slot_end = datetime.time(hour=slot_end_hour)
|
| 24 |
+
day_schedule.append({
|
| 25 |
+
"time": f"{slot_start.strftime('%H:%M')} - {slot_end.strftime('%H:%M')}",
|
| 26 |
+
"subject": subject,
|
| 27 |
+
"teacher": teacher
|
| 28 |
+
})
|
| 29 |
timetable[day] = day_schedule
|
| 30 |
return timetable
|
| 31 |
|
|
|
|
| 35 |
for day, slots in timetable_dict.items():
|
| 36 |
formatted += f"### {day}\n"
|
| 37 |
for slot in slots:
|
| 38 |
+
formatted += f"- {slot['time']}: {slot['subject']} (Teacher: {slot['teacher']})\n"
|
| 39 |
formatted += "\n"
|
| 40 |
return formatted
|
| 41 |
|
| 42 |
+
# Export timetable to PDF
|
| 43 |
+
def export_pdf(timetable_dict, filename="timetable.pdf"):
|
| 44 |
+
pdf = FPDF()
|
| 45 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 46 |
+
pdf.add_page()
|
| 47 |
+
pdf.set_font("Arial", "B", 16)
|
| 48 |
+
pdf.cell(0, 10, "Timetable", ln=True, align="C")
|
| 49 |
+
pdf.ln(5)
|
| 50 |
+
pdf.set_font("Arial", "", 12)
|
| 51 |
+
for day, slots in timetable_dict.items():
|
| 52 |
+
pdf.set_font("Arial", "B", 14)
|
| 53 |
+
pdf.cell(0, 10, day, ln=True)
|
| 54 |
+
pdf.set_font("Arial", "", 12)
|
| 55 |
+
for slot in slots:
|
| 56 |
+
pdf.multi_cell(0, 8, f"{slot['time']}: {slot['subject']} (Teacher: {slot['teacher']})")
|
| 57 |
+
pdf.ln(2)
|
| 58 |
+
pdf.output(filename)
|
| 59 |
+
return filename
|
| 60 |
+
|
| 61 |
+
# Export timetable to Word
|
| 62 |
+
def export_word(timetable_dict, filename="timetable.docx"):
|
| 63 |
+
doc = Document()
|
| 64 |
+
doc.add_heading("Timetable", 0)
|
| 65 |
+
for day, slots in timetable_dict.items():
|
| 66 |
+
doc.add_heading(day, level=1)
|
| 67 |
+
for slot in slots:
|
| 68 |
+
doc.add_paragraph(f"{slot['time']}: {slot['subject']} (Teacher: {slot['teacher']})")
|
| 69 |
+
doc.save(filename)
|
| 70 |
+
return filename
|
| 71 |
+
|
| 72 |
+
# Main function for Gradio
|
| 73 |
+
def timetable_app(days_input, subjects_input, teachers_input, export_format):
|
| 74 |
days = [d.strip().capitalize() for d in days_input.split(",") if d.strip()]
|
| 75 |
subjects = [s.strip().capitalize() for s in subjects_input.split(",") if s.strip()]
|
| 76 |
+
teachers = [t.strip().capitalize() for t in teachers_input.split(",") if t.strip()]
|
| 77 |
+
|
| 78 |
+
if not days or not subjects or not teachers:
|
| 79 |
+
return "Please enter valid days, subjects, and teacher names."
|
| 80 |
+
|
| 81 |
+
timetable = generate_timetable(days, subjects, teachers)
|
| 82 |
+
formatted = format_timetable(timetable)
|
| 83 |
+
|
| 84 |
+
# Export file if requested
|
| 85 |
+
file_path = None
|
| 86 |
+
if export_format == "PDF":
|
| 87 |
+
file_path = export_pdf(timetable, "timetable.pdf")
|
| 88 |
+
elif export_format == "Word":
|
| 89 |
+
file_path = export_word(timetable, "timetable.docx")
|
| 90 |
+
elif export_format == "Text":
|
| 91 |
+
with open("timetable.txt", "w") as f:
|
| 92 |
+
f.write(formatted)
|
| 93 |
+
file_path = "timetable.txt"
|
| 94 |
+
|
| 95 |
+
return formatted, file_path
|
| 96 |
|
| 97 |
+
# Gradio UI
|
| 98 |
with gr.Blocks(title="Automatic Time Table Generation Agent (Improved)") as demo:
|
| 99 |
gr.Markdown("# 🗓 Automatic Time Table Generation Agent (Improved)")
|
| 100 |
+
gr.Markdown("Generate unique timetables with teacher names and export as PDF, Word, or Text.")
|
| 101 |
|
| 102 |
+
days_input = gr.Textbox(label="Days (comma-separated)", placeholder="Monday, Tuesday, Wednesday, Thursday, Friday")
|
| 103 |
+
subjects_input = gr.Textbox(label="Subjects (comma-separated)", placeholder="Math, English, Science")
|
| 104 |
+
teachers_input = gr.Textbox(label="Teachers (comma-separated, match subjects count or fewer)", placeholder="Mr. Smith, Ms. Johnson, Mr. Lee")
|
| 105 |
+
export_format = gr.Dropdown(label="Export Format", choices=["None", "PDF", "Word", "Text"], value="None")
|
| 106 |
|
| 107 |
+
output_text = gr.Markdown()
|
| 108 |
+
output_file = gr.File(label="Download File")
|
| 109 |
|
| 110 |
generate_btn = gr.Button("Generate Timetable")
|
| 111 |
+
generate_btn.click(
|
| 112 |
+
timetable_app,
|
| 113 |
+
inputs=[days_input, subjects_input, teachers_input, export_format],
|
| 114 |
+
outputs=[output_text, output_file]
|
| 115 |
+
)
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
demo.launch(server_name="0.0.0.0", share=True)
|