Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
transformers
Commits
a17841ac
Unverified
Commit
a17841ac
authored
Apr 03, 2023
by
Vladimir Blagojevic
Committed by
GitHub
Apr 03, 2023
Browse files
Generate: Enable easier TextStreamer customization (#22516)
parent
80d1319e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
2 deletions
+6
-2
src/transformers/generation/streamers.py
src/transformers/generation/streamers.py
+6
-2
No files found.
src/transformers/generation/streamers.py
View file @
a17841ac
...
@@ -88,7 +88,7 @@ class TextStreamer(BaseStreamer):
...
@@ -88,7 +88,7 @@ class TextStreamer(BaseStreamer):
printable_text
=
text
[
self
.
print_len
:
text
.
rfind
(
" "
)
+
1
]
printable_text
=
text
[
self
.
print_len
:
text
.
rfind
(
" "
)
+
1
]
self
.
print_len
+=
len
(
printable_text
)
self
.
print_len
+=
len
(
printable_text
)
print
(
printable_text
,
flush
=
True
,
end
=
""
)
self
.
on_finalized_text
(
printable_text
)
def
end
(
self
):
def
end
(
self
):
"""Flushes any remaining cache and prints a newline to stdout."""
"""Flushes any remaining cache and prints a newline to stdout."""
...
@@ -102,7 +102,11 @@ class TextStreamer(BaseStreamer):
...
@@ -102,7 +102,11 @@ class TextStreamer(BaseStreamer):
printable_text
=
""
printable_text
=
""
# Print a newline (and the remaining text, if any)
# Print a newline (and the remaining text, if any)
print
(
printable_text
,
flush
=
True
)
self
.
on_finalized_text
(
printable_text
,
stream_end
=
True
)
def
on_finalized_text
(
self
,
token
:
str
,
stream_end
:
bool
=
False
):
"""Prints the new text to stdout."""
print
(
token
,
flush
=
True
,
end
=
""
if
not
stream_end
else
None
)
class
TextIteratorStreamer
(
BaseStreamer
):
class
TextIteratorStreamer
(
BaseStreamer
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment