Commit
·
2d31646
1
Parent(s):
31812e1
Created db.py and nlp.py for executing queries and converting queries to sql respectively
Browse files- app/db.py +17 -51
- app/nlp.py +14 -0
- requirements.txt +0 -0
app/db.py
CHANGED
|
@@ -1,52 +1,18 @@
|
|
| 1 |
import sqlite3
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
''')
|
| 20 |
-
|
| 21 |
-
cursor.execute('''CREATE TABLE IF NOT EXISTS Departments(
|
| 22 |
-
ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 23 |
-
Name TEXT NOT NULL,
|
| 24 |
-
Manager TEXT NOT NULL
|
| 25 |
-
)
|
| 26 |
-
''')
|
| 27 |
-
|
| 28 |
-
employees = [
|
| 29 |
-
(1, 'Alice', 'Sales', 50000, '2021-01-15'),
|
| 30 |
-
(2, 'Bob', 'Engineering', 70000, '2020-06-10'),
|
| 31 |
-
(3, 'Charlie', 'Marketing', 60000, '2022-03-20')
|
| 32 |
-
]
|
| 33 |
-
|
| 34 |
-
departments = [
|
| 35 |
-
(1, 'Sales', 'Alice'),
|
| 36 |
-
(2, 'Engineering', 'Bob'),
|
| 37 |
-
(3, 'Marketing', 'Charlie')
|
| 38 |
-
]
|
| 39 |
-
|
| 40 |
-
cursor.executemany('INSERT INTO Employees VALUES (?,?,?,?,?)', employees)
|
| 41 |
-
cursor.executemany('INSERT INTO Departments VALUES (?,?,?)', departments)
|
| 42 |
-
|
| 43 |
-
conn.commit()
|
| 44 |
-
conn.close()
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if __name__ == "__main__":
|
| 49 |
-
create_tables()
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
+
from sqlite3 import Error
|
| 3 |
+
|
| 4 |
+
class Database:
|
| 5 |
+
def __init__(self, db_path='data/database.sqlite'):
|
| 6 |
+
self.db_path = db_path
|
| 7 |
+
|
| 8 |
+
def execute_query(self, query):
|
| 9 |
+
try:
|
| 10 |
+
conn = sqlite3.connect(self.db_path)
|
| 11 |
+
cursor = conn.cursor()
|
| 12 |
+
cursor.execute(query)
|
| 13 |
+
results = cursor.fetchall()
|
| 14 |
+
columns = [desc[0] for desc in cursor.description]
|
| 15 |
+
conn.close()
|
| 16 |
+
return {"columns": columns, "data": results}
|
| 17 |
+
except Error as e:
|
| 18 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/nlp.py
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
class NLPToSQL:
|
| 4 |
+
def __init__(self):
|
| 5 |
+
self.model = pipeline(
|
| 6 |
+
"text2text-generation",
|
| 7 |
+
model="mrm8488/t5-base-finetuned-wikiSQL",
|
| 8 |
+
tokenizer="t5-base"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def query_to_sql(self, user_query):
|
| 12 |
+
prompt = f"translate English to SQL: {user_query}"
|
| 13 |
+
result = self.model(prompt, max_length=200)
|
| 14 |
+
return result[0]['generated_text']
|
requirements.txt
CHANGED
|
Binary files a/requirements.txt and b/requirements.txt differ
|
|
|