Spaces:
Sleeping
Sleeping
Update README.md
Browse files
README.md
CHANGED
|
@@ -8,5 +8,146 @@ sdk_version: 5.30.0
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
+
# π§ Natural Language to SQL Query Generator with Results π§
|
| 12 |
+
|
| 13 |
+
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!
|
| 14 |
+
|
| 15 |
+
## π Features
|
| 16 |
+
|
| 17 |
+
* π **Understand Natural Language:** Converts simple English questions to SQL using powerful embeddings and LLMs (LLaMA3 via Groq).
|
| 18 |
+
* π§ **Smart Table Matching:** Uses SentenceTransformer-based embeddings and cosine similarity to pick the most relevant tables.
|
| 19 |
+
* π οΈ **Dynamic SQL Generation:** Automatically generates JOINs when needed based on metadata.
|
| 20 |
+
* π **Predefined Schema:** Works with three tables β `student`, `employee`, and `course`.
|
| 21 |
+
* π§ͺ **Instant Execution:** Runs the generated SQL query and shows the result in the UI.
|
| 22 |
+
* π **Interactive UI:** Clean, web-based interface built with Gradio.
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
## π§° Tech Stack
|
| 27 |
+
|
| 28 |
+
* Python π
|
| 29 |
+
* [Gradio](https://gradio.app/) β For the frontend UI
|
| 30 |
+
* [Sentence Transformers](https://www.sbert.net/) β For encoding metadata and queries
|
| 31 |
+
* [Groq API](https://console.groq.com/) β For LLaMA3-powered SQL generation
|
| 32 |
+
* SQLite β Lightweight DB backend
|
| 33 |
+
|
| 34 |
+
---
|
| 35 |
+
|
| 36 |
+
## ποΈ Tables Overview
|
| 37 |
+
|
| 38 |
+
### π§βπ `student`
|
| 39 |
+
|
| 40 |
+
| Column | Description |
|
| 41 |
+
| -------------------- | -------------------- |
|
| 42 |
+
| student\_id | Unique ID of student |
|
| 43 |
+
| first\_name | First name |
|
| 44 |
+
| last\_name | Last name |
|
| 45 |
+
| date\_of\_birth | Birthdate |
|
| 46 |
+
| email | Email address |
|
| 47 |
+
| phone\_number | Contact number |
|
| 48 |
+
| major | Field of study |
|
| 49 |
+
| year\_of\_enrollment | Year enrolled |
|
| 50 |
+
|
| 51 |
+
### π¨βπΌ `employee`
|
| 52 |
+
|
| 53 |
+
| Column | Description |
|
| 54 |
+
| ----------------- | --------------------- |
|
| 55 |
+
| employee\_id | Unique ID of employee |
|
| 56 |
+
| first\_name | First name |
|
| 57 |
+
| last\_name | Last name |
|
| 58 |
+
| email | Email address |
|
| 59 |
+
| department | Department name |
|
| 60 |
+
| position | Job title |
|
| 61 |
+
| salary | Salary |
|
| 62 |
+
| date\_of\_joining | Joining date |
|
| 63 |
+
|
| 64 |
+
### π `course`
|
| 65 |
+
|
| 66 |
+
| Column | Description |
|
| 67 |
+
| -------------- | ------------------------------ |
|
| 68 |
+
| course\_id | Unique ID of course |
|
| 69 |
+
| course\_name | Course title |
|
| 70 |
+
| course\_code | Code for the course |
|
| 71 |
+
| instructor\_id | Refers to `employee_id` |
|
| 72 |
+
| department | Department offering the course |
|
| 73 |
+
| credits | Number of credits |
|
| 74 |
+
| semester | Semester offered |
|
| 75 |
+
|
| 76 |
+
---
|
| 77 |
+
|
| 78 |
+
## βοΈ Setup Instructions
|
| 79 |
+
|
| 80 |
+
### 1. Clone the Repo
|
| 81 |
+
|
| 82 |
+
```bash
|
| 83 |
+
git clone https://github.com/your-username/sql-query-generator.git
|
| 84 |
+
cd sql-query-generator
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
### 2. Install Dependencies
|
| 88 |
+
|
| 89 |
+
```bash
|
| 90 |
+
pip install -r requirements.txt
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
### 3. Set Up `.env`
|
| 94 |
+
|
| 95 |
+
Create a `.env` file in the root directory and add your [Groq API Key](https://console.groq.com/):
|
| 96 |
+
|
| 97 |
+
```
|
| 98 |
+
groq_api_key=your_groq_api_key_here
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
### 4. Launch the App
|
| 102 |
+
|
| 103 |
+
```bash
|
| 104 |
+
python app.py
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
Or just run:
|
| 108 |
+
|
| 109 |
+
```bash
|
| 110 |
+
streamlit run app.py # If you're using a streamlit version
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
### 5. Use the Web Interface
|
| 114 |
+
|
| 115 |
+
Go to the URL provided in the terminal (typically `http://127.0.0.1:7860`) and enter your natural language query.
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## π§ͺ Sample Queries
|
| 120 |
+
|
| 121 |
+
| Natural Language Query | Action |
|
| 122 |
+
| --------------------------------------------------- | ----------------------------- |
|
| 123 |
+
| "List all students enrolled in 2022" | Filters `student` table |
|
| 124 |
+
| "Show all employees in Computer Science department" | Filters `employee` table |
|
| 125 |
+
| "Get courses taught in Fall semester" | Filters `course` table |
|
| 126 |
+
| "Find course names and instructor names" | Joins `course` and `employee` |
|
| 127 |
+
|
| 128 |
+
---
|
| 129 |
+
|
| 130 |
+
## π Security & Limitations
|
| 131 |
+
|
| 132 |
+
* This is a prototype/demo and should not be used in production without input sanitization.
|
| 133 |
+
* Currently limited to SELECT queries only.
|
| 134 |
+
* Works only with the predefined schema (3 tables).
|
| 135 |
+
|
| 136 |
+
---
|
| 137 |
+
|
| 138 |
+
## π€ Contributing
|
| 139 |
+
|
| 140 |
+
Contributions are welcome! Feel free to open an issue or pull request for:
|
| 141 |
+
|
| 142 |
+
* New table additions
|
| 143 |
+
* Better embeddings
|
| 144 |
+
* UI/UX improvements
|
| 145 |
+
* Model optimization
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
## π¨βπ» Author
|
| 149 |
+
|
| 150 |
+
Built by \Jamgala Bala Swaroop
|
| 151 |
+
π« Email: [jbsroop@gmail.com](mailto:jbsroop@gmail.com)
|
| 152 |
+
|
| 153 |
|
|
|