-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from MLDSAI/feat/ascii
add ascii_mixin.py
- Loading branch information
Showing
7 changed files
with
110 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from puterbot.strategies.base import BaseReplayStrategy | ||
from puterbot.strategies.naive import NaiveReplayStrategy | ||
from puterbot.strategies.llm_ocr_demo import LLMOCRDemoReplayStrategy | ||
from puterbot.strategies.demo import DemoReplayStrategy | ||
# add more strategies here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
Implements a ReplayStrategy mixin for converting images to ASCII. | ||
Usage: | ||
class MyReplayStrategy(ASCIIReplayStrategyMixin): | ||
... | ||
""" | ||
|
||
from ascii_magic import AsciiArt | ||
from loguru import logger | ||
|
||
from puterbot.models import Recording, Screenshot | ||
from puterbot.strategies.base import BaseReplayStrategy | ||
|
||
|
||
COLUMNS = 120 | ||
WIDTH_RATIO = 2.2 | ||
MONOCHROME = True | ||
|
||
|
||
class ASCIIReplayStrategyMixin(BaseReplayStrategy): | ||
|
||
def __init__( | ||
self, | ||
recording: Recording, | ||
): | ||
super().__init__(recording) | ||
|
||
def get_ascii_text( | ||
self, | ||
screenshot: Screenshot, | ||
monochrome: bool = MONOCHROME, | ||
columns: int = COLUMNS, | ||
width_ratio: float = WIDTH_RATIO, | ||
|
||
): | ||
ascii_art = AsciiArt.from_pillow_image(screenshot.image) | ||
ascii_text = ascii_art.to_ascii( | ||
monochrome=monochrome, | ||
columns=columns, | ||
width_ratio=width_ratio, | ||
) | ||
logger.debug(f"ascii_text=\n{ascii_text}") | ||
return ascii_text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
Demonstration of LLM, OCR, and ASCII ReplayStrategyMixins. | ||
Usage: | ||
$ python puterbot/replay.py DemoReplayStrategy | ||
""" | ||
|
||
from loguru import logger | ||
import numpy as np | ||
|
||
from puterbot.events import get_events | ||
from puterbot.models import Recording, Screenshot | ||
from puterbot.strategies.base import BaseReplayStrategy | ||
from puterbot.strategies.llm_mixin import LLMReplayStrategyMixin | ||
from puterbot.strategies.ocr_mixin import OCRReplayStrategyMixin | ||
from puterbot.strategies.ascii_mixin import ASCIIReplayStrategyMixin | ||
|
||
|
||
class DemoReplayStrategy( | ||
LLMReplayStrategyMixin, | ||
OCRReplayStrategyMixin, | ||
ASCIIReplayStrategyMixin, | ||
BaseReplayStrategy, | ||
): | ||
|
||
def __init__( | ||
self, | ||
recording: Recording, | ||
): | ||
super().__init__(recording) | ||
|
||
def get_next_input_event( | ||
self, | ||
screenshot: Screenshot, | ||
): | ||
ascii_text = self.get_ascii_text(screenshot) | ||
logger.info(f"ascii_text=\n{ascii_text}") | ||
|
||
ocr_text = self.get_ocr_text(screenshot) | ||
logger.info(f"ocr_text=\n{ocr_text}") | ||
|
||
max_tokens = 2 | ||
|
||
_min = 2 | ||
_max = 10 | ||
tokens = np.random.permutation( | ||
["click"] * np.random.randint(_min, _max) + | ||
["type"] * np.random.randint(_min, _max) + | ||
["scroll"] * np.random.randint(_min, _max) | ||
) | ||
prompt = " ".join(tokens) | ||
logger.info(f"{prompt=}") | ||
completion = self.get_completion(prompt, max_tokens) | ||
logger.info(f"{completion=}") | ||
|
||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
alembic==1.8.1 | ||
ascii_magic==2.3.0 | ||
bokeh==2.4.3 | ||
clipboard==0.0.4 | ||
deepdiff==6.2.2 | ||
|