# samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample- architecture

> Architecture diagram of the GitHub repository samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample-, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample-
- Repository: https://github.com/samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample-

## Overview

##### Project Type & Purpose
- This is a full-stack, single-page Streamlit application that leverages Google’s Gemini API to analyze video content.
- It includes functionalities for batch prediction of questions, long context handling via text chunking, caching for ongoing conversation history, and YouTube transcript extraction.

##### Main Components
- User Interface (UI): Represented by app.py, which is the entry-point of the Streamlit-based UI.
- Utility Modules (under utils/ directory):
- youtube_handler.py: Handles extraction of video transcripts from YouTube URLs.
- text_chunker.py: Manages smart chunking of long transcripts to maintain coherent context.
- context_cache.py: Implements caching logic (in-memory cache with TTL) for conversation history and context.
- batch_processor.py: Contains the batch processing logic, grouping questions, rate-limiting, retries, and synthesizing responses.
- External Services: Represent Google’s Gemini API as an external dependency that receives input from the batch processing component and sends back predictions/analysis.
- Error Handling & Logging: Indicate where robust error-handling logic is applied across data flows (for issues like invalid URLs, API rate limits, etc.).

- User Interaction:
- Transcript Extraction & Processing:
- Flow the transcript into text_chunker that splits the transcript into manageable overlapping chunks.
- Context Management:
- Batch Processing:
- Batch_processor takes input from the text chunker (and possibly cached context) along with user questions, grouping them for efficient API calls.
- External API Interaction:
- Results Display:

##### Architectural Patterns & Design Principles
- Modularization: The clear separation of concerns in the utils folder (each file handling a specific aspect like caching, text chunking, etc.) should be highlighted.
- Error Handling: Include annotations (or separate subcomponents) that indicate where error handling and validation occur (especially on transcript extraction, API response, and network issues).
- Integration and Reusability: Emphasize how modules like the caching system and batch processor work together to optimize performance and reliability.

## Diagram (Mermaid)

```mermaid
flowchart TD
    User("User Input"):::frontend
    UI("UI (Streamlit - app.py)"):::frontend
    Config("Configuration (config.py)"):::config

    subgraph "Utility Modules"
        YT("YouTube Transcript Extraction (youtube_handler.py)"):::utility
        TC("Text Chunking (text_chunker.py)"):::utility
        Cache("Context Caching (context_cache.py)"):::utility
        Batch("Batch Processing (batch_processor.py)"):::utility
    end

    Gemini("Google Gemini API"):::external
    Error("Error Handling & Logging"):::error

    User -->|"submit"| UI
    UI -->|"loadConfig"| Config
    UI -->|"extractTranscript"| YT
    YT -->|"transcriptData"| TC
    TC -->|"chunkedText"| Cache
    Cache -->|"context+Questions"| Batch
    Batch -->|"batchQuestions"| Gemini
    Gemini -->|"APIResponse"| Batch
    Batch -->|"displayResults"| UI
    Batch -.->|"logsErrors"| Error

    subgraph "Legend"
        LEGND("Frontend=Blue,Config=Purple,Utility=Orange,External=Green,Error=Red")
    end

    click UI "https://github.com/samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample-/blob/main/app.py"
    click Config "https://github.com/samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample-/blob/main/config.py"
    click YT "https://github.com/samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample-/blob/main/utils/youtube_handler.py"
    click TC "https://github.com/samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample-/blob/main/utils/text_chunker.py"
    click Cache "https://github.com/samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample-/blob/main/utils/context_cache.py"
    click Batch "https://github.com/samarthghag/batch-prediction-with-long-context-and-context-caching-code-sample-/blob/main/utils/batch_processor.py"

classDef frontend fill:#d1e8ff,stroke:#0277bd,stroke-width:2px;
classDef config fill:#f3e5f5,stroke:#8e24aa,stroke-width:2px;
classDef utility fill:#fae3d9,stroke:#d89c5e,stroke-width:2px;
classDef external fill:#e2f0cb,stroke:#8bbe0e,stroke-width:2px;
classDef error fill:#ffcccc,stroke:#ff0000,stroke-width:2px;
```
