2
2
import requests
3
3
import gradio as gr
4
4
from PIL import Image
5
- from threading import Thread
6
- from transformers import TextIteratorStreamer
5
+ from threading import Event , Thread
6
+ from queue import Queue
7
7
8
+ import openvino_genai as ov_genai
8
9
9
- def make_demo (model , processor ):
10
- model_name = Path (model .config ._name_or_path ).parent .name
11
10
11
+ class TextQueue :
12
+ def __init__ (self ) -> None :
13
+ self .text_queue = Queue ()
14
+ self .stop_signal = None
15
+ self .stop_tokens = []
16
+
17
+ def __call__ (self , text ):
18
+ self .text_queue .put (text )
19
+
20
+ def __iter__ (self ):
21
+ return self
22
+
23
+ def __next__ (self ):
24
+ value = self .text_queue .get ()
25
+ if value == self .stop_signal or value in self .stop_tokens :
26
+ raise StopIteration ()
27
+ else :
28
+ return value
29
+
30
+ def reset (self ):
31
+ self .text_queue = Queue ()
32
+
33
+ def end (self ):
34
+ self .text_queue .put (self .stop_signal )
35
+
36
+
37
+ def make_demo (pipe , read_images , model_name ):
12
38
example_image_urls = [
13
39
("https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/dd5105d6-6a64-4935-8a34-3058a82c8d5d" , "small.png" ),
14
40
("https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/1221e2a8-a6da-413a-9af6-f04d56af3754" , "chart.png" ),
@@ -23,69 +49,50 @@ def bot_streaming(message, history):
23
49
print (f"history is - { history } " )
24
50
files = message ["files" ] if isinstance (message , dict ) else message .files
25
51
message_text = message ["text" ] if isinstance (message , dict ) else message .text
52
+
53
+ image = None
26
54
if files :
27
55
# message["files"][-1] is a Dict or just a string
28
56
if isinstance (files [- 1 ], dict ):
29
57
image = files [- 1 ]["path" ]
30
58
else :
31
- image = files [- 1 ] if isinstance (files [- 1 ], (list , tuple , str )) else files [- 1 ].path
32
- else :
33
- # if there's no image uploaded for this turn, look for images in the past turns
34
- # kept inside tuples, take the last one
35
- for hist in history :
36
- if type (hist [0 ]) == tuple :
37
- image = hist [0 ][0 ]
59
+ if isinstance (files [- 1 ], (str , Path )):
60
+ image = files [- 1 ]
61
+ else :
62
+ image = files [- 1 ] if isinstance (files [- 1 ], (list , tuple )) else files [- 1 ].path
38
63
try :
39
64
if image is None :
40
65
# Handle the case where image is None
41
66
raise gr .Error ("You need to upload an image for Phi3-Vision to work. Close the error and try again with an Image." )
42
67
except NameError :
43
68
# Handle the case where 'image' is not defined at all
44
69
raise gr .Error ("You need to upload an image for Phi3-Vision to work. Close the error and try again with an Image." )
70
+ image = read_images (image )
45
71
46
- conversation = []
47
- flag = False
48
- for user , assistant in history :
49
- if assistant is None :
50
- # pass
51
- flag = True
52
- conversation .extend ([{"role" : "user" , "content" : "" }])
53
- continue
54
- if flag == True :
55
- conversation [0 ]["content" ] = f"<|image_1|>\n { user } "
56
- conversation .extend ([{"role" : "assistant" , "content" : assistant }])
57
- flag = False
58
- continue
59
- conversation .extend ([{"role" : "user" , "content" : user }, {"role" : "assistant" , "content" : assistant }])
60
-
61
- if len (history ) == 0 :
62
- conversation .append ({"role" : "user" , "content" : f"<|image_1|>\n { message_text } " })
63
- else :
64
- conversation .append ({"role" : "user" , "content" : message_text })
65
- print (f"prompt is -\n { conversation } " )
66
- prompt = processor .tokenizer .apply_chat_template (conversation , tokenize = False , add_generation_prompt = True )
67
- image = Image .open (image )
68
- inputs = processor (prompt , image , return_tensors = "pt" )
69
-
70
- streamer = TextIteratorStreamer (
71
- processor ,
72
- ** {
73
- "skip_special_tokens" : True ,
74
- "skip_prompt" : True ,
75
- "clean_up_tokenization_spaces" : False ,
76
- },
77
- )
78
- generation_kwargs = dict (
79
- inputs ,
80
- streamer = streamer ,
81
- max_new_tokens = 1024 ,
82
- do_sample = False ,
83
- temperature = 0.0 ,
84
- eos_token_id = processor .tokenizer .eos_token_id ,
85
- )
86
-
87
- thread = Thread (target = model .generate , kwargs = generation_kwargs )
88
- thread .start ()
72
+ print (f"prompt is -\n { message_text } " )
73
+
74
+ config = ov_genai .GenerationConfig ()
75
+ config .max_new_tokens = 200
76
+ config .do_sample = False
77
+ config .set_eos_token_id (pipe .get_tokenizer ().get_eos_token_id ())
78
+
79
+ streamer = TextQueue ()
80
+ stream_complete = Event ()
81
+
82
+ def generate_and_signal_complete ():
83
+ """
84
+ generation function for single thread
85
+ """
86
+ streamer .reset ()
87
+ generation_kwargs = {"prompt" : message_text , "generation_config" : config , "streamer" : streamer }
88
+ if image is not None :
89
+ generation_kwargs ["images" ] = image
90
+ pipe .generate (** generation_kwargs )
91
+ stream_complete .set ()
92
+ streamer .end ()
93
+
94
+ t1 = Thread (target = generate_and_signal_complete )
95
+ t1 .start ()
89
96
90
97
buffer = ""
91
98
for new_text in streamer :
0 commit comments