Skip to content

rohazshaik/AI-Powered-Query-Generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– Text-to-SQL GenAI System

Transform natural language into SQL queries using local AI - completely free!

React FastAPI Ollama SQLite License


๐ŸŒŸ Overview

A modern full-stack application that converts natural language questions into SQL queries using Ollama (local LLM). Built with a beautiful, responsive UI and powered by FastAPI backend.

Perfect for:

  • Learning SQL through natural language
  • Quick database exploration
  • Demonstrating AI/LLM integration
  • Portfolio projects

Key Highlights:

  • โœ… 100% Free - No API costs, runs completely locally
  • โœ… Privacy-First - All data stays on your machine
  • โœ… Production-Ready - Includes security, validation, and error handling
  • โœ… Beautiful UI - Modern design with dark mode and animations

โœจ Features

  • ๐Ÿค– AI-Powered SQL Generation - Convert natural language to SQL using Ollama (local LLM)
  • ๐Ÿ“ File Upload - Upload your own CSV/Excel/JSON files and query them
  • ๐Ÿ”„ Database Switching - Toggle between default database and uploaded data
  • ๐Ÿ’Ž Modern UI - Beautiful dark mode interface with glassmorphism and smooth animations
  • ๐Ÿ“Š Interactive Results - View query results in formatted, responsive tables
  • ๐Ÿ“œ Query History - Save and reload previous queries
  • ๐Ÿ—„๏ธ Schema Viewer - Browse database structure with collapsible tables
  • โšก Real-time Execution - Execute generated queries instantly
  • ๐Ÿ”’ Secure - Only SELECT queries allowed, with SQL injection protection

๐Ÿ› ๏ธ Tech Stack

Frontend

Technology Purpose
React 18 UI framework
Vite Build tool & dev server
Tailwind CSS Styling
Framer Motion Animations
Monaco Editor SQL syntax highlighting
Axios HTTP client

Backend

Technology Purpose
FastAPI REST API framework
Ollama Local LLM for SQL generation
SQLite Database
Motor Async MongoDB (optional)
Pydantic Data validation


๐Ÿš€ Installation & Setup

Step 1: Install Ollama

Windows

# Download from https://ollama.com/download
# Or use winget:
winget install Ollama.Ollama

macOS

brew install ollama

Linux

curl -fsSL https://ollama.com/install.sh | sh

Step 2: Pull the LLM Model

# Start Ollama service
ollama serve

# In a new terminal, pull the model
ollama pull qwen2.5:0.5b

Note: qwen2.5:0.5b is a lightweight model (~400MB). For better accuracy, you can use llama3.2 (~2GB).

Step 3: Clone the Repository

git clone https://github.com/yourusername/text-to-sql-genai.git
cd text-to-sql-genai

Step 4: Backend Setup

# Navigate to backend directory
cd backend

# Create virtual environment (recommended)
python -m venv venv

# Activate virtual environment
# Windows:
venv\Scripts\activate
# macOS/Linux:
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Create .env file (optional - for MongoDB)
# The app works without MongoDB using in-memory storage
echo "MONGO_URL=mongodb://localhost:27017" > .env
echo "DB_NAME=text_to_sql_db" >> .env
echo "CORS_ORIGINS=*" >> .env

Step 5: Frontend Setup

# Navigate to frontend directory (from project root)
cd frontend

# Install dependencies
npm install

๐ŸŽฎ Usage

Starting the Application

You need 3 terminals open:

Terminal 1: Start Ollama

ollama serve

Keep this running in the background

Terminal 2: Start Backend

cd backend
# Activate venv if not already activated
venv\Scripts\activate  # Windows
# source venv/bin/activate  # macOS/Linux

# Start FastAPI server
python -m uvicorn server:app --reload --port 8000

Backend will be available at: http://localhost:8000

Terminal 3: Start Frontend

cd frontend
npm run dev

The frontend will be available at http://localhost:3000

๐Ÿ“ Upload Your Own Data

Supported Formats

  • CSV (.csv)
  • Excel (.xlsx, .xls)
  • JSON (.json)

How to Upload

  1. Click the "Upload Data" button in the header

  2. Drag and drop your file or click to browse

    • Maximum file size: 10MB
    • The system will automatically detect column types
  3. Preview your data

    • See the first 5 rows
    • Review the detected schema
    • Check row and column counts
  4. Start querying

    • The database will automatically switch to your uploaded data
    • Use natural language to query your own data
    • Example: "Show me the top 10 rows"

Database Switching

  • Use the Database Selector dropdown to switch between:
    • Default (E-commerce) database
    • Your uploaded databases
  • Delete uploaded databases when no longer needed
  • Schema viewer updates automatically when switching databases

Example Upload

Create a CSV file sales_data.csv:

date,product,quantity,revenue,region
2024-01-15,Laptop,5,6499.95,North
2024-01-16,Mouse,20,599.80,South
2024-01-17,Keyboard,15,1499.85,East

Then ask:

  • "What's the total revenue by region?"
  • "Which product sold the most quantity?"
  • "Show me sales from January 2024"

Using the Application

  1. Open your browser to http://localhost:3000

  2. View the database schema in the left sidebar to see available tables

  3. Enter a natural language question, for example:

    • "Show me all products in the Electronics category"
    • "What are the top 5 most expensive products?"
    • "List all customers from New York"
    • "Count how many products are in each category"
    • "Show me all orders with their customer names and product names"
  4. Click "Generate SQL Query" - Wait ~5-10 seconds for Ollama to process

  5. Review the generated SQL in the Monaco Editor

  6. Click "Execute Query" to run the SQL and see results

  7. View query history in the sidebar to reload previous queries


๐Ÿ“ Project Structure

text-to-sql-genai/
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ server.py              # FastAPI application
โ”‚   โ”œโ”€โ”€ requirements.txt       # Python dependencies
โ”‚   โ”œโ”€โ”€ .env                   # Environment variables (create this)
โ”‚   โ””โ”€โ”€ ecommerce.db          # SQLite database (auto-created)
โ”‚
โ”œโ”€โ”€ frontend/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ App.jsx           # Main React component
โ”‚   โ”‚   โ”œโ”€โ”€ index.css         # Tailwind styles
โ”‚   โ”‚   โ””โ”€โ”€ main.jsx          # Entry point
โ”‚   โ”œโ”€โ”€ package.json          # Node dependencies
โ”‚   โ””โ”€โ”€ vite.config.js        # Vite configuration
โ”‚
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ DEPLOYMENT.md             # Deployment guide

๐Ÿค Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ“ž Support


๐ŸŽฏ What's Next?

Potential improvements:

  • Add support for multiple databases (PostgreSQL, MySQL)
  • Implement query optimization suggestions
  • Add data visualization charts
  • Support for INSERT/UPDATE queries (with proper auth)
  • Multi-language support
  • Export results to CSV/JSON
  • Query performance metrics

Built with โค๏ธ using free and open-source technologies

Star โญ this repo if you find it helpful!

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published