Skip to content

Mdskun/Txt2SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”ฎ Txt2SQL

Transform Natural Language into SQL Queries with AI

Python License AI Powered

Features โ€ข Installation โ€ข Usage โ€ข Examples โ€ข Contributing


๐Ÿ“– About

Txt2SQL is an intelligent command-line tool that converts your natural language questions into SQL queries and executes them on your SQLite database. No need to remember complex SQL syntaxโ€”just ask in plain English!

๐ŸŽฏ Perfect For

  • ๐Ÿ“Š Quick database queries without writing SQL
  • ๐ŸŽ“ Learning SQL by seeing generated queries
  • ๐Ÿ” Data exploration and analysis
  • ๐Ÿ› ๏ธ Rapid prototyping and testing

โœจ Features

๐Ÿค– AI-Powered

Uses fine-tuned T5 transformer model for accurate SQL generation

๐Ÿ”’ 100% Local

All processing happens on your machineโ€”no API calls, no data sharing

โšก CPU Optimized

Efficient inference designed for everyday computers

๐ŸŽจ Beautiful CLI

Interactive interface with colors, autocomplete, and command history

๐Ÿ›ก๏ธ Safe by Design

Warns before executing dangerous operations like DELETE or DROP

๐Ÿ“Š Smart Schema Detection

Automatically reads and understands your database structure


๐Ÿš€ Installation

Prerequisites

  • Python 3.8 or higher
  • 2GB+ RAM recommended
  • SQLite database

Step 1: Clone the Repository

git clone https://github.com/yourusername/Txt2SQL.git
cd Txt2SQL

Step 2: Install Dependencies

pip install -r requirements.txt

Step 3: Download T5 Model

Download the pre-trained model from HuggingFace:

# Using git-lfs
git lfs install
git clone https://huggingface.co/mrm8488/t5-small-finetuned-wikiSQL

Or download manually from: HuggingFace Model Hub

Step 4: Configure Environment

cp .env.example .env

Edit .env and set your model path:

WIKISQL_MODEL=/path/to/t5-small-finetuned-wikiSQL

๐Ÿ’ป Usage

Starting the Application

Simply run:

python txt2sql.py

The program will:

  1. โœ… Load the AI model
  2. ๐Ÿ“‚ Ask you to select a database
  3. ๐ŸŽฏ Start the interactive session

Interactive Session

โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘                                                                  โ•‘
โ•‘  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•—         โ•‘
โ•‘  โ•šโ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ•šโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•”โ•โ•šโ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ•šโ•โ•โ•โ•โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•โ–ˆโ–ˆโ•”โ•โ•โ•โ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘         โ•‘
โ•‘     โ–ˆโ–ˆโ•‘    โ•šโ–ˆโ–ˆโ–ˆโ•”โ•    โ–ˆโ–ˆโ•‘    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘         โ•‘
โ•‘     โ–ˆโ–ˆโ•‘    โ–ˆโ–ˆโ•”โ–ˆโ–ˆโ•—    โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•”โ•โ•โ•โ• โ•šโ•โ•โ•โ•โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘โ–„โ–„ โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ•‘         โ•‘
โ•‘     โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•”โ• โ–ˆโ–ˆโ•—   โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•‘โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—    โ•‘
โ•‘     โ•šโ•โ•   โ•šโ•โ•  โ•šโ•โ•   โ•šโ•โ•   โ•šโ•โ•โ•โ•โ•โ•โ•โ•šโ•โ•โ•โ•โ•โ•โ• โ•šโ•โ•โ–€โ–€โ•โ• โ•šโ•โ•โ•โ•โ•โ•โ•    โ•‘
โ•‘                                                                  โ•‘
โ•‘              Natural Language to SQL Query Converter             โ•‘
โ•‘                                                                  โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

โœ“ Model loaded
โœ“ Database: mydb.db
โœ“ Tables: customers, orders, products

โฏ show me all customers from California

๐ŸŽฎ Commands

Command Description
? or help Show help message
schema Display database schema
tables List all tables
clear Clear the screen
exit or quit Exit the program

๐Ÿ’ก Features

  • โ†‘โ†“ Arrow Keys: Navigate command history
  • Tab: Autocomplete commands
  • Ctrl+R: Search command history (if prompt-toolkit installed)

