Chit1324 commited on
Commit
cc22cb2
·
verified ·
1 Parent(s): a656655

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +14 -142
README.md CHANGED
@@ -1,142 +1,14 @@
1
- # TextToSQL
2
-
3
- TextToSQL is a web application that lets you query a SQLite database using plain English. The system converts your natural language questions into SQL queries using an NLP model, executes these queries on the database, and then returns the results through an interactive Gradio-based chat interface.
4
-
5
- ## Table of Contents
6
- - [Installation](#installation)
7
- - [Database Setup](#database-setup)
8
- - [Running the Application](#running-the-application)
9
- - [Project Structure and File Explanations](#project-structure-and-file-explanations)
10
- - [Usage](#usage)
11
- - [Customization](#customization)
12
-
13
- ## Installation
14
-
15
- 1. **Clone the Repository**
16
-
17
- ```bash
18
- git clone https://github.com/chithindocha/TextToSQL.git
19
- cd TextToSQL
20
- ```
21
-
22
- 2. **Install Required Modules**
23
-
24
- Ensure you have Python (version 3.7 or higher) installed. Install all necessary modules using the provided `requirements.txt` file:
25
-
26
- ```bash
27
- pip install -r requirements.txt
28
- ```
29
-
30
- ## Database Setup
31
-
32
- Before running the application, you need to initialize the SQLite database:
33
-
34
- 1. **Run the Initialization Script**
35
-
36
- Execute the following command to create the SQLite database (`chat_assistant.db`) with the necessary tables and sample data:
37
-
38
- ```bash
39
- python db/init_db.py
40
- ```
41
-
42
- This script will:
43
- - Create the **employees** and **departments** tables.
44
- - Create metadata tables (**table_metadata** and **column_metadata**).
45
- - Insert sample data into the **employees** and **departments** tables.
46
-
47
- **Note:** Feel free to modify or extend the sample data in `db/init_db.py` as needed.
48
-
49
- ## Running the Application
50
-
51
- Start the web application by running:
52
-
53
- ```bash
54
- python app.py
55
- ```
56
-
57
- This command launches a Gradio web interface where you can ask the SQL Chat Assistant questions in plain English. The assistant will translate your query into SQL, execute it against the database, and display the results.
58
-
59
- ## Project Structure and File Explanations
60
-
61
- ### 1. **db/database.py**
62
-
63
- - **Purpose:**
64
- This file contains the `Database` class, which manages the SQLite database connection, executes SQL queries, and handles closing the connection.
65
-
66
- - **Key Methods:**
67
- - `connect()`: Establishes a connection to the SQLite database.
68
- - `execute_query(query, params=None)`: Executes a SQL query with optional parameters.
69
- - `close()`: Closes the active database connection.
70
-
71
- ### 2. **db/init_db.py**
72
-
73
- - **Purpose:**
74
- Initializes the SQLite database by creating the necessary tables and inserting sample data.
75
-
76
- - **Key Operations:**
77
- - Creates the **employees**, **departments**, **table_metadata**, and **column_metadata** tables.
78
- - Inserts sample records into the **employees** and **departments** tables.
79
- - Adds metadata entries for table and column descriptions.
80
-
81
- - **Usage:**
82
- Run this file to set up the database. You can modify the data here if you wish to customize the sample entries.
83
-
84
- ### 3. **nlp/query_processor.py**
85
-
86
- - **Purpose:**
87
- Processes user queries by converting natural language input into SQL statements and executing them.
88
-
89
- - **Key Components:**
90
- - **Date Conversion:**
91
- Contains methods (`convert_date` and `extract_and_convert_date`) to detect and convert various date formats found in queries.
92
- - **Query Processing:**
93
- Uses the `SQLModel` (from `nlp/sql_model.py`) to generate an SQL query based on the user's input, executes the query using the `Database` class, and returns the results.
94
-
95
- ### 4. **nlp/sql_model.py**
96
-
97
- - **Purpose:**
98
- Handles the translation of natural language queries into SQL commands.
99
-
100
- - **Key Functionality:**
101
- - Utilizes the Hugging Face `InferenceClient` with a specified model (default: `"HuggingFaceH4/zephyr-7b-beta"`) to convert plain English queries into SQL.
102
- - Provides a detailed prompt with schema information and example queries to guide the model.
103
- - Cleans and extracts the SQL command from the model's output before returning it.
104
-
105
- ### 5. **app.py**
106
-
107
- - **Purpose:**
108
- Serves as the main entry point for the application and sets up the web interface.
109
-
110
- - **Key Operations:**
111
- - Initializes the `Database` and `QueryProcessor` objects.
112
- - Defines a `respond` function that processes user queries and fetches results from the database.
113
- - Uses Gradio’s `ChatInterface` to build an interactive chat UI where users can type their queries and view responses.
114
-
115
- ## Usage
116
-
117
- 1. **Start the Application**
118
-
119
- Run the following command to launch the chat interface:
120
-
121
- ```bash
122
- python app.py
123
- ```
124
-
125
- 2. **Ask a Query**
126
-
127
- In the chat window, type your question in plain English (e.g., "Show me all employees in the Sales department"). The application will:
128
- - Convert your query to an SQL command.
129
- - Execute the command against the SQLite database.
130
- - Return and display the results in the chat window.
131
-
132
- ## Customization
133
-
134
- - **Database Content:**
135
- You can modify the sample data or the database schema by editing the `db/init_db.py` file. Re-run the script to update the database with your changes.
136
-
137
- - **SQL Model Prompt:**
138
- Adjust the prompt and example queries in `nlp/sql_model.py` to better fit your database schema or use cases.
139
-
140
- - **User Interface:**
141
- Customize the Gradio interface settings in `app.py` to change the title, description, or layout of the chat UI.
142
-
 
1
+ ---
2
+ title: TextToSQL
3
+ emoji: 💬
4
+ colorFrom: yellow
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 5.0.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: other
11
+ short_description: Converts English text into SQL Query and retrieves data
12
+ ---
13
+
14
+ An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).