Spaces:
Runtime error
Runtime error
Commit ·
67524d7
1
Parent(s): fb64f16
bug fixes
Browse files- app.py +0 -13
- gptManager.py +5 -1
- utils.py +24 -12
app.py
CHANGED
|
@@ -27,25 +27,12 @@ warnings.filterwarnings("ignore")
|
|
| 27 |
|
| 28 |
dbCreds = DataWrapper(DB_CREDS_DATA)
|
| 29 |
dbEngine = DbEngine(dbCreds)
|
| 30 |
-
dbEngine.connect()
|
| 31 |
|
| 32 |
tablesAndCols = getAllTablesInfo(dbEngine, SCHEMA_NAME)
|
| 33 |
metadataLayout = MetaDataLayout(schemaName=SCHEMA_NAME, allTablesAndCols=tablesAndCols)
|
| 34 |
metadataLayout.setSelection(DEFAULT_TABLES_COLS)
|
| 35 |
|
| 36 |
selectedTablesAndCols = metadataLayout.getSelectedTablesAndCols()
|
| 37 |
-
|
| 38 |
-
def getSampleDataForTablesAndCols(dbEngine, schemaName, tablesAndCols, maxRows):
|
| 39 |
-
data = {}
|
| 40 |
-
conn = dbEngine.connection
|
| 41 |
-
for table in tablesAndCols.keys():
|
| 42 |
-
try:
|
| 43 |
-
sqlQuery = f"""select * from {schemaName}.{table} limit {maxRows}"""
|
| 44 |
-
data[table] = pd.read_sql_query(sqlQuery, con=conn)
|
| 45 |
-
except Exception as e:
|
| 46 |
-
print(e)
|
| 47 |
-
print(f"couldn't read table data. Table: {table}")
|
| 48 |
-
return data
|
| 49 |
|
| 50 |
|
| 51 |
openAIClient = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
|
| 27 |
|
| 28 |
dbCreds = DataWrapper(DB_CREDS_DATA)
|
| 29 |
dbEngine = DbEngine(dbCreds)
|
|
|
|
| 30 |
|
| 31 |
tablesAndCols = getAllTablesInfo(dbEngine, SCHEMA_NAME)
|
| 32 |
metadataLayout = MetaDataLayout(schemaName=SCHEMA_NAME, allTablesAndCols=tablesAndCols)
|
| 33 |
metadataLayout.setSelection(DEFAULT_TABLES_COLS)
|
| 34 |
|
| 35 |
selectedTablesAndCols = metadataLayout.getSelectedTablesAndCols()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
openAIClient = OpenAI(api_key=OPENAI_API_KEY)
|
gptManager.py
CHANGED
|
@@ -67,6 +67,10 @@ class ChatgptManager:
|
|
| 67 |
if newMessage!=None:
|
| 68 |
messages.append(newMessage)
|
| 69 |
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
currentTokensInMessages = getWordsCount(combinedContent)
|
| 72 |
return currentTokensInMessages
|
|
|
|
| 67 |
if newMessage!=None:
|
| 68 |
messages.append(newMessage)
|
| 69 |
|
| 70 |
+
if len(messages)!=0:
|
| 71 |
+
combinedContent = " ".join(msg["content"] for msg in messages)
|
| 72 |
+
else:
|
| 73 |
+
combinedContent = ""
|
| 74 |
+
|
| 75 |
currentTokensInMessages = getWordsCount(combinedContent)
|
| 76 |
return currentTokensInMessages
|
utils.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import psycopg2
|
| 2 |
import re
|
|
|
|
| 3 |
|
| 4 |
class DataWrapper:
|
| 5 |
def __init__(self, data):
|
|
@@ -53,23 +54,33 @@ class MetaDataLayout:
|
|
| 53 |
class DbEngine:
|
| 54 |
def __init__(self, dbCreds):
|
| 55 |
self.dbCreds = dbCreds
|
| 56 |
-
self.
|
| 57 |
|
| 58 |
def connect(self):
|
| 59 |
dbCreds = self.dbCreds
|
| 60 |
-
if self.
|
| 61 |
-
self.
|
| 62 |
password = dbCreds.password, host = dbCreds.host,
|
| 63 |
port = dbCreds.port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def disconnect(self):
|
| 66 |
-
if self.
|
| 67 |
-
self.
|
| 68 |
|
| 69 |
def execute_query(self, query):
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return result
|
| 74 |
|
| 75 |
|
|
@@ -78,7 +89,8 @@ def executeQuery(dbEngine, query):
|
|
| 78 |
return result
|
| 79 |
|
| 80 |
def executeColumnsQuery(dbEngine, columnQuery):
|
| 81 |
-
|
|
|
|
| 82 |
cursor.execute(columnQuery)
|
| 83 |
columns = [desc[0] for desc in cursor.description]
|
| 84 |
return columns
|
|
@@ -99,9 +111,9 @@ def getAllTablesInfo(dbEngine, schemaName):
|
|
| 99 |
return tablesAndCols
|
| 100 |
|
| 101 |
def getSampleDataForTablesAndCols(dbEngine, schemaName, tablesAndCols, maxRows):
|
| 102 |
-
|
| 103 |
data = {}
|
| 104 |
-
|
|
|
|
| 105 |
for table in tablesAndCols.keys():
|
| 106 |
try:
|
| 107 |
sqlQuery = f"""select * from {schemaName}.{table} limit {maxRows}"""
|
|
@@ -138,7 +150,7 @@ def extractSqlFromGptResponse(gptReponse):
|
|
| 138 |
def addSchemaToTableInSQL(sqlQuery, schemaName, tablesList):
|
| 139 |
|
| 140 |
for table in tablesList:
|
| 141 |
-
pattern = re.compile(rf'(?<!
|
| 142 |
replacement = f'{schemaName}.{table}'
|
| 143 |
sqlQuery = re.sub(pattern, replacement, sqlQuery)
|
| 144 |
return sqlQuery
|
|
|
|
| 1 |
import psycopg2
|
| 2 |
import re
|
| 3 |
+
import pandas as pd
|
| 4 |
|
| 5 |
class DataWrapper:
|
| 6 |
def __init__(self, data):
|
|
|
|
| 54 |
class DbEngine:
|
| 55 |
def __init__(self, dbCreds):
|
| 56 |
self.dbCreds = dbCreds
|
| 57 |
+
self._connection = None
|
| 58 |
|
| 59 |
def connect(self):
|
| 60 |
dbCreds = self.dbCreds
|
| 61 |
+
if self._connection is None or self._connection.closed != 0:
|
| 62 |
+
self._connection = psycopg2.connect(database=dbCreds.database, user = dbCreds.user,
|
| 63 |
password = dbCreds.password, host = dbCreds.host,
|
| 64 |
port = dbCreds.port)
|
| 65 |
+
|
| 66 |
+
def getConnection(self):
|
| 67 |
+
if self._connection is None or self._connection.closed != 0:
|
| 68 |
+
self.connect()
|
| 69 |
+
return self._connection
|
| 70 |
+
|
| 71 |
|
| 72 |
def disconnect(self):
|
| 73 |
+
if self._connection is not None and self._connection.closed == 0:
|
| 74 |
+
self._connection.close()
|
| 75 |
|
| 76 |
def execute_query(self, query):
|
| 77 |
+
try:
|
| 78 |
+
self.connect()
|
| 79 |
+
with self._connection.cursor() as cursor:
|
| 80 |
+
cursor.execute(query)
|
| 81 |
+
result = cursor.fetchall()
|
| 82 |
+
except Exception as e:
|
| 83 |
+
raise Exception(e)
|
| 84 |
return result
|
| 85 |
|
| 86 |
|
|
|
|
| 89 |
return result
|
| 90 |
|
| 91 |
def executeColumnsQuery(dbEngine, columnQuery):
|
| 92 |
+
dbEngine.connect()
|
| 93 |
+
with dbEngine._connection.cursor() as cursor:
|
| 94 |
cursor.execute(columnQuery)
|
| 95 |
columns = [desc[0] for desc in cursor.description]
|
| 96 |
return columns
|
|
|
|
| 111 |
return tablesAndCols
|
| 112 |
|
| 113 |
def getSampleDataForTablesAndCols(dbEngine, schemaName, tablesAndCols, maxRows):
|
|
|
|
| 114 |
data = {}
|
| 115 |
+
dbEngine.connect()
|
| 116 |
+
conn = dbEngine.getConnection()
|
| 117 |
for table in tablesAndCols.keys():
|
| 118 |
try:
|
| 119 |
sqlQuery = f"""select * from {schemaName}.{table} limit {maxRows}"""
|
|
|
|
| 150 |
def addSchemaToTableInSQL(sqlQuery, schemaName, tablesList):
|
| 151 |
|
| 152 |
for table in tablesList:
|
| 153 |
+
pattern = re.compile(rf'(?<!\S){re.escape(table)}(?!\S)', re.IGNORECASE)
|
| 154 |
replacement = f'{schemaName}.{table}'
|
| 155 |
sqlQuery = re.sub(pattern, replacement, sqlQuery)
|
| 156 |
return sqlQuery
|