anumaurya114exp commited on
Commit
f896425
·
1 Parent(s): 0c9edb0

reverting to amardeep's version

Browse files
Files changed (2) hide show
  1. app.py +208 -231
  2. requirements.txt +2 -3
app.py CHANGED
@@ -1,194 +1,222 @@
1
- from openai import OpenAI
 
 
 
 
 
 
 
2
  import pandas as pd
 
 
 
3
  import psycopg2
4
  import time
5
  import gradio as gr
6
  import sqlparse
7
- import re
8
  import os
9
- import warnings
10
 
 
 
11
 
12
- from persistStorage import saveLog, getAllLogFilesPaths
13
- from config import *
14
- from constants import *
15
- from utils import *
16
- from gptManager import ChatgptManager
17
- from queryHelperManager import QueryHelper
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- logsDir = os.getenv("HF_HOME", "/data")
 
 
20
 
 
 
 
 
 
21
 
22
- pd.set_option('display.max_columns', None)
23
- pd.set_option('display.max_rows', 10)
24
-
25
- # Filter out all warning messages
26
- warnings.filterwarnings("ignore")
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)
39
- gptInstance = ChatgptManager(openAIClient, model=GPT_MODEL)
40
- queryHelper = QueryHelper(gptInstance=gptInstance,
41
- schemaName=SCHEMA_NAME,platform=PLATFORM,
42
- metadataLayout=metadataLayout,
43
- sampleDataRows=SAMPLE_ROW_MAX,
44
- gptSampleRows=GPT_SAMPLE_ROWS,
45
- dbEngine=dbEngine,
46
- getSampleDataForTablesAndCols=getSampleDataForTablesAndCols)
47
-
48
- def checkAuth(username, password):
49
- global ADMIN, PASSWD
50
- if username == ADMIN and password == PASSWD:
51
- return True
52
- return False
53
-
54
-
55
-
56
- # Function to save history of chat
57
- def respond(message, chatHistory):
58
- """gpt response handler for gradio ui"""
59
- global queryHelper
60
- try:
61
- if "modify" in message:
62
- botMessage, prospectTablesAndCols = queryHelper.getQueryForUserInput(message, chatHistory)
63
- else:
64
- botMessage, prospectTablesAndCols = queryHelper.getQueryForUserInput(message, [])
65
- except Exception as e:
66
- errorMessage = {"function":"queryHelper.getQueryForUserInput","error":str(e), "userInput":message}
67
- saveLog(errorMessage, 'error')
68
- raise ValueError(str(e))
69
- queryGenerated = extractSqlFromGptResponse(botMessage)
70
- logMessage = {"userInput":message, "tablesColsSelectedByGpt":str(prospectTablesAndCols) , "queryGenerated":queryGenerated, "completeGptResponse":botMessage}
71
- saveLog(logMessage)
72
- chatHistory.append((message, botMessage))
73
- time.sleep(2)
74
- return "", chatHistory
75
-
76
-
77
-
78
- def preProcessSQL(sql):
79
  sql=sql.replace(';', '')
