Back to .md Directory

PR #5: RuleBased Provider - Guaranteed Offline Fallback Implementation

This PR implements the `GenerateScriptAsync` method for the `RuleBasedLlmProvider`, providing a guaranteed offline fallback mechanism for video script generation. This ensures that Aura Video Studio can always generate video scripts without requiring internet connectivity, API keys, or external dependencies.

May 2, 2026
0 downloads
0 views
ai llm prompt
View source

PR #5: RuleBased Provider - Guaranteed Offline Fallback Implementation

Summary

This PR implements the GenerateScriptAsync method for the RuleBasedLlmProvider, providing a guaranteed offline fallback mechanism for video script generation. This ensures that Aura Video Studio can always generate video scripts without requiring internet connectivity, API keys, or external dependencies.

Changes Made

New Method: GenerateScriptAsync

public Task<Core.Models.Generation.Script> GenerateScriptAsync(string brief, int durationSeconds, CancellationToken ct = default)

Purpose: Generate a complete Script object with structured scenes from a simple brief string and target duration.

Key Features:

  • Guaranteed Success: Never throws exceptions; always returns a valid script (with fallback if errors occur)
  • Fast Execution: Completes in under 1 second (tested at 0-26ms)
  • Zero Dependencies: No external API calls, no internet required, no API keys needed
  • Zero Cost: Completely free to use (no token usage, no API costs)
  • Offline Mode: Works completely offline with deterministic output

Helper Methods Implemented

1. ExtractKeywords(string brief)

  • Extracts top 5 keywords using frequency analysis
  • Filters out common stop words (the, a, an, is, are, etc.)
  • Keeps words with length >= 4 characters
  • Returns generic keywords for empty/short briefs: ["video", "content", "information"]

2. DetectVideoType(List<string> keywords, string brief)

  • Detects video type from content analysis
  • Tutorial: "tutorial", "how", "learn", "guide", "teach", "lesson", "course", "training"
  • Marketing: "product", "buy", "sale", "offer", "discount", "deal", "purchase", "customer"
  • Review: "review", "opinion", "thoughts", "rating", "recommend", "experience", "pros", "cons"
  • General: Default fallback for other content
  • Logs detected type for debugging

3. GenerateSceneNarration(...)

  • Generates template-based narration for each scene
  • Provides distinct templates for each video type:
    • Tutorial: Educational, step-by-step guidance
    • Marketing: Promotional, benefit-focused messaging
    • Review: Opinion-based, rating-focused content
    • General: Informative, discussion-style narration
  • First scene: Welcome/introduction
  • Middle scenes: Content exploration
  • Last scene: Conclusion/call-to-action

4. GenerateVisualPrompt(string narration, List<string> keywords)

  • Creates professional visual prompts under 100 characters
  • Style descriptors: "professional photograph of" or "modern illustration of"
  • Includes context: "clean white background, studio lighting, high quality"
  • Fallback: "abstract gradient background, blue and purple colors"
  • Example: "professional photograph of laptop computer, clean white background, studio lighting"

5. CalculateSceneDurations(int sceneCount, int totalDurationSeconds)

  • Calculates proper scene timing:
    • Intro scene: 15% of total duration
    • Outro scene: 15% of total duration
    • Middle scenes: Split remaining 70% evenly
  • Ensures total duration matches target within 5% tolerance
  • Accounts for transition buffers

6. DetermineTransition(bool isLast)

  • Last scene uses Fade transition for professional ending
  • All other scenes use Cut transition

Scene Count Calculation

var sceneCount = Math.Max(3, Math.Min(20, durationSeconds / 10));
  • Minimum: 3 scenes (intro, content, outro)
  • Maximum: 20 scenes (prevents over-segmentation)
  • Formula: Approximately 1 scene per 10 seconds
  • Examples:
    • 30s video → 3 scenes
    • 60s video → 6 scenes
    • 120s video → 12 scenes
    • 300s video → 20 scenes (capped)

Script Metadata

Generated scripts include complete metadata:

  • ProviderName: "RuleBased"
  • ModelUsed: "Template-Based"
  • TokensUsed: 0
  • EstimatedCost: $0.00
  • Tier: Free
  • GenerationTime: Actual execution time

Testing

Test Coverage

Created comprehensive test suite in Aura.Tests/RuleBasedGenerateScriptAsyncTests.cs:

  • 26 test methods covering all functionality
  • 100% success rate in manual testing

Test Scenarios

  1. Basic Functionality:

    • Valid briefs of various lengths
    • Empty/short briefs with fallback keywords
    • Different target durations (30s, 60s, 120s, 300s)
  2. Video Type Detection:

    • Tutorial keywords → Tutorial template
    • Marketing keywords → Marketing template
    • Review keywords → Review template
    • Generic content → General template
  3. Scene Generation:

    • Correct scene count calculation
    • All scenes have valid narration
    • All scenes have visual prompts under 100 characters
    • Scene durations sum to target duration (±5%)
    • Intro/outro get 15% each, middle scenes split 70%
  4. Performance:

    • All generations complete in < 1 second
    • Tested up to 300s videos (20 scenes)
    • Measured execution times: 0-26ms
  5. Resilience:

    • Never throws exceptions
    • Handles null/empty briefs gracefully
    • Handles special characters
    • Handles extremely long briefs (1000+ characters)
    • Always returns valid script
  6. Metadata:

    • Provider name correctly set
    • Zero cost confirmed
    • Free tier confirmed
    • Generation time recorded

