Spaces:
Runtime error
Runtime error
Commit ·
9152447
1
Parent(s): 508f16f
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,21 +14,20 @@ import psycopg2
|
|
| 14 |
import time
|
| 15 |
import gradio as gr
|
| 16 |
import sqlparse
|
| 17 |
-
|
| 18 |
-
# from google.colab import drive
|
| 19 |
|
| 20 |
#EA_key
|
| 21 |
-
openai.api_key =
|
| 22 |
-
|
| 23 |
|
| 24 |
pd.set_option('display.max_columns', None)
|
| 25 |
pd.set_option('display.max_rows', None)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
conn = psycopg2.connect(database=db_name, user = user_db, password = pwd_db, host = host_db, port = port_db)
|
| 34 |
|
|
@@ -54,9 +53,6 @@ df_calendar = pd.read_sql_query(sql9, con=conn)
|
|
| 54 |
|
| 55 |
conn.close()
|
| 56 |
df_customer.head(2)
|
| 57 |
-
# df_customer.head(2)
|
| 58 |
-
# df_product.head(2)
|
| 59 |
-
# df_sales.head(2)
|
| 60 |
|
| 61 |
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']
|
| 62 |
product_col=['product_id', 'product_name', 'product_price', 'department', 'class', 'discount', 'category', 'department_desc', 'department_type', 'product_type', 'manufacturer', 'color']
|
|
@@ -170,51 +166,19 @@ def generate_sql_query(text):
|
|
| 170 |
sql_query = request['choices'][0]['message']['content']
|
| 171 |
return sql_query
|
| 172 |
|
| 173 |
-
# text="the customer who made a total transaction with more than 50 dollars ?"
|
| 174 |
-
# text="give me the list of male customer from california ?"
|
| 175 |
-
|
| 176 |
text = "for female customer who did a transaction of more than 100 dollars in year 2020 please write sql query ?"
|
| 177 |
|
| 178 |
-
|
| 179 |
schema_name = 'lpdatamart'
|
| 180 |
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])
|
| 181 |
messages = [{"role": "system", "content": prompt}]
|
| 182 |
|
| 183 |
sql_query=generate_sql_query(text)
|
| 184 |
print("Generated SQL query: ",sql_query)
|
| 185 |
-
# if sql_query:
|
| 186 |
-
# result=execute_sql_query(sql_query)
|
| 187 |
-
# print("ChatGPT Response=>",result)
|
| 188 |
-
|
| 189 |
-
# Close database connection
|
| 190 |
-
# cursor1.close()
|
| 191 |
-
# conn1.close()
|
| 192 |
-
|
| 193 |
-
# cursor2.close()
|
| 194 |
-
# conn2.close()
|
| 195 |
-
|
| 196 |
-
# cursor3.close()
|
| 197 |
-
# conn3.close()
|
| 198 |
-
|
| 199 |
|
| 200 |
# 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])
|
| 201 |
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])
|
| 202 |
messages = [{"role": "system", "content": prompt}]
|
| 203 |
|
| 204 |
-
# def CustomChatGPT(Question):
|
| 205 |
-
# messages.append({"role": "user", "content": Question})
|
| 206 |
-
# response = openai.ChatCompletion.create(
|
| 207 |
-
# model = "gpt-4",
|
| 208 |
-
# messages = messages
|
| 209 |
-
# )
|
| 210 |
-
# ChatGPT_reply = response["choices"][0]["message"]["content"]
|
| 211 |
-
# messages.append({"role": "assistant", "content": ChatGPT_reply})
|
| 212 |
-
# return ChatGPT_reply
|
| 213 |
-
|
| 214 |
-
# demo = gradio.Interface(fn=CustomChatGPT, inputs = "text", outputs = "text", title = "Query Helper")
|
| 215 |
-
|
| 216 |
-
# demo.launch(share=True)
|
| 217 |
-
|
| 218 |
import time
|
| 219 |
import gradio as gr
|
| 220 |
def CustomChatGPT(user_inp):
|
|
@@ -240,12 +204,6 @@ def test_Sql(sql):
|
|
| 240 |
sql = str(sql)
|
| 241 |
sql = sqlparse.format(sql, reindent=True, keyword_case='upper')
|
| 242 |
|
| 243 |
-
db_name = 'express'
|
| 244 |
-
user_db = "amardeep"
|
| 245 |
-
pwd_db = 'Welcome!23'
|
| 246 |
-
host_db = "ea-non-prod.cxw4zfxatj9b.us-west-1.redshift.amazonaws.com"
|
| 247 |
-
port_db = "5439"
|
| 248 |
-
|
| 249 |
conn = psycopg2.connect(database=db_name, user = user_db, password = pwd_db, host = host_db, port = port_db)
|
| 250 |
df = pd.read_sql_query(sql, con=conn)
|
| 251 |
conn.close()
|
|
@@ -267,5 +225,4 @@ with gr.Blocks() as demo:
|
|
| 267 |
clear = gr.ClearButton([text_input, text_output])
|
| 268 |
text_button.click(test_Sql, inputs=text_input, outputs=text_output)
|
| 269 |
|
| 270 |
-
demo.launch(share=True)
|
| 271 |
-
# inf.launch(share=True)
|
|
|
|
| 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 |
|
|
|
|
| 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']
|
|
|
|
| 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):
|
|
|
|
| 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()
|
|
|
|
| 225 |
clear = gr.ClearButton([text_input, text_output])
|
| 226 |
text_button.click(test_Sql, inputs=text_input, outputs=text_output)
|
| 227 |
|
| 228 |
+
demo.launch(share=True)
|
|
|