80
- disclaimerOutputStripping = ""
81
- if ('limit' in sql[-15:].lower())==False:
82
- sql = sql + ' ' + 'limit 5'
83
- disclaimerOutputStripping = """Results are stripped to show only top 5 rows.
84
- Please add your custom limit to get extend result.
85
- eg\n select * from schema.table limit 20\n\n"""
86
- # sql = str(sql)
87
- # sql = sqlparse.format(sql, reindent=True, keyword_case='upper')
88
- return sql, disclaimerOutputStripping
89
-
90
- def testSQL(sql):
91
- global dbCreds, queryHelper
92
- dbEngine2 = DbEngine(dbCreds)
93
-
94
- sql, disclaimerOutputStripping = preProcessSQL(sql=sql)
95
- if not isDataQuery(sql):
96
- return "Sorry not allowed to run. As the query modifies the data."
97
- try:
98
- dbEngine2.connect()
99
- conn = dbEngine2.getConnection()
100
- df = pd.read_sql_query(sql, con=conn)
101
- dbEngine2.disconnect()
102
- return disclaimerOutputStripping + str(pd.DataFrame(df))
103
- except Exception as e:
104
- errorMessage = {"function":"testSQL","error":str(e), "userInput":sql}
105
- saveLog(errorMessage, 'error')
106
- print(f"Error occured during running the query {sql}.\n and the error is {str(e)}")
107
- # prompt = f"Please correct the following sql query, also it has to be run on {PLATFORM}. sql query is \n {sql}. the error occured is {str(e)}."
108
- # modifiedSql = queryHelper.modifySqlQueryEnteredByUser(prompt)
109
- # logMessage = {"function":"queryHelper.modifySqlQueryEnteredByUser", "sqlQuery":sql, "modifiedSQLQuery":modifiedSql}
110
- # saveLog(logMessage, 'info')
111
- dbEngine2.disconnect()
112
- return f"The query you entered throws some error. Here is the error. Please try different query\n {str(e)}"
113
-
114
-
115
- def onSelectedTablesChange(tablesSelected):
116
- #Updates tables visible and allow selecting columns for them
117
- global queryHelper
118
- print(f"Selected tables : {tablesSelected}")
119
- metadataLayout = queryHelper.getMetadata()
120
- allTablesAndCols = metadataLayout.getAllTablesCols()
121
- selectedTablesAndCols = metadataLayout.getSelectedTablesAndCols()
122
- allTablesList = list(allTablesAndCols.keys())
123
- tableBoxes = []
124
- for i in range(len(allTablesList)):
125
- if allTablesList[i] in tablesSelected:
126
- dd = gr.Dropdown(
127
- allTablesAndCols[allTablesList[i]],visible=True,value=selectedTablesAndCols.get(allTablesList[i],None), multiselect=True, label=allTablesList[i], info="Select columns of a table"
128
- )
129
- tableBoxes.append(dd)
130
- else:
131
- dd = gr.Dropdown(
132
- allTablesAndCols[allTablesList[i]],visible=False,value=selectedTablesAndCols.get(allTablesList[i],None), multiselect=True, label=allTablesList[i], info="Select columns of a table"
133
- )
134
- tableBoxes.append(dd)
135
- return tableBoxes
136
-
137
- def onSelectedColumnsChange(*tableBoxes):
138
- #update selection of columns and tables (include new tables and cols in gpts context)
139
- global queryHelper
140
- metadataLayout = queryHelper.getMetadata()
141
- allTablesAndCols = metadataLayout.getAllTablesCols()
142
- allTablesList = list(allTablesAndCols.keys())
143
- tablesAndCols = {}
144
- result = ''
145
- print("Getting selected tables and columns from gradio")
146
- for tableBox, table in zip(tableBoxes, allTablesList):
147
- if isinstance(tableBox, list):
148
- if len(tableBox)!=0:
149
- tablesAndCols[table] = tableBox
150
- else:
151
- pass
152
-
153
- metadataLayout.setSelection(tablesAndCols=tablesAndCols)
154
- print("metadata updated")
155
- print("Updating queryHelper state, and sample data")
156
- queryHelper.updateMetadata(metadataLayout)
157
- return "Columns udpated"
158
-
159
- def onResetToDefaultSelection():
160
- global queryHelper
161
- metadataLayout = queryHelper.getMetadata()
162
- metadataLayout.setSelection(tablesAndCols=tablesAndCols)
163
- queryHelper.updateMetadata(metadataLayout)
164
-
165
- metadataLayout = queryHelper.getMetadata()
166
- allTablesAndCols = metadataLayout.getAllTablesCols()
167
- selectedTablesAndCols = metadataLayout.getSelectedTablesAndCols()
168
- allTablesList = list(allTablesAndCols.keys())
169
- tableBoxes = []
170
- for i in range(len(allTablesList)):
171
- if allTablesList[i] in selectedTablesAndCols.keys():
172
- dd = gr.Dropdown(
173
- allTablesAndCols[allTablesList[i]],visible=True,value=selectedTablesAndCols.get(allTablesList[i],None), multiselect=True, label=allTablesList[i], info="Select columns of a table"
174
- )
175
- tableBoxes.append(dd)
176
- else:
177
- dd = gr.Dropdown(
178
- allTablesAndCols[allTablesList[i]],visible=False,value=selectedTablesAndCols.get(allTablesList[i],None), multiselect=True, label=allTablesList[i], info="Select columns of a table"
179
- )
180
- tableBoxes.append(dd)
181
-
182
- return tableBoxes
183
-
184
- def onSyncLogsWithDataDir():
185
- downloadableFilesPaths = getAllLogFilesPaths()
186
- fileComponent = gr.File(downloadableFilesPaths, file_count='multiple')
187
- return fileComponent
188
 
 
 
 
 
 
 
