-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
252 lines (193 loc) · 5.19 KB
/
main.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import sqlite3
import japanize_matplotlib
import matplotlib.pyplot as plt
import openai
import pandas as pd
import seaborn as sns
import streamlit as st
from faker import Faker
from faker.providers import BaseProvider
table_definition = """
CREATE TABLE users (
id INTEGER,
age INTEGER NOT NULL,
name TEXT NOT NULL,
sex VARCHAR(10) NOT NULL,
job VARCHAR(50) NOT NULL,
team VARCHAR(50) NOT NULL,
country VARCHAR(50) NOT NULL,
grade VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
"""
fake = Faker("ja_JP")
class MyProvider(BaseProvider):
def job(self, team):
print(team)
if team == "開発部":
return self.random_element(
[
"エンジニア",
"デザイナー",
"PdM",
]
)
elif team == "営業部":
return self.random_element(
[
"営業",
]
)
elif team == "人事部":
return self.random_element(
[
"人事",
]
)
return "その他"
def sex(self):
return self.random_element(
[
"男性",
"女性",
]
)
def age(self):
return self.random_int(min=20, max=65)
def country(self):
return self.random_element(
[
"日本",
"アメリカ",
"カナダ",
"イギリス",
]
)
def team(self):
return self.random_element(
[
"開発部",
"営業部",
"人事部",
]
)
def grade(self):
return self.random_element(
[
"G1",
"G2",
"G3",
"G4",
"G5",
"G6",
"G7",
]
)
fake.add_provider(MyProvider)
def create_table(conn):
c = conn.cursor()
create_query = table_definition
c.execute(create_query)
def insert_data(conn):
c = conn.cursor()
for i in range(100):
age = fake.age()
name = fake.name()
sex = fake.sex()
team = fake.team()
job = fake.job(team)
country = fake.country()
grade = fake.grade()
# Insert query
insert_query = f"""
INSERT INTO users (age, name, sex, job, team, country, grade)
VALUES ({age}, '{name}', '{sex}', '{job}', '{team}', '{country}', '{grade}')
"""
print(insert_query)
c.execute(insert_query)
conn.commit()
def init_dataframe(conn, query, multi_index=False):
c = conn.cursor()
c.execute(query)
results = c.fetchall()
columns = []
for v in c.description:
columns.append(v[0])
df = pd.DataFrame(data=results, columns=columns)
return df
@st.cache_resource
def load_data():
conn = sqlite3.connect(":memory:", check_same_thread=False)
create_table(conn)
insert_data(conn)
df = init_dataframe(conn, "SELECT * FROM users")
print(df)
return conn, df
conn, df = load_data()
if "conn" not in st.session_state:
st.session_state["conn"] = conn
st.write("Default data")
st.dataframe(df)
def on_change():
instruction = st.session_state.instruction
if instruction == "":
return
sql = generate_sql(instruction)
st.session_state.query = sql
conn = st.session_state["conn"]
df = init_dataframe(conn, sql)
st.session_state["aggregated_df"] = df
st.text_area(key="instruction", label="instruction", on_change=on_change)
st.text_area(key="query", label="SQL (Debug)")
if "aggregated_df" in st.session_state:
aggregated_df = st.session_state["aggregated_df"]
st.dataframe(aggregated_df)
# st.bar_chart(aggregated_df)
index_names = aggregated_df.index.names
columns = aggregated_df.columns
x = columns[0]
hue = None
if len(columns) > 2:
hue = columns[1]
y = columns[1]
if len(columns) > 2:
y = columns[2]
fig = plt.figure(figsize=(15, 10))
sns.barplot(
data=aggregated_df,
x=x,
y=y,
hue=hue,
)
st.pyplot(fig)
def generate_sql(instruction):
content = f"""
There is a table defined by the following CREATE statement in sqlite3.
{table_definition}
Each column corresponds to the following.
id: ID
age: 年齢
name: 氏名
job: 職種
team: 部署
country: 出身国
grade: 等級
And there are the following definitions.
リーダー: grade is 'G5' or 'G6' or 'G7'
外国籍: country is not '日本'
Write a SQL that achieve the following instruction.
The instruction is written in Japanese.
{instruction}
Output only SQL in raw text.
Remove any information except for SQL.
If you cannot write SQL, write 'null'.
"""
print(content)
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": content}],
temperature=0.0,
)
print(completion)
sql = completion.choices[0].message.content
return sql