File size: 4,631 Bytes
eff938e
 
 
 
 
 
 
 
 
 
b97c8b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eff938e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---
title: 2 Natural Lang2SQL Query
emoji: 🐠
colorFrom: indigo
colorTo: blue
sdk: gradio
sdk_version: 5.30.0
app_file: app.py
pinned: false
---
# 🧠 Natural Language to SQL Query Generator with Results 🧠

Convert human-readable questions into valid SQL queries using a combination of **semantic search**, **LLMs**, and **Gradio UI** β€” and see the **query results** live from your database!

## πŸš€ Features

* πŸ” **Understand Natural Language:** Converts simple English questions to SQL using powerful embeddings and LLMs (LLaMA3 via Groq).
* 🧠 **Smart Table Matching:** Uses SentenceTransformer-based embeddings and cosine similarity to pick the most relevant tables.
* πŸ› οΈ **Dynamic SQL Generation:** Automatically generates JOINs when needed based on metadata.
* πŸ“„ **Predefined Schema:** Works with three tables β€” `student`, `employee`, and `course`.
* πŸ§ͺ **Instant Execution:** Runs the generated SQL query and shows the result in the UI.
* 🌐 **Interactive UI:** Clean, web-based interface built with Gradio.

---

## 🧰 Tech Stack

* Python 🐍
* [Gradio](https://gradio.app/) β€” For the frontend UI
* [Sentence Transformers](https://www.sbert.net/) β€” For encoding metadata and queries
* [Groq API](https://console.groq.com/) β€” For LLaMA3-powered SQL generation
* SQLite β€” Lightweight DB backend

---

## πŸ—ƒοΈ Tables Overview

### πŸ§‘β€πŸŽ“ `student`

| Column               | Description          |
| -------------------- | -------------------- |
| student\_id          | Unique ID of student |
| first\_name          | First name           |
| last\_name           | Last name            |
| date\_of\_birth      | Birthdate            |
| email                | Email address        |
| phone\_number        | Contact number       |
| major                | Field of study       |
| year\_of\_enrollment | Year enrolled        |

### πŸ‘¨β€πŸ’Ό `employee`

| Column            | Description           |
| ----------------- | --------------------- |
| employee\_id      | Unique ID of employee |
| first\_name       | First name            |
| last\_name        | Last name             |
| email             | Email address         |
| department        | Department name       |
| position          | Job title             |
| salary            | Salary                |
| date\_of\_joining | Joining date          |

### πŸ“˜ `course`

| Column         | Description                    |
| -------------- | ------------------------------ |
| course\_id     | Unique ID of course            |
| course\_name   | Course title                   |
| course\_code   | Code for the course            |
| instructor\_id | Refers to `employee_id`        |
| department     | Department offering the course |
| credits        | Number of credits              |
| semester       | Semester offered               |

---

## βš™οΈ Setup Instructions

### 1. Clone the Repo

```bash
git clone https://github.com/your-username/sql-query-generator.git
cd sql-query-generator
```

### 2. Install Dependencies

```bash
pip install -r requirements.txt
```

### 3. Set Up `.env`

Create a `.env` file in the root directory and add your [Groq API Key](https://console.groq.com/):

```
groq_api_key=your_groq_api_key_here
```

### 4. Launch the App

```bash
python app.py
```

Or just run:

```bash
streamlit run app.py  # If you're using a streamlit version
```

### 5. Use the Web Interface

Go to the URL provided in the terminal (typically `http://127.0.0.1:7860`) and enter your natural language query.

---

## πŸ§ͺ Sample Queries

| Natural Language Query                              | Action                        |
| --------------------------------------------------- | ----------------------------- |
| "List all students enrolled in 2022"                | Filters `student` table       |
| "Show all employees in Computer Science department" | Filters `employee` table      |
| "Get courses taught in Fall semester"               | Filters `course` table        |
| "Find course names and instructor names"            | Joins `course` and `employee` |

---

## πŸ”’ Security & Limitations

* This is a prototype/demo and should not be used in production without input sanitization.
* Currently limited to SELECT queries only.
* Works only with the predefined schema (3 tables).

---

## 🀝 Contributing

Contributions are welcome! Feel free to open an issue or pull request for:

* New table additions
* Better embeddings
* UI/UX improvements
* Model optimization


## πŸ‘¨β€πŸ’» Author

Built by \Jamgala Bala Swaroop
πŸ“« Email: [jbsroop@gmail.com](mailto:jbsroop@gmail.com)