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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# !pip install gradio
|
| 2 |
+
# !pip install openai
|
| 3 |
+
|
| 4 |
+
# import openai
|
| 5 |
+
import gradio
|
| 6 |
+
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import psycopg2
|
| 9 |
+
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import openai
|
| 12 |
+
|
| 13 |
+
import sqlite3
|
| 14 |
+
import psycopg2
|
| 15 |
+
import time
|
| 16 |
+
import gradio as gr
|
| 17 |
+
import sqlparse
|
| 18 |
+
|
| 19 |
+
# from google.colab import drive
|
| 20 |
+
|
| 21 |
+
#EA_key
|
| 22 |
+
openai.api_key = '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'
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
pd.set_option('display.max_columns', None)
|
| 26 |
+
pd.set_option('display.max_rows', None)
|
| 27 |
+
|
| 28 |
+
db_name = 'express'
|
| 29 |
+
user_db = "amardeep"
|
| 30 |
+
pwd_db = 'Welcome!23'
|
| 31 |
+
host_db = "ea-non-prod.cxw4zfxatj9b.us-west-1.redshift.amazonaws.com"
|
| 32 |
+
port_db = "5439"
|
| 33 |
+
|
| 34 |
+
conn = psycopg2.connect(database=db_name, user = user_db, password = pwd_db, host = host_db, port = port_db)
|
| 35 |
+
|
| 36 |
+
# 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"
|
| 37 |
+
sql2="""select * from lpdatamart.tbl_d_customer limit 10000"""
|
| 38 |
+
sql3="""select * from lpdatamart.tbl_d_product limit 1000"""
|
| 39 |
+
sql4="""select * from lpdatamart.tbl_f_sales limit 10000"""
|
| 40 |
+
# sql5="""select * from lpdatamart.tbl_d_time limit 10000"""
|
| 41 |
+
sql6="""select * from lpdatamart.tbl_d_store limit 10000"""
|
| 42 |
+
sql7="""select * from lpdatamart.tbl_d_channel limit 10000"""
|
| 43 |
+
sql8="""select * from lpdatamart.tbl_d_lineaction_code limit 10000"""
|
| 44 |
+
sql9 = """select * from lpdatamart.tbl_d_calendar limit 10000"""
|
| 45 |
+
|
| 46 |
+
df_customer = pd.read_sql_query(sql2, con=conn)
|
| 47 |
+
df_product = pd.read_sql_query(sql3, con=conn)
|
| 48 |
+
df_sales = pd.read_sql_query(sql4, con=conn)
|
| 49 |
+
# df_time = pd.read_sql_query(sql5, con=conn)
|
| 50 |
+
df_store = pd.read_sql_query(sql6, con=conn)
|
| 51 |
+
df_channel = pd.read_sql_query(sql7, con=conn)
|
| 52 |
+
df_lineaction = pd.read_sql_query(sql8, con=conn)
|
| 53 |
+
df_calendar = pd.read_sql_query(sql9, con=conn)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
conn.close()
|
| 57 |
+
df_customer.head(2)
|
| 58 |
+
# df_customer.head(2)
|
| 59 |
+
# df_product.head(2)
|
| 60 |
+
# df_sales.head(2)
|
| 61 |
+
|
| 62 |
+
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']
|
| 63 |
+
product_col=['product_id', 'product_name', 'product_price', 'department', 'class', 'discount', 'category', 'department_desc', 'department_type', 'product_type', 'manufacturer', 'color']
|
| 64 |
+
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']
|
| 65 |
+
# time_col = ['time_id', 'hour', 'minute', 'second', 'am_pm']
|
| 66 |
+
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']
|
| 67 |
+
channel_col = ['channel_id', 'channel_name', 'channel_code']
|
| 68 |
+
lineaction_col = ['line_action_code', 'line_action_code_desc', 'load_date', 'catgory', 'sales_type']
|
| 69 |
+
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']
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
df_customer=df_customer[customer_col]
|
| 73 |
+
df_product=df_product[product_col]
|
| 74 |
+
df_sales=df_sales[sales_col]
|
| 75 |
+
# df_time = df_time[time_col]
|
| 76 |
+
df_store = df_store[store_col]
|
| 77 |
+
df_channel = df_channel[channel_col]
|
| 78 |
+
df_lineaction = df_lineaction[lineaction_col]
|
| 79 |
+
df_calendar = df_calendar[calendar_col]
|
| 80 |
+
|
| 81 |
+
# df = pd.read_csv('/content/drive/MyDrive/tbl_m_querygen.csv')
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
import sqlite3
|
| 85 |
+
import openai
|
| 86 |
+
|
| 87 |
+
# Connect to SQLite database
|
| 88 |
+
conn1 = sqlite3.connect('chatgpt.db')
|
| 89 |
+
cursor1 = conn1.cursor()
|
| 90 |
+
|
| 91 |
+
# Connect to SQLite database
|
| 92 |
+
conn2 = sqlite3.connect('chatgpt.db')
|
| 93 |
+
cursor2 = conn2.cursor()
|
| 94 |
+
|
| 95 |
+
# Connect to SQLite database
|
| 96 |
+
conn3 = sqlite3.connect('chatgpt.db')
|
| 97 |
+
cursor3 = conn3.cursor()
|
| 98 |
+
|
| 99 |
+
# Connect to SQLite database
|
| 100 |
+
conn4 = sqlite3.connect('chatgpt.db')
|
| 101 |
+
cursor4 = conn4.cursor()
|
| 102 |
+
|
| 103 |
+
# Connect to SQLite database
|
| 104 |
+
conn5 = sqlite3.connect('chatgpt.db')
|
| 105 |
+
cursor5 = conn5.cursor()
|
| 106 |
+
|
| 107 |
+
# Connect to SQLite database
|
| 108 |
+
conn5 = sqlite3.connect('chatgpt.db')
|
| 109 |
+
cursor5 = conn5.cursor()
|
| 110 |
+
|
| 111 |
+
# Connect to SQLite database
|
| 112 |
+
conn6 = sqlite3.connect('chatgpt.db')
|
| 113 |
+
cursor6 = conn6.cursor()
|
| 114 |
+
|
| 115 |
+
# Connect to SQLite database
|
| 116 |
+
conn7 = sqlite3.connect('chatgpt.db')
|
| 117 |
+
cursor7 = conn7.cursor()
|
| 118 |
+
|
| 119 |
+
# Connect to SQLite database
|
| 120 |
+
conn8 = sqlite3.connect('chatgpt.db')
|
| 121 |
+
cursor8 = conn8.cursor()
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# openai.api_key = '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'
|
| 125 |
+
|
| 126 |
+
# Insert DataFrame into SQLite database
|
| 127 |
+
df_customer.to_sql('tbl_d_customer', conn1, if_exists='replace', index=False)
|
| 128 |
+
df_product.to_sql('tbl_d_product', conn2, if_exists='replace', index=False)
|
| 129 |
+
df_sales.to_sql('tbl_f_sales', conn3, if_exists='replace', index=False)
|
| 130 |
+
# df_time.to_sql('tbl_d_time', conn4, if_exists='replace', index=False)
|
| 131 |
+
df_store.to_sql('tbl_d_store', conn5, if_exists='replace', index=False)
|
| 132 |
+
df_channel.to_sql('tbl_d_channel', conn6, if_exists='replace', index=False)
|
| 133 |
+
df_lineaction.to_sql('tbl_d_lineaction_code', conn7, if_exists='replace', index=False)
|
| 134 |
+
df_calendar.to_sql('tbl_d_calendar', conn8, if_exists ='replace',index=False)
|
| 135 |
+
|
| 136 |
+
# Function to get table columns from SQLite database
|
| 137 |
+
def get_table_columns(table_name1, table_name2):
|
| 138 |
+
cursor1.execute("PRAGMA table_info({})".format(table_name1))
|
| 139 |
+
columns1 = cursor1.fetchall()
|
| 140 |
+
# print(columns)
|
| 141 |
+
|
| 142 |
+
cursor2.execute("PRAGMA table_info({})".format(table_name2))
|
| 143 |
+
columns2 = cursor2.fetchall()
|
| 144 |
+
|
| 145 |
+
return [column[1] for column in columns1], [column[1] for column in columns2]
|
| 146 |
+
|
| 147 |
+
table_name1 = 'tbl_d_customer'
|
| 148 |
+
table_name2 = 'tbl_d_product'
|
| 149 |
+
table_name3 = 'tbl_f_sales'
|
| 150 |
+
|
| 151 |
+
# table_name4 = 'tbl_d_time'
|
| 152 |
+
table_name5 = 'tbl_d_store'
|
| 153 |
+
table_name6 = 'tbl_d_channel'
|
| 154 |
+
table_name7 = 'tbl_d_lineaction_code'
|
| 155 |
+
table_name8 = 'tbl_d_calendar'
|
| 156 |
+
|
| 157 |
+
columns1,columns2 = get_table_columns(table_name1,table_name2)
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
# Function to generate SQL query from input text using ChatGPT
|
| 162 |
+
def generate_sql_query(text):
|
| 163 |
+
# 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)
|
| 164 |
+
|
| 165 |
+
messages.append({"role": "user", "content": text})
|
| 166 |
+
# print(prompt)
|
| 167 |
+
request = openai.ChatCompletion.create(
|
| 168 |
+
model="gpt-4",
|
| 169 |
+
messages=messages
|
| 170 |
+
)
|
| 171 |
+
print(request)
|
| 172 |
+
sql_query = request['choices'][0]['message']['content']
|
| 173 |
+
return sql_query
|
| 174 |
+
|
| 175 |
+
# text="the customer who made a total transaction with more than 50 dollars ?"
|
| 176 |
+
# text="give me the list of male customer from california ?"
|
| 177 |
+
|
| 178 |
+
text = "for female customer who did a transaction of more than 100 dollars in year 2020 please write sql query ?"
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
schema_name = 'lpdatamart'
|
| 182 |
+
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])
|
| 183 |
+
messages = [{"role": "system", "content": prompt}]
|
| 184 |
+
|
| 185 |
+
sql_query=generate_sql_query(text)
|
| 186 |
+
print("Generated SQL query: ",sql_query)
|
| 187 |
+
# if sql_query:
|
| 188 |
+
# result=execute_sql_query(sql_query)
|
| 189 |
+
# print("ChatGPT Response=>",result)
|
| 190 |
+
|
| 191 |
+
# Close database connection
|
| 192 |
+
# cursor1.close()
|
| 193 |
+
# conn1.close()
|
| 194 |
+
|
| 195 |
+
# cursor2.close()
|
| 196 |
+
# conn2.close()
|
| 197 |
+
|
| 198 |
+
# cursor3.close()
|
| 199 |
+
# conn3.close()
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
# 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])
|
| 203 |
+
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])
|
| 204 |
+
messages = [{"role": "system", "content": prompt}]
|
| 205 |
+
|
| 206 |
+
# def CustomChatGPT(Question):
|
| 207 |
+
# messages.append({"role": "user", "content": Question})
|
| 208 |
+
# response = openai.ChatCompletion.create(
|
| 209 |
+
# model = "gpt-4",
|
| 210 |
+
# messages = messages
|
| 211 |
+
# )
|
| 212 |
+
# ChatGPT_reply = response["choices"][0]["message"]["content"]
|
| 213 |
+
# messages.append({"role": "assistant", "content": ChatGPT_reply})
|
| 214 |
+
# return ChatGPT_reply
|
| 215 |
+
|
| 216 |
+
# demo = gradio.Interface(fn=CustomChatGPT, inputs = "text", outputs = "text", title = "Query Helper")
|
| 217 |
+
|
| 218 |
+
# demo.launch(share=True)
|
| 219 |
+
|
| 220 |
+
import time
|
| 221 |
+
import gradio as gr
|
| 222 |
+
def CustomChatGPT(user_inp):
|
| 223 |
+
messages.append({"role": "user", "content": user_inp})
|
| 224 |
+
response = openai.ChatCompletion.create(
|
| 225 |
+
model = "gpt-4",
|
| 226 |
+
messages = messages
|
| 227 |
+
)
|
| 228 |
+
ChatGPT_reply = response["choices"][0]["message"]["content"]
|
| 229 |
+
messages.append({"role": "assistant", "content": ChatGPT_reply})
|
| 230 |
+
return ChatGPT_reply
|
| 231 |
+
|
| 232 |
+
def respond(message, chat_history):
|
| 233 |
+
bot_message = CustomChatGPT(message)
|
| 234 |
+
chat_history.append((message, bot_message))
|
| 235 |
+
time.sleep(2)
|
| 236 |
+
return "", chat_history
|
| 237 |
+
|
| 238 |
+
# to test the generated sql query
|
| 239 |
+
def test_Sql(sql):
|
| 240 |
+
sql=sql.replace(';', '')
|
| 241 |
+
sql = sql + ' ' + 'limit 5'
|
| 242 |
+
sql = str(sql)
|
| 243 |
+
sql = sqlparse.format(sql, reindent=True, keyword_case='upper')
|
| 244 |
+
|
| 245 |
+
db_name = 'express'
|
| 246 |
+
user_db = "amardeep"
|
| 247 |
+
pwd_db = 'Welcome!23'
|
| 248 |
+
host_db = "ea-non-prod.cxw4zfxatj9b.us-west-1.redshift.amazonaws.com"
|
| 249 |
+
port_db = "5439"
|
| 250 |
+
|
| 251 |
+
conn = psycopg2.connect(database=db_name, user = user_db, password = pwd_db, host = host_db, port = port_db)
|
| 252 |
+
df = pd.read_sql_query(sql, con=conn)
|
| 253 |
+
conn.close()
|
| 254 |
+
return pd.DataFrame(df)
|
| 255 |
+
|
| 256 |
+
with gr.Blocks() as demo:
|
| 257 |
+
with gr.Tab("Query Helper"):
|
| 258 |
+
gr.Markdown("""<h1><center> Query Helper</center></h1>""")
|
| 259 |
+
chatbot = gr.Chatbot()
|
| 260 |
+
msg = gr.Textbox()
|
| 261 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 262 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 263 |
+
|
| 264 |
+
with gr.Tab("Run Query"):
|
| 265 |
+
# gr.Markdown("""<h1><center> Run Query </center></h1>""")
|
| 266 |
+
text_input = gr.Textbox(label = 'Input SQL Query', placeholder="Write your SQL query here ...")
|
| 267 |
+
text_output = gr.Textbox(label = 'Result')
|
| 268 |
+
text_button = gr.Button("RUN QUERY")
|
| 269 |
+
clear = gr.ClearButton([text_input, text_output])
|
| 270 |
+
text_button.click(test_Sql, inputs=text_input, outputs=text_output)
|
| 271 |
+
|
| 272 |
+
demo.launch(share=True)
|
| 273 |
+
# inf.launch(share=True)
|