Spaces:
Runtime error
Runtime error
Timothyxxx
commited on
Commit
·
9611943
1
Parent(s):
ce102ad
Fix bugs; Add more demonstration for execution steps; Add input tables
Browse files- app.py +45 -16
- nsql/nsql_exec.py +6 -0
app.py
CHANGED
|
@@ -49,11 +49,12 @@ def get_key():
|
|
| 49 |
ip = requests.get('https://checkip.amazonaws.com').text.strip()
|
| 50 |
print(ip)
|
| 51 |
|
| 52 |
-
URL = "http://54.242.37.195:8080/api/predict"
|
| 53 |
-
# The springboard machine we built to protect the key, 20217 is the birthday of Tianbao's girlfriend
|
| 54 |
-
# we will only let the demo machine have the access to the keys
|
| 55 |
-
|
| 56 |
-
one_key = requests.post(url=URL, json={"data": "Hi, binder server. Give me a key!"}).json()['data'][0]
|
|
|
|
| 57 |
return one_key
|
| 58 |
|
| 59 |
|
|
@@ -96,6 +97,20 @@ def generate_binder_program(_args, _generator, _data_item):
|
|
| 96 |
return response_dict["0"][0][0]
|
| 97 |
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
# Set up
|
| 100 |
import nltk
|
| 101 |
|
|
@@ -217,34 +232,48 @@ try:
|
|
| 217 |
if selected_language == 'SQL':
|
| 218 |
with open("tmp_for_vis/{}_tmp_for_vis_steps.txt".format(stamp), "r") as f:
|
| 219 |
steps = json.load(f)
|
| 220 |
-
col1, col2, col3 = st.columns([4.7, 0.6, 4.7])
|
| 221 |
-
# col1.subheader('Steps')
|
| 222 |
-
# col3.subheader('Intermediate results')
|
| 223 |
for i, step in enumerate(steps):
|
| 224 |
-
col1, _, _ = st.columns([
|
| 225 |
with col1:
|
| 226 |
st.markdown(f'**Step#{i + 1}**')
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
| 228 |
with col1:
|
| 229 |
st.markdown('```sql\n' + step + '\n```')
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
with col2:
|
| 234 |
st.markdown('$\\rightarrow$')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
with st.spinner('...'):
|
| 236 |
time.sleep(1)
|
| 237 |
with open("tmp_for_vis/{}_result_step_{}.txt".format(stamp, i), "r") as f:
|
| 238 |
result_in_this_step = json.load(f)
|
| 239 |
with col3:
|
| 240 |
if isinstance(result_in_this_step, Dict):
|
| 241 |
-
|
| 242 |
-
|
|
|
|
| 243 |
if isinstance(header, list):
|
| 244 |
for idx in range(len(header)):
|
| 245 |
if header[idx].startswith('col_'):
|
| 246 |
header[idx] = step
|
| 247 |
-
st.dataframe(pd.DataFrame(
|
| 248 |
else:
|
| 249 |
st.markdown(result_in_this_step)
|
| 250 |
with st.spinner('...'):
|
|
|
|
| 49 |
ip = requests.get('https://checkip.amazonaws.com').text.strip()
|
| 50 |
print(ip)
|
| 51 |
|
| 52 |
+
# URL = "http://54.242.37.195:8080/api/predict"
|
| 53 |
+
# # The springboard machine we built to protect the key, 20217 is the birthday of Tianbao's girlfriend
|
| 54 |
+
# # we will only let the demo machine have the access to the keys
|
| 55 |
+
#
|
| 56 |
+
# one_key = requests.post(url=URL, json={"data": "Hi, binder server. Give me a key!"}).json()['data'][0]
|
| 57 |
+
one_key = "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"
|
| 58 |
return one_key
|
| 59 |
|
| 60 |
|
|
|
|
| 97 |
return response_dict["0"][0][0]
|
| 98 |
|
| 99 |
|
| 100 |
+
def remove_row_id(table):
|
| 101 |
+
new_table = {"header": [], "rows": []}
|
| 102 |
+
header: list = table['header']
|
| 103 |
+
rows = table['rows']
|
| 104 |
+
|
| 105 |
+
if not 'row_id' in header:
|
| 106 |
+
return table
|
| 107 |
+
|
| 108 |
+
new_table['header'] = header[1:]
|
| 109 |
+
new_table['rows'] = [row[1:] for row in rows]
|
| 110 |
+
|
| 111 |
+
return new_table
|
| 112 |
+
|
| 113 |
+
|
| 114 |
# Set up
|
| 115 |
import nltk
|
| 116 |
|
|
|
|
| 232 |
if selected_language == 'SQL':
|
| 233 |
with open("tmp_for_vis/{}_tmp_for_vis_steps.txt".format(stamp), "r") as f:
|
| 234 |
steps = json.load(f)
|
|
|
|
|
|
|
|
|
|
| 235 |
for i, step in enumerate(steps):
|
| 236 |
+
col1, _, _ = st.columns([7, 1, 2])
|
| 237 |
with col1:
|
| 238 |
st.markdown(f'**Step#{i + 1}**')
|
| 239 |
+
if i == len(steps) - 1:
|
| 240 |
+
col1, col1_25, col1_5, col2, col3 = st.columns([5, 0.3, 1.7, 1, 2])
|
| 241 |
+
else:
|
| 242 |
+
col1, col1_25, col1_5, col2, col3 = st.columns([3, 0.3, 2.7, 1, 2])
|
| 243 |
with col1:
|
| 244 |
st.markdown('```sql\n' + step + '\n```')
|
| 245 |
+
with col1_25:
|
| 246 |
+
st.markdown("on")
|
| 247 |
+
with col1_5:
|
| 248 |
+
if i == len(steps) - 1:
|
| 249 |
+
st.metric(label="whole", value="Table", delta = None)
|
| 250 |
+
else:
|
| 251 |
+
with open("tmp_for_vis/{}_result_step_{}_input.txt".format(stamp, i), "r") as f:
|
| 252 |
+
sub_tables_input = json.load(f)
|
| 253 |
+
for sub_table in sub_tables_input:
|
| 254 |
+
sub_table_to_print = remove_row_id(sub_table)
|
| 255 |
+
st.dataframe(pd.DataFrame(sub_table_to_print['rows'], columns=sub_table_to_print['header']))
|
| 256 |
with col2:
|
| 257 |
st.markdown('$\\rightarrow$')
|
| 258 |
+
if i == len(steps) - 1:
|
| 259 |
+
# The final step
|
| 260 |
+
st.metric(label="+", value="{}".format(selected_language), delta="Interpreter")
|
| 261 |
+
else:
|
| 262 |
+
st.metric(label="+", value="Codex", delta="10+ examples")
|
| 263 |
with st.spinner('...'):
|
| 264 |
time.sleep(1)
|
| 265 |
with open("tmp_for_vis/{}_result_step_{}.txt".format(stamp, i), "r") as f:
|
| 266 |
result_in_this_step = json.load(f)
|
| 267 |
with col3:
|
| 268 |
if isinstance(result_in_this_step, Dict):
|
| 269 |
+
|
| 270 |
+
rows = remove_row_id(result_in_this_step)["rows"]
|
| 271 |
+
header = remove_row_id(result_in_this_step)["header"]
|
| 272 |
if isinstance(header, list):
|
| 273 |
for idx in range(len(header)):
|
| 274 |
if header[idx].startswith('col_'):
|
| 275 |
header[idx] = step
|
| 276 |
+
st.dataframe(pd.DataFrame(rows, columns=header), use_container_width=True)
|
| 277 |
else:
|
| 278 |
st.markdown(result_in_this_step)
|
| 279 |
with st.spinner('...'):
|
nsql/nsql_exec.py
CHANGED
|
@@ -120,6 +120,8 @@ class NSQLExecutor(object):
|
|
| 120 |
qa_type="map",
|
| 121 |
new_col_name_s=step.produced_col_name_s,
|
| 122 |
verbose=verbose)
|
|
|
|
|
|
|
| 123 |
with open("tmp_for_vis/{}_result_step_{}.txt".format(stamp, steps.index(step)), "w") as f:
|
| 124 |
json.dump(sub_table, f)
|
| 125 |
db.add_sub_table(sub_table, verbose=verbose)
|
|
@@ -131,6 +133,8 @@ class NSQLExecutor(object):
|
|
| 131 |
qa_type="map",
|
| 132 |
new_col_name_s=["col_{}".format(col_idx)],
|
| 133 |
verbose=verbose)
|
|
|
|
|
|
|
| 134 |
with open("tmp_for_vis/{}_result_step_{}.txt".format(stamp, steps.index(step)), "w") as f:
|
| 135 |
json.dump(sub_table, f)
|
| 136 |
return extract_answers(sub_table)
|
|
@@ -143,6 +147,8 @@ class NSQLExecutor(object):
|
|
| 143 |
table_title=db.table_title,
|
| 144 |
qa_type="ans",
|
| 145 |
verbose=verbose)
|
|
|
|
|
|
|
| 146 |
with open("tmp_for_vis/{}_result_step_{}.txt".format(stamp, steps.index(step)), "w") as f:
|
| 147 |
json.dump(answer, f)
|
| 148 |
if step.father:
|
|
|
|
| 120 |
qa_type="map",
|
| 121 |
new_col_name_s=step.produced_col_name_s,
|
| 122 |
verbose=verbose)
|
| 123 |
+
with open("tmp_for_vis/{}_result_step_{}_input.txt".format(stamp, steps.index(step)), "w") as f:
|
| 124 |
+
json.dump(sql_executed_sub_tables, f)
|
| 125 |
with open("tmp_for_vis/{}_result_step_{}.txt".format(stamp, steps.index(step)), "w") as f:
|
| 126 |
json.dump(sub_table, f)
|
| 127 |
db.add_sub_table(sub_table, verbose=verbose)
|
|
|
|
| 133 |
qa_type="map",
|
| 134 |
new_col_name_s=["col_{}".format(col_idx)],
|
| 135 |
verbose=verbose)
|
| 136 |
+
with open("tmp_for_vis/{}_result_step_{}_input.txt".format(stamp, steps.index(step)), "w") as f:
|
| 137 |
+
json.dump(sql_executed_sub_tables, f)
|
| 138 |
with open("tmp_for_vis/{}_result_step_{}.txt".format(stamp, steps.index(step)), "w") as f:
|
| 139 |
json.dump(sub_table, f)
|
| 140 |
return extract_answers(sub_table)
|
|
|
|
| 147 |
table_title=db.table_title,
|
| 148 |
qa_type="ans",
|
| 149 |
verbose=verbose)
|
| 150 |
+
with open("tmp_for_vis/{}_result_step_{}_input.txt".format(stamp, steps.index(step)), "w") as f:
|
| 151 |
+
json.dump(sql_executed_sub_tables, f)
|
| 152 |
with open("tmp_for_vis/{}_result_step_{}.txt".format(stamp, steps.index(step)), "w") as f:
|
| 153 |
json.dump(answer, f)
|
| 154 |
if step.father:
|