๐Ÿ“ Examples

Example 1: Simple Query

โฏ show all customers

Generated SQL:
SELECT * FROM customers

โ”‚ id  โ”‚ name          โ”‚ email                โ”‚ state       โ”‚
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚ 1   โ”‚ John Doe      โ”‚ john@example.com     โ”‚ California  โ”‚
โ”‚ 2   โ”‚ Jane Smith    โ”‚ jane@example.com     โ”‚ New York    โ”‚
โ”‚ 3   โ”‚ Bob Johnson   โ”‚ bob@example.com      โ”‚ Texas       โ”‚

(3 rows)

Example 2: Filtered Query

โฏ how many customers are from California?

Generated SQL:
SELECT COUNT(*) FROM customers WHERE state = 'California'

โ”‚ COUNT(*) โ”‚
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚ 15       โ”‚

(1 row)

Example 3: Join Query

โฏ show customer names and their order counts

Generated SQL:
SELECT customers.name, COUNT(orders.id) 
FROM customers 
LEFT JOIN orders ON customers.id = orders.customer_id 
GROUP BY customers.name

โ”‚ name          โ”‚ COUNT(orders.id) โ”‚
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โ”‚ John Doe      โ”‚ 5                โ”‚
โ”‚ Jane Smith    โ”‚ 3                โ”‚
โ”‚ Bob Johnson   โ”‚ 7                โ”‚

(3 rows)

๐Ÿ—๏ธ Project Structure

Txt2SQL/
โ”œโ”€โ”€ ๐Ÿ“„ config.py              # Configuration management
โ”œโ”€โ”€ ๐Ÿ“„ database.py            # Database operations
โ”œโ”€โ”€ ๐Ÿ“„ generator.py           # SQL generation with T5
โ”œโ”€โ”€ ๐Ÿ“„ cli.py                 # Interactive CLI interface
โ”œโ”€โ”€ ๐Ÿ“„ utils.py               # Utility functions
โ”œโ”€โ”€ ๐Ÿ“„ txt2sql.py             # Main entry point
โ”‚
โ”œโ”€โ”€ ๐Ÿ“‹ requirements.txt       # Python dependencies
โ”œโ”€โ”€ ๐Ÿ“‹ requirements-dev.txt   # Development dependencies
โ”œโ”€โ”€ ๐Ÿงช test_basic.py          # Unit tests
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ .env.example           # Environment template
โ”œโ”€โ”€ ๐Ÿ“ .gitignore             # Git ignore rules
โ”œโ”€โ”€ ๐Ÿ“ LICENSE                # License file
โ””โ”€โ”€ ๐Ÿ“ README.md              # This file

๐Ÿ›ก๏ธ Safety Features

Feature Description
โš ๏ธ Dangerous Operation Warnings Prompts confirmation before DELETE, DROP, TRUNCATE, ALTER
โœ… Input Validation Validates all user inputs and SQL queries
๐Ÿ” Query Preview Shows generated SQL before execution
๐Ÿ“ Error Messages Clear, helpful error messages

๐ŸŽจ Customization

Environment Variables

Only one required variable in .env:

WIKISQL_MODEL=/path/to/model

Model Parameters

You can adjust these in config.py:

self.max_length = 128      # Maximum SQL length
self.num_beams = 2         # Beam search beams
self.torch_threads = 2     # CPU threads

๐Ÿงช Development

Install Development Dependencies

pip install -r requirements-dev.txt

Run Tests

pytest test_basic.py -v

Code Formatting

black *.py
ruff check *.py

๐Ÿค Contributing

Contributions are welcome! Here's how you can help:

  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.


๐Ÿ™ Acknowledgments


๐Ÿ“ž Support

Found a bug? Open an issue

Have a question? Start a discussion

Love the project? Give it a โญ on GitHub!


๐Ÿš€ Ready to get started?

git clone https://github.com/yourusername/Txt2SQL.git
cd Txt2SQL
pip install -r requirements.txt
python txt2sql.py

Made with โค๏ธ and ๐Ÿค– by the Txt2SQL Team

About

A simple, offline tool that uses T5 transformer model to convert English questions into SQL queries and execute them on your SQLite database.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages