CoolFace
Apppublic

DEKU02/nl2sql

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
App README

๐Ÿง  Multi-Domain NLโ†’SQL Copilot

A production-quality Natural Language โ†’ SQL system that converts plain English questions into executable SQL across three real-world databases โ€” using schema-aware RAG, multiple LLM backends, and strict SQL guardrails.

Live Demo

Pick a database, enter your API key in the sidebar, ask a question, and click Run SQL.

Databases:

  • โ€”๐Ÿ™๏ธ NYC 311 โ€” 300k+ government service requests (complaints, agencies, boroughs)
  • โ€”๐Ÿ›’ Olist E-Commerce โ€” 100k+ Brazilian e-commerce orders (sellers, products, reviews)
  • โ€”๐Ÿฅ Synthea Healthcare โ€” 10k synthetic patients (encounters, conditions, medications)

Example questions:

  • โ€”"What are the top 5 most common complaint types across all boroughs?"
  • โ€”"Which product category generates the most total revenue?"
  • โ€”"How many patients have diabetes as a condition?"

How It Works

  1. 1.Schema RAG โ€” your question is embedded and matched against pre-indexed schema chunks (ChromaDB + sentence-transformers)
  2. 2.Prompt construction โ€” top-K schema chunks + domain glossary + few-shot examples โ†’ structured prompt
  3. 3.LLM generation โ€” generates SQL (SELECT-only, guardrails enforced)
  4. 4.Safe execution โ€” SQL is validated (no DDL/DML), LIMIT is injected, then run against a bundled SQLite database
  5. 5.Results โ€” table view + CSV download

Benchmark Results

ModelAccuracy (59 gold queries)
OpenAI gpt-4o-mini96.6%
Groq llama-3.3-70b-versatile (free)96.6%
Groq llama-3.1-8b-instant (free)94.9%
Ollama llama3.2:3b (local)67.8%

Supported LLM Backends

BackendKey requiredCost
โšก Groq llama-3.3-70bFree at console.groq.com$0.00
๐Ÿค– OpenAI gpt-4o-miniplatform.openai.com~$0.03/59 queries
๐Ÿค— HuggingFace Inferencehuggingface.co/settings/tokensFree tier
๐Ÿ–ฅ๏ธ Ollama (local)No key needed$0.00

Enabling the Olist Database

Olist data comes from Kaggle and cannot be downloaded during Docker build (secrets aren't available at that stage). To enable it:

  1. 1.Get a free Kaggle account at kaggle.com
  2. 2.Go to Settings โ†’ API โ†’ Create New Token to get your credentials
  3. 3.In your Space settings, add two Secrets:
  4. 4.KAGGLE_USERNAME โ€” your Kaggle username
  5. 5.KAGGLE_KEY โ€” your API key
  6. 6.Restart the Space โ€” Olist will build automatically on first boot (~2 min)

Without these secrets, the Space runs with NYC 311 and Synthea only (both auto-download).

Tech Stack

  • โ€”RAG: ChromaDB + sentence-transformers (all-MiniLM-L6-v2)
  • โ€”LLM: Groq / OpenAI / HuggingFace / Ollama
  • โ€”DB: SQLite (Spaces) / PostgreSQL (local Docker)
  • โ€”Guardrails: sqlglot AST parsing โ€” SELECT/WITH only, LIMIT enforcement
  • โ€”UI: Streamlit
  • โ€”Tests: 77 pytest unit tests