189
 
190
  with gr.Blocks() as demo:
191
- # screen 1 : Chatbot for question answering to generate sql query from user input in english
192
  with gr.Tab("Query Helper"):
193
  gr.Markdown("""<h1><center> Query Helper</center></h1>""")
194
  chatbot = gr.Chatbot()
@@ -196,63 +224,12 @@ with gr.Blocks() as demo:
196
  clear = gr.ClearButton([msg, chatbot])
197
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
198
 
199
- # screen 2 : To run sql query against database
200
  with gr.Tab("Run Query"):
201
- gr.Markdown("""<h1><center> Run Query </center></h1>""")
202
  text_input = gr.Textbox(label = 'Input SQL Query', placeholder="Write your SQL query here ...")
203
  text_output = gr.Textbox(label = 'Result')
204
  text_button = gr.Button("RUN QUERY")
205
  clear = gr.ClearButton([text_input, text_output])
206
- text_button.click(testSQL, inputs=text_input, outputs=text_output)
207
- # screen 3 : To set creds, schema, tables and columns
208
- with gr.Tab("Setup"):
209
- gr.Markdown("""<h1><center> Run Query </center></h1>""")
210
- text_input = gr.Textbox(label = 'schema name', value= SCHEMA_NAME)
211
- allTablesAndCols = queryHelper.getMetadata().getAllTablesCols()
212
- selectedTablesAndCols = queryHelper.getMetadata().getSelectedTablesAndCols()
213
- allTablesList = list(allTablesAndCols.keys())
214
- selectedTablesList = list(selectedTablesAndCols.keys())
215
-
216
- dropDown = gr.Dropdown(
217
- allTablesList, value=selectedTablesList, multiselect=True, label="Selected Tables", info="Select Tables from available tables of the schema"
218
- )
219
-
220
- refreshTables = gr.Button("Refresh selected tables")
221
-
222
- tableBoxes = []
223
-
224
- for i in range(len(allTablesList)):
225
- if allTablesList[i] in selectedTablesList:
226
- columnsDropDown = gr.Dropdown(
227
- allTablesAndCols[allTablesList[i]],visible=True,value=selectedTablesAndCols.get(allTablesList[i],None), multiselect=True, label=allTablesList[i], info="Select columns of a table"
228
- )
229
- #tableBoxes[allTables[i]] = columnsDropDown
230
- tableBoxes.append(columnsDropDown)
231
- else:
232
- columnsDropDown = gr.Dropdown(
233
- allTablesAndCols[allTablesList[i]], visible=False, value=None, multiselect=True, label=allTablesList[i], info="Select columns of a table"
234
- )
235
- #tableBoxes[allTables[i]] = columnsDropDown
236
- tableBoxes.append(columnsDropDown)
237
-
238
- refreshTables.click(onSelectedTablesChange, inputs=dropDown, outputs=tableBoxes)
239
-
240
-
241
-
242
-
243
- columnsTextBox = gr.Textbox(label = 'Result')
244
- refreshColumns = gr.Button("Refresh selected columns and Reload Data")
245
- refreshColumns.click(onSelectedColumnsChange, inputs=tableBoxes, outputs=columnsTextBox)
246
-
247
- resetToDefaultSelection = gr.Button("Reset to Default")
248
- resetToDefaultSelection.click(onResetToDefaultSelection, inputs=None, outputs=tableBoxes)
249
-
250
- #screen 4 for downloading logs
251
- with gr.Tab("Log-files"):
252
- downloadableFilesPaths = getAllLogFilesPaths()
253
- fileComponent = gr.File(downloadableFilesPaths, file_count='multiple')
254
- refreshLogs = gr.Button("Sync Log files from /data")
255
- refreshLogs.click(onSyncLogsWithDataDir, inputs=None, outputs=fileComponent)
256
-
257
- demo.launch(share=True, debug=True, ssl_verify=False, auth=checkAuth)
258
- dbEngine.connect()
 
