A FastAPI-based application that converts natural language questions into SQL queries using the Hugging Face Inference API. This project uses the DeepSeek model to interpret user queries, generate SQL commands, execute them on a database, and return the results — all automatically.
- Converts natural language questions into SQL queries
- Executes the generated SQL on a local SQLite database
- Uses Hugging Face Inference API (no local model load required)
- Lightweight and fast (no GPU dependency)
- Built with FastAPI + Uvicorn
| Component | Technology |
|---|---|
| Backend Framework | FastAPI |
| Language Model | DeepSeek (Hugging Face API) |
| Database | SQLite |
| Environment | Python 3.10+ |
| Deployment | Uvicorn (for local or production runs) |
git clone https://github.com/<your-username>/text-to-sql-huggingface.git
cd text-to-sql-huggingfacepython3 -m venv venv
source venv/bin/activate # For Linux/Mac
venv\Scripts\activate # For Windowspip install -r requirements.txtYour requirements.txt should include:
fastapi
uvicorn
sqlite-utils
python-dotenv
huggingface-hub
Create a .env file in your project root:
HF_API_KEY=your_huggingface_api_key_here
HF_MODEL=deepseek-ai/DeepSeek-V3.2-Exp
DATABASE_PATH=./sample.db
🔑 You can get your Hugging Face API key from: https://huggingface.co/settings/tokens
You can create a simple SQLite database like this:
import sqlite3
conn = sqlite3.connect("sample.db")
cur = conn.cursor()
cur.execute("""
CREATE TABLE customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
email TEXT,
city TEXT,
join_date TEXT
);
""")
cur.executemany("""
INSERT INTO customers (name, email, city, join_date)
VALUES (?, ?, ?, ?)
""", [
("John Doe", "john@example.com", "Delhi", "2023-01-01"),
("Jane Smith", "jane@example.com", "Mumbai", "2022-05-14"),
("Ravi Kumar", "ravi@example.com", "Delhi", "2021-11-11")
])
conn.commit()
conn.close()text-to-sql-huggingface/
│
├── main.py # FastAPI entrypoint
├── llm_utils.py # Handles Hugging Face API calls
├── database.py # SQLite helper functions
├── requirements.txt
├── .env
└── README.md
The DeepSeek model receives a minimal structured prompt:
You are a Text-to-SQL assistant.
Given a database schema and a user question, write a single SELECT query.
Return only the SQL query — no explanations or markdown.
Example query input:
{ "question": "List names and emails of customers from Delhi" }Expected model output:
SELECT name, email FROM customers WHERE city = 'Delhi';Start the FastAPI server:
uvicorn main:app --reloadNow open your browser or use cURL:
curl -X POST "http://127.0.0.1:8000/query" \
-H "Content-Type: application/json" \
-d '{"question": "List names and emails of customers from Delhi"}'Example JSON Response:
{
"sql": "SELECT name, email FROM customers WHERE city = 'Delhi';",
"result": [
{ "name": "John Doe", "email": "john@example.com" },
{ "name": "Ravi Kumar", "email": "ravi@example.com" }
]
}You can test your Hugging Face connection directly:
from huggingface_hub import InferenceClient
import os
client = InferenceClient(api_key=os.getenv("HF_API_KEY"))
response = client.chat.completions.create(
model=os.getenv("HF_MODEL"),
messages=[
{"role": "system", "content": "Return only SQL query"},
{"role": "user", "content": "Show customers from Delhi"}
]
)
print(response.choices[0].message.content)| Error | Reason | Fix |
|---|---|---|
400: Only single SELECT queries allowed |
Model returned multiple statements or explanations | Refine your prompt to ensure only one SELECT query |
401 Unauthorized |
Invalid Hugging Face API key | Check .env file and re-add token |
Empty or invalid SQL output |
Model response not formatted as SQL | Use improved prompt (see llm_utils.py) |
- Support multiple databases (MySQL, PostgreSQL)
- Add web-based UI for interactive queries
- Integrate OpenAI or Ollama models as alternatives
- Include query optimization layer
For a detailed guide on the project concept, visit: 👉 Building Text-to-SQL Apps with Hugging Face API and FastAPI
Bhai Tabahi 🎥 YouTube: CodeInfiltr4tor 💻 GitHub: github.com/CodeInfiltr4tor 🚀 Projects on FastAPI • Cybersecurity • AI Apps
This project is licensed under the MIT License — feel free to use and modify it.