Spaces:
Runtime error
Runtime error
Update app_dialogue.py
Browse files- app_dialogue.py +63 -1
app_dialogue.py
CHANGED
@@ -75,23 +75,33 @@ logger = logging.getLogger()
|
|
75 |
|
76 |
# Monkey patch adapted from gradio.components.image.Image - mostly to make the `save` step optional in `pil_to_temp_file`
|
77 |
def hash_bytes(bytes: bytes):
|
|
|
|
|
78 |
sha1 = hashlib.sha1()
|
79 |
sha1.update(bytes)
|
|
|
80 |
return sha1.hexdigest()
|
81 |
|
82 |
|
83 |
def pil_to_temp_file(img: PIL.Image.Image, dir: str = DEFAULT_TEMP_DIR, format: str = "png") -> str:
|
84 |
-
"""
|
|
|
|
|
|
|
|
|
85 |
bytes_data = processing_utils.encode_pil_to_bytes(img, format)
|
86 |
temp_dir = Path(dir) / hash_bytes(bytes_data)
|
87 |
temp_dir.mkdir(exist_ok=True, parents=True)
|
88 |
filename = str(temp_dir / f"image.{format}")
|
89 |
if not os.path.exists(filename):
|
90 |
img.save(filename, pnginfo=processing_utils.get_pil_metadata(img))
|
|
|
91 |
return filename
|
92 |
|
93 |
|
94 |
def add_file(file):
|
|
|
|
|
95 |
return file.name
|
96 |
|
97 |
|
@@ -103,6 +113,8 @@ def split_str_on_im_markdown(string: str) -> List[str]:
|
|
103 |
- `User:Describe this image.` would become `["User:", "https://favurl.com/chicken_on_money.png", "Describe this image."]`
|
104 |
- `User:Describe this image.` would become `["User:", "/my_temp/chicken_on_money.png", "Describe this image."]`
|
105 |
"""
|
|
|
|
|
106 |
IMAGES_PATTERN = re.compile(r"!\[[^\]]*\]\((.*?)\s*(\"(?:.*[^\"])\")?\s*\)")
|
107 |
parts = []
|
108 |
cursor = 0
|
@@ -117,6 +129,8 @@ def split_str_on_im_markdown(string: str) -> List[str]:
|
|
117 |
cursor = pattern.end()
|
118 |
if cursor != len(string):
|
119 |
parts.append(string[cursor:])
|
|
|
|
|
120 |
return parts
|
121 |
|
122 |
|
@@ -124,6 +138,9 @@ def is_image(string: str) -> bool:
|
|
124 |
"""
|
125 |
There are two ways for images: local image path or url.
|
126 |
"""
|
|
|
|
|
|
|
127 |
return is_url(string) or string.startswith(DEFAULT_TEMP_DIR)
|
128 |
|
129 |
|
@@ -132,9 +149,12 @@ def is_url(string: str) -> bool:
|
|
132 |
Checks if the passed string contains a valid url and nothing else. e.g. if space is included it's immediately
|
133 |
invalidated the url
|
134 |
"""
|
|
|
|
|
135 |
if " " in string:
|
136 |
return False
|
137 |
result = urlparse(string)
|
|
|
138 |
return all([result.scheme, result.netloc])
|
139 |
|
140 |
|
@@ -161,6 +181,8 @@ def isolate_images_urls(prompt_list: List) -> List:
|
|
161 |
]
|
162 |
```
|
163 |
"""
|
|
|
|
|
164 |
linearized_list = []
|
165 |
for prompt in prompt_list:
|
166 |
# Prompt can be either a string, or a PIL image
|
@@ -183,11 +205,15 @@ def isolate_images_urls(prompt_list: List) -> List:
|
|
183 |
f"Unrecognized type for `prompt`. Got {type(type(prompt))}. Was expecting something in [`str`,"
|
184 |
" `PIL.Image.Image`]"
|
185 |
)
|
|
|
186 |
return linearized_list
|
187 |
|
188 |
|
189 |
def fetch_images(url_list: str) -> PIL.Image.Image:
|
190 |
"""Fetching images"""
|
|
|
|
|
|
|
191 |
return PROCESSOR.image_processor.fetch_images(url_list)
|
192 |
|
193 |
|
@@ -196,6 +222,8 @@ def handle_manual_images_in_user_prompt(user_prompt: str) -> List[str]:
|
|
196 |
Handle the case of textually manually inputted images (i.e. the `<fake_token_around_image><image:IMG_URL><fake_token_around_image>`) in the user prompt
|
197 |
by fetching them, saving them locally and replacing the whole sub-sequence the image local path.
|
198 |
"""
|
|
|
|
|
199 |
if "<fake_token_around_image>" in user_prompt:
|
200 |
splitted_user_prompt = isolate_images_urls([user_prompt])
|
201 |
resulting_user_prompt = []
|
@@ -206,13 +234,18 @@ def handle_manual_images_in_user_prompt(user_prompt: str) -> List[str]:
|
|
206 |
resulting_user_prompt.append(tmp_file)
|
207 |
else:
|
208 |
resulting_user_prompt.append(u_p)
|
|
|
209 |
return resulting_user_prompt
|
210 |
else:
|
|
|
211 |
return [user_prompt]
|
212 |
|
213 |
|
214 |
def gradio_link(img_path: str) -> str:
|
|
|
|
|
215 |
url = f"{GRADIO_LINK}/file={img_path}"
|
|
|
216 |
return url
|
217 |
|
218 |
|
@@ -221,6 +254,8 @@ def prompt_list_to_markdown(prompt_list: List[str]) -> str:
|
|
221 |
Convert a user prompt in the list format (i.e. elements are either a PIL image or a string) into
|
222 |
the markdown format that is used for the chatbot history and rendering.
|
223 |
"""
|
|
|
|
|
224 |
resulting_string = ""
|
225 |
for elem in prompt_list:
|
226 |
if is_image(elem):
|
@@ -230,6 +265,7 @@ def prompt_list_to_markdown(prompt_list: List[str]) -> str:
|
|
230 |
resulting_string += f""
|
231 |
else:
|
232 |
resulting_string += elem
|
|
|
233 |
return resulting_string
|
234 |
|
235 |
|
@@ -238,6 +274,8 @@ def prompt_list_to_tgi_input(prompt_list: List[str]) -> str:
|
|
238 |
TGI expects a string that contains both text and images in the image markdown format (i.e. the `![]()` ).
|
239 |
The images links are parsed on TGI side
|
240 |
"""
|
|
|
|
|
241 |
result_string_input = ""
|
242 |
for elem in prompt_list:
|
243 |
if is_image(elem):
|
@@ -247,13 +285,17 @@ def prompt_list_to_tgi_input(prompt_list: List[str]) -> str:
|
|
247 |
result_string_input += f"})"
|
248 |
else:
|
249 |
result_string_input += elem
|
|
|
250 |
return result_string_input
|
251 |
|
252 |
|
253 |
def remove_spaces_around_token(text: str) -> str:
|
|
|
|
|
254 |
pattern = r"\s*(<fake_token_around_image>)\s*"
|
255 |
replacement = r"\1"
|
256 |
result = re.sub(pattern, replacement, text)
|
|
|
257 |
return result
|
258 |
|
259 |
|
@@ -265,6 +307,10 @@ def format_user_prompt_with_im_history_and_system_conditioning(
|
|
265 |
Produces the resulting list that needs to go inside the processor.
|
266 |
It handles the potential image box input, the history and the system conditionning.
|
267 |
"""
|
|
|
|
|
|
|
|
|
268 |
resulting_list = copy.deepcopy(SYSTEM_PROMPT)
|
269 |
|
270 |
# Format history
|
@@ -300,6 +346,7 @@ def format_user_prompt_with_im_history_and_system_conditioning(
|
|
300 |
resulting_list.extend(["\nUser:", current_image, f"{current_user_prompt_str}<end_of_utterance>\nAssistant:"])
|
301 |
current_user_prompt_list = [current_user_prompt_str]
|
302 |
|
|
|
303 |
return resulting_list, current_user_prompt_list
|
304 |
|
305 |
|
@@ -472,6 +519,16 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
|
|
472 |
repetition_penalty,
|
473 |
top_p,
|
474 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
475 |
if user_prompt_str.strip() == "" and image is None:
|
476 |
return "", None, chat_history
|
477 |
|
@@ -542,6 +599,8 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
|
|
542 |
last_turn = chat_history.pop(-1)
|
543 |
last_turn[-1] += acc_text
|
544 |
chat_history.append(last_turn)
|
|
|
|
|
545 |
yield "", None, chat_history
|
546 |
acc_text = ""
|
547 |
|
@@ -557,6 +616,7 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
|
|
557 |
repetition_penalty=None,
|
558 |
top_p=0.95,
|
559 |
)
|
|
|
560 |
return clear_msg, image_value, chat
|
561 |
|
562 |
textbox.submit(
|
@@ -595,6 +655,8 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
|
|
595 |
)
|
596 |
|
597 |
def remove_last_turn(chat_history):
|
|
|
|
|
598 |
if len(chat_history) == 0:
|
599 |
return gr.Update(), gr.Update()
|
600 |
last_interaction = chat_history[-1]
|
|
|
75 |
|
76 |
# Monkey patch adapted from gradio.components.image.Image - mostly to make the `save` step optional in `pil_to_temp_file`
|
77 |
def hash_bytes(bytes: bytes):
|
78 |
+
print("********* hash_bytes ********")
|
79 |
+
print(f"param: bytes - {bytes}")
|
80 |
sha1 = hashlib.sha1()
|
81 |
sha1.update(bytes)
|
82 |
+
print(f"Returns - sha1.hexdigest() - {sha1.hexdigest()}")
|
83 |
return sha1.hexdigest()
|
84 |
|
85 |
|
86 |
def pil_to_temp_file(img: PIL.Image.Image, dir: str = DEFAULT_TEMP_DIR, format: str = "png") -> str:
|
87 |
+
"""
|
88 |
+
Save a PIL image into a temp file
|
89 |
+
"""
|
90 |
+
print("************ pil_to_temp_file ***********")
|
91 |
+
print(f"img - {img}, dir - {dir}, format - {format}")
|
92 |
bytes_data = processing_utils.encode_pil_to_bytes(img, format)
|
93 |
temp_dir = Path(dir) / hash_bytes(bytes_data)
|
94 |
temp_dir.mkdir(exist_ok=True, parents=True)
|
95 |
filename = str(temp_dir / f"image.{format}")
|
96 |
if not os.path.exists(filename):
|
97 |
img.save(filename, pnginfo=processing_utils.get_pil_metadata(img))
|
98 |
+
print(f"filename - {filename}")
|
99 |
return filename
|
100 |
|
101 |
|
102 |
def add_file(file):
|
103 |
+
print("****** add_file *******")
|
104 |
+
print(f"returns - file name - {file.name}")
|
105 |
return file.name
|
106 |
|
107 |
|
|
|
113 |
- `User:Describe this image.` would become `["User:", "https://favurl.com/chicken_on_money.png", "Describe this image."]`
|
114 |
- `User:Describe this image.` would become `["User:", "/my_temp/chicken_on_money.png", "Describe this image."]`
|
115 |
"""
|
116 |
+
print("************ split_str_on_im_markdown ***********")
|
117 |
+
print(f"string - {string}")
|
118 |
IMAGES_PATTERN = re.compile(r"!\[[^\]]*\]\((.*?)\s*(\"(?:.*[^\"])\")?\s*\)")
|
119 |
parts = []
|
120 |
cursor = 0
|
|
|
129 |
cursor = pattern.end()
|
130 |
if cursor != len(string):
|
131 |
parts.append(string[cursor:])
|
132 |
+
|
133 |
+
print(f"returns - parts - {parts}")
|
134 |
return parts
|
135 |
|
136 |
|
|
|
138 |
"""
|
139 |
There are two ways for images: local image path or url.
|
140 |
"""
|
141 |
+
print("************ is_image ***********")
|
142 |
+
print(f"string - {string}")
|
143 |
+
print(f"returns - a bool - {is_url(string) or string.startswith(DEFAULT_TEMP_DIR)}")
|
144 |
return is_url(string) or string.startswith(DEFAULT_TEMP_DIR)
|
145 |
|
146 |
|
|
|
149 |
Checks if the passed string contains a valid url and nothing else. e.g. if space is included it's immediately
|
150 |
invalidated the url
|
151 |
"""
|
152 |
+
print("************ is_url ***********")
|
153 |
+
print(f"string - {string}")
|
154 |
if " " in string:
|
155 |
return False
|
156 |
result = urlparse(string)
|
157 |
+
print(f"returns - a bool - {all([result.scheme, result.netloc])}")
|
158 |
return all([result.scheme, result.netloc])
|
159 |
|
160 |
|
|
|
181 |
]
|
182 |
```
|
183 |
"""
|
184 |
+
print("************ isolate_images_urls ***********")
|
185 |
+
print(f"params: prompt_list - {prompt_list}")
|
186 |
linearized_list = []
|
187 |
for prompt in prompt_list:
|
188 |
# Prompt can be either a string, or a PIL image
|
|
|
205 |
f"Unrecognized type for `prompt`. Got {type(type(prompt))}. Was expecting something in [`str`,"
|
206 |
" `PIL.Image.Image`]"
|
207 |
)
|
208 |
+
print(f"linearized_list - {linearized_list}")
|
209 |
return linearized_list
|
210 |
|
211 |
|
212 |
def fetch_images(url_list: str) -> PIL.Image.Image:
|
213 |
"""Fetching images"""
|
214 |
+
print("************ fetch_images ***********")
|
215 |
+
print(f"params: url_list - {url_list}")
|
216 |
+
print(f"returns - PROCESSOR.image_processor.fetch_images(url_list) - {PROCESSOR.image_processor.fetch_images(url_list)}")
|
217 |
return PROCESSOR.image_processor.fetch_images(url_list)
|
218 |
|
219 |
|
|
|
222 |
Handle the case of textually manually inputted images (i.e. the `<fake_token_around_image><image:IMG_URL><fake_token_around_image>`) in the user prompt
|
223 |
by fetching them, saving them locally and replacing the whole sub-sequence the image local path.
|
224 |
"""
|
225 |
+
print("************ handle_manual_images_in_user_prompt ***********")
|
226 |
+
print(f"params: user_prompt - {user_prompt}")
|
227 |
if "<fake_token_around_image>" in user_prompt:
|
228 |
splitted_user_prompt = isolate_images_urls([user_prompt])
|
229 |
resulting_user_prompt = []
|
|
|
234 |
resulting_user_prompt.append(tmp_file)
|
235 |
else:
|
236 |
resulting_user_prompt.append(u_p)
|
237 |
+
print(f"returns - resulting_user_prompt - {resulting_user_prompt}")
|
238 |
return resulting_user_prompt
|
239 |
else:
|
240 |
+
print(f"returns - [user_prompt] - {[user_prompt]}")
|
241 |
return [user_prompt]
|
242 |
|
243 |
|
244 |
def gradio_link(img_path: str) -> str:
|
245 |
+
print("************ gradio_link ***********")
|
246 |
+
print(f"params: img_path - {img_path}")
|
247 |
url = f"{GRADIO_LINK}/file={img_path}"
|
248 |
+
print(f"returns - url - {url}")
|
249 |
return url
|
250 |
|
251 |
|
|
|
254 |
Convert a user prompt in the list format (i.e. elements are either a PIL image or a string) into
|
255 |
the markdown format that is used for the chatbot history and rendering.
|
256 |
"""
|
257 |
+
print("************ prompt_list_to_markdown ***********")
|
258 |
+
print(f"params: prompt_list - {prompt_list}")
|
259 |
resulting_string = ""
|
260 |
for elem in prompt_list:
|
261 |
if is_image(elem):
|
|
|
265 |
resulting_string += f""
|
266 |
else:
|
267 |
resulting_string += elem
|
268 |
+
print(f"returns - resulting_string - {resulting_string}")
|
269 |
return resulting_string
|
270 |
|
271 |
|
|
|
274 |
TGI expects a string that contains both text and images in the image markdown format (i.e. the `![]()` ).
|
275 |
The images links are parsed on TGI side
|
276 |
"""
|
277 |
+
print("************ prompt_list_to_tgi_input ***********")
|
278 |
+
print(f"params: prompt_list - {prompt_list}")
|
279 |
result_string_input = ""
|
280 |
for elem in prompt_list:
|
281 |
if is_image(elem):
|
|
|
285 |
result_string_input += f"})"
|
286 |
else:
|
287 |
result_string_input += elem
|
288 |
+
print(f"returns - result_string_input - {result_string_input}")
|
289 |
return result_string_input
|
290 |
|
291 |
|
292 |
def remove_spaces_around_token(text: str) -> str:
|
293 |
+
print("************ remove_spaces_around_token ***********")
|
294 |
+
print(f"params: text - {text}")
|
295 |
pattern = r"\s*(<fake_token_around_image>)\s*"
|
296 |
replacement = r"\1"
|
297 |
result = re.sub(pattern, replacement, text)
|
298 |
+
print(f"returns - result - {result}")
|
299 |
return result
|
300 |
|
301 |
|
|
|
307 |
Produces the resulting list that needs to go inside the processor.
|
308 |
It handles the potential image box input, the history and the system conditionning.
|
309 |
"""
|
310 |
+
print("************ format_user_prompt_with_im_history_and_system_conditioning ***********")
|
311 |
+
print(f"params: current_user_prompt_str - {current_user_prompt_str}")
|
312 |
+
print(f"params: current_image - {current_image}")
|
313 |
+
print(f"params: history - {history}")
|
314 |
resulting_list = copy.deepcopy(SYSTEM_PROMPT)
|
315 |
|
316 |
# Format history
|
|
|
346 |
resulting_list.extend(["\nUser:", current_image, f"{current_user_prompt_str}<end_of_utterance>\nAssistant:"])
|
347 |
current_user_prompt_list = [current_user_prompt_str]
|
348 |
|
349 |
+
print(f"returns - resulting_list - {resulting_list}, current_user_prompt_list - {current_user_prompt_list}")
|
350 |
return resulting_list, current_user_prompt_list
|
351 |
|
352 |
|
|
|
519 |
repetition_penalty,
|
520 |
top_p,
|
521 |
):
|
522 |
+
print("************ model_inference ***********")
|
523 |
+
print(f"params: model_selector - {model_selector}")
|
524 |
+
print(f"params: user_prompt_str - {user_prompt_str}")
|
525 |
+
print(f"params: chat_history - {chat_history}")
|
526 |
+
print(f"params: image - {image}")
|
527 |
+
print(f"params: decoding_strategy - {decoding_strategy}")
|
528 |
+
print(f"params: temperature - {temperature}")
|
529 |
+
print(f"params: max_new_tokens - {max_new_tokens}")
|
530 |
+
print(f"params: repetition_penalty - {repetition_penalty}")
|
531 |
+
print(f"params: top_p - {top_p}")
|
532 |
if user_prompt_str.strip() == "" and image is None:
|
533 |
return "", None, chat_history
|
534 |
|
|
|
599 |
last_turn = chat_history.pop(-1)
|
600 |
last_turn[-1] += acc_text
|
601 |
chat_history.append(last_turn)
|
602 |
+
print(f"yields - chat_history -{chat_history} ")
|
603 |
+
print(f"acc_text -{acc_text} ")
|
604 |
yield "", None, chat_history
|
605 |
acc_text = ""
|
606 |
|
|
|
616 |
repetition_penalty=None,
|
617 |
top_p=0.95,
|
618 |
)
|
619 |
+
print("************ process_example ***********")
|
620 |
return clear_msg, image_value, chat
|
621 |
|
622 |
textbox.submit(
|
|
|
655 |
)
|
656 |
|
657 |
def remove_last_turn(chat_history):
|
658 |
+
print("************ remove_last_turn ***********")
|
659 |
+
print(f"params: chat_history - {chat_history}")
|
660 |
if len(chat_history) == 0:
|
661 |
return gr.Update(), gr.Update()
|
662 |
last_interaction = chat_history[-1]
|