1
+ # !pip install gradio
2
+ # !pip install openai
3
+ # import openai
4
+
5
+ import gradio
6
+ import pandas as pd
7
+ import psycopg2
8
+
9
  import pandas as pd
10
+ import openai
11
+
12
+ import sqlite3
13
  import psycopg2
14
  import time
15
  import gradio as gr
16
  import sqlparse
 
17
  import os
 
18
 
19
+ #EA_key
20
+ openai.api_key = os.getenv("api_key")
21
 
22
+ pd.set_option('display.max_columns', None)
23
+ pd.set_option('display.max_rows', None)
24
+
25
+ #database credential
26
+ db_name = os.getenv("db_name")
27
+ user_db = os.getenv("user_db")
28
+ pwd_db = os.getenv("pwd_db")
29
+ host_db = os.getenv("host_db")
30
+ port_db = os.getenv("port_db")
31
+
32
+ conn = psycopg2.connect(database=db_name, user = user_db, password = pwd_db, host = host_db, port = port_db)
33
+
34
+ # sql="select master_customer_id, c.gender,c.city_name,c.state_name, c.zip_code,product_name,department,class,category,d.date_value,s.city_name as store_city,s.state_name as store_state,s.zip_code as store_zip,s.store_name,s.opened_dt,s.closed_dt, f.transaction_amt,ch.type from oyster_demo.tbl_d_customer c,oyster_demo.tbl_d_product p,oyster_demo.tbl_f_sales f,oyster_demo.tbl_d_date d, oyster_demo.tbl_d_store s,oyster_demo.tbl_d_channel ch where p.product_id=f.product_id and c.customer_id=f.customer_id and d.date_id=f.date_id and s.store_id=f.store_id and ch.channel_id=f.channel_id"
35
+ sql2="""select * from lpdatamart.tbl_d_customer limit 10000"""
36
+ sql3="""select * from lpdatamart.tbl_d_product limit 1000"""
37
+ sql4="""select * from lpdatamart.tbl_f_sales limit 10000"""
38
+ # sql5="""select * from lpdatamart.tbl_d_time limit 10000"""
39
+ sql6="""select * from lpdatamart.tbl_d_store limit 10000"""
40
+ sql7="""select * from lpdatamart.tbl_d_channel limit 10000"""
41
+ sql8="""select * from lpdatamart.tbl_d_lineaction_code limit 10000"""
42
+ sql9 = """select * from lpdatamart.tbl_d_calendar limit 10000"""
43
+
44
+ df_customer = pd.read_sql_query(sql2, con=conn)
45
+ df_product = pd.read_sql_query(sql3, con=conn)
46
+ df_sales = pd.read_sql_query(sql4, con=conn)
47
+ # df_time = pd.read_sql_query(sql5, con=conn)
48
+ df_store = pd.read_sql_query(sql6, con=conn)
49
+ df_channel = pd.read_sql_query(sql7, con=conn)
50
+ df_lineaction = pd.read_sql_query(sql8, con=conn)
51
+ df_calendar = pd.read_sql_query(sql9, con=conn)
52
+
53
+
54
+ conn.close()
55
+ df_customer.head(2)
56
+
57
+ customer_col=['customer_id','customer_type', 'first_name', 'middle_name', 'household_name', 'last_name', 'personal_email', 'city', 'state', 'zip_code', 'address1', 'country', 'gender', 'phone_number', 'reward_number']
58
+ product_col=['product_id', 'product_name', 'product_price', 'department', 'class', 'discount', 'category', 'department_desc', 'department_type', 'product_type', 'manufacturer', 'color']
59
+ sales_col = ['store_id', 'customer_id', 'channel_id', 'product_id', 'time_id', 'date_id','order_id', 'line_action', 'discount_amount', 'shipping_amount','transaction_date', 'transaction_amount', 'transaction_type', 'qty_sold']
60
+ # time_col = ['time_id', 'hour', 'minute', 'second', 'am_pm']
61
+ store_col = ['store_id', 'store_number', 'store_name', 'store_designation', 'store_longitude', 'store_latitude', 'store_manager_name', 'zip_code', 'state_code', 'city', 'street_number', 'street_name', 'store_region', 'store_type', 'address1','sublocationcode', 'channel', 'company_flag', 'kiosk_physical_store', 'sublocation_code']
62
+ channel_col = ['channel_id', 'channel_name', 'channel_code']
63
+ lineaction_col = ['line_action_code', 'line_action_code_desc', 'load_date', 'catgory', 'sales_type']
64
+ calendar_col = ['date_id','calendar_date','calendar_month','day_of_week','calendar_week_number','calendar_month_number','calendar_quarter_number','day_of_month','day_of_quarter','day_of_the_year','us_holiday','lp_holiday','work_day','year','ad_week','ad_week_year','ad_month','lp_day','lp_week','lp_month','lp_year','lp_quarter','event_day']
65
+
66
+ df_customer=df_customer[customer_col]
67
+ df_product=df_product[product_col]
68
+ df_sales=df_sales[sales_col]
69
+ # df_time = df_time[time_col]
70
+ df_store = df_store[store_col]
71
+ df_channel = df_channel[channel_col]
72
+ df_lineaction = df_lineaction[lineaction_col]
73
+ df_calendar = df_calendar[calendar_col]
74
+
75
+ # df = pd.read_csv('/content/drive/MyDrive/tbl_m_querygen.csv')
76
+
77
+
78
+ import sqlite3
79
+ import openai
80
+
81
+ # Connect to SQLite database
82
+ conn1 = sqlite3.connect('chatgpt.db')
83
+ cursor1 = conn1.cursor()
84
+
85
+ # Connect to SQLite database
86
+ conn2 = sqlite3.connect('chatgpt.db')
87
+ cursor2 = conn2.cursor()
88
+
89
+ # Connect to SQLite database
90
+ conn3 = sqlite3.connect('chatgpt.db')
91
+ cursor3 = conn3.cursor()
92
+
93
+ # Connect to SQLite database
94
+ conn4 = sqlite3.connect('chatgpt.db')
95
+ cursor4 = conn4.cursor()
96
+
97
+ # Connect to SQLite database
98
+ conn5 = sqlite3.connect('chatgpt.db')
99
+ cursor5 = conn5.cursor()
100
+
101
+ # Connect to SQLite database
102
+ conn5 = sqlite3.connect('chatgpt.db')
103
+ cursor5 = conn5.cursor()
104
+
105
+ # Connect to SQLite database
106
+ conn6 = sqlite3.connect('chatgpt.db')
107
+ cursor6 = conn6.cursor()
108
+
109
+ # Connect to SQLite database
110
+ conn7 = sqlite3.connect('chatgpt.db')
111
+ cursor7 = conn7.cursor()
112
+
113
+ # Connect to SQLite database
114
+ conn8 = sqlite3.connect('chatgpt.db')
115
+ cursor8 = conn8.cursor()
116
+
117
+
118
+ # openai.api_key = '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'
119
+
120
+ # Insert DataFrame into SQLite database
121
+ df_customer.to_sql('tbl_d_customer', conn1, if_exists='replace', index=False)
122
+ df_product.to_sql('tbl_d_product', conn2, if_exists='replace', index=False)
123
+ df_sales.to_sql('tbl_f_sales', conn3, if_exists='replace', index=False)
124
+ # df_time.to_sql('tbl_d_time', conn4, if_exists='replace', index=False)
125
+ df_store.to_sql('tbl_d_store', conn5, if_exists='replace', index=False)
126
+ df_channel.to_sql('tbl_d_channel', conn6, if_exists='replace', index=False)
127
+ df_lineaction.to_sql('tbl_d_lineaction_code', conn7, if_exists='replace', index=False)
128
+ df_calendar.to_sql('tbl_d_calendar', conn8, if_exists ='replace',index=False)
129
+
130
+ # Function to get table columns from SQLite database
131
+ def get_table_columns(table_name1, table_name2):
132
+ cursor1.execute("PRAGMA table_info({})".format(table_name1))
133
+ columns1 = cursor1.fetchall()
134
+ # print(columns)
135
+
136
+ cursor2.execute("PRAGMA table_info({})".format(table_name2))
137
+ columns2 = cursor2.fetchall()
138
+
139
+ return [column[1] for column in columns1], [column[1] for column in columns2]
140
 
