melikakheirieh/nl2sql-copilot-prototype
π§ NL2SQL Copilot β Prototype
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.
π Live Demo on Hugging Face Spaces
Status: Prototype (v0.1). This demonstrates structure and UX; advanced safety/verification pipelines are planned.
β¨ Features (v0.1)
- Gradio UI for quick interactions
- Config-driven environment (dotenv)
- Pluggable LLM endpoint (proxy or direct OpenAI)
- SQLite read-only connection (no data mutation)
Planned next:
- Query planning and verification
- Safer SQL guardrails (AST / blocklist / dialect checks)
- Self-repair on failed queries
- Semantic cache and telemetry
π Project Structure
nl2sql-copilot-prototype/
ββ app.py
ββ config.py
ββ requirements.txt
ββ .env.example
ββ .gitignore
ββ README.md
π§© Database Samples
Two example SQLite databases are included in the db/ folder for quick testing:
You can use them directly in the Gradio UI by uploading one of these files, or reference them in code for local runs.
π§ Sample Questions for Chinook_Sqlite.sqlite
Try asking your copilot questions like:
- βList the top 5 artists by total track count.β
- βWhich album has the most tracks?β
- βShow all tracks longer than 6 minutes.β
- βFind the average track length by genre.β
- βShow total invoice amount by billing country.β
- βTop 10 most popular genres by number of tracks.β
- βHow many customers have purchased Jazz albums?β
- βShow the total revenue by employee (sales support).β
- βList customers who spent more than $100.β
- βWhich customers are from Canada?β
π Sample Questions for WMSales.sqlite
You can try:
- βShow total sales per month in 2024.β
- βList the top 10 customers by revenue.β
- βWhich product category had the highest sales this year?β
- βFind the average unit price per product.β
- βShow all orders placed in the last 30 days.β
- βList total sales by region and salesperson.β
- βWhat is the best-selling product overall?β
- βShow total discount given per month.β
- βFind customers who made more than 5 purchases.β
- βWhatβs the total revenue by payment method?β ---
βοΈ Requirements
- Python 3.10+
- A proxy/provider API key (OpenAI / custom proxy)
- SQLite DB file (uploaded via UI)
π Environment Variables
Copy the example and fill your own values:
cp .env.example .env.env.example (proxy-agnostic):
# ---- LLM provider or proxy (preferred) ----
PROXY_API_KEY="your-proxy-or-provider-api-key"
PROXY_BASE_URL="https://your-proxy-or-provider-base-url/v1"
# ---- Optional direct OpenAI fallback ----
#OPENAI_API_KEY="your-openai-api-key"
#OPENAI_BASE_URL="https://api.openai.com/v1"config.py should select PROXY_* first; if empty, it falls back to OPENAI_*.
π§ͺ Local Quickstart
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env # then edit .env and add your keys
python app.py # open the Gradio link in browserUpload a SQLite file and try a prompt like:
βTop 5 customers by total orders in 2024.β
π§° Safety Notes (Prototype)
- 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). - Consider an AST approach (e.g.,
sqlglot) for a stricter parse/allow-list.
βοΈ Deploy to Hugging Face Spaces (Gradio)
1) Create a new Space
- Go to Hugging Face β Spaces β New Space
- Name:
nl2sql-copilot-prototype - Space SDK: Gradio
- Hardware: CPU Basic
- Visibility: Public (or Private)
2) Add project files
Commit/push these files to the Space repo:
app.py,config.py,requirements.txt,.env.example,README.md,.gitignore
3) Set Secrets (Variables and secrets)
In Space β Settings β Variables and secrets:
PROXY_API_KEY: your real keyPROXY_BASE_URL: e.g.,https://.../v1- (Optional)
OPENAI_API_KEYandOPENAI_BASE_URL
Do not commit a real .env. Use Space Secrets.4) Build & Run
- Spaces auto-install from
requirements.txt. - If not auto-started, set App file: main.py, SDK: Gradio, Python: 3.10+.
5) Test
- Open Space URL
- Upload a small sample SQLite DB
- Check Logs tab for errors
Persistence note: Uploads are ephemeral; include a tiny demo DB in the repo if needed.
π§ Usage Tips
- Prefer concise prompts (e.g., βShow avg price by category for 2023β).
- If a query fails, rephrase or reduce columns.
- For bigger DBs, add a schema introspection step or a βDescribe tablesβ helper.
π‘οΈ Security & Privacy
- Never log raw API keys.
- Keep
.envout of Git; commit only.env.example. - Enforce read-only and block multi-statement SQL.
πΊοΈ Roadmap
- [ ] Planner β Generator β Safety β Executor β Verifier loop
- [ ] AST-based guardrails (sqlglot)
- [ ] Self-repair on DB/SQL errors
- [ ] Semantic cache + telemetry
- [ ] Streamlit / FastAPI variants
