Spaces:
Sleeping
Sleeping
Delete src/jupyter_backend.py
Browse files- src/jupyter_backend.py +0 -100
src/jupyter_backend.py
DELETED
|
@@ -1,100 +0,0 @@
|
|
| 1 |
-
import jupyter_client
|
| 2 |
-
import re
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
def delete_color_control_char(string):
|
| 6 |
-
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')
|
| 7 |
-
return ansi_escape.sub('', string)
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
class JupyterKernel:
|
| 11 |
-
def __init__(self, work_dir):
|
| 12 |
-
self.kernel_manager, self.kernel_client = jupyter_client.manager.start_new_kernel(kernel_name='python3')
|
| 13 |
-
self.work_dir = work_dir
|
| 14 |
-
self._create_work_dir()
|
| 15 |
-
self.available_functions = {
|
| 16 |
-
'execute_code': self.execute_code,
|
| 17 |
-
'python': self.execute_code
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
def execute_code_(self, code):
|
| 21 |
-
msg_id = self.kernel_client.execute(code)
|
| 22 |
-
|
| 23 |
-
# Get the output of the code
|
| 24 |
-
iopub_msg = self.kernel_client.get_iopub_msg()
|
| 25 |
-
|
| 26 |
-
all_output = []
|
| 27 |
-
while True:
|
| 28 |
-
if iopub_msg['msg_type'] == 'stream':
|
| 29 |
-
if iopub_msg['content'].get('name') == 'stdout':
|
| 30 |
-
output = iopub_msg['content']['text']
|
| 31 |
-
all_output.append(('stdout', output))
|
| 32 |
-
iopub_msg = self.kernel_client.get_iopub_msg()
|
| 33 |
-
elif iopub_msg['msg_type'] == 'execute_result':
|
| 34 |
-
if 'data' in iopub_msg['content']:
|
| 35 |
-
if 'text/plain' in iopub_msg['content']['data']:
|
| 36 |
-
output = iopub_msg['content']['data']['text/plain']
|
| 37 |
-
all_output.append(('execute_result_text', output))
|
| 38 |
-
if 'text/html' in iopub_msg['content']['data']:
|
| 39 |
-
output = iopub_msg['content']['data']['text/html']
|
| 40 |
-
all_output.append(('execute_result_html', output))
|
| 41 |
-
if 'image/png' in iopub_msg['content']['data']:
|
| 42 |
-
output = iopub_msg['content']['data']['image/png']
|
| 43 |
-
all_output.append(('execute_result_png', output))
|
| 44 |
-
if 'image/jpeg' in iopub_msg['content']['data']:
|
| 45 |
-
output = iopub_msg['content']['data']['image/jpeg']
|
| 46 |
-
all_output.append(('execute_result_jpeg', output))
|
| 47 |
-
iopub_msg = self.kernel_client.get_iopub_msg()
|
| 48 |
-
elif iopub_msg['msg_type'] == 'display_data':
|
| 49 |
-
if 'data' in iopub_msg['content']:
|
| 50 |
-
if 'text/plain' in iopub_msg['content']['data']:
|
| 51 |
-
output = iopub_msg['content']['data']['text/plain']
|
| 52 |
-
all_output.append(('display_text', output))
|
| 53 |
-
if 'text/html' in iopub_msg['content']['data']:
|
| 54 |
-
output = iopub_msg['content']['data']['text/html']
|
| 55 |
-
all_output.append(('display_html', output))
|
| 56 |
-
if 'image/png' in iopub_msg['content']['data']:
|
| 57 |
-
output = iopub_msg['content']['data']['image/png']
|
| 58 |
-
all_output.append(('display_png', output))
|
| 59 |
-
if 'image/jpeg' in iopub_msg['content']['data']:
|
| 60 |
-
output = iopub_msg['content']['data']['image/jpeg']
|
| 61 |
-
all_output.append(('display_jpeg', output))
|
| 62 |
-
iopub_msg = self.kernel_client.get_iopub_msg()
|
| 63 |
-
elif iopub_msg['msg_type'] == 'error':
|
| 64 |
-
if 'traceback' in iopub_msg['content']:
|
| 65 |
-
output = '\n'.join(iopub_msg['content']['traceback'])
|
| 66 |
-
all_output.append(('error', output))
|
| 67 |
-
iopub_msg = self.kernel_client.get_iopub_msg()
|
| 68 |
-
elif iopub_msg['msg_type'] == 'status' and iopub_msg['content'].get('execution_state') == 'idle':
|
| 69 |
-
break
|
| 70 |
-
else:
|
| 71 |
-
iopub_msg = self.kernel_client.get_iopub_msg()
|
| 72 |
-
|
| 73 |
-
return all_output
|
| 74 |
-
|
| 75 |
-
def execute_code(self, code):
|
| 76 |
-
text_to_gpt = []
|
| 77 |
-
content_to_display = self.execute_code_(code)
|
| 78 |
-
for mark, out_str in content_to_display:
|
| 79 |
-
if mark in ('stdout', 'execute_result_text', 'display_text'):
|
| 80 |
-
text_to_gpt.append(out_str)
|
| 81 |
-
elif mark in ('execute_result_png', 'execute_result_jpeg', 'display_png', 'display_jpeg'):
|
| 82 |
-
text_to_gpt.append('[image]')
|
| 83 |
-
elif mark == 'error':
|
| 84 |
-
text_to_gpt.append(delete_color_control_char(out_str))
|
| 85 |
-
|
| 86 |
-
return '\n'.join(text_to_gpt), content_to_display
|
| 87 |
-
|
| 88 |
-
def _create_work_dir(self):
|
| 89 |
-
# set work dir in jupyter environment
|
| 90 |
-
init_code = f"import os\n" \
|
| 91 |
-
f"if not os.path.exists('{self.work_dir}'):\n" \
|
| 92 |
-
f" os.mkdir('{self.work_dir}')\n" \
|
| 93 |
-
f"os.chdir('{self.work_dir}')\n" \
|
| 94 |
-
f"del os"
|
| 95 |
-
self.execute_code_(init_code)
|
| 96 |
-
|
| 97 |
-
def restart_jupyter_kernel(self):
|
| 98 |
-
self.kernel_client.shutdown()
|
| 99 |
-
self.kernel_manager, self.kernel_client = jupyter_client.manager.start_new_kernel(kernel_name='python3')
|
| 100 |
-
self._create_work_dir()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|