Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Script Generation Workflow - n8n

This n8n workflow automates the generation of video scripts for agricultural products using AI (Google Gemini), with built-in quality control and regeneration capabilities.

Overview

The workflow creates farmer-friendly video scripts for agricultural products by:

  • Fetching product data from a database
  • Generating scripts using Google Gemini AI
  • Performing quality control checks
  • Automatically regenerating scripts if they don't meet standards
  • Updating script status in the database
  • Triggering video generation workflows

Complete Workflow

Workflow Components

1. Triggers

  • Cron_Hourly: Scheduled trigger that runs every hour
  • Script-Generation: Webhook trigger for manual script generation

2. Main Script Generation Flow

Product Selection & Processing

  1. DB_SelectNextProduct1: Selects the next product without a script from the database
  2. Func_BuildProductJSON: Formats product data into a structured JSON format
  3. Nano_Banana_Node: Calls Google Gemini API to generate the initial script
  4. Edit Fields: Extracts the generated script text from the API response
  5. DB_InsertScriptPending: Saves the script to database with "pending_approval" status

Quality Control System

  1. Set_CarryIds: Carries forward script and product IDs
  2. Func_QC_Evaluate: Performs comprehensive quality checks on the generated script

The QC system validates:

  • Scene Structure: Exactly 6 scenes with Voice/Visual format
  • Content Requirements:
    • Composition information
    • Dosage and mixing instructions
    • Crop suitability and application stage
    • Safety precautions
    • Advantages and call-to-action
  1. Switch_QC_Result: Routes the script based on quality assessment:
    • auto_approve: Script meets all requirements
    • needs_edit: Script has minor issues, kept for manual review
    • bad_regen: Script has major issues, triggers regeneration

3. Script Approval Path

  • DB_UpdateScriptApproved: Updates script status to "approved"
  • HTTP Request2: Triggers video generation workflow

4. Manual Review Path

  • DB_KeepPendingApproval: Keeps script in pending status for human review
  • HTTP Request: Notifies video generation system

5. Regeneration System

TikTok Workflow Integration

When a script fails quality control, the workflow enters a sophisticated regeneration loop:

Regeneration Flow

  1. DB_UpdateRegenerating: Marks script as being regenerated
  2. Set_RegenPayload: Prepares regeneration parameters
  3. Set_AttemptArray: Creates array for retry attempts (up to 10 attempts)
  4. Loop Over Items: Iterates through regeneration attempts
  5. Gemini regeneration HTTP node: Calls Gemini with specific improvement instructions
  6. Set_RegenScript: Extracts the regenerated script
  7. Func_QC_Regen: Re-evaluates the regenerated script
  8. Switch_QC_Regen: Routes based on regeneration results

The regeneration system uses targeted prompts that specifically address the issues found in the original script, making the AI focus on fixing only the problematic areas.

Mermaid Flowchart

flowchart TD
    A[Cron_Hourly] --> B[DB_SelectNextProduct1]
    B --> C[Func_BuildProductJSON]
    C --> D[Nano_Banana_Node<br/>Gemini API]
    D --> E[Edit Fields]
    E --> F[DB_InsertScriptPending]
    F --> G[Set_CarryIds]
    G --> H[Func_QC_Evaluate]
    H --> I{Switch_QC_Result}
    
    I -->|auto_approve| J[DB_UpdateScriptApproved]
    I -->|needs_edit| K[DB_KeepPendingApproval]
    I -->|bad_regen| L[DB_UpdateRegenerating]
    
    J --> M[HTTP Request2<br/>Video Generation]
    K --> N[HTTP Request<br/>Manual Review]
    
    L --> O[Set_RegenPayload]
    O --> P[Set_AttemptArray]
    P --> Q[Loop Over Items]
    Q --> R[Gemini Regeneration<br/>HTTP Node]
    R --> S[Set_RegenScript]
    S --> T[Func_QC_Regen]
    T --> U{Switch_QC_Regen}
    
    U -->|auto_approve| V[Set_CarryIds1]
    U -->|needs_edit| K
    U -->|bad_regen| P
    
    V --> J
    
    W[Script-Generation<br/>Webhook] --> X[Script-callback]
    
    style A fill:#e1f5fe
    style W fill:#e1f5fe
    style D fill:#fff3e0
    style R fill:#fff3e0
    style I fill:#f3e5f5
    style U fill:#f3e5f5
    style J fill:#e8f5e8
    style K fill:#fff8e1
    style L fill:#ffebee
