Skip to content

Automate Lesson HTML Generation via GPT API #1025

Description

@amirnaderi343

Overview

Currently, the lesson production workflow is semi-manual. For each lesson, we take the source lesson HTML/content and a predefined prompt, send them to ChatGPT manually, receive the generated CKEditor-compatible HTML, then continue with image redesign, image upload, and final human quality control.

We want to automate the first step of this workflow inside the website by using the GPT API.

The goal of this issue is to implement a backend feature that receives the lesson source HTML and the selected prompt, sends them to the GPT API, and returns the generated lesson HTML directly inside the admin panel.


Current Workflow

  1. Copy the source lesson HTML/content.
  2. Copy the appropriate lesson-generation prompt.
  3. Send both manually to ChatGPT.
  4. Receive the generated CKEditor-compatible HTML.
  5. Review and copy the output.
  6. Paste the final HTML into the lesson editor.
  7. Redesign lesson images manually.
  8. Upload redesigned images to the server.
  9. Run final human quality control.

This issue focuses only on automating steps 1–5.


Target Workflow

Inside the admin panel, the user should be able to:

  1. Open a lesson generation page/tool.
  2. Paste or load the source lesson HTML.
  3. Select the appropriate generation prompt/template.
  4. Click a “Generate Lesson HTML” button.
  5. The system sends the source HTML + prompt to the GPT API.
  6. The generated HTML is displayed in an editable output box.
  7. The user can review, edit, copy, or save the generated HTML.

Main Requirements

1. Admin UI

Create an admin-side interface with the following fields:

  • Source HTML input

    • Large textarea or editor field.
    • Used for pasting the original lesson HTML/content.
  • Prompt selector

    • Dropdown to choose from predefined prompt templates.
    • Example prompt types:
      • Primary lesson page
      • Lower secondary lesson page
      • Exercise section
      • Summary box
      • Investigation/practical box
      • Other future templates
  • Prompt preview/edit field

    • The selected prompt should be visible before submission.
    • Admin should be able to edit the prompt manually before sending.
  • Generate button

    • Sends request to backend.
  • Generated HTML output

    • Large editable textarea/editor.
    • Should show the generated CKEditor-compatible HTML.
    • Include copy/save functionality if possible.

2. Backend API Integration

Implement a backend endpoint that:

  • Receives:

    • source_html
    • prompt_template
    • optional edited prompt
    • lesson metadata if available, such as:
      • lesson title
      • grade
      • subject
      • topic
      • microtopic
  • Builds the final GPT request.

  • Sends it to the GPT API.

  • Receives the generated HTML.

  • Returns the generated output to the admin UI.


3. Output Rules

The generated output should follow our existing lesson HTML rules:

  • CKEditor 4 compatible.
  • Inline styles only.
  • No <script>, <style>, <iframe>, external CSS, or JavaScript.
  • No full HTML document structure such as:
    • <html>
    • <head>
    • <body>
    • <meta>
    • <title>
  • Use only allowed tags according to our current lesson prompt rules.
  • Preserve math formatting as much as possible.
  • Keep the final output clean and ready to paste into the lesson editor.
  • Avoid unnecessary explanations outside the HTML output.

4. Prompt Management

We need a simple way to manage prompts.

Possible implementation:

  • Store prompts in the database.
  • Each prompt should have:
    • title
    • description
    • level/type
    • prompt body
    • status: active/inactive
    • created/updated timestamps

Admin should be able to:

  • Add a new prompt.
  • Edit an existing prompt.
  • Duplicate a prompt.
  • Deactivate old prompts.
  • Select prompts during lesson generation.

5. Error Handling

The system should handle:

  • Empty source HTML.
  • Empty prompt.
  • GPT API failure.
  • Timeout.
  • Invalid response.
  • Output that is too long.
  • Rate limit errors.
  • Authentication/API key errors.

Display clear admin-friendly error messages.


6. Logging

For debugging and quality control, store generation logs.

Each log should include:

  • lesson ID if available
  • selected prompt ID
  • source HTML snapshot or reference
  • generated output
  • model name
  • token usage if available
  • generation time
  • admin user ID
  • created timestamp
  • error message if failed

This will help us compare outputs, debug problems, and improve prompts over time.


7. Security

  • GPT API key must be stored securely in environment variables.
  • API key must never be exposed to frontend.
  • Only authorized admin users should access this tool.
  • Validate and sanitize inputs where needed.
  • Add request size limits to avoid very large accidental submissions.

8. Acceptance Criteria

This issue is complete when:

  • Admin can paste lesson source HTML into a form.
  • Admin can select or edit a lesson-generation prompt.
  • Admin can click a button to generate lesson HTML.
  • Backend sends the request to GPT API successfully.
  • Generated CKEditor-compatible HTML is shown in the admin panel.
  • Admin can copy or save the generated HTML.
  • Errors are handled clearly.
  • Generation attempts are logged.
  • API key is securely stored and not visible in frontend.

Future Scope

These items are not required for this issue but should be considered later:

  • Automatic image extraction from source HTML.
  • Automatic image redesign workflow.
  • Image upload integration.
  • Automatic replacement of old image URLs with redesigned image URLs.
  • Automated quality-control checklist.
  • Human review dashboard.
  • Version comparison between original source and generated lesson.
  • Batch lesson generation.
  • Retry generation with improved prompt.
  • AI-based HTML validation before saving.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions