h3110Fr13nd commited on
Commit
ba07482
0 Parent(s):
Departments.csv ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ DepartmentID,DepartmentName
2
+ 101,Finance
3
+ 102,Marketing
4
+ 103,Human Resources
5
+ 104,Research and Development
Employees.csv ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ EmployeeID,FirstName,LastName,DepartmentID,Salary
2
+ 1,John,Doe,101,50000.00
3
+ 2,Jane,Smith,102,60000.00
4
+ 3,Michael,Johnson,101,55000.00
5
+ 4,Emily,Williams,103,52000.00
6
+ 5,David,Brown,102,58000.00
README.md ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AI SQL Query Builder
2
+
3
+ ## Getting Started
4
+
5
+ To get started with CURE AI, follow these steps:
6
+
7
+ ### Clone Repository
8
+ ```
9
+ git clone https://github.com/Shrey-patel-07/ai-sql-query-builder.git
10
+ ```
11
+
12
+ ### Install the dependencies from requirement.txt
13
+ ```
14
+ pip install -r requirements.txt
15
+ ```
16
+
17
+ ### Run ddl_query_generator.py database_selection.py to generate SQL Query from DLL
18
+ ```
19
+ python ddl_query_generator.py
20
+ ```
21
+
22
+ ### Run database_selection.py to generate outputs from database
23
+ ```
24
+ python database_selection.py
25
+ ```
26
+
27
+ ### Launch it on your browser
28
+ ```
29
+ http://localhost:5713
30
+ ```
database_selection.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request
2
+ import pandas as pd
3
+ import sqlite3
4
+ import os
5
+
6
+ app = Flask(__name__)
7
+
8
+ UPLOAD_FOLDER = 'uploads'
9
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
10
+
11
+ # Create the 'uploads' directory if it doesn't exist
12
+ if not os.path.exists(UPLOAD_FOLDER):
13
+ os.makedirs(UPLOAD_FOLDER)
14
+
15
+ # Initialize history list to store query history
16
+ history = []
17
+
18
+ def run_sql_query(csv_files, sql_query):
19
+ conn = sqlite3.connect(':memory:')
20
+
21
+ for file_path in csv_files:
22
+ df_name = os.path.splitext(os.path.basename(file_path))[0] # Extract file name without extension
23
+ df = pd.read_csv(file_path)
24
+ df.to_sql(df_name, conn, index=False)
25
+
26
+ result = pd.read_sql_query(sql_query, con=conn)
27
+
28
+ return result.to_html()
29
+
30
+ @app.route('/', methods=['GET', 'POST'])
31
+ def index():
32
+ result = None
33
+ query = None
34
+
35
+ if request.method == 'POST':
36
+ uploaded_files = request.files.getlist('file')
37
+ csv_files = []
38
+ for file in uploaded_files:
39
+ if file.filename != '':
40
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
41
+ file.save(file_path)
42
+ csv_files.append(file_path)
43
+
44
+ sql_query = request.form['sql_query']
45
+ query = sql_query
46
+
47
+ # Execute SQL query and store result in history
48
+ result = run_sql_query(csv_files, sql_query)
49
+ history.append({'query': sql_query, 'result': result})
50
+
51
+ return render_template('database_selection_index.html', result=result, query=query, history=history)
52
+
53
+ if __name__ == '__main__':
54
+ app.run(debug=True)
ddl_query_generator.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sqlite3
3
+ import warnings
4
+ import pandas as pd
5
+ from flask import Flask, render_template, request
6
+ from langchain_community.llms import Ollama
7
+ from langchain import PromptTemplate, LLMChain
8
+
9
+ app = Flask(__name__)
10
+
11
+ # Suppressing warnings
12
+ warnings.filterwarnings("ignore")
13
+
14
+ # Initializing the language model
15
+ llm = Ollama(model="pxlksr/defog_sqlcoder-7b-2:Q4_K")
16
+
17
+ UPLOAD_FOLDER = 'uploads'
18
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
19
+
20
+ # Prompt template for language model
21
+ template = """
22
+ ### Task
23
+ Generate a SQL query to answer [QUESTION]{user_question}[/QUESTION]
24
+
25
+ ### Instructions
26
+ - If you cannot answer the question with the available database schema, return 'I do not know'
27
+
28
+ ### Database Schema
29
+ The query will run on a database with the following schema:
30
+ {table_metadata_string}
31
+
32
+ ### Answer
33
+ Given the database schema, here is the SQL query that answers [QUESTION]{user_question}[/QUESTION]
34
+ [SQL]
35
+ """
36
+
37
+ prompt = PromptTemplate(template=template, input_variables=["user_question", "table_metadata_string"])
38
+
39
+ # Function to get response from language model
40
+ def get_llm_response(user_question, table_metadata_string):
41
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
42
+ response = llm_chain.run({"user_question": user_question, "table_metadata_string": table_metadata_string})
43
+ return response
44
+
45
+ def run_sql_query(csv_files, sql_query):
46
+ conn = sqlite3.connect(':memory:')
47
+
48
+ for file_path in csv_files:
49
+ df_name = os.path.splitext(os.path.basename(file_path))[0] # Extract file name without extension
50
+ df = pd.read_csv(file_path)
51
+ df.to_sql(df_name, conn, index=False)
52
+
53
+ result = pd.read_sql_query(sql_query, con=conn)
54
+
55
+ return result.to_html()
56
+
57
+
58
+ history = []
59
+
60
+ @app.route('/', methods=['GET', 'POST'])
61
+ def ddl_query():
62
+ ddl = None
63
+ if request.method == 'POST':
64
+ ddl = request.form['ddl']
65
+ user_question = request.form.get('user_question', None)
66
+
67
+ if user_question:
68
+ output = get_llm_response(user_question, ddl)
69
+ # Insert the new history item at the beginning of the list
70
+ history.insert(0, {'query': user_question, 'response': output})
71
+
72
+ return render_template('index.html', history=history, ddl=ddl)
73
+
74
+
75
+ @app.route('/dbms_query', methods=['GET', 'POST'])
76
+ def index():
77
+ result = None
78
+ query = None
79
+
80
+ if request.method == 'POST':
81
+ uploaded_files = request.files.getlist('file')
82
+ csv_files = []
83
+ for file in uploaded_files:
84
+ if file.filename != '':
85
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
86
+ file.save(file_path)
87
+ csv_files.append(file_path)
88
+
89
+ sql_query = request.form['sql_query']
90
+ query = sql_query
91
+
92
+ # Execute SQL query and store result in history
93
+ result = run_sql_query(csv_files, sql_query)
94
+ history.append({'query': sql_query, 'result': result})
95
+
96
+ return render_template('database_selection_index.html', result=result, query=query, history=history)
97
+
98
+ if __name__ == '__main__':
99
+ app.run(debug=True, port = 7860, host = '0.0.0.0')
100
+
demo.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ollama
2
+ import streamlit as st
3
+
4
+ st.title("Ollama Python Chatbot")
5
+
6
+ # initialize history
7
+ if "messages" not in st.session_state:
8
+ st.session_state["messages"] = []
9
+
10
+
11
+ # init models
12
+ if "model" not in st.session_state:
13
+ st.session_state["model"] = ""
14
+
15
+ models = [model["name"] for model in ollama.list()["models"]]
16
+ st.session_state["model"] = st.selectbox("Choose your model", models)
17
+
18
+ def model_res_generator():
19
+ stream = ollama.chat(
20
+ model=st.session_state["model"],
21
+ messages=st.session_state["messages"],
22
+ stream=True,
23
+ )
24
+ for chunk in stream:
25
+ yield chunk["message"]["content"]
26
+
27
+ # Display chat messages from history on app rerun
28
+ for message in st.session_state["messages"]:
29
+ with st.chat_message(message["role"]):
30
+ st.markdown(message["content"])
31
+
32
+ if prompt := st.chat_input("What is up?"):
33
+ # add latest message to history in format {role, content}
34
+ st.session_state["messages"].append({"role": "user", "content": prompt})
35
+
36
+ with st.chat_message("user"):
37
+ st.markdown(prompt)
38
+
39
+ with st.chat_message("assistant"):
40
+ message = st.write_stream(model_res_generator())
41
+ st.session_state["messages"].append({"role": "assistant", "content": message})
natural-language.Dockerfile ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ FROM ollama/ollama as ollama
2
+ RUN ollama serve & sleep 5 && ollama pull pxlksr/defog_sqlcoder-7b-2:Q4_K
3
+ ENV HOME=/home
4
+ RUN apt-get update && apt-get install -y python3.12 python3-pip
5
+ COPY requirements.txt ./
6
+ RUN python3.12 -m pip install --no-cache-dir -r requirements.txt
7
+ COPY . .
8
+ CMD ["python3.12", "ddl_query_generator.py"]
requirements.txt ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiohttp==3.9.5
2
+ aiosignal==1.3.1
3
+ annotated-types==0.6.0
4
+ anyio==4.3.0
5
+ attrs==23.2.0
6
+ blinker==1.7.0
7
+ certifi==2024.2.2
8
+ charset-normalizer==3.3.2
9
+ click==8.1.7
10
+ dataclasses-json==0.6.4
11
+ Flask==3.0.3
12
+ frozenlist==1.4.1
13
+ greenlet==3.0.3
14
+ h11==0.14.0
15
+ httpcore==1.0.5
16
+ httpx==0.27.0
17
+ idna==3.7
18
+ itsdangerous==2.2.0
19
+ Jinja2==3.1.3
20
+ jsonpatch==1.33
21
+ jsonpointer==2.4
22
+ langchain==0.1.16
23
+ langchain-community==0.0.34
24
+ langchain-core==0.1.45
25
+ langchain-text-splitters==0.0.1
26
+ langsmith==0.1.49
27
+ MarkupSafe==2.1.5
28
+ marshmallow==3.21.1
29
+ multidict==6.0.5
30
+ mypy-extensions==1.0.0
31
+ numpy==1.26.4
32
+ ollama==0.1.8
33
+ orjson==3.10.1
34
+ packaging==23.2
35
+ pandas==2.2.2
36
+ pydantic==2.7.0
37
+ pydantic_core==2.18.1
38
+ python-dateutil==2.9.0.post0
39
+ pytz==2024.1
40
+ PyYAML==6.0.1
41
+ requests==2.31.0
42
+ six==1.16.0
43
+ sniffio==1.3.1
44
+ SQLAlchemy==2.0.29
45
+ tenacity==8.2.3
46
+ typing-inspect==0.9.0
47
+ typing_extensions==4.11.0
48
+ tzdata==2024.1
49
+ urllib3==2.2.1
50
+ Werkzeug==3.0.2
51
+ yarl==1.9.4
templates/database_selection_index.html ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>CSV File Upload</title>
8
+ <style>
9
+ body {
10
+ font-family: Arial, sans-serif;
11
+ text-align: center;
12
+ margin: 0;
13
+ padding: 0;
14
+ background-color: #f0f0f0;
15
+ color: #fff;
16
+ }
17
+
18
+ h1 {
19
+ margin-top: 40px;
20
+ color: #fff;
21
+ }
22
+
23
+ form {
24
+ margin: 0 auto;
25
+ max-width: 600px;
26
+ padding: 20px;
27
+ text-align: left;
28
+ background-color: #444;
29
+ border-radius: 8px;
30
+ }
31
+
32
+ input[type="file"],
33
+ textarea,
34
+ input[type="submit"] {
35
+ width: 100%;
36
+ padding: 10px;
37
+ margin-bottom: 10px;
38
+ box-sizing: border-box;
39
+ background-color: #555;
40
+ border: none;
41
+ border-radius: 4px;
42
+ color: #fff;
43
+ }
44
+
45
+ input[type="submit"]:hover {
46
+ background-color: #007bff;
47
+ }
48
+
49
+ ul {
50
+ list-style-type: none;
51
+ padding: 0;
52
+ text-align: left;
53
+ margin: 0 auto;
54
+ max-width: 600px;
55
+ }
56
+
57
+ li {
58
+ margin-bottom: 10px;
59
+ }
60
+
61
+ div {
62
+ margin-bottom: 20px;
63
+ }
64
+
65
+ /* Dark mode compatible styles */
66
+ @media (prefers-color-scheme: dark) {
67
+ body {
68
+ background-color: #1a1a1a;
69
+ color: #fff;
70
+ }
71
+
72
+ form {
73
+ background-color: #333;
74
+ }
75
+
76
+ input[type="file"],
77
+ textarea,
78
+ input[type="submit"] {
79
+ background-color: #444;
80
+ color: #fff;
81
+ }
82
+
83
+ input[type="submit"]:hover {
84
+ background-color: #007bff;
85
+ }
86
+ }
87
+ </style>
88
+ </head>
89
+
90
+ <body>
91
+ <h1>CSV File Upload</h1>
92
+ <form action="/" method="post" enctype="multipart/form-data">
93
+ <input type="file" name="file" multiple>
94
+ <textarea name="sql_query" placeholder="Enter SQL query"></textarea>
95
+ <input type="submit" value="Upload and Run Query">
96
+ </form>
97
+ <h2>Query Result:</h2>
98
+ <div id="cnt">
99
+ <ul>
100
+ {% for entry in history %}
101
+ <li><strong>Query:</strong> {{ entry.query }}</li>
102
+ <li><strong>Result:</strong></li>
103
+ <center>
104
+ <div>{{ entry.result | safe }}</div>
105
+ </center>
106
+ {% endfor %}
107
+ </ul>
108
+ </div>
109
+ </body>
110
+
111
+ </html>
templates/index.html ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
+ <title>Language Model SQL Query Generator</title>
8
+ <style>
9
+ body {
10
+ background-color: #333;
11
+ color: #fff;
12
+ font-family: Arial, sans-serif;
13
+ text-align: center;
14
+ }
15
+
16
+ form {
17
+ margin: 0 auto;
18
+ max-width: 600px;
19
+ padding: 20px;
20
+ background-color: #444;
21
+ border-radius: 8px;
22
+ }
23
+
24
+ input[type="text"],
25
+ textarea {
26
+ width: 100%;
27
+ padding: 10px;
28
+ margin-bottom: 10px;
29
+ box-sizing: border-box;
30
+ background-color: #555;
31
+ border: none;
32
+ border-radius: 4px;
33
+ color: #fff;
34
+ }
35
+
36
+ input[type="submit"] {
37
+ padding: 10px 20px;
38
+ background-color: #007bff;
39
+ border: none;
40
+ border-radius: 4px;
41
+ color: #fff;
42
+ cursor: pointer;
43
+ }
44
+
45
+ input[type="submit"]:hover {
46
+ background-color: #0056b3;
47
+ }
48
+
49
+ h1,
50
+ h2 {
51
+ margin-top: 40px;
52
+ color: #fff;
53
+ }
54
+
55
+ <style>... table {
56
+ margin: 0 auto;
57
+ width: 80%;
58
+ border-collapse: collapse;
59
+ border: 1px solid #ddd;
60
+ /* Added border */
61
+ border-radius: 10px;
62
+ /* Rounded corners */
63
+ overflow: hidden;
64
+ /* Ensures rounded corners are visible */
65
+ }
66
+
67
+ th,
68
+ td {
69
+ padding: 8px;
70
+ border-bottom: 1px solid #ddd;
71
+ color: #fff;
72
+ }
73
+
74
+ th {
75
+ background-color: #555;
76
+ }
77
+
78
+ /* Updated styles for query and response cells */
79
+ .query {
80
+ background-color: #333;
81
+ /* Dark grey */
82
+ color: #ddd;
83
+ /* Light black text */
84
+ text-align: left;
85
+ }
86
+
87
+ .response {
88
+ background-color: #444;
89
+ /* Dark grey */
90
+ color: #ddd;
91
+ /* Light black text */
92
+ }
93
+
94
+ .history {
95
+ margin-top: 40px;
96
+ text-align: center;
97
+ /* Centered the History heading */
98
+ margin-left: auto;
99
+ margin-right: auto;
100
+ width: 80%;
101
+ }
102
+
103
+ .history table {
104
+ margin: 0 auto;
105
+ /* Center the table */
106
+ text-align: left;
107
+ border: 1px solid #ddd;
108
+ /* Added border */
109
+ border-radius: 10px;
110
+ /* Rounded corners */
111
+ overflow: hidden;
112
+ }
113
+
114
+ .history strong {
115
+ color: #fff;
116
+ }
117
+
118
+ /* Added hover effect to the entire table */
119
+ table:hover {
120
+ background-color: #666;
121
+ }
122
+ </style>
123
+
124
+ </style>
125
+
126
+ </head>
127
+
128
+ <body>
129
+ <h1>Language Model SQL Query Generator</h1>
130
+
131
+ <!-- Combined Form -->
132
+ <form action="/" method="post">
133
+ <!-- DDL Input -->
134
+ <label for="ddl">Enter DDL:</label><br>
135
+ <textarea id="ddl" name="ddl" rows="5" cols="50">{% if ddl %}{{ ddl }}{% endif %}</textarea><br><br>
136
+
137
+ <!-- Query Input -->
138
+ <label for="user_question">Enter Query:</label><br>
139
+ <input type="text" id="user_question" name="user_question"><br><br>
140
+
141
+ <!-- Submit Button -->
142
+ <input type="submit" value="Submit">
143
+ </form>
144
+
145
+ <!-- History -->
146
+ <div class="history">
147
+
148
+ <table>
149
+ <caption>
150
+ <h2>History</h2>
151
+ </caption>
152
+ <tbody>
153
+ {% for item in history %}
154
+ <tr>
155
+ <td style="background-color: #333;"><strong>Query:</strong> {{ item.query }}</td>
156
+ </tr>
157
+ <tr>
158
+ <td style="background-color: #666;"><strong>Response:</strong> {{ item.response }}</td>
159
+ </tr>
160
+ {% endfor %}
161
+ </tbody>
162
+ </table>
163
+ </div>
164
+ </body>
165
+
166
+ </html>
167
+
uploads/Departments.csv ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ DepartmentID,DepartmentName
2
+ 101,Finance
3
+ 102,Marketing
4
+ 103,Human Resources
5
+ 104,Research and Development
uploads/Employees.csv ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ EmployeeID,FirstName,LastName,DepartmentID,Salary
2
+ 1,John,Doe,101,50000.00
3
+ 2,Jane,Smith,102,60000.00
4
+ 3,Michael,Johnson,101,55000.00
5
+ 4,Emily,Williams,103,52000.00
6
+ 5,David,Brown,102,58000.00