DEV Community

Cover image for Optimizing Classroom Time: Using Lyzr-Automata to Generate Effective Timetables
Rasswanth Shankar
Rasswanth Shankar

Posted on

Optimizing Classroom Time: Using Lyzr-Automata to Generate Effective Timetables

Juggling teacher availability, student needs, and subject requirements for your classroom can be quite a hassle. But what if there was a way to take the stress out of scheduling? In this blog post, we'll explore how Lyzr-Automata can help you build efficient, balanced, and fair timetables that meet the needs of your students and teachers.

Setup

Create a folder, set up a virtual environment and activate it. Create .env file with your OPENAI_API_KEY. Then install the following libraries to get started.

Libraries

  • streamlit: for building the web app interface.
  • lyzr_automata : for implementing our AI models, and tasks.
  • dotenv: for loading environment variables (API key).
lyzr-automata==0.1.2
streamlit==1.33.0
python-dotenv==1.0.1
Enter fullscreen mode Exit fullscreen mode

Getting Started

We will split the task into 2 files. One for frontend components (main.py) while the other for Lyzr-Automata (lyzr_functions.py)

main.py

1.Import Libraries

import streamlit as st
from lyzr_functions import generate_basic_timetable
Enter fullscreen mode Exit fullscreen mode

2. Input Components

  • st.text_area — Text area to enter timeslots, subjects list and constraints.
  • st.button — Button to submit input
# Time Slots
timeslot_input = st.text_area(
    "TIMESLOTS",
    '''08:00 AM - 09:00 AM
09:15 AM - 10:15 AM
10:30 AM - 11:30 AM
11:45 AM - 12:45 PM
01:00 PM - 02:00 PM
02:15 PM - 03:15 PM
03:30 PM - 04:30 PM
04:45 PM - 05:45 PM'''
)

# Subjects
subjects_input = st.text_area(
    "SUBJECTS",
    '''Mathematics
Science
English
Social Studies
History
Geography
Physical Education
Computer
Art
Music'''
)

# Constraints
constraints_input = st.text_area(
    "CONSTRAINTS",
    '''1. Mathematics - 5 classes a week
2. Science - 5 classes a week
3. English - 4 classes a week
4. Social Studies - 4 classes a week
5. History - 3 classes a week
6. Geography - 3 classes a week
7. Physical Education - 4 classes a week
8. Computer - 5 classes a week
9. Art - 2 class a week
10. Music - 2 class a week
11. Each classroom is allocated once per time slot on any given day.
12. Same subjects cannot be assigned consecutively'''
)

# Submit button
submit_inputs = st.button("Submit", type="primary")
Enter fullscreen mode Exit fullscreen mode

3. Handle Inputs

if submit_inputs:
    # Save inputs to a file
    with open("example_slot.txt", "w") as my_file:
        my_file.write("TIMESLOTS")
        my_file.write("\n \n")
        my_file.write(timeslot_input)
        my_file.write("\n \n")

        my_file.write("SUBJECTS")
        my_file.write("\n \n")
        my_file.write(subjects_input)
        my_file.write("\n \n")

        my_file.write("CONSTRAINTS")
        my_file.write("\n \n")
        my_file.write(constraints_input)

    # Call timetable generator
    generate_basic_timetable()

    # Read and display from result file
    with open("example_result.txt", "r") as my_file:
        content = my_file.read()

    st.write(content)
Enter fullscreen mode Exit fullscreen mode

lyzr_functions.py

1.Import Libraries

from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata.memory.open_ai import OpenAIMemory
from lyzr_automata import Agent, Task
from lyzr_automata.tasks.task_literals import InputType, OutputType

from dotenv import load_dotenv
import os

load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
Enter fullscreen mode Exit fullscreen mode

2. Initialize Model and Memory

  • OpenAIModel — Create our models using OpenAI Key and specify the model type and name.
  • OpenAIMemorytimetable_memory, Create a memory of instructions for the agent.
# OpenAI Text Model
open_ai_model_text = OpenAIModel(
api_key=OPENAI_API_KEY,
parameters={
    "model": "gpt-4-turbo-preview",
    "temperature": 0.1,
    "max_tokens": 4096,
    },
)

# OpenAI Memory file
timetable_memory = OpenAIMemory(
    file_path='example_slot.txt'
)
Enter fullscreen mode Exit fullscreen mode

3. generate_basic_timetable functions

  • Agenttimetable_agent, Lyzr Agent with instructions and persona to create timetable.
  • Tasktimetable_task, Lyzr Task to create timetable.
  • Agenttimetable_checker_agent, Lyzr Agent with instructions and persona to verify timetable.
  • Tasktimetable_checker_task, Lyzr Task to verify if the timetable is valid.
def generate_basic_timetable():
    # Remove file if exists
    if os.path.exists("assistant_ids.json"):
        os.remove("assistant_ids.json")

    # Timetable generator Agent
    timetable_agent = Agent(
        prompt_persona="You are an intelligent agent that can create efficient class timetables for a week in a simple, structured format. Do not assign more classes than required, assign free slots instead. Generate timetable for every day from Monday to Friday.",
        role="Timetable creator",
        memory=timetable_memory
    )

    # Timetable generator Task
    timetable_task = Task(
        name="Timetable Creator",
        agent=timetable_agent,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
        model=open_ai_model_text,
        instructions="Using the time slots, subject details and requirements, create a timetable that satisfies every constraint. Return the timetable in a simple format and a count of number of classes scheduled for each subject.",
        log_output=True,
        enhance_prompt=False,
    ).execute()

    # Save output to a file
    with open("example_result.txt", "w") as my_file:
        my_file.write("# GENERATED TIMETABLE - Timetable Agent")
        my_file.write("\n \n")
        my_file.write(timetable_task)
        my_file.write("\n")

    # Timetable verification Agent
    timetable_checker_agent = Agent(
        prompt_persona="You are an intelligent agent that can verify if a generated timetable fulfills all the constraints or not. Make sure all classes meet the exact requirements; not more, not less.",
        role="Timetable checker",
        memory=timetable_memory
    )

    # Timetable verification Task
    timetable_checker_task = Task(
        name="Timetable Checker",
        agent=timetable_checker_agent,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
        model=open_ai_model_text,
        instructions="Verify that the generated timetable input fulfills the requirements mentioned in the file. If VALID, return <!VALID!> and the timetable in a table format; If INVALID, return <!INVALID!> the reason why invalid. Do not return anything else.",
        log_output=True,
        enhance_prompt=False,
        previous_output=timetable_task
    ).execute()

    # Save output to a file
    with open("example_result.txt", "a") as my_file:
        my_file.write("\n \n")
        my_file.write("# GENERATED VERIFICATION - Verification Agent")
        my_file.write("\n \n")
        my_file.write(timetable_checker_task)
        my_file.write("\n")
Enter fullscreen mode Exit fullscreen mode

Run App

streamlit run main.py

Flow Diagram

Flow diagram

Want to create more of such amazing AI Workflows? Visit our website at GitHub to learn more about Lyzr-Automata!

Also checkout Lyzr SDKs at GitHub

Lyzr Website: Lyzr.ai
Lyzr Community Channel: Discord

Code: https://github.com/rasswanth-lyzr/timetable_bot
Video Walkthrough: https://youtu.be/h9c-bL0a3TM
Demo: https://rasswanth-lyzr-timetable-bot-main-n3n6ul.streamlit.app/

Top comments (0)