Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Feb 17, 2024
2 parents 2488250 + 7751441 commit 4bf4779
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ You can also start a server identical to the one above by simply running `interp

## Android

The step-by-step guide for installing Open Interpreter on your Android device can be found in the [open-interpreter-termux repo](https://github.com/Arrendy/open-interpreter-termux).
The step-by-step guide for installing Open Interpreter on your Android device can be found in the [open-interpreter-termux repo](https://github.com/MikeBirdTech/open-interpreter-termux).

## Safety Notice

Expand Down
4 changes: 2 additions & 2 deletions docs/language-models/local-models/lm-studio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Once the server is running, you can begin your conversation with Open Interprete

(When you run the command `interpreter --local`, the steps above will be displayed.)

<Info>Local mode sets your `context_window` to 3000, and your `max_tokens` to 1000. If your model has different requirements, [set these parameters manually.](/language-model-setup/local-models/settings)</Info>
<Info>Local mode sets your `context_window` to 3000, and your `max_tokens` to 1000. If your model has different requirements, [set these parameters manually.](/settings#language-model)</Info>

# Python

Expand All @@ -44,4 +44,4 @@ interpreter.llm.api_base = "http://localhost:1234/v1" # Point this at any OpenAI
interpreter.chat()
```

Simply ensure that **LM Studio**, or any other OpenAI compatible server, is running at `api_base`.
Simply ensure that **LM Studio**, or any other OpenAI compatible server, is running at `api_base`.
9 changes: 3 additions & 6 deletions interpreter/core/computer/clipboard/clipboard.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import os
from ...utils.lazy_import import lazy_import

try:
import pyperclip
except:
# Optional package
pass

# Lazy import of optional packages
pyperclip = lazy_import('pyperclip')

class Clipboard:
def __init__(self, computer):
Expand Down
36 changes: 22 additions & 14 deletions interpreter/core/computer/display/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,42 @@
import warnings
from io import BytesIO

import matplotlib.pyplot as plt
import requests

from ..utils.recipient_utils import format_to_recipient
from ...utils.lazy_import import lazy_import

# Still experimenting with this
# from utils.get_active_window import get_active_window

try:
import numpy as np
import pyautogui
except:
# Optional packages
pass
# Lazy import of optional packages
pyautogui = lazy_import('pyautogui')
np = lazy_import('numpy')
plt = lazy_import('matplotlib.pyplot')

from ..utils.computer_vision import find_text_in_image, pytesseract_get_text


class Display:
def __init__(self, computer):
self.computer = computer

try:
self.width, self.height = pyautogui.size()
except:
# pyautogui is an optional package, so it's okay if this fails
pass

#set width and height to None initially to prevent pyautogui from importing until it's needed
self._width = None
self._height = None

# We use properties here so that this code only executes when height/width are accessed for the first time
@property
def width(self):
if self._width is None:
self._width, _ = pyautogui.size()
return self._width

@property
def height(self):
if self._height is None:
_, self._height = pyautogui.size()
return self._height

def size(self):
"""
Returns the current screen size as a tuple (width, height).
Expand Down
8 changes: 5 additions & 3 deletions interpreter/core/computer/docs/docs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import inspect
import os

from aifs import search
from ...utils.lazy_import import lazy_import

# Lazy import of aifs, imported when needed to speed up start time
aifs = lazy_import('aifs')

class Docs:
def __init__(self, computer):
self.computer = computer

def search(self, query, module=None, paths=None):
if paths:
return search(query, file_paths=paths, python_docstrings_only=True)
return aifs.search(query, file_paths=paths, python_docstrings_only=True)

if module is None:
module = self.computer
Expand All @@ -19,5 +21,5 @@ def search(self, query, module=None, paths=None):
module_path = os.path.dirname(inspect.getfile(module.__class__))

# Use aifs to search over the files in the module path
results = search(query, path=module_path, python_docstrings_only=True)
results = aifs.search(query, path=module_path, python_docstrings_only=True)
return results
6 changes: 4 additions & 2 deletions interpreter/core/computer/files/files.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import difflib

from aifs import search
from ...utils.lazy_import import lazy_import

# Lazy import of aifs, imported when needed
aifs = lazy_import('aifs')

class Files:
def __init__(self, computer):
Expand All @@ -11,7 +13,7 @@ def search(self, *args, **kwargs):
"""
Search the filesystem for the given query.
"""
return search(*args, **kwargs)
return aifs.search(*args, **kwargs)

def edit(self, path, original_text, replacement_text):
"""
Expand Down
9 changes: 3 additions & 6 deletions interpreter/core/computer/keyboard/keyboard.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import os
import platform
import time
from ...utils.lazy_import import lazy_import

try:
import pyautogui
except:
# Optional packages
pass

# Lazy import of pyautogui
pyautogui = lazy_import('pyautogui')