Manual Validation

Created standalone test programs to verify:

  1. /tmp/TestRuleBasedProvider/Program.cs:

    • Ran 6 test scenarios
    • All tests passed
    • Verified timing, scene count, and content quality
  2. /tmp/OfflineDemo/Program.cs:

    • Demonstrated offline capabilities
    • Showed different video types
    • Confirmed zero-cost operation
    • Validated sub-second execution

Demo Output

╔════════════════════════════════════════════════════════════╗
║   Aura Video Studio - RuleBased Provider Offline Demo     ║
║          PR #5: Guaranteed Offline Fallback               ║
╚════════════════════════════════════════════════════════════╝

✅ Tutorial Example: 6 scenes in 19ms
✅ Marketing Example: 4 scenes in 0ms
✅ Review Example: 3 scenes in 0ms
✅ Educational Example: 9 scenes in 0ms
✅ Empty Brief (Resilience): 3 scenes in 0ms

Key Achievements:
  • All scripts generated successfully
  • All generation times < 1 second
  • Zero external dependencies
  • Zero exceptions thrown
  • Works completely offline

Benefits

For Users

  • Guaranteed video generation: Never fails due to API issues, network problems, or quota limits
  • Zero cost: No API fees for basic video generation
  • Instant feedback: Sub-second generation for rapid prototyping
  • Privacy: No data sent to external services
  • Offline capability: Works anywhere, anytime without internet

For Developers

  • Reliable fallback: System always has a working provider
  • Testing: Fast, deterministic output for testing
  • Development: No API keys needed for development
  • CI/CD: Tests run without external dependencies

For the Application

  • Resilience: Graceful degradation when premium providers unavailable
  • Onboarding: New users can try without setup
  • Demos: Quick demos work instantly without configuration
  • Prototyping: Fast iteration on video concepts

Technical Implementation Details

Error Handling

The method includes comprehensive error handling:

try {
    // Main generation logic
} catch (Exception ex) {
    _logger.LogWarning(ex, "Error in RuleBased script generation, returning fallback");
    // Returns valid fallback script with single scene
}

Even if the main logic fails, the method returns a valid script ensuring the system never crashes.

Keywords Example

For brief: "Learn how to code and build tutorial applications"

  1. Tokenization: Split into words
  2. Stop word filtering: Remove "how", "to", "and"
  3. Frequency count: Count remaining words
  4. Top 5 selection: ["learn", "code", "build", "tutorial", "applications"]

Video Type Detection Example

Brief: "Buy our amazing product on sale"
Keywords extracted: ["amazing", "product", "sale"]
Marketing keywords matched: ["product", "sale"]
Detected type: Marketing

Template Application Example

Marketing template for first scene:

"Looking for the best {mainTopic}? You're in the right place!"

With mainTopic = "product":

"Looking for the best product? You're in the right place!"

Code Quality

Zero-Placeholder Policy Compliance

✅ No TODO comments ✅ No FIXME comments ✅ No HACK comments ✅ No WIP markers ✅ All code production-ready

Build Status

  • ✅ Compiles successfully with zero errors
  • ⚠️ Some pre-existing warnings in other files (not introduced by this PR)
  • ✅ No new compilation warnings introduced

Performance

  • ⚡ 0-26ms execution time (well under 1-second requirement)
  • 💾 Minimal memory footprint (no large allocations)
  • 🔄 Deterministic output (same input → same output)

Future Enhancements

While not required for this PR, potential future improvements could include:

  1. Template Expansion: More video type templates (explainer, testimonial, etc.)
  2. Style Variations: Multiple narration styles per video type
  3. Keyword Sophistication: TF-IDF or other advanced keyword extraction
  4. Scene Variety: More diverse scene narration patterns
  5. Visual Intelligence: Smarter visual prompt generation based on scene content
  6. Localization: Support for multiple languages in templates

However, all current functionality is complete and production-ready as specified in the requirements.

Migration Notes

For Existing Code

No breaking changes. The new GenerateScriptAsync method:

  • Is a new method addition
  • Does not modify existing ILlmProvider interface methods
  • Works alongside existing DraftScriptAsync method
  • Can be called independently

For Consumers

// New simplified API for offline generation
var provider = new RuleBasedLlmProvider(logger);
var script = await provider.GenerateScriptAsync("Tutorial about coding", 60);

// Existing API still works
var draftScript = await provider.DraftScriptAsync(brief, spec, ct);

Conclusion

This implementation fulfills all requirements from PR #5:

✅ Complete GenerateScriptAsync method implementation
✅ Keyword extraction with stop word filtering
✅ Video type detection for 4 types
✅ Template-based narration generation
✅ Visual prompts under 100 characters
✅ Proper scene duration calculation
✅ Never throws exceptions
✅ Execution time < 1 second
✅ Works completely offline
✅ Comprehensive test coverage
✅ Zero-placeholder policy compliance

The RuleBased provider now serves as a robust, guaranteed offline fallback that ensures Aura Video Studio can always generate video scripts, regardless of external service availability.

Related Documents