Features โข Installation โข Usage โข Examples โข Contributing
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!
- ๐ Quick database queries without writing SQL
- ๐ Learning SQL by seeing generated queries
- ๐ Data exploration and analysis
- ๐ ๏ธ Rapid prototyping and testing
|
Uses fine-tuned T5 transformer model for accurate SQL generation All processing happens on your machineโno API calls, no data sharing Efficient inference designed for everyday computers |
Interactive interface with colors, autocomplete, and command history Warns before executing dangerous operations like DELETE or DROP Automatically reads and understands your database structure |
- Python 3.8 or higher
- 2GB+ RAM recommended
- SQLite database
git clone https://github.com/yourusername/Txt2SQL.git
cd Txt2SQLpip install -r requirements.txtDownload the pre-trained model from HuggingFace:
# Using git-lfs
git lfs install
git clone https://huggingface.co/mrm8488/t5-small-finetuned-wikiSQLOr download manually from: HuggingFace Model Hub
cp .env.example .envEdit .env and set your model path:
WIKISQL_MODEL=/path/to/t5-small-finetuned-wikiSQLSimply run:
python txt2sql.pyThe program will:
- โ Load the AI model
- ๐ Ask you to select a database
- ๐ฏ Start the interactive session
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโ โโโโโโโ โโโ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โโโ โโโโโโ โโโ โโโโโโโโโโโโโโโโโโ โโโโโโ โ
โ โโโ โโโโโโ โโโ โโโโโโโ โโโโโโโโโโโโโ โโโโโโ โ
โ โโโ โโโโ โโโ โโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โโโ โโโ โโโ โโโ โโโโโโโโโโโโโโโโ โโโโโโโ โโโโโโโโ โ
โ โ
โ Natural Language to SQL Query Converter โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Model loaded
โ Database: mydb.db
โ Tables: customers, orders, products
โฏ show me all customers from California
| 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 |
- โโ Arrow Keys: Navigate command history
- Tab: Autocomplete commands
- Ctrl+R: Search command history (if prompt-toolkit installed)
โฏ 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)
โฏ how many customers are from California?
Generated SQL:
SELECT COUNT(*) FROM customers WHERE state = 'California'
โ COUNT(*) โ
โโโโโโโโโโโโ
โ 15 โ
(1 row)
โฏ 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)
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
| Feature | Description |
|---|---|
| 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 |
Only one required variable in .env:
WIKISQL_MODEL=/path/to/modelYou 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 threadspip install -r requirements-dev.txtpytest test_basic.py -vblack *.py
ruff check *.pyContributions are welcome! Here's how you can help:
- ๐ด Fork the repository
- ๐ง Create a feature branch (
git checkout -b feature/amazing-feature) - ๐พ Commit your changes (
git commit -m 'Add amazing feature') - ๐ค Push to the branch (
git push origin feature/amazing-feature) - ๐ Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Google Research - T5 Transformer Model
- Salesforce - WikiSQL Dataset
- Hugging Face - Transformers Library
- Manuel Romero - Fine-tuned WikiSQL Model
Found a bug? Open an issue
Have a question? Start a discussion
Love the project? Give it a โญ on GitHub!