Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions demo/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def bot(user_message, history, jupyter_state, dialog_info, interpreter):
else:
(
code_block_output,
image_output,
error_flag,
) = interpreter.execute_code_and_return_output(
f"{generated_code_block}",
Expand All @@ -237,8 +238,12 @@ def bot(user_message, history, jupyter_state, dialog_info, interpreter):
code_block_output = "Execution result: \n" + code_block_output
else:
code_block_output = "Code is executed, but result is empty. Please make sure that you include test case in your code."

history.append([code_block_output, ""])
if image_output:
img_str = f"data:image/png;base64,{image_output}"
image_html_output = f"<img src='{img_str}' alt='Display Image'>"
history.append([code_block_output + "\n" + image_html_output, ""])
else:
history.append([code_block_output, ""])

interpreter.dialog.append({"role": "user", "content": code_block_output})

Expand Down
6 changes: 3 additions & 3 deletions demo/code_interpreter/BaseCodeInterpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ def extract_code_blocks(text: str):
return [block.strip() for block in code_blocks]

def execute_code_and_return_output(self, code_str: str, nb):
_, _ = nb.add_and_run(GUARD_CODE)
outputs, error_flag = nb.add_and_run(code_str)
return outputs, error_flag
(_, _), _ = nb.add_and_run(GUARD_CODE)
(outputs, image), error_flag = nb.add_and_run(code_str)
return outputs, image, error_flag
9 changes: 7 additions & 2 deletions demo/code_interpreter/JupyterClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ def __init__(self):

def clean_output(self, outputs):
outputs_only_str = list()
image = None
for i in outputs:
if type(i) == dict:
if "text/plain" in list(i.keys()):
outputs_only_str.append(i["text/plain"])
if "image/png" in list(i.keys()):
image = i["image/png"]
elif type(i) == str:
outputs_only_str.append(i)
elif type(i) == list:
error_msg = "\n".join(i)
error_msg = re.sub(r"\x1b\[.*?m", "", error_msg)
outputs_only_str.append(error_msg)

return "\n".join(outputs_only_str).strip()
return "\n".join(outputs_only_str).strip(), image

def add_and_run(self, code_string):
# This inner function will be executed in a separate thread
Expand All @@ -48,7 +51,9 @@ def run_code_in_thread():
elif msg_type == "error":
error_flag = True
outputs.append(content["traceback"])

elif msg_type == 'display_data':
if 'image/png' in msg['content']['data']:
outputs.append(content["data"])
# If the execution state of the kernel is idle, it means the cell finished executing
if msg_type == "status" and content["execution_state"] == "idle":
break
Expand Down