Spaces:
Sleeping
Sleeping
Melika Kheirieh
commited on
Commit
Β·
8e92467
1
Parent(s):
570f7bd
docs: update README with project overview
Browse files
README.md
CHANGED
|
@@ -1,198 +1,99 @@
|
|
| 1 |
-
|
| 2 |
-
title: NL2SQL Copilot
|
| 3 |
-
emoji: π§
|
| 4 |
-
colorFrom: indigo
|
| 5 |
-
colorTo: purple
|
| 6 |
-
sdk: gradio
|
| 7 |
-
python_version: "3.11"
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
---
|
| 11 |
-
# π§ NL2SQL Copilot β Prototype
|
| 12 |
-
|
| 13 |
-
A minimal **Text-to-SQL Copilot** built with **LangChain + Gradio**, designed to translate natural language questions into **safe SQL** and run them on a **read-only SQLite** database.
|
| 14 |
-
|
| 15 |
-
π [Live Demo on Hugging Face Spaces](https://huggingface.co/spaces/melikakheirieh/nl2sql-copilot-prototype)
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
---
|
| 21 |
|
| 22 |
-
##
|
| 23 |
-
- Gradio UI for quick interactions
|
| 24 |
-
- Config-driven environment (dotenv)
|
| 25 |
-
- Pluggable LLM endpoint (proxy or direct OpenAI)
|
| 26 |
-
- SQLite **read-only** connection (no data mutation)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
## π Project Structure
|
| 37 |
-
```
|
| 38 |
-
nl2sql-copilot-prototype/
|
| 39 |
-
ββ app.py
|
| 40 |
-
ββ config.py
|
| 41 |
-
ββ requirements.txt
|
| 42 |
-
ββ .env.example
|
| 43 |
-
ββ .gitignore
|
| 44 |
-
ββ README.md
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
```
|
| 47 |
-
## π§© Database Samples
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
| File | Description | Download |
|
| 52 |
-
|------|--------------|-----------|
|
| 53 |
-
| **Chinook_Sqlite.sqlite** | Classic sample DB with artists, albums, and tracks (music store example). | [β¬οΈ Download](https://github.com/melika-kheirieh/nl2sql-copilot-prototype/raw/main/db/Chinook_Sqlite.sqlite) |
|
| 54 |
-
| **WMSales.sqlite** | Simple sales database (for demoing aggregate and filter queries). | [β¬οΈ Download](https://github.com/melika-kheirieh/nl2sql-copilot-prototype/raw/main/db/WMSales.sqlite) |
|
| 55 |
-
|
| 56 |
-
You can use them directly in the Gradio UI by uploading one of these files, or reference them in code for local runs.
|
| 57 |
|
| 58 |
---
|
| 59 |
|
| 60 |
-
|
| 61 |
-
Try asking your copilot questions like:
|
| 62 |
-
|
| 63 |
-
1. βList the top 5 artists by total track count.β
|
| 64 |
-
2. βWhich album has the most tracks?β
|
| 65 |
-
3. βShow all tracks longer than 6 minutes.β
|
| 66 |
-
4. βFind the average track length by genre.β
|
| 67 |
-
5. βShow total invoice amount by billing country.β
|
| 68 |
-
6. βTop 10 most popular genres by number of tracks.β
|
| 69 |
-
7. βHow many customers have purchased Jazz albums?β
|
| 70 |
-
8. βShow the total revenue by employee (sales support).β
|
| 71 |
-
9. βList customers who spent more than $100.β
|
| 72 |
-
10. βWhich customers are from Canada?β
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
---
|
| 76 |
|
| 77 |
-
|
| 78 |
-
You can try:
|
| 79 |
-
|
| 80 |
-
1. βShow total sales per month in 2024.β
|
| 81 |
-
2. βList the top 10 customers by revenue.β
|
| 82 |
-
3. βWhich product category had the highest sales this year?β
|
| 83 |
-
4. βFind the average unit price per product.β
|
| 84 |
-
5. βShow all orders placed in the last 30 days.β
|
| 85 |
-
6. βList total sales by region and salesperson.β
|
| 86 |
-
7. βWhat is the best-selling product overall?β
|
| 87 |
-
8. βShow total discount given per month.β
|
| 88 |
-
9. βFind customers who made more than 5 purchases.β
|
| 89 |
-
10. βWhatβs the total revenue by payment method?β
|
| 90 |
-
---
|
| 91 |
-
|
| 92 |
-
## βοΈ Requirements
|
| 93 |
-
- Python 3.10+
|
| 94 |
-
- A proxy/provider API key (OpenAI / custom proxy)
|
| 95 |
-
- SQLite DB file (uploaded via UI)
|
| 96 |
-
|
| 97 |
-
---
|
| 98 |
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
|
| 103 |
```bash
|
| 104 |
-
|
| 105 |
```
|
| 106 |
|
| 107 |
-
|
| 108 |
-
```bash
|
| 109 |
-
# ---- LLM provider or proxy (preferred) ----
|
| 110 |
-
PROXY_API_KEY="your-proxy-or-provider-api-key"
|
| 111 |
-
PROXY_BASE_URL="https://your-proxy-or-provider-base-url/v1"
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
#OPENAI_BASE_URL="https://api.openai.com/v1"
|
| 116 |
```
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
---
|
| 121 |
-
|
| 122 |
-
## π§ͺ Local Quickstart
|
| 123 |
|
| 124 |
```bash
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
pip install -r requirements.txt
|
| 128 |
-
cp .env.example .env # then edit .env and add your keys
|
| 129 |
-
python app.py # open the Gradio link in browser
|
| 130 |
```
|
| 131 |
|
| 132 |
-
Upload a SQLite file and try a prompt like:
|
| 133 |
-
> βTop 5 customers by total orders in 2024.β
|
| 134 |
-
|
| 135 |
---
|
| 136 |
|
| 137 |
-
##
|
| 138 |
-
- DB is opened in **read-only** mode, but you should still block multi-statement payloads and dangerous tokens (e.g., `ATTACH`, `PRAGMA`, `sqlite_master`, DDL/INSERT/UPDATE/DELETE).
|
| 139 |
-
- Consider an AST approach (e.g., `sqlglot`) for a stricter parse/allow-list.
|
| 140 |
-
|
| 141 |
-
---
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
-
|
| 149 |
-
- **Hardware:** CPU Basic
|
| 150 |
-
- **Visibility:** Public (or Private)
|
| 151 |
-
|
| 152 |
-
### 2) Add project files
|
| 153 |
-
Commit/push these files to the Space repo:
|
| 154 |
-
- `app.py`, `config.py`, `requirements.txt`, `.env.example`, `README.md`, `.gitignore`
|
| 155 |
-
|
| 156 |
-
### 3) Set Secrets (Variables and secrets)
|
| 157 |
-
In Space β **Settings β Variables and secrets**:
|
| 158 |
-
- `PROXY_API_KEY`: your real key
|
| 159 |
-
- `PROXY_BASE_URL`: e.g., `https://.../v1`
|
| 160 |
-
- (Optional) `OPENAI_API_KEY` and `OPENAI_BASE_URL`
|
| 161 |
-
|
| 162 |
-
> Do **not** commit a real `.env`. Use Space **Secrets**.
|
| 163 |
-
|
| 164 |
-
### 4) Build & Run
|
| 165 |
-
- Spaces auto-install from `requirements.txt`.
|
| 166 |
-
- If not auto-started, set **App file: main.py**, SDK: **Gradio**, Python: **3.10+**.
|
| 167 |
-
|
| 168 |
-
### 5) Test
|
| 169 |
-
- Open Space URL
|
| 170 |
-
- Upload a small sample SQLite DB
|
| 171 |
-
- Check **Logs** tab for errors
|
| 172 |
-
|
| 173 |
-
**Persistence note:** Uploads are ephemeral; include a tiny demo DB in the repo if needed.
|
| 174 |
|
| 175 |
---
|
| 176 |
|
| 177 |
-
##
|
| 178 |
-
- Prefer concise prompts (e.g., βShow avg price by category for 2023β).
|
| 179 |
-
- If a query fails, rephrase or reduce columns.
|
| 180 |
-
- For bigger DBs, add a schema introspection step or a βDescribe tablesβ helper.
|
| 181 |
-
|
| 182 |
-
---
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
---
|
| 190 |
|
| 191 |
-
##
|
| 192 |
-
- [ ] Planner β Generator β Safety β Executor β Verifier loop
|
| 193 |
-
- [ ] AST-based guardrails (sqlglot)
|
| 194 |
-
- [ ] Self-repair on DB/SQL errors
|
| 195 |
-
- [ ] Semantic cache + telemetry
|
| 196 |
-
- [ ] Streamlit / FastAPI variants
|
| 197 |
|
|
|
|
| 198 |
|
|
|
|
| 1 |
+
# π§© NL2SQL Copilot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
A modular **Text-to-SQL Copilot** that converts natural language questions into safe and verified SQL queries.
|
| 4 |
+
Built with **FastAPI**, **LangGraph**, and **SQLAlchemy**, designed for read-only databases and evaluation on Spider/Dr.Spider benchmarks.
|
| 5 |
|
| 6 |
---
|
| 7 |
|
| 8 |
+
## π Quick Start
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
### 1οΈβ£ Clone the repo
|
| 11 |
+
```bash
|
| 12 |
+
git clone https://github.com/melika-kheirieh/nl2sql-copilot.git
|
| 13 |
+
cd nl2sql-copilot
|
| 14 |
+
````
|
| 15 |
|
| 16 |
+
### 2οΈβ£ Build and run with Docker
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
```bash
|
| 19 |
+
docker build -t nl2sql-copilot .
|
| 20 |
+
docker run --rm -p 8000:8000 nl2sql-copilot
|
| 21 |
```
|
|
|
|
| 22 |
|
| 23 |
+
Then open [http://localhost:8000/docs](http://localhost:8000/docs) π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
---
|
| 26 |
|
| 27 |
+
## π§± Project Structure
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
```
|
| 30 |
+
nl2sql-copilot/
|
| 31 |
+
β
|
| 32 |
+
βββ app/ # FastAPI app, routers, schemas
|
| 33 |
+
βββ nl2sql/ # Core pipeline (planner β generator β safety β executor β verifier)
|
| 34 |
+
βββ adapters/ # Database and LLM adapters
|
| 35 |
+
βββ benchmarks/ # Evaluation scripts and results
|
| 36 |
+
βββ ui/ # Streamlit dashboard
|
| 37 |
+
β
|
| 38 |
+
βββ Dockerfile
|
| 39 |
+
βββ requirements.in
|
| 40 |
+
βββ requirements.txt
|
| 41 |
+
βββ README.md
|
| 42 |
+
```
|
| 43 |
|
| 44 |
---
|
| 45 |
|
| 46 |
+
## π§ͺ Development
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
### Install dependencies
|
| 49 |
|
| 50 |
+
(Recommended: Python 3.12+ and virtualenv)
|
| 51 |
|
| 52 |
```bash
|
| 53 |
+
pip install -r requirements.txt
|
| 54 |
```
|
| 55 |
|
| 56 |
+
### Run tests
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
```bash
|
| 59 |
+
pytest -q
|
|
|
|
| 60 |
```
|
| 61 |
|
| 62 |
+
### Lint and type-check
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
```bash
|
| 65 |
+
ruff check .
|
| 66 |
+
mypy .
|
|
|
|
|
|
|
|
|
|
| 67 |
```
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
---
|
| 70 |
|
| 71 |
+
## π§ Features
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
* β
Modular multi-stage pipeline (Planner β Generator β Safety β Executor β Verifier β Repair)
|
| 74 |
+
* π‘οΈ SQL safety filters (SELECT-only, forbidden keywords)
|
| 75 |
+
* π Self-repair loop on failed executions
|
| 76 |
+
* π Streamlit benchmark dashboard (latency, accuracy, cost)
|
| 77 |
+
* π§© PostgreSQL + SQLite adapters
|
| 78 |
+
* π§ Powered by `pydantic-ai` and `LangGraph`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
---
|
| 81 |
|
| 82 |
+
## π§° Tech Stack
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
| Layer | Tools |
|
| 85 |
+
| ---------------- | --------------------------------------- |
|
| 86 |
+
| Backend API | FastAPI, Uvicorn |
|
| 87 |
+
| Pipeline Core | Python 3.12, Pydantic, SQLGlot |
|
| 88 |
+
| LLM Interface | pydantic-ai (OpenAI, Anthropic, Ollama) |
|
| 89 |
+
| Database | SQLite (default), PostgreSQL |
|
| 90 |
+
| Evaluation | Spider / Dr.Spider |
|
| 91 |
+
| UI | Streamlit + Plotly |
|
| 92 |
+
| Containerization | Docker / Docker Compose |
|
| 93 |
|
| 94 |
---
|
| 95 |
|
| 96 |
+
## π License
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
MIT Β© 2025 Melika Kheirieh
|
| 99 |
|