# onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines architecture

> Architecture diagram of the GitHub repository onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines
- Repository: https://github.com/onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines

## Overview

##### Main Components
- User Interface (UI) Layer:
- Templates Folder: Contains HTML files (index.html, 404.html) for rendering the web pages.
- Static Folder: Holds CSS (404.css, style.css) and JavaScript (script.js) for UI styling and behavior.
- API / Backend Layer:
- app.py: The main application file handling API endpoints such as database creation (/create_database) and query processing (/query).
- Endpoints for:
▪ NL query analysis,
▪ Database/table creation,
▪ Query execution,
▪ Results download (as JSON / YAML).
- AI Processing Component:
- NL Query Analysis: Uses SentenceTransformer to encode the user’s query, understands context, and then processes the query.
- Relevant Table Selection: Implements a select_relevant_tables() function that leverages cosine similarity to filter the tables in the schema.
- NL-to-SQL Conversion Preparation: Function (query_gemini()) that packages only the relevant tables and schema details for conversion.
- Data Storage and Schema Management:
- Database Files (newr.db) and JSON/YAML schema files (database_data.json, query_results.json, query_results.yaml).
- CSV File (table_structure.csv) which might be used to either validate or display table structures.
- Uploads Folder: Stores additional or updated schema files (uploads/database_data.json, uploads/newr_schema.json).

##### Analyzing Relationships and Interactions
- The User Interface sends a NL query via HTTP requests (using form submissions or AJAX calls).
- The API layer (app.py) receives the query and triggers the AI Processing Component.
- First, the NL query is processed by SentenceTransformer to extract semantic meaning.
- The schema is loaded (from JSON/YAML files and the CSV) and then analyzed using cosine similarity to identify the relevant tables.
- The filtered result is then forwarded (possibly through query_gemini()) to the NL-to-SQL conversion engine for further processing.
- Additionally, there’s an endpoint for creating or updating the database; this involves reading from schema files and populating newr.db, if needed.
- The system sends back responses (query results) in JSON/YAML format to the UI or the client requesting the data.

##### Highlighting Architectural Patterns and Design Principles
- Layered Architecture:
- Separation between the frontend (Templates and Static) and backend logic (app.py).
- Clear division of concerns, e.g., UI rendering versus business logic/API.
- Modular Design:
- The project segments different functionalities (query analysis, table selection, database management) into distinct functions or modules within the backend.
- API-Driven Interaction:
- Use of clearly defined HTTP endpoints for actions like querying, database creation, and result download.
- Error Handling and Input Validation:
- Robust checks for scenarios such as missing API keys, erroneous files, or invalid queries, as stated in the README.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "UI Layer"
        UI_Templates["Templates (index.html, 404.html)"]:::ui
        UI_Static["Static Assets (CSS/JS)"]:::ui
    end

    subgraph "Backend/API Layer"
        API["API Endpoint (app.py)"]:::backend
    end

    subgraph "AI Processing Module"
        AI_NLQuery["NL Query Analysis (SentenceTransformer)"]:::ai
        AI_TableSelect["Table Selection (cosine similarity)"]:::ai
        AI_NL2SQL["NL-to-SQL Conversion Prep (query_gemini())"]:::ai
    end

    subgraph "Data Storage & Schema Management"
        DB_Primary["Database (newr.db)"]:::storage
        DS_Schema["Schema Files\n(database_data.json,\nquery_results.json,\nquery_results.yaml)"]:::storage
        DS_CSV["Table Structure (table_structure.csv)"]:::storage
        DS_Uploads["Uploads (updated schema files)"]:::storage
    end

    %% Data Flow
    UI_Templates -->|"forms"| API
    UI_Static -->|"AJAX"| API
    API -->|"process_NL_query"| AI_NLQuery
    API -->|"load_schema"| DS_Schema
    API -->|"update_database"| DB_Primary
    AI_NLQuery -->|"forward_analysis"| AI_TableSelect
    AI_TableSelect -->|"filter_schema"| AI_NL2SQL
    AI_NL2SQL -->|"return_processed_data"| API
    DS_Schema -->|"supply_schema"| AI_NLQuery
    DB_Primary -->|"store_results"| API
    DS_CSV -.->|"validate_structure"| API
    DS_Uploads -.->|"load_additional_schema"| DS_Schema
    API -->|"send_results"| UI_Templates

    %% Click Events
    click UI_Templates "https://github.com/onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines/tree/main/Templates/"
    click UI_Static "https://github.com/onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines/tree/main/static/"
    click API "https://github.com/onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines/blob/main/app.py"
    click DB_Primary "https://github.com/onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines/blob/main/newr.db"
    click DS_Schema "https://github.com/onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines/blob/main/database_data.json"
    click DS_CSV "https://github.com/onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines/blob/main/table_structure.csv"
    click DS_Uploads "https://github.com/onlyvenkatadanush/al-powered-schema-selection-for-nl-to-sql-pipelines/tree/main/uploads/"

    %% Styles
    classDef ui fill:#f9e79f,stroke:#833,stroke-width:2px;
    classDef backend fill:#aed6f1,stroke:#1b4f72,stroke-width:2px;
    classDef ai fill:#a9dfbf,stroke:#196f3d,stroke-width:2px;
    classDef storage fill:#f5cba7,stroke:#7d6608,stroke-width:2px;
```
