An autonomous Natural Language to SQL Analytics Agent built with Flask, Google Gemini, LangChain, and React. Designed for production demonstrations, portfolio reviews, and interactive data exploration.
- Zero-Friction 1-Click Demo: Includes a pre-populated SQLite E-Commerce database (
customers,orders,order_items,products,categories) so evaluators can test instantly without setting up MySQL. - Custom Database Support: Connect seamlessly to any MySQL server or custom SQLAlchemy URI (PostgreSQL, SQLite, etc.).
- Self-Explanatory UX: Displays real-time schema table badges and interactive one-click starter questions.
- Full Execution Transparency: Returns and renders:
- Executive Natural Language Summary
- Generated SQL Query (with 1-click clipboard copy)
- Interactive Tabular Data Grid (showing records returned directly from the database)
- Query Execution Time Metrics
- Safety & Mutation Guardrails: AST & regex validation ensures only safe read-only (
SELECT,WITH) queries are executed, blocking malicious injections (DROP,DELETE,UPDATE,ALTER). - Autonomous Self-Correction: If a generated query encounters a syntax error, the agent feeds the error traceback back to Gemini to self-heal and re-execute.
- Frontend: React.js 18, Material UI, Modern Glassmorphism CSS
- Backend API: Python 3.12, Flask, Flask-CORS
- AI & LLM Orchestration: LangChain, Google Gemini (
gemini-1.5-flash), Python-Dotenv - Databases: SQLite (bundled demo), MySQL (via PyMySQL), SQLAlchemy
cd backend
# Create & activate virtual environment (optional but recommended)
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Configure your Google Gemini API Key in .env
# Get a free key at: https://aistudio.google.com/app/apikey
cp .env.example .env
# Edit .env and set GOOGLE_API_KEY=your_actual_key_here
# Start the Flask API server
python3 run.pyBackend runs on http://127.0.0.1:5000
cd frontend
# Install node dependencies
npm install --legacy-peer-deps
# Start the React development server
npm startFrontend opens on http://localhost:3000
- Open
http://localhost:3000in your browser. - Select "Pre-Loaded Sample E-Commerce DB" and click "Launch Demo with Sample DB".
- Use the sidebar to inspect tables in the schema (
customers,orders,products, etc.). - Click any of the suggested prompt pills:
- "What is the total revenue generated across all completed orders?"
- "Who are the top 5 customers by total spending and where are they from?"
- "Which product categories generate the highest total sales?"
- Or type any freeform question in plain English!
- Inspect the generated SQL, the data table rows, and the AI executive summary.
The application intercepts all generated SQL before execution:
# Prohibits DROP, DELETE, INSERT, UPDATE, ALTER, TRUNCATE, etc.
is_safe_read_only_query(sql_query) -> boolAny attempts to modify or delete data are rejected with an explicit security alert.
MIT License
