Transform natural language into SQL queries using local AI - completely free!
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
- ๐ค 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
| 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 |
| Technology | Purpose |
|---|---|
| FastAPI | REST API framework |
| Ollama | Local LLM for SQL generation |
| SQLite | Database |
| Motor | Async MongoDB (optional) |
| Pydantic | Data validation |
# Download from https://ollama.com/download
# Or use winget:
winget install Ollama.Ollamabrew install ollamacurl -fsSL https://ollama.com/install.sh | sh# Start Ollama service
ollama serve
# In a new terminal, pull the model
ollama pull qwen2.5:0.5bNote:
qwen2.5:0.5bis a lightweight model (~400MB). For better accuracy, you can usellama3.2(~2GB).
git clone https://github.com/yourusername/text-to-sql-genai.git
cd text-to-sql-genai# 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# Navigate to frontend directory (from project root)
cd frontend
# Install dependencies
npm installYou need 3 terminals open:
ollama serveKeep this running in the background
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 8000Backend will be available at: http://localhost:8000
cd frontend
npm run devThe frontend will be available at
http://localhost:3000
- CSV (.csv)
- Excel (.xlsx, .xls)
- JSON (.json)
-
Click the "Upload Data" button in the header
-
Drag and drop your file or click to browse
- Maximum file size: 10MB
- The system will automatically detect column types
-
Preview your data
- See the first 5 rows
- Review the detected schema
- Check row and column counts
-
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"
- 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
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,EastThen ask:
- "What's the total revenue by region?"
- "Which product sold the most quantity?"
- "Show me sales from January 2024"
-
Open your browser to
http://localhost:3000 -
View the database schema in the left sidebar to see available tables
-
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"
-
Click "Generate SQL Query" - Wait ~5-10 seconds for Ollama to process
-
Review the generated SQL in the Monaco Editor
-
Click "Execute Query" to run the SQL and see results
-
View query history in the sidebar to reload previous queries
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
Contributions are welcome! Please follow these steps:
- 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.
- Issues: GitHub Issues
- Discussions: GitHub Discussions
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!