Skip to content

Commit

Permalink
Fixed color traceback outputs, shrunk screenshots for OS mode's visio…
Browse files Browse the repository at this point in the history
…n model
  • Loading branch information
KillianLucas committed Dec 15, 2023
1 parent 89f715b commit fed1e00
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
7 changes: 7 additions & 0 deletions interpreter/core/computer/display/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def screenshot(self, show=True, quadrant=None):
else:
raise ValueError("Invalid quadrant. Choose between 1 and 4.")

# Shrink screenshot so it's no more than 1024 wide
screenshot_width, screenshot_height = screenshot.size
if screenshot_width > 1024:
ratio = 1024.0 / screenshot_width
new_height = int(screenshot_height * ratio)
screenshot = screenshot.resize((1024, new_height), Image.ANTIALIAS)

screenshot.save(temp_file.name)

# Open the image file with PIL
Expand Down
28 changes: 20 additions & 8 deletions interpreter/core/computer/terminal/languages/jupyter_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,22 @@ def __init__(self):
# Give it our same matplotlib backend
# backend = matplotlib.get_backend()

# DISABLED because we actually do want HTML output to work, other types of output too
# Get backend which bubbles everything up as images
backend = "Agg"

code_to_run = f"""
import matplotlib
matplotlib.use('{backend}')
"""
self.run(code_to_run)
# backend = "Agg"
# code = f"""
# import matplotlib
# matplotlib.use('{backend}')
# """
# self.run(code)

# DISABLED because it doesn't work??
# Disable color outputs in the terminal, which don't look good in OI and aren't useful
# code = """
# from IPython.core.getipython import get_ipython
# get_ipython().colors = 'NoColor'
# """
# self.run(code)

def terminate(self):
self.kc.stop_channels()
Expand Down Expand Up @@ -104,11 +112,15 @@ def iopub_message_listener():
{"type": "console", "format": "output", "content": line}
)
elif msg["msg_type"] == "error":
content = "\n".join(content["traceback"])
# Remove color codes
ansi_escape = re.compile(r"\x1B\[[0-?]*[ -/]*[@-~]")
content = ansi_escape.sub("", content)
message_queue.put(
{
"type": "console",
"format": "output",
"content": "\n".join(content["traceback"]),
"content": content,
}
)
elif msg["msg_type"] in ["display_data", "execute_result"]:
Expand Down
9 changes: 9 additions & 0 deletions interpreter/core/respond.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ def respond(interpreter):
# don't let it import computer on os mode — we handle that!
if interpreter.os and language == "python":
code = code.replace("import computer", "")
code = code.replace(
"from computer import keyboard", "keyboard = computer.keyboard"
)
code = code.replace(
"from computer import mouse", "mouse = computer.mouse"
)
code = code.replace(
"from computer import display", "mouse = computer.display"
)

# yield each line
for line in interpreter.computer.run(language, code):
Expand Down
13 changes: 6 additions & 7 deletions interpreter/terminal_interface/start_terminal_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,12 @@ def start_terminal_interface(interpreter):

# FOR TESTING ONLY
# Install Open Interpreter from GitHub
print("INSTALLING OI FROM GITHUB")
for chunk in interpreter.computer.run(
"shell",
"pip install git+https://github.com/KillianLucas/open-interpreter.git",
):
if chunk.get("format") != "active_line":
print(chunk.get("content"))
# for chunk in interpreter.computer.run(
# "shell",
# "pip install git+https://github.com/KillianLucas/open-interpreter.git",
# ):
# if chunk.get("format") != "active_line":
# print(chunk.get("content"))

# Give it access to the computer via Python
for _ in interpreter.computer.run(
Expand Down

0 comments on commit fed1e00

Please sign in to comment.