Skip to content

Commit

Permalink
fix python
Browse files Browse the repository at this point in the history
  • Loading branch information
sbalandi committed Jan 9, 2025
1 parent d18fe16 commit d308e48
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 24 deletions.
3 changes: 1 addition & 2 deletions samples/python/chat_sample/chat_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import openvino_genai


def streamer(subword) -> openvino_genai.StreamerRunningStatus:
def streamer(subword):
print(subword, end='', flush=True)

return openvino_genai.StreamerRunningStatus.RUNNING

def main():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import argparse
import openvino_genai

def streamer(subword):
print(subword, end='', flush=True)
# Return flag corresponds whether generation should be stopped.
# False means continue generation.
return False
def streamer(subword):
print(subword, end='', flush=True)
# Return flag corresponds whether generation should be stopped.
return openvino_genai.StreamerRunningStatus.RUNNING

def main():
parser = argparse.ArgumentParser()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import threading

def streamer(subword):
print(subword, end='', flush=True)
# Return flag corresponds whether generation should be stopped.
# False means continue generation.
return False
print(subword, end='', flush=True)
# Return flag corresponds whether generation should be stopped.
return openvino_genai.StreamerRunningStatus.RUNNING

def main():
parser = argparse.ArgumentParser()
Expand Down
8 changes: 5 additions & 3 deletions samples/python/visual_language_chat/visual_language_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from pathlib import Path


def streamer(subword: str) -> bool:
def streamer(subword: str) -> openvino_genai.StreamerRunningStatus:
'''
Args:
Expand All @@ -25,6 +25,8 @@ def streamer(subword: str) -> bool:
# No value is returned as in this example we don't want to stop the generation in this method.
# "return None" will be treated the same as "return False".

return openvino_genai.StreamerRunningStatus.RUNNING


def read_image(path: str) -> Tensor:
'''
Expand Down Expand Up @@ -66,7 +68,7 @@ def main():
config = openvino_genai.GenerationConfig()
config.max_new_tokens = 100

pipe.start_chat()
# pipe.start_chat()
prompt = input('question:\n')
pipe.generate(prompt, images=rgbs, generation_config=config, streamer=streamer)

Expand All @@ -77,7 +79,7 @@ def main():
except EOFError:
break
pipe.generate(prompt, generation_config=config, streamer=streamer)
pipe.finish_chat()
# pipe.finish_chat()


if '__main__' == __name__:
Expand Down
9 changes: 0 additions & 9 deletions src/python/py_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,6 @@ ov::genai::StreamerVariant pystreamer_to_streamer(const PyBindStreamerVariant& p
ov::genai::StreamerVariant streamer = std::monostate();

std::visit(overloaded {
[&streamer](const std::function<bool(py::str)>& py_callback){
// Wrap python streamer with manual utf-8 decoding. Do not rely
// on pybind automatic decoding since it raises exceptions on incomplete strings.
auto callback_wrapped = [py_callback](std::string subword) -> bool {
auto py_str = PyUnicode_DecodeUTF8(subword.data(), subword.length(), "replace");
return py_callback(py::reinterpret_borrow<py::str>(py_str));
};
streamer = callback_wrapped;
},
[&streamer](const std::function<ov::genai::StreamerRunningStatus(py::str)>& py_callback){
// Wrap python streamer with manual utf-8 decoding. Do not rely
// on pybind automatic decoding since it raises exceptions on incomplete strings.
Expand Down
2 changes: 1 addition & 1 deletion src/python/py_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace ov::genai::pybind::utils {

// When StreamerVariant is used utf-8 decoding is done by pybind and can lead to exception on incomplete texts.
// Therefore strings decoding should be handled with PyUnicode_DecodeUTF8(..., "replace") to not throw errors.
using PyBindStreamerVariant = std::variant<std::function<StreamerRunningStatus(py::str)>, std::function<bool(py::str)>, std::shared_ptr<StreamerBase>, std::monostate>;
using PyBindStreamerVariant = std::variant<std::function<StreamerRunningStatus(py::str)>, std::shared_ptr<StreamerBase>, std::monostate>;

template <class... Ts>
struct overloaded : Ts... {
Expand Down

0 comments on commit d308e48

Please sign in to comment.