141
+ table_name1 = 'tbl_d_customer'
142
+ table_name2 = 'tbl_d_product'
143
+ table_name3 = 'tbl_f_sales'
144
 
145
+ # table_name4 = 'tbl_d_time'
146
+ table_name5 = 'tbl_d_store'
147
+ table_name6 = 'tbl_d_channel'
148
+ table_name7 = 'tbl_d_lineaction_code'
149
+ table_name8 = 'tbl_d_calendar'
150
 
151
+ columns1,columns2 = get_table_columns(table_name1,table_name2)
152
+
153
+
154
+
155
+ # Function to generate SQL query from input text using ChatGPT
156
+ def generate_sql_query(text):
157
+ # prompt = """You are a ChatGPT language model that can generate SQL queries. Please provide a natural language input text, and I will generate the corresponding SQL query and Answer the provided question if possible for you.The table name is {} and the following data:\n {} and corresponding columns are {}.\nInput: {}\nSQL Query:""".format(table_name,read_csv, columns,text)
158
+
159
+ messages.append({"role": "user", "content": text})
160
+ # print(prompt)
161
+ request = openai.ChatCompletion.create(
162
+ model="gpt-4",
163
+ messages=messages
164
+ )
165
+ print(request)
166
+ sql_query = request['choices'][0]['message']['content']
167
+ return sql_query
168
+
169
+ text = "for female customer who did a transaction of more than 100 dollars in year 2020 please write sql query ?"
170
+
171
+ schema_name = 'lpdatamart'
172
+ prompt = """Given an input text, and You will generate the corresponding SQL query. The schema name is {}. The first table name is {} and the following data:\n {}. The second table name is {} and the following data for second table:\n {}. The third table name is {} and the following data for third table:\n {}. The fourth table name is {} and the following data for fourth table:\n {}. The fifth table name is {} and the following data for fifth table:\n {}. The sixth table name is {} and the following data for sixth table:\n {}. The seventh table name is {} and the following data for seventh table:\n {} \n""".format(schema_name,table_name1,df_customer.loc[:5], table_name2, df_product.loc[:5], table_name3, df_sales.loc[:5], table_name5, df_store.loc[:5], table_name6, df_channel.loc[:5],table_name7, df_lineaction.loc[:5], table_name8, df_calendar.loc[:5])
173
+ messages = [{"role": "system", "content": prompt}]
174
+
175
+ sql_query=generate_sql_query(text)
176
+ print("Generated SQL query: ",sql_query)
177
+
178
+ # prompt = """Given an input text, and You will generate the corresponding SQL query. The first table name is {} and the following data:\n {}. The second table name is {} and the following data for second table:\n {}. The third table name is {} and the following data for third table:\n {}.\n""".format(table_name1,df2.loc[:5], table_name2, df3.loc[:5], table_name3, df4.loc[:5])
179
+ prompt = """Given an input text, and You will generate the corresponding SQL query. The schema name is {}. The first table name is {} and the following data:\n {}. The second table name is {} and the following data for second table:\n {}. The third table name is {} and the following data for third table:\n {}. The fourth table name is {} and the following data for fourth table:\n {}. The fifth table name is {} and the following data for fifth table:\n {}. The sixth table name is {} and the following data for sixth table:\n {}. The seventh table name is {} and the following data for seventh table:\n {} \n""".format(schema_name,table_name1,df_customer.loc[:5], table_name2, df_product.loc[:5], table_name3, df_sales.loc[:5], table_name5, df_store.loc[:5], table_name6, df_channel.loc[:5],table_name7, df_lineaction.loc[:5], table_name8, df_calendar.loc[:5])
180
+ messages = [{"role": "system", "content": prompt}]
181
+
182
+ import time
183
+ import gradio as gr
184
+ def CustomChatGPT(user_inp):
185
+ messages.append({"role": "user", "content": user_inp})
186
+ response = openai.ChatCompletion.create(
187
+ model = "gpt-4",
188
+ messages = messages
189
+ )
190
+ ChatGPT_reply = response["choices"][0]["message"]["content"]
191
+ messages.append({"role": "assistant", "content": ChatGPT_reply})
192
+ return ChatGPT_reply
193
+
194
+ def respond(message, chat_history):
195
+ bot_message = CustomChatGPT(message)
196
+ chat_history.append((message, bot_message))
197
+ time.sleep(2)
198
+ return "", chat_history
199
+
200
+ # to test the generated sql query
201
+ def test_Sql(sql):
 
 
 
 
 
 
202
  sql=sql.replace(';', '')
