Skip to content

pankajc2005/SQL-GPT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SQLGPT

Natural Language β†’ SQL Query Generator

SQLGPT is a web app that converts plain English into SQL queries using Google Gemini API, with a FastAPI backend and a lightweight HTML/CSS/JS frontend.


πŸ“ Folder Structure

SQL-GPT/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py          # FastAPI backend
β”‚   └── model.py         # Gemini integration logic
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ index.html       # UI layout
β”‚   β”œβ”€β”€ script.js        # Frontend logic
β”‚   └── style.css        # Styling
β”œβ”€β”€ .env                 # API key config
β”œβ”€β”€ README.md            # Project docs
β”œβ”€β”€ requirements.txt     # Python dependencies

✨ Features

  • βœ… Convert natural language to SQL
  • βœ… Syntax-highlighted output (via Prism.js)
  • βœ… Multi-tab chat interface
  • βœ… Copy and download SQL results
  • βœ… Light/dark mode support
  • βœ… Handles irrelevant or off-topic questions gracefully

πŸ›  Tech Stack

Layer Technology
Backend Python, FastAPI
Frontend HTML, CSS, JavaScript
LLM Google Gemini API
Highlight Prism.js

πŸš€ Getting Started

πŸ”§ Prerequisites

  • Python 3.9 or higher
  • pip installed
  • Gemini API key from Google Cloud Console

πŸ“¦ Installation

# 1. Clone the repository
git clone https://github.com/your-user/sqlgpt.git
cd sqlgpt

# 2. Create virtual environment
python -m venv venv

# 3. Activate environment
# macOS/Linux
source venv/bin/activate
# Windows
venv\Scripts\activate

# 4. Install dependencies
pip install -r requirements.txt

πŸ” Set Up Gemini API Key (Step-by-Step)

To use Google’s Gemini API, you need to get an API key from your Google Cloud Console.

βœ… Step 1: Create a Google Cloud Project

  1. Go to: https://console.cloud.google.com
  2. Click on the project dropdown β†’ β€œNew Project”
  3. Name it (e.g., SQLGPT) and click Create

βœ… Step 2: Enable the Gemini API

  1. Inside your new project, go to APIs & Services β†’ Library
  2. Search for β€œGemini API”
  3. Click on it, then click Enable

βœ… Step 3: Create an API Key

  1. Go to APIs & Services β†’ Credentials
  2. Click β€œ+ Create Credentials” β†’ choose API Key
  3. Copy the generated key

βœ… Step 4: Create the .env File

In your project root (SQLGPT/), create a new file named .env:

GEMINI_API_KEY=your_actual_gemini_api_key_here

πŸ”’ Important: Never commit this file to GitHub or share your key publicly.


▢️ Run the App (Step-by-Step Guide)

Here's how to run the backend and frontend of SQLGPT locally:


βœ… 1. Clone the Repository

git clone https://github.com/your-user/sqlgpt.git
cd sqlgpt

βœ… 2. Set Up Python Virtual Environment

python -m venv venv

➀ Activate the Environment

  • macOS/Linux:

    source venv/bin/activate
  • Windows:

    venv\Scripts\activate

βœ… 3. Install Python Dependencies

pip install -r requirements.txt

This installs FastAPI, the Gemini API client, and other required packages.


βœ… 4. Add Your .env File

Make sure your .env file (created earlier) is in the project root like this:

SQLGPT/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py
β”‚   └── model.py
β”œβ”€β”€ frontend/
β”‚   └── ...
β”œβ”€β”€ .env   πŸ‘ˆ HERE

The file should contain:

GEMINI_API_KEY=your_actual_gemini_api_key_here

βœ… 5. Start the FastAPI Backend

From the root directory, run:

uvicorn app.main:app --reload

You should see output like:

Uvicorn running on http://127.0.0.1:8000

Your backend is now live at: πŸ‘‰ http://localhost:8000


βœ… 6. Start the Frontend

In a new terminal window (or tab):

cd frontend
python -m http.server 5500

This serves the frontend at: πŸ‘‰ http://localhost:5500

βœ… Open it in your browser and start typing questions like: β€œGet employees in HR with salary above 50000”


πŸ“Œ Development Tips

  • Keep both backend and frontend terminals open while developing.
  • If .env changes, restart the backend to reload your API key.
  • Any change to main.py or model.py will auto-reload the FastAPI server in development mode (--reload).

πŸ§ͺ Example Prompts

Try asking:

  • List employees in the HR department with salary over 50000
  • Get average sales per region
  • Create a table for student enrollments
  • Insert 5 rows into orders table
  • Update salary of employee with ID 102 to 80000

πŸ†˜ Troubleshooting

πŸ”Έ Missing modules?

pip install -r requirements.txt

πŸ”Έ Frontend not loading?

  • Make sure you start it via:

    cd frontend
    python -m http.server 5500

πŸ”Έ β€œServer Error” in browser?

  • Check that the FastAPI backend is running:

    uvicorn app.main:app --reload

πŸ”Έ Gemini API issues?

  • Confirm .env is correctly set
  • Restart backend after editing .env

πŸ” Security Tips

  • Use .env for local dev β€” never expose API keys
  • Use environment variables in production
  • Add rate limiting, CORS restrictions, and error handling in production environments

πŸ“„ License

This project is intended for educational and personal use. Feel free to fork and modify it to suit your needs.

About

Convert natural language into SQL using Google Gemini API. FastAPI backend + clean frontend for quick, AI-powered query generation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors