Skip to content

Commit e3d6d6c

Browse files
Backend: migrate to pydantic-settings, update Groq model, add env ignores; tests passing; demo verified
1 parent b420b58 commit e3d6d6c

28 files changed

Lines changed: 634 additions & 211 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ celerybeat.pid
121121

122122
# Environments
123123
.env
124+
backend/.env
124125
.venv
125126
env/
126127
venv/

README.md

Lines changed: 81 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,68 @@
1+
# Text-to-SQL Platform (FastAPI + Groq)
12

2-
# SQL Query Generator with Google Gemini
3+
This repository provides a modular backend that converts natural language into SQL and executes it on an SQLite database (`student.db`).
34

4-
This project is a Streamlit application that converts English questions into SQL queries using Google Gemini's generative AI capabilities. It allows users to retrieve data from an SQLite database named **STUDENT**, which contains information about students, their classes, sections, and marks.
5+
The backend is built with FastAPI and uses Groq’s OpenAI-compatible API to generate SQL from English questions. A separate modern UI will be added by the frontend team.
56

67
## Table of Contents
78

89
- [Features](#features)
910
- [Technologies Used](#technologies-used)
10-
- [Installation](#installation)
11-
- [Usage](#usage)
11+
- [Getting Started](#getting-started)
1212
- [Example Queries](#example-queries)
1313
- [Database Schema](#database-schema)
14+
- [API Endpoints (v1)](#api-endpoints-v1)
15+
- [Testing & Coverage](#testing--coverage)
16+
- [Roadmap](#roadmap)
1417
- [Contributing](#contributing)
1518
- [License](#license)
19+
- [License](#license)
1620

1721
## Features
1822

19-
- Convert natural language questions into SQL queries.
20-
- Execute generated SQL queries against an SQLite database.
21-
- User-friendly interface built with Streamlit.
23+
- Convert natural language questions into SQL queries (Groq).
24+
- Execute generated SQL queries against the SQLite database.
25+
- Clean SQL responses (strip markdown, enforce semicolon).
26+
- Modern FastAPI backend with auto docs at `/docs`.
27+
- Full test suite with coverage and temporary DB isolation.
2228

2329
## Technologies Used
2430

25-
- [Streamlit](https://streamlit.io/) - For building the web application.
26-
- [SQLite](https://www.sqlite.org/index.html) - Lightweight database to store student records.
27-
- [Google Generative AI](https://developers.google.com/generative-ai) - To generate SQL queries from text input.
28-
- [Python](https://www.python.org/) - Programming language used to build the application.
29-
30-
## Installation
31-
32-
To get started, clone the repository and install the required dependencies.
33-
34-
### Prerequisites
35-
36-
- Python 3.7 or higher
37-
- pip (Python package installer)
38-
39-
### Steps to Install
31+
- [FastAPI](https://fastapi.tiangolo.com/) for the backend API
32+
- [Groq](https://groq.com/) for NL→SQL (OpenAI-compatible API)
33+
- [SQLite](https://www.sqlite.org/) for the demo database
34+
- [Pydantic](https://docs.pydantic.dev/) for validation
35+
- [Pytest](https://docs.pytest.org/) for testing and coverage
4036

41-
1. **Clone the repository:**
42-
```bash
43-
git clone https://github.com/yourusername/sql-query-generator.git
44-
cd sql-query-generator
45-
```
37+
## Getting Started
4638

47-
2. **Create a virtual environment (optional but recommended):**
48-
```bash
49-
python -m venv venv
50-
source venv/bin/activate # On Windows use `venv\Scripts\activate`
51-
```
39+
Backend is located in `backend/`. You can use Conda for a reproducible setup.
5240

53-
3. **Install required packages:**
54-
```bash
55-
pip install -r requirements.txt
56-
```
57-
58-
4. **Set up your environment variables:**
59-
- Create a `.env` file in the project root and add your Google API key:
60-
```
61-
GOOGLE_API_KEY=your_api_key_here
62-
```
41+
1) Create environment (Option A: from file)
42+
```bash
43+
conda env create -f backend/environment.yml
44+
conda activate text2sql-backend
45+
```
6346

64-
## Usage
47+
Or Option B (manual):
48+
```bash
49+
conda create -n text2sql-backend python=3.11 -y
50+
conda activate text2sql-backend
51+
pip install -e backend[dev]
52+
```
6553

66-
1. Run the Streamlit application:
67-
```bash
68-
streamlit run app.py
69-
```
54+
2) Configure environment variables
55+
```bash
56+
cp backend/.env.example backend/.env
57+
# edit backend/.env and set GROQ_API_KEY and (optionally) GROQ_MODEL
58+
```
7059

71-
2. Open your browser and go to `http://localhost:8501`.
60+
3) Run the API (from repo root)
61+
```bash
62+
uvicorn app.main:app --reload --port 8000 --app-dir backend
63+
```
7264

73-
3. Input your question in the text box and click the "Ask the question" button. The app will generate an SQL query based on your input and execute it against the database, displaying the results.
65+
Open API docs at: http://127.0.0.1:8000/docs
7466

7567
## Example Queries
7668

@@ -82,14 +74,49 @@ Here are some example questions you can ask:
8274

8375
## Database Schema
8476

85-
The database **STUDENT** has the following schema:
77+
The database **student.db** has the following schema:
8678

8779
| Column | Type | Description |
8880
|---------|---------|--------------------------------------|
89-
| NAME | TEXT | Name of the student |
90-
| CLASS | TEXT | Class of the student |
91-
| SECTION | TEXT | Section of the student |
92-
| MARKS | INTEGER | Marks obtained by the student |
81+
| NAME | VARCHAR(25) | Name of the student |
82+
| CLASS | VARCHAR(25) | Class of the student |
83+
| SECTION | VARCHAR(25) | Section of the student |
84+
| MARKS | INT | Marks obtained by the student |
85+
86+
## API Endpoints (v1)
87+
88+
- GET `/api/v1/health` → health check
89+
- GET `/api/v1/students` → list all students
90+
- POST `/api/v1/sql` → execute provided SQL
91+
- POST `/api/v1/nl2sql` → convert NL to SQL using Groq
92+
93+
Example curl (after starting the server):
94+
```bash
95+
curl http://127.0.0.1:8000/api/v1/health
96+
curl http://127.0.0.1:8000/api/v1/students
97+
curl -X POST http://127.0.0.1:8000/api/v1/sql \
98+
-H 'Content-Type: application/json' \
99+
-d '{"sql":"SELECT COUNT(*) FROM STUDENT;"}'
100+
curl -X POST http://127.0.0.1:8000/api/v1/nl2sql \
101+
-H 'Content-Type: application/json' \
102+
-d '{"question":"How many entries of records are present?"}'
103+
```
104+
105+
## Testing & Coverage
106+
107+
```bash
108+
cd backend
109+
pytest -q --cov=app --cov-report=term-missing
110+
```
111+
112+
Tests use a temporary SQLite DB and do not touch your real `student.db`.
113+
114+
## Roadmap
115+
116+
- Frontend UI integration (modern SPA)
117+
- AuthN/Z and rate limiting
118+
- Schema introspection and multi-table support
119+
- Safer SQL generation and validation guardrails
93120

94121
## Contributing
95122

@@ -105,23 +132,4 @@ Contributions are welcome! Please open an issue or submit a pull request if you'
105132

106133
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
107134

108-
```
109-
110-
### Explanation of Sections
111-
112-
1. **Title & Introduction:** Gives an overview of what the project is about.
113-
2. **Table of Contents:** Helps users navigate the document quickly.
114-
3. **Features:** Highlights the main features of the app.
115-
4. **Technologies Used:** Lists the technologies and frameworks involved in the project.
116-
5. **Installation:** Step-by-step guide on how to install and set up the project.
117-
6. **Usage:** Instructions on how to run the app and interact with it.
118-
7. **Example Queries:** Provides sample questions users can input to test the functionality.
119-
8. **Database Schema:** Describes the structure of the SQLite database.
120-
9. **Contributing:** Encourages collaboration and outlines how others can contribute to the project.
121-
10. **License:** States the licensing information for the project.
122-
123-
### Tips for Customization
124135

125-
- Replace placeholders like `yourusername` and `your_api_key_here` with actual values relevant to your project.
126-
- Adjust the content based on any additional features or changes you have made.
127-
- Make sure to include any additional documentation or instructions relevant to your specific project needs.

app.py

Lines changed: 0 additions & 95 deletions
This file was deleted.

backend/.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copy this file to .env and fill in the values
2+
APP_NAME=Text-to-SQL Backend
3+
SQLITE_PATH=../student.db
4+
5+
# Groq settings
6+
GROQ_API_KEY="your_groq_api_key_here"
7+
GROQ_MODEL="llama-3.3-70b-versatile"

0 commit comments

Comments
 (0)