Spaces:
Sleeping
Sleeping
A newer version of the Gradio SDK is available:
6.5.0
metadata
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, andcourse. - π§ͺ 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 β For the frontend UI
- Sentence Transformers β For encoding metadata and queries
- Groq API β 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 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 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
git clone https://github.com/your-username/sql-query-generator.git
cd sql-query-generator
2. Install Dependencies
pip install -r requirements.txt
3. Set Up .env
Create a .env file in the root directory and add your Groq API Key:
groq_api_key=your_groq_api_key_here
4. Launch the App
python app.py
Or just run:
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