Loading

Key Features

1. Intelligent Quality Control

  • Multi-criteria validation: Checks scene structure, content completeness, and format compliance
  • Graduated responses: Different actions based on severity of issues
  • Automated decision making: Reduces manual intervention for high-quality scripts

2. Adaptive Regeneration

  • Targeted improvements: AI receives specific feedback about what needs fixing
  • Retry mechanism: Up to 10 attempts to generate acceptable scripts
  • Loop prevention: Prevents infinite regeneration cycles

3. Database Integration

  • Product management: Selects products without scripts based on priority
  • Script tracking: Maintains script status throughout the lifecycle
  • Audit trail: Complete history of script generation and modifications

4. External System Integration

  • Video generation: Automatically triggers video creation for approved scripts
  • Webhook support: Allows external systems to trigger script generation
  • API callbacks: Notifies external systems of script status changes

Script Quality Standards

The workflow enforces strict quality standards for generated scripts:

Required Structure

  • 6 scenes exactly: Each with Voice and Visual components
  • Scene separation: Uses --- delimiter between scenes
  • Consistent format: Voice: and Visual: labels for each scene

Required Content Sections

  1. Introduction: Product name and basic overview
  2. Composition: Active ingredients and formulation details
  3. Dosage & Mixing: Application rates and preparation instructions
  4. Crops & Application Stage: Target crops and timing
  5. Safety: Precautions and safety measures
  6. Advantages & CTA: Benefits and call-to-action (Kissan Ghar)

Quality Thresholds

  • Auto-approve: All requirements met
  • Needs edit: 1-2 minor issues, human review required
  • Bad regeneration: 3+ major issues, automatic regeneration triggered

Configuration

Environment Variables

  • Gemini API Key: AIzaSyAenJhmCjfUqiQ0_iDqNFZbxTQdjUW612g
  • Database Connection: PostgreSQL connection for product and script data
  • Webhook URLs: External system integration endpoints

Database Schema

The workflow expects these database tables:

  • products: Product information (id, product_name, company, composition, etc.)
  • scripts: Generated scripts (id, product_id, content, status, created_at)

Webhook Endpoints

  • Script Generation: /webhook/script-generate
  • Video Generation: /webhook/video_gen
  • Script Callback: /api/webhooks/n8n/script-callback

Usage

Automatic Operation

The workflow runs automatically every hour, processing products that don't have scripts yet, prioritized by the priority field in the products table.

Manual Triggering

Scripts can be generated on-demand using the webhook endpoint:

POST /webhook/script-generate
Content-Type: application/json

{
  "video_id": "your_video_id"
}

Monitoring

Monitor the workflow through:

  • Database script status fields
  • n8n execution logs
  • External system callbacks

Error Handling

The workflow includes robust error handling:

  • Retry mechanisms: Automatic regeneration for failed scripts
  • Graceful degradation: Manual review fallback for edge cases
  • Status tracking: Clear audit trail of all script states
  • Loop prevention: Maximum attempt limits prevent infinite loops

Integration Points

This workflow integrates with:

  1. Product Database: Source of agricultural product information
  2. Google Gemini AI: Script generation engine
  3. Video Generation System: Downstream video creation workflow
  4. External APIs: Webhook-based integrations for system coordination

The workflow serves as a critical component in an automated content creation pipeline for agricultural marketing materials.

About

This complete workflow comes with a postgresql database and stores the complete scripts of any youtube or tiktok videos in the desired repository and has them integrated with another workflow working with the actual video generation.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors