-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Terminal colors #905
base: main
Are you sure you want to change the base?
Terminal colors #905
Changes from all commits
6d3800f
38fc7be
5a08f12
aaffff2
acea90e
cdb8314
43da50b
0b13ca8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ node_modules | |
.eggs/ | ||
.env | ||
.DS_Store | ||
.idea | ||
|
||
# Ignore native library built by setup | ||
guidance/*.so | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,19 +9,24 @@ | |
import time | ||
import warnings | ||
|
||
|
||
from pprint import pprint | ||
from typing import Dict, TYPE_CHECKING | ||
|
||
|
||
import numpy as np | ||
|
||
try: | ||
from IPython import get_ipython | ||
from IPython.display import clear_output, display, HTML | ||
|
||
ipython_is_imported = True | ||
except ImportError: | ||
ipython_is_imported = False | ||
notebook_mode = False | ||
else: | ||
ipython_is_imported = True | ||
_ipython = get_ipython() | ||
notebook_mode = ( | ||
_ipython is not None | ||
and "IPKernelApp" in _ipython.config | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on the way that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had to tackle this detection back in InterpretML too, here's how we did it there: https://github.com/interpretml/interpret/blob/develop/python/interpret-core/interpret/provider/_environment.py. We could re-use some of this logic here perhaps cc @nopdive There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, hadn't been watching my notifications -- code seems reasonable. Yes, this should be tested (either manually/automatically is fine) on multiple platforms before merge: terminal (Window/Linux/Mac), vscode, jupyter notebook/lab, azure/google/amazon/databricks notebooks. We should be relatively okay if these target environments work. |
||
) | ||
|
||
try: | ||
import torch | ||
|
||
|
@@ -39,7 +44,7 @@ | |
) | ||
from .. import _cpp as cpp | ||
from ._guidance_engine_metrics import GuidanceEngineMetrics | ||
from .._utils import softmax, CaptureEvents | ||
from .._utils import softmax, CaptureEvents, ReadableOutputCLIStream | ||
from .._parser import EarleyCommitParser, Parser | ||
from .._grammar import ( | ||
GrammarFunction, | ||
|
@@ -857,11 +862,13 @@ def __init__(self, engine, echo=True, **kwargs): | |
self._variables = {} # these are the state variables stored with the model | ||
self._variables_log_probs = {} # these are the state variables stored with the model | ||
self._cache_state = {} # mutable caching state used to save computation | ||
self._state_list = [] | ||
self._state = "" # the current bytes that represent the state of the model | ||
self._event_queue = None # TODO: these are for streaming results in code, but that needs implemented | ||
self._event_parent = None | ||
self._last_display = 0 # used to track the last display call to enable throttling | ||
self._last_event_stream = 0 # used to track the last event streaming call to enable throttling | ||
self._state_dict_parser = ReadableOutputCLIStream() # used to parse the state for cli display | ||
|
||
@property | ||
def active_role_end(self): | ||
|
@@ -975,11 +982,11 @@ def _update_display(self, throttle=True): | |
else: | ||
self._last_display = curr_time | ||
|
||
if ipython_is_imported: | ||
if notebook_mode: | ||
clear_output(wait=True) | ||
display(HTML(self._html())) | ||
else: | ||
pprint(self._state) | ||
print(self._state_dict_parser.feed(self._state_list), end='', flush=True) | ||
|
||
def reset(self, clear_variables=True): | ||
"""This resets the state of the model object. | ||
|
@@ -995,6 +1002,7 @@ def reset(self, clear_variables=True): | |
self._variables_log_probs = {} | ||
return self | ||
|
||
# Is this used anywhere? | ||
def _repr_html_(self): | ||
if ipython_is_imported: | ||
clear_output(wait=True) | ||
|
@@ -1327,9 +1335,18 @@ def _run_stateless(self, stateless_function, temperature=0.0, top_p=1.0, n=1): | |
|
||
if len(chunk.new_bytes) > 0: | ||
generated_value += new_text | ||
|
||
# Add text to state list | ||
self._state_list.append([new_text, None]) | ||
|
||
if chunk.is_generated: | ||
lm += f"<||_html:<span style='background-color: rgba({165*(1-chunk.new_bytes_prob) + 0}, {165*chunk.new_bytes_prob + 0}, 0, {0.15}); border-radius: 3px;' title='{chunk.new_bytes_prob}'>_||>" | ||
|
||
# If that was generated text - color it | ||
self._state_list[-1][1] = (165 * (1 - chunk.new_bytes_prob) + 0, 165 * chunk.new_bytes_prob + 0, 0, 0.15) | ||
|
||
lm += new_text | ||
|
||
if chunk.is_generated: | ||
lm += "<||_html:</span>_||>" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this, or is it replaced by
notebook_mode
?