CoolFace
Apppublic

melikakheirieh/nl2sql-copilot-prototype

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
App README

🧠 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:

FileDescriptionDownload
Chinook_Sqlite.sqliteClassic sample DB with artists, albums, and tracks (music store example).⬇️ Download
WMSales.sqliteSimple sales database (for demoing aggregate and filter queries).⬇️ Download

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:

  1. 1.β€œList the top 5 artists by total track count.”
  2. 2.β€œWhich album has the most tracks?”
  3. 3.β€œShow all tracks longer than 6 minutes.”
  4. 4.β€œFind the average track length by genre.”
  5. 5.β€œShow total invoice amount by billing country.”
  6. 6.β€œTop 10 most popular genres by number of tracks.”
  7. 7.β€œHow many customers have purchased Jazz albums?”
  8. 8.β€œShow the total revenue by employee (sales support).”
  9. 9.β€œList customers who spent more than $100.”
  10. 10.β€œWhich customers are from Canada?”

πŸ“Š Sample Questions for WMSales.sqlite

You can try:

  1. 1.β€œShow total sales per month in 2024.”
  2. 2.β€œList the top 10 customers by revenue.”
  3. 3.β€œWhich product category had the highest sales this year?”
  4. 4.β€œFind the average unit price per product.”
  5. 5.β€œShow all orders placed in the last 30 days.”
  6. 6.β€œList total sales by region and salesperson.”
  7. 7.β€œWhat is the best-selling product overall?”
  8. 8.β€œShow total discount given per month.”
  9. 9.β€œFind customers who made more than 5 purchases.”
  10. 10.β€œ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:

bash
cp .env.example .env

.env.example (proxy-agnostic):

bash
# ---- 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

bash
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 browser

Upload 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 key
  • β€”PROXY_BASE_URL: e.g., https://.../v1
  • β€”(Optional) OPENAI_API_KEY and OPENAI_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 .env out 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