class Keyboard:
"""A class to simulate keyboard inputs"""
Expand Down
14 changes: 6 additions & 8 deletions interpreter/core/computer/mouse/mouse.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import time
import warnings

import matplotlib.pyplot as plt

from ..utils.recipient_utils import format_to_recipient
from ...utils.lazy_import import lazy_import

try:
import cv2
import numpy as np
import pyautogui
except:
# Optional packages
pass
# Lazy import of optional packages
cv2 = lazy_import('cv2', )
np = lazy_import('numpy')
pyautogui = lazy_import('pyautogui')
plt = lazy_import('matplotlib.pyplot')


class Mouse:
Expand Down
5 changes: 4 additions & 1 deletion interpreter/core/computer/skills/skills.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import glob
import os

import aifs
from ...utils.lazy_import import lazy_import

# Lazy import of aifs, imported when needed to speed up start time
aifs = lazy_import('aifs')


class Skills:
Expand Down
24 changes: 9 additions & 15 deletions interpreter/core/computer/utils/computer_vision.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import io

from PIL import Image
from ...utils.lazy_import import lazy_import

try:
import cv2
import numpy as np
except:
# Optional packages
pass

try:
from pytesseract import Output, pytesseract
except:
# this is very very optional, we don't even reccomend it unless the api has failed
pass
# Lazy import of optional packages
np = lazy_import('numpy')
cv2 = lazy_import('cv2')
PIL = lazy_import('PIL')
# pytesseract is very very optional, we don't even recommend it unless the api has failed
pytesseract = lazy_import('pytesseract')


def pytesseract_get_text(img):
Expand All @@ -37,7 +31,7 @@ def find_text_in_image(img, text):
gray = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)

# Use pytesseract to get the data from the image
d = pytesseract.image_to_data(gray, output_type=Output.DICT)
d = pytesseract.image_to_data(gray, output_type=pytesseract.Output.DICT)

# Initialize an empty list to store the centers of the bounding boxes
centers = []
Expand Down Expand Up @@ -175,7 +169,7 @@ def find_text_in_image(img, text):
if centers:
break

bounding_box_image = Image.fromarray(img_draw)
bounding_box_image = PIL.Image.fromarray(img_draw)
bounding_box_image.format = img.format

# Convert centers to relative
Expand Down
8 changes: 4 additions & 4 deletions interpreter/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
This file defines the Interpreter class.
It's the main file. `from interpreter import interpreter` will import an instance of this class.
"""

import asyncio
import json
import os
from pathlib import Path
import threading
import time
from datetime import datetime
from pathlib import Path

from ..terminal_interface.terminal_interface import terminal_interface
from ..terminal_interface.utils.display_markdown_message import display_markdown_message
Expand Down Expand Up @@ -100,9 +98,11 @@ def __init__(

# Computer
self.computer = Computer() if computer is None else computer
self.sync_computer = (
True # Sync the interpreter's computer with the user's interpreter.computer
)
self.computer.skills.skills_dir = (
skills_dir if skills_dir else str(Path(oi_dir) / "skills")

)
if import_skills:
self.computer.skills.import_skills()
Expand Down
4 changes: 2 additions & 2 deletions interpreter/core/respond.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def respond(interpreter):
break

# don't let it import computer on os mode — we handle that!
if interpreter.os and language == "python":
if interpreter.sync_computer and language == "python":
code = code.replace("import computer\n", "pass\n")
code = re.sub(
r"import computer\.(\w+) as (\w+)", r"\2 = computer.\1", code
Expand Down Expand Up @@ -202,7 +202,7 @@ def respond(interpreter):

# sync up the interpreter's computer with your computer
try:
if interpreter.os and language == "python":
if interpreter.sync_computer and language == "python":
computer_dict = interpreter.computer.to_dict()
if computer_dict:
computer_json = json.dumps(computer_dict)
Expand Down
27 changes: 27 additions & 0 deletions interpreter/core/utils/lazy_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import importlib.util
import sys

def lazy_import(name, optional=True):
"""Lazily import a module, specified by the name. Useful for optional packages, to speed up startup times."""
# Check if module is already imported
if name in sys.modules:
return sys.modules[name]

# Find the module specification from the module name
spec = importlib.util.find_spec(name)
if spec is None:
if optional:
return None # Do not raise an error if the module is optional
else:
raise ImportError(f"Module '{name}' cannot be found")

# Use LazyLoader to defer the loading of the module
loader = importlib.util.LazyLoader(spec.loader)
spec.loader = loader

# Create a module from the spec and set it up for lazy loading
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
loader.exec_module(module)

return module
1 change: 0 additions & 1 deletion interpreter/terminal_interface/conversation_navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
def conversation_navigator(interpreter):
import time

time.sleep(5)
conversations_dir = get_storage_path("conversations")

display_markdown_message(
Expand Down

0 comments on commit 4bf4779

Please sign in to comment.