-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp2.py
More file actions
145 lines (118 loc) · 4.38 KB
/
app2.py
File metadata and controls
145 lines (118 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import streamlit as st
from sqlalchemy import create_engine
from sqlalchemy import text
import pandas as pd
import re
import openai
from secret_key import openai_key
openai.api_key = openai_key
import streamlit as st
# Custom CSS style
custom_style = """
<style>
@import url('https://fonts.googleapis.com/css2?family=Curlz+MT&display=swap');
.center-top-container {
text-align: center;
padding-top: 15vh; /* Adjust the top padding as needed */
}
.custom-title {
font-family: 'Showcard Gothic', cursive;
font-size: 5em;
font-style: italic;
font-weight: bold;
margin: 0; /* Remove default margin */
}
.custom-title .pink-q {
color: pink; /* Make the Q pink */
}
.custom-title .pink-l {
color: pink; /* Make the L pink */
}
</style>
"""
st.markdown(custom_style, unsafe_allow_html=True)
# Display the centered title at the top
st.markdown('<div class="center-top-container"><p class="custom-title"> <span class="pink-q">Q</span>uerio<span class="pink-l"> L</span>ingua</p></div>', unsafe_allow_html=True)
def create_table_prompt(df):
"""
This Function returns a prompt informs GPT that we want to work with SQL Tables
"""
prompt = '''Sql column names for table project are given below:
{}
'''.format(list(df.columns))
return prompt
# Uploading the file
with st.sidebar:
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
if uploaded_file is None:
st.sidebar.warning("Upload a dataset")
else:
df = pd.read_csv(uploaded_file, index_col=0)
st.write("Uploaded Data:")
my_db = create_engine('sqlite:///:memory:')
data = df.to_sql(name = 'project',con = my_db, index=False)
st.write("Showing only the 10 rows ~")
st.table(df.head(10))
col1,col2,col3 = st.columns(3)
cat_col=[]
num_col=[]
for i in df.columns:
if df[i].dtype == "object":
cat_col.append(i)
else:
num_col.append(i)
# Description
if st.button("Show Descriptions"):
with col1:
st.write("Number of Columns ~", len(df.columns))
st.write("Number of Rows ~", len(df))
with col2:
if len(cat_col) ==0 :
st.write("No Catagorical Columns")
else:
st.write("Catagorical Columns", cat_col)
with col3:
if len(num_col) ==0:
st.write("No Catagorical Columns")
else:
st.write("Numerical Columns", num_col)
# Column wise Description
# Taking the querry input
nlp_text = st.text_input("Enter information you want to obtain:")
if nlp_text is None:
st.warning("Enter a query to obtain information")
else:
def combine_prompts(df, query_prompt):
defination = create_table_prompt(df)
query_init_string = f'### Give me SQL code to retrieve: {query_prompt}\n"SELECT"'
return defination+query_init_string
response = openai.Completion.create(
model = "text-davinci-003",
prompt = combine_prompts(df, nlp_text),
temperature = 1,
max_tokens =150,
top_p =1.0,
frequency_penalty =0.0,
presence_penalty = 0.0,
stop =["#", ";"]
)
def handle_response(response):
query = response["choices"][0]["text"]
query = re.sub(r'"', '', query)
if query.startswith(" "):
query = "Select"+ query
return query
with my_db.connect() as conn:
results = conn.execute(text(handle_response(response)))
# Desigining the Output
col1, col2 = st.columns(2)
if st.button("Show The SQL Querry and the Output"):
with col1:
st.markdown('**:green[SQL Query]**')
response_output = handle_response(response)
st.code(response_output)
with col2:
st.markdown('**:green[Query Output]**')
if nlp_text.strip(): # Only display if nlp_text is not empty
for result in results.all():
st.write(result)