--- 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)