Skip to content

Commit 1e47c31

Browse files
committed
colored vis
1 parent c0165df commit 1e47c31

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

lmdeploy/pytorch/tools/utils.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,31 +132,63 @@ def visualize_pipe_out(outputs, enable_meta: bool = True):
132132

133133
from lmdeploy.messages import Response
134134

135+
try:
136+
from termcolor import colored
137+
except ImportError:
138+
139+
def colored(text, color=None, on_color=None, attrs=None):
140+
return text
141+
135142
if isinstance(outputs, Response):
136143
outputs = [outputs]
137144
try:
138145
term_size = os.get_terminal_size().columns
139146
except Exception:
140147
term_size = 100
141148

142-
def _lined_print(msg: str, line_format: str = '-', full_line: bool = False):
143-
print(msg)
144-
if full_line:
145-
columns = term_size
146-
else:
147-
columns = max(len(m) for m in msg.split('\n'))
148-
print(line_format * columns)
149+
border_color = 'cyan'
150+
meta_color = 'light_grey'
151+
number_color = 'green'
152+
153+
def _print_title(title: str, color: str = border_color):
154+
title_text = f' {title} '
155+
print(colored(f'【{title_text}】', color, attrs=['bold']))
156+
157+
def _print_section(title: str, content: str, color: str = border_color):
158+
"""Simple title and content printing."""
159+
_print_title(title, color)
160+
print(content)
161+
162+
def _print_meta(out: Response):
163+
"""Enhanced meta information display."""
164+
# Create a clean table-like format
165+
finish_color = 'yellow' if out.finish_reason == 'stop' else 'red'
166+
meta_content = [
167+
f"{colored('• Input Tokens:', meta_color)} {colored(out.input_token_len, number_color)}",
168+
f"{colored('• Generated Tokens:', meta_color)} {colored(out.generate_token_len, number_color)}",
169+
f"{colored('• Finish Reason:', meta_color)} {colored(out.finish_reason, finish_color)}"
170+
]
171+
172+
lines = '\n'.join(meta_content)
173+
lines += '\n'
174+
_print_section('METADATA', lines, border_color)
175+
176+
# Main loop
177+
print(colored('━' * term_size, border_color))
149178

150179
outputs: List[Response] = outputs
151-
term_line = '—' * term_size
152-
print(term_line)
153180
for idx, out in enumerate(outputs):
154-
_lined_print(f'output[{idx}]', '=')
181+
header = f'OUTPUT [{idx + 1}/{len(outputs)}]'
182+
header_formatted = colored(f'✦ {header}', 'light_magenta', attrs=['bold'])
183+
print(header_formatted)
184+
print()
185+
155186
if enable_meta:
156-
_lined_print('meta', '-')
157-
_lined_print(
158-
f'input_token_len={out.input_token_len}\n'
159-
f'generate_token_len={out.generate_token_len}\n'
160-
f'finish_reason="{out.finish_reason}"', '—')
161-
_lined_print('text', '-')
162-
_lined_print(f'{out.text}', '—', full_line=True)
187+
_print_meta(out)
188+
189+
_print_section('TEXT', out.text, border_color)
190+
191+
if idx < len(outputs) - 1: # Add separator when it's not the last output
192+
print(colored('─' * (term_size), border_color, attrs=['dark']))
193+
else:
194+
print(colored('━' * term_size, border_color))

0 commit comments

Comments
 (0)