203
+ sql = sql + ' ' + 'limit 5'
204
+ sql = str(sql)
205
+ sql = sqlparse.format(sql, reindent=True, keyword_case='upper')
206
+
207
+ conn = psycopg2.connect(database=db_name, user = user_db, password = pwd_db, host = host_db, port = port_db)
208
+ df = pd.read_sql_query(sql, con=conn)
209
+ conn.close()
210
+ return pd.DataFrame(df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
 
212
+ admin = os.getenv("admin")
213
+ paswd = os.getenv("paswd")
214
+
215
+ def same_auth(username, password):
216
+ if username == admin and password == paswd:
217
+ return 1
218
 
219
  with gr.Blocks() as demo:
 
220
  with gr.Tab("Query Helper"):
221
  gr.Markdown("""<h1><center> Query Helper</center></h1>""")
222
  chatbot = gr.Chatbot()
 
224
  clear = gr.ClearButton([msg, chatbot])
225
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
226
 
 
227
  with gr.Tab("Run Query"):
228
+ # gr.Markdown("""<h1><center> Run Query </center></h1>""")
229
  text_input = gr.Textbox(label = 'Input SQL Query', placeholder="Write your SQL query here ...")
230
  text_output = gr.Textbox(label = 'Result')
231
  text_button = gr.Button("RUN QUERY")
232
  clear = gr.ClearButton([text_input, text_output])
233
+ text_button.click(test_Sql, inputs=text_input, outputs=text_output)
234
+
235
+ demo.launch(share=True, auth=same_auth)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
  pandas
2
  psycopg2
3
- openai==1.3.5
4
- sqlparse
5
- gradio==3.50.1
 
1
  pandas
2
  psycopg2
3
+ openai
4
+ sqlparse