VictorSanh commited on
Commit
adf07d5
1 Parent(s): 7d606d8

change name

Browse files
Files changed (3) hide show
  1. app_dialogue.py +251 -580
  2. old_app_dialogue.py +733 -0
  3. playground.py +0 -404
app_dialogue.py CHANGED
@@ -1,25 +1,19 @@
1
  import copy
2
- import hashlib
3
  import os
4
- import re
5
  import spaces
6
  import subprocess
7
  import torch
8
- import PIL
9
 
10
- from pathlib import Path
11
  from threading import Thread
12
- from typing import List, Optional, Tuple
13
  from urllib.parse import urlparse
14
  from PIL import Image
15
 
16
  import gradio as gr
17
- from gradio import processing_utils
18
  from gradio_client.client import DEFAULT_TEMP_DIR
19
- from transformers import AutoProcessor, AutoModelForCausalLM, TextIteratorStreamer, logging
20
-
21
- from utils import create_model_inputs
22
-
23
 
24
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
25
 
@@ -55,7 +49,7 @@ FAKE_TOK_AROUND_IMAGE = "<fake_token_around_image>"
55
  BOS_TOKEN = PROCESSOR.tokenizer.bos_token
56
  BAD_WORDS_IDS = PROCESSOR.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids
57
  EOS_WORDS_IDS = PROCESSOR.tokenizer("<end_of_utterance>", add_special_tokens=False).input_ids + [PROCESSOR.tokenizer.eos_token_id]
58
- IMAGE_SEQ_LEN = list(MODELS.values())[0].config.perceiver_config.resampler_n_latents
59
 
60
  SYSTEM_PROMPT = [
61
  # """The following is a conversation between a highly knowledgeable and intelligent visual AI assistant, called Assistant, and a human user, called User. In the following interactions, User and Assistant will converse in natural language, and Assistant will do its best to answer User’s questions. Assistant has the ability to perceive images and reason about the content of visual inputs. Assistant was built to be respectful, polite and inclusive. It knows a lot, and always tells the truth. When prompted with an image, it does not make up facts.
@@ -88,52 +82,87 @@ API_TOKEN = os.getenv("HF_AUTH_TOKEN")
88
  BOT_AVATAR = "IDEFICS_logo.png"
89
 
90
 
91
- # Monkey patch adapted from gradio.components.image.Image - mostly to make the `save` step optional in `pil_to_temp_file`
92
- def hash_bytes(bytes: bytes):
93
- sha1 = hashlib.sha1()
94
- sha1.update(bytes)
95
- return sha1.hexdigest()
96
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- def pil_to_temp_file(img: PIL.Image.Image, dir: str = DEFAULT_TEMP_DIR, format: str = "png") -> str:
99
- """Save a PIL image into a temp file"""
100
- bytes_data = processing_utils.encode_pil_to_bytes(img, format)
101
- temp_dir = Path(dir) / hash_bytes(bytes_data)
102
- temp_dir.mkdir(exist_ok=True, parents=True)
103
- filename = str(temp_dir / f"image.{format}")
104
- if not os.path.exists(filename):
105
- img.save(filename, pnginfo=processing_utils.get_pil_metadata(img))
106
- return filename
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
- def add_file(file):
110
- return file.name, gr.update(label='🖼️ Uploaded!')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
 
 
112
 
113
- # Utils to handle the image markdown display logic
114
- def split_str_on_im_markdown(string: str) -> List[str]:
115
- """
116
- Extract from a string (typically the user prompt string) the potential images from markdown
117
- Examples:
118
- - `User:![](/file=/my_temp/chicken_on_money.png)Describe this image.` would become `["User:", "/my_temp/chicken_on_money.png", "Describe this image."]`
119
- """
120
- IMAGES_PATTERN = re.compile(r"!\[[^\]]*\]\((.*?)\s*(\"(?:.*[^\"])\")?\s*\)")
121
- parts = []
122
- cursor = 0
123
- for pattern in IMAGES_PATTERN.finditer(string):
124
- start = pattern.start()
125
- if start != cursor:
126
- parts.append(string[cursor:start])
127
- image_url = pattern.group(1)
128
- if image_url.startswith("/file="):
129
- image_url = image_url[6:] # Remove the 'file=' prefix
130
- parts.append(image_url)
131
- cursor = pattern.end()
132
- if cursor != len(string):
133
- parts.append(string[cursor:])
134
- return parts
135
 
136
 
 
137
  def is_image(string: str) -> bool:
138
  """
139
  There are two ways for images: local image path or url.
@@ -152,107 +181,14 @@ def is_url(string: str) -> bool:
152
  return all([result.scheme, result.netloc])
153
 
154
 
155
- def isolate_images_urls(prompt_list: List) -> List:
156
- """
157
- Convert a full string prompt to the list format expected by the processor.
158
- In particular, image urls (as delimited by <fake_token_around_image>) should be their own elements.
159
- From:
160
- ```
161
- [
162
- "bonjour<fake_token_around_image><image:IMG_URL><fake_token_around_image>hello",
163
- PIL.Image.Image,
164
- "Aurevoir",
165
- ]
166
- ```
167
- to:
168
- ```
169
- [
170
- "bonjour",
171
- IMG_URL,
172
- "hello",
173
- PIL.Image.Image,
174
- "Aurevoir",
175
- ]
176
- ```
177
- """
178
- linearized_list = []
179
- for prompt in prompt_list:
180
- # Prompt can be either a string, or a PIL image
181
- if isinstance(prompt, PIL.Image.Image):
182
- linearized_list.append(prompt)
183
- elif isinstance(prompt, str):
184
- if "<fake_token_around_image>" not in prompt:
185
- linearized_list.append(prompt)
186
- else:
187
- prompt_splitted = prompt.split("<fake_token_around_image>")
188
- for ps in prompt_splitted:
189
- if ps == "":
190
- continue
191
- if ps.startswith("<image:"):
192
- linearized_list.append(ps[7:-1])
193
- else:
194
- linearized_list.append(ps)
195
- else:
196
- raise TypeError(
197
- f"Unrecognized type for `prompt`. Got {type(type(prompt))}. Was expecting something in [`str`,"
198
- " `PIL.Image.Image`]"
199
- )
200
- return linearized_list
201
-
202
-
203
- def fetch_images(url_list: str) -> PIL.Image.Image:
204
- """Fetching images"""
205
- return PROCESSOR.image_processor.fetch_images(url_list)
206
-
207
-
208
- def handle_manual_images_in_user_prompt(user_prompt: str) -> List[str]:
209
- """
210
- 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
211
- by fetching them, saving them locally and replacing the whole sub-sequence the image local path.
212
- """
213
- if "<fake_token_around_image>" in user_prompt:
214
- splitted_user_prompt = isolate_images_urls([user_prompt])
215
- resulting_user_prompt = []
216
- for u_p in splitted_user_prompt:
217
- if is_url(u_p):
218
- img = fetch_images([u_p])[0]
219
- tmp_file = pil_to_temp_file(img)
220
- resulting_user_prompt.append(tmp_file)
221
- else:
222
- resulting_user_prompt.append(u_p)
223
- return resulting_user_prompt
224
- else:
225
- return [user_prompt]
226
-
227
-
228
- def prompt_list_to_markdown(prompt_list: List[str]) -> str:
229
- """
230
- Convert a user prompt in the list format (i.e. elements are either a PIL image or a string) into
231
- the markdown format that is used for the chatbot history and rendering.
232
- """
233
- resulting_string = ""
234
- for elem in prompt_list:
235
- if is_image(elem):
236
- if is_url(elem):
237
- resulting_string += f"![]({elem})"
238
- else:
239
- resulting_string += f"![](/file={elem})"
240
- else:
241
- resulting_string += elem
242
- return resulting_string
243
-
244
-
245
  def prompt_list_to_model_input(prompt_list: List[str]) -> Tuple[str, List[Image.Image]]:
246
  """
247
- Create the final input string and image list to feed to the model's processor.
248
  """
249
  images = []
250
  for idx, part in enumerate(prompt_list):
251
  if is_image(part):
252
- if is_url(part):
253
- images.append(fetch_images([part])[0])
254
- else:
255
- images.append(Image.open(part))
256
  prompt_list[idx] = f"{FAKE_TOK_AROUND_IMAGE}{'<image>' * IMAGE_SEQ_LEN}{FAKE_TOK_AROUND_IMAGE}"
257
  input_text = "".join(prompt_list)
258
  input_text = input_text.replace(FAKE_TOK_AROUND_IMAGE * 2, FAKE_TOK_AROUND_IMAGE)
@@ -260,85 +196,181 @@ def prompt_list_to_model_input(prompt_list: List[str]) -> Tuple[str, List[Image.
260
  return input_text, images
261
 
262
 
263
- def remove_spaces_around_token(text: str) -> str:
264
- pattern = r"\s*(<fake_token_around_image>)\s*"
265
- replacement = r"\1"
266
- result = re.sub(pattern, replacement, text)
267
- return result
268
 
269
 
270
- # Chatbot utils
271
  def format_user_prompt_with_im_history_and_system_conditioning(
272
- current_user_prompt_str: str, current_image: Optional[str], history: List[Tuple[str, str]]
273
- ) -> Tuple[List[str], List[str]]:
274
  """
275
  Produces the resulting list that needs to go inside the processor.
276
- It handles the potential image box input, the history and the system conditionning.
277
  """
278
  resulting_list = copy.deepcopy(SYSTEM_PROMPT)
279
 
280
  # Format history
281
- for turn in history:
282
- user_utterance, assistant_utterance = turn
283
- splitted_user_utterance = split_str_on_im_markdown(user_utterance)
284
-
285
- optional_space = ""
286
- if not is_image(splitted_user_utterance[0]):
287
- optional_space = " "
288
- resulting_list.append(f"\nUser:{optional_space}")
289
- resulting_list.extend(splitted_user_utterance)
290
- resulting_list.append(f"<end_of_utterance>\nAssistant: {assistant_utterance}")
 
 
291
 
292
  # Format current input
293
- current_user_prompt_str = remove_spaces_around_token(current_user_prompt_str)
294
- if current_image is None:
295
- if "![](" in current_user_prompt_str:
296
- current_user_prompt_list = split_str_on_im_markdown(current_user_prompt_str)
297
- else:
298
- current_user_prompt_list = handle_manual_images_in_user_prompt(current_user_prompt_str)
299
-
300
- optional_space = ""
301
- if not is_image(current_user_prompt_list[0]):
302
- # Check if the first element is an image (and more precisely a path to an image)
303
- optional_space = " "
304
- resulting_list.append(f"\nUser:{optional_space}")
305
- resulting_list.extend(current_user_prompt_list)
306
- resulting_list.append("<end_of_utterance>\nAssistant:")
307
  else:
308
  # Choosing to put the image first when the image is inputted through the UI, but this is an arbiratrary choice.
309
- resulting_list.extend(["\nUser:", current_image, f"{current_user_prompt_str}<end_of_utterance>\nAssistant:"])
310
- current_user_prompt_list = [current_user_prompt_str]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
 
312
- return resulting_list, current_user_prompt_list
 
 
 
 
313
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
 
315
- textbox = gr.Textbox(
316
- placeholder="Upload an image and send a message",
317
- show_label=False,
318
- # value="Describe the battle against the fierce dragons.",
319
- visible=True,
320
- container=False,
321
- label="Text input",
322
- scale=6,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  )
324
- with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
325
- gr.HTML("""<h1 align="center">🐶 IDEFICS Playground</h1>""")
326
- # with gr.Row(variant="panel"):
327
- # with gr.Column(scale=1):
328
- # gr.Image(IDEFICS_LOGO, elem_id="banner-image", show_label=False, show_download_button=False)
329
- # with gr.Column(scale=5):
330
- # gr.HTML("""
331
- # <p>This demo showcases <strong>IDEFICS</strong>, a open-access large visual language model. Like GPT-4, the multimodal model accepts arbitrary sequences of image and text inputs and produces text outputs. IDEFICS can answer questions about images, describe visual content, create stories grounded in multiple images, etc.</p>
332
- # <p>IDEFICS (which stands for <strong>I</strong>mage-aware <strong>D</strong>ecoder <strong>E</strong>nhanced à la <strong>F</strong>lamingo with <strong>I</strong>nterleaved <strong>C</strong>ross-attention<strong>S</strong>) is an open-access reproduction of <a href="https://huggingface.co/papers/2204.14198">Flamingo</a>, a closed-source visual language model developed by Deepmind. IDEFICS was built solely on publicly available data and models. It is currently the only visual language model of this scale (80 billion parameters) that is available in open-access.</p>
333
- # <p>📚 The variants available in this demo were fine-tuned on a mixture of supervised and instruction fine-tuning datasets to make the models more suitable in conversational settings. For more details, we refer to our <a href="https://huggingface.co/blog/idefics">blog post</a>.</p>
334
- # <p>🅿️ <strong>Intended uses:</strong> This demo along with the <a href="https://huggingface.co/models?sort=trending&amp;search=HuggingFaceM4%2Fidefics">supporting models</a> are provided as research artifacts to the community. We detail misuses and out-of-scope uses <a href="https://huggingface.co/HuggingFaceM4/idefics-80b#misuse-and-out-of-scope-use">here</a>.</p>
335
- # <p>⛔️ <strong>Limitations:</strong> The model can produce factually incorrect texts, hallucinate facts (with or without an image) and will struggle with small details in images. While the model will tend to refuse answering questionable user requests, it can produce problematic outputs (including racist, stereotypical, and disrespectful texts), in particular when prompted to do so. We encourage users to read our findings from evaluating the model for potential biases in the <a href="https://huggingface.co/HuggingFaceM4/idefics-80b#bias-evaluation">model card</a>.</p>
336
- # """)
337
 
 
338
  with gr.Row(elem_id="model_selector_row"):
339
  model_selector = gr.Dropdown(
340
  choices=MODELS.keys(),
341
- value="284 - neftune - opt 18'500",
342
  interactive=True,
343
  show_label=False,
344
  container=False,
@@ -346,388 +378,27 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
346
  visible=True,
347
  )
348
 
349
- imagebox = gr.Image(type="filepath", label="Image input", visible=False)
350
-
351
- with gr.Row():
352
- # def prefetch_images_in_history(user_prompt_str):
353
- # """
354
- # Pre-fetch the images that are passed in the chatbot default history.
355
- # """
356
- # return prompt_list_to_markdown(handle_manual_images_in_user_prompt(user_prompt_str))
357
-
358
- chatbot = gr.Chatbot(
359
- elem_id="chatbot",
360
- label="IDEFICS",
361
- visible=True,
362
- height=750,
363
- avatar_images=[None, BOT_AVATAR]
364
- )
365
-
366
- with gr.Group():
367
- with gr.Row():
368
- textbox.render()
369
- submit_btn = gr.Button(value="▶️ Submit", visible=True)
370
- clear_btn = gr.ClearButton([textbox, imagebox, chatbot], value="🧹 Clear")
371
- regenerate_btn = gr.Button(value="🔄 Regenerate", visible=True)
372
- upload_btn = gr.UploadButton("📁 Upload image", file_types=["image"])
373
-
374
- with gr.Row():
375
- with gr.Accordion("Advanced settings", open=False, visible=True) as parameter_row:
376
- max_new_tokens = gr.Slider(
377
- minimum=8,
378
- maximum=1024,
379
- value=512,
380
- step=1,
381
- interactive=True,
382
- label="Maximum number of new tokens to generate",
383
- )
384
- repetition_penalty = gr.Slider(
385
- minimum=0.01,
386
- maximum=5.0,
387
- value=1.0,
388
- step=0.01,
389
- interactive=True,
390
- label="Repetition penalty",
391
- info="1.0 is equivalent to no penalty",
392
- )
393
- decoding_strategy = gr.Radio(
394
- [
395
- "Greedy",
396
- "Top P Sampling",
397
- ],
398
- value="Greedy",
399
- label="Decoding strategy",
400
- interactive=True,
401
- info="Higher values is equivalent to sampling more low-probability tokens.",
402
- )
403
- temperature = gr.Slider(
404
- minimum=0.0,
405
- maximum=5.0,
406
- value=0.4,
407
- step=0.1,
408
- interactive=True,
409
- visible=False,
410
- label="Sampling temperature",
411
- info="Higher values will produce more diverse outputs.",
412
- )
413
- decoding_strategy.change(
414
- fn=lambda selection: gr.Slider(
415
- visible=(
416
- selection in ["contrastive_sampling", "beam_sampling", "Top P Sampling", "sampling_top_k"]
417
- )
418
- ),
419
- inputs=decoding_strategy,
420
- outputs=temperature,
421
- )
422
- top_p = gr.Slider(
423
- minimum=0.01,
424
- maximum=0.99,
425
- value=0.8,
426
- step=0.01,
427
- interactive=True,
428
- visible=False,
429
- label="Top P",
430
- info="Higher values is equivalent to sampling more low-probability tokens.",
431
- )
432
- decoding_strategy.change(
433
- fn=lambda selection: gr.Slider(visible=(selection in ["Top P Sampling"])),
434
- inputs=decoding_strategy,
435
- outputs=top_p,
436
- )
437
-
438
- @spaces.GPU(duration=180)
439
- def model_inference(
440
- model_selector,
441
- user_prompt_str,
442
- chat_history,
443
- image,
444
- decoding_strategy,
445
- temperature,
446
- max_new_tokens,
447
- repetition_penalty,
448
- top_p,
449
- ):
450
- if user_prompt_str.strip() == "" and image is None:
451
- return "", None, chat_history
452
-
453
- formated_prompt_list, user_prompt_list = format_user_prompt_with_im_history_and_system_conditioning(
454
- current_user_prompt_str=user_prompt_str.strip(),
455
- current_image=image,
456
- history=chat_history,
457
- )
458
-
459
- streamer = TextIteratorStreamer(
460
- PROCESSOR.tokenizer,
461
- skip_prompt=True,
462
- )
463
-
464
- # Common parameters to all decoding strategies
465
- # This documentation is useful to read: https://huggingface.co/docs/transformers/main/en/generation_strategies
466
- generation_args = {
467
- "max_new_tokens": max_new_tokens,
468
- "repetition_penalty": repetition_penalty,
469
- "bad_words_ids": BAD_WORDS_IDS,
470
- "eos_token_id": EOS_WORDS_IDS,
471
- "streamer": streamer,
472
- }
473
-
474
- assert decoding_strategy in [
475
- "Greedy",
476
- "Top P Sampling",
477
- ]
478
- if decoding_strategy == "Greedy":
479
- generation_args["do_sample"] = False
480
- elif decoding_strategy == "Top P Sampling":
481
- generation_args["temperature"] = temperature
482
- generation_args["do_sample"] = True
483
- generation_args["top_p"] = top_p
484
-
485
- if image is None:
486
- # Case where there is no image OR the image is passed as `<fake_token_around_image><image:IMAGE_URL><fake_token_around_image>`
487
- chat_history.append([prompt_list_to_markdown(user_prompt_list), ''])
488
- else:
489
- # Case where the image is passed through the Image Box.
490
- # Convert the image into base64 for both passing it through the chat history and
491
- # displaying the image inside the same bubble as the text.
492
- chat_history.append(
493
- [
494
- f"{prompt_list_to_markdown([image] + user_prompt_list)}",
495
- '',
496
- ]
497
  )
498
-
499
- # Creating model inputs
500
- input_text, images = prompt_list_to_model_input(formated_prompt_list)
501
- inputs = create_model_inputs([input_text], [images])
502
- inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
503
- generation_args.update(inputs)
504
-
505
- thread = Thread(
506
- target=MODELS[model_selector].generate,
507
- kwargs=generation_args,
508
- )
509
-
510
- thread.start()
511
- acc_text = ""
512
- for idx, text_token in enumerate(streamer):
513
-
514
- acc_text += text_token
515
- last_turn = chat_history.pop(-1)
516
- last_turn[-1] += acc_text
517
- if last_turn[-1].endswith("\nUser"):
518
- # Safeguard: sometimes (rarely), the model won't generate the token `<end_of_utterance>` and will go directly to generating `\nUser:`
519
- # It will thus stop the generation on `\nUser:`. But when it exits, it will have already generated `\nUser`
520
- # This post-processing ensures that we don't have an additional `\nUser` wandering around.
521
- last_turn[-1] = last_turn[-1][:-5]
522
- chat_history.append(last_turn)
523
- yield "", None, chat_history
524
- acc_text = ""
525
-
526
- def process_example(message, image):
527
- """
528
- Same as `model_inference` but in greedy mode and with the 80b-instruct.
529
- Specifically for pre-computing the default examples.
530
- """
531
- model_selector = "284 - neftune - opt 18'500"
532
- user_prompt_str = message
533
- chat_history = []
534
- max_new_tokens = 512
535
-
536
- formated_prompt_list, user_prompt_list = format_user_prompt_with_im_history_and_system_conditioning(
537
- current_user_prompt_str=user_prompt_str.strip(),
538
- current_image=image,
539
- history=chat_history,
540
- )
541
-
542
- # Common parameters to all decoding strategies
543
- # This documentation is useful to read: https://huggingface.co/docs/transformers/main/en/generation_strategies
544
- generation_args = {
545
- "max_new_tokens": max_new_tokens,
546
- "repetition_penalty": None,
547
- "bad_words_ids": BAD_WORDS_IDS,
548
- "eos_token_id": EOS_WORDS_IDS,
549
- "do_sample": False,
550
- }
551
-
552
- if image is None:
553
- # Case where there is no image OR the image is passed as `<fake_token_around_image><image:IMAGE_URL><fake_token_around_image>`
554
- chat_history.append([prompt_list_to_markdown(user_prompt_list), ''])
555
- else:
556
- # Case where the image is passed through the Image Box.
557
- # Convert the image into base64 for both passing it through the chat history and
558
- # displaying the image inside the same bubble as the text.
559
- chat_history.append(
560
- [
561
- f"{prompt_list_to_markdown([image] + user_prompt_list)}",
562
- '',
563
- ]
564
- )
565
-
566
- # Creating model inputs
567
- input_text, images = prompt_list_to_model_input(formated_prompt_list)
568
- inputs = create_model_inputs([input_text], [images])
569
- inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
570
- generation_args.update(inputs)
571
-
572
- generated_ids = MODELS[model_selector].generate(**generation_args)
573
- generated_text = PROCESSOR.batch_decode(generated_ids, skip_special_tokens=True)[0]
574
-
575
- if generated_text.endswith("\nUser"):
576
- generated_text = generated_text[:-5]
577
-
578
- last_turn = chat_history.pop(-1)
579
- last_turn[-1] += generated_text
580
- chat_history.append(last_turn)
581
- return "", None, chat_history
582
-
583
- textbox.submit(
584
- fn=model_inference,
585
- inputs=[
586
- model_selector,
587
- textbox,
588
- chatbot,
589
- imagebox,
590
- decoding_strategy,
591
- temperature,
592
- max_new_tokens,
593
- repetition_penalty,
594
- top_p,
595
- ],
596
- outputs=[textbox, imagebox, chatbot],
597
  )
598
- submit_btn.click(
599
- fn=model_inference,
600
- inputs=[
601
- model_selector,
602
- textbox,
603
- chatbot,
604
- imagebox,
605
- decoding_strategy,
606
- temperature,
607
- max_new_tokens,
608
- repetition_penalty,
609
- top_p,
610
- ],
611
- outputs=[
612
- textbox,
613
- imagebox,
614
- chatbot,
615
- ],
616
  )
617
 
618
- def remove_last_turn(chat_history):
619
- if len(chat_history) == 0:
620
- return gr.Update(), gr.Update()
621
- last_interaction = chat_history[-1]
622
- chat_history = chat_history[:-1]
623
- chat_update = gr.update(value=chat_history)
624
- text_update = gr.update(value=last_interaction[0])
625
- return chat_update, text_update
626
-
627
- regenerate_btn.click(fn=remove_last_turn, inputs=chatbot, outputs=[chatbot, textbox]).then(
628
  fn=model_inference,
629
- inputs=[
630
- model_selector,
631
- textbox,
632
- chatbot,
633
- imagebox,
634
- decoding_strategy,
635
- temperature,
636
- max_new_tokens,
637
- repetition_penalty,
638
- top_p,
639
- ],
640
- outputs=[
641
- textbox,
642
- imagebox,
643
- chatbot,
644
- ],
645
  )
646
 
647
- upload_btn.upload(add_file, [upload_btn], [imagebox, upload_btn], queue=False)
648
- submit_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
649
- textbox.submit(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
650
- clear_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
651
-
652
- # examples_path = os.path.dirname(__file__)
653
- # gr.Examples(
654
- # examples=[
655
- # [
656
- # (
657
- # "Which famous person does the person in the image look like? Could you craft an engaging narrative"
658
- # " featuring this character from the image as the main protagonist?"
659
- # ),
660
- # f"{examples_path}/example_images/obama-harry-potter.jpg",
661
- # ],
662
- # [
663
- # "Can you describe the image? Do you think it's real?",
664
- # f"{examples_path}/example_images/rabbit_force.png",
665
- # ],
666
- # ["Explain this meme to me.", f"{examples_path}/example_images/meme_french.jpg"],
667
- # ["Give me a short and easy recipe for this dish.", f"{examples_path}/example_images/recipe_burger.webp"],
668
- # [
669
- # "I want to go somewhere similar to the one in the photo. Give me destinations and travel tips.",
670
- # f"{examples_path}/example_images/travel_tips.jpg",
671
- # ],
672
- # [
673
- # "Can you name the characters in the image and give their French names?",
674
- # f"{examples_path}/example_images/gaulois.png",
675
- # ],
676
- # ["Write a complete sales ad for this product.", f"{examples_path}/example_images/product_ad.jpg"],
677
- # [
678
- # (
679
- # "As an art critic AI assistant, could you describe this painting in details and make a thorough"
680
- # " critic?"
681
- # ),
682
- # f"{examples_path}/example_images/art_critic.png",
683
- # ],
684
- # [
685
- # "Can you tell me a very short story based on this image?",
686
- # f"{examples_path}/example_images/chicken_on_money.png",
687
- # ],
688
- # ["Write 3 funny meme texts about this image.", f"{examples_path}/example_images/elon_smoking.jpg"],
689
- # [
690
- # "Who is in this picture? Why do people find it surprising?",
691
- # f"{examples_path}/example_images/pope_doudoune.webp",
692
- # ],
693
- # ["What are the armed baguettes guarding?", f"{examples_path}/example_images/baguettes_guarding_paris.png"],
694
- # ["What is this animal and why is it unusual?", f"{examples_path}/example_images/blue_dog.png"],
695
- # [
696
- # "What is this object and do you think it is horrifying?",
697
- # f"{examples_path}/example_images/can_horror.png",
698
- # ],
699
- # [
700
- # (
701
- # "What is this sketch for? How would you make an argument to prove this sketch was made by Picasso"
702
- # " himself?"
703
- # ),
704
- # f"{examples_path}/example_images/cat_sketch.png",
705
- # ],
706
- # ["Which celebrity does this claymation figure look like?", f"{examples_path}/example_images/kanye.jpg"],
707
- # ["What can you tell me about the cap in this image?", f"{examples_path}/example_images/ironman_cap.png"],
708
- # [
709
- # "Can you write an advertisement for Coca-Cola based on this image?",
710
- # f"{examples_path}/example_images/polar_bear_coke.png",
711
- # ],
712
- # [
713
- # "What is happening in this image? Which famous personality does this person in center looks like?",
714
- # f"{examples_path}/example_images/gandhi_selfie.jpg",
715
- # ],
716
- # [
717
- # "What do you think the dog is doing and is it unusual?",
718
- # f"{examples_path}/example_images/surfing_dog.jpg",
719
- # ],
720
- # ],
721
- # inputs=[textbox, imagebox],
722
- # outputs=[textbox, imagebox, chatbot],
723
- # fn=process_example,
724
- # cache_examples=False,
725
- # examples_per_page=6,
726
- # label=(
727
- # "Click on any example below to get started.\nFor convenience, the model generations have been"
728
- # " pre-computed with `idefics-80b-instruct`."
729
- # ),
730
- # )
731
-
732
- demo.queue(max_size=40)
733
  demo.launch()
 
1
  import copy
 
2
  import os
 
3
  import spaces
4
  import subprocess
5
  import torch
 
6
 
 
7
  from threading import Thread
8
+ from typing import List, Tuple
9
  from urllib.parse import urlparse
10
  from PIL import Image
11
 
12
  import gradio as gr
 
13
  from gradio_client.client import DEFAULT_TEMP_DIR
14
+ from transformers import AutoProcessor, AutoModelForCausalLM, TextIteratorStreamer
15
+ from transformers.image_utils import to_numpy_array, PILImageResampling, ChannelDimension
16
+ from transformers.image_transforms import resize, to_channel_dimension_format
 
17
 
18
  subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
19
 
 
49
  BOS_TOKEN = PROCESSOR.tokenizer.bos_token
50
  BAD_WORDS_IDS = PROCESSOR.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids
51
  EOS_WORDS_IDS = PROCESSOR.tokenizer("<end_of_utterance>", add_special_tokens=False).input_ids + [PROCESSOR.tokenizer.eos_token_id]
52
+ IMAGE_SEQ_LEN = 64#list(MODELS.values())[0].config.perceiver_config.resampler_n_latents
53
 
54
  SYSTEM_PROMPT = [
55
  # """The following is a conversation between a highly knowledgeable and intelligent visual AI assistant, called Assistant, and a human user, called User. In the following interactions, User and Assistant will converse in natural language, and Assistant will do its best to answer User’s questions. Assistant has the ability to perceive images and reason about the content of visual inputs. Assistant was built to be respectful, polite and inclusive. It knows a lot, and always tells the truth. When prompted with an image, it does not make up facts.
 
82
  BOT_AVATAR = "IDEFICS_logo.png"
83
 
84
 
85
+ # Model processing utils - these will be handled in the model processor directly ultimately
86
+ def convert_to_rgb(image):
87
+ # `image.convert("RGB")` would only work for .jpg images, as it creates a wrong background
88
+ # for transparent images. The call to `alpha_composite` handles this case
89
+ if image.mode == "RGB":
90
+ return image
91
+
92
+ image_rgba = image.convert("RGBA")
93
+ background = Image.new("RGBA", image_rgba.size, (255, 255, 255))
94
+ alpha_composite = Image.alpha_composite(background, image_rgba)
95
+ alpha_composite = alpha_composite.convert("RGB")
96
+ return alpha_composite
97
+
98
+
99
+ def custom_transform(x):
100
+ x = convert_to_rgb(x)
101
+ x = to_numpy_array(x)
102
+
103
+ height, width = x.shape[:2]
104
+ aspect_ratio = width / height
105
+ if width >= height and width > 980:
106
+ width = 980
107
+ height = int(width / aspect_ratio)
108
+ elif height > width and height > 980:
109
+ height = 980
110
+ width = int(height * aspect_ratio)
111
+ width = max(width, 378)
112
+ height = max(height, 378)
113
+
114
+ x = resize(x, (height, width), resample=PILImageResampling.BILINEAR)
115
+ x = PROCESSOR.image_processor.rescale(x, scale=1 / 255)
116
+ x = PROCESSOR.image_processor.normalize(
117
+ x,
118
+ mean=PROCESSOR.image_processor.image_mean,
119
+ std=PROCESSOR.image_processor.image_std
120
+ )
121
+ x = to_channel_dimension_format(x, ChannelDimension.FIRST)
122
+ x = torch.tensor(x)
123
+ return x
124
 
 
 
 
 
 
 
 
 
 
125
 
126
+ def create_model_inputs(
127
+ input_texts: List[str],
128
+ image_lists: List[List[Image.Image]],
129
+ ):
130
+ """
131
+ All this logic will eventually be handled inside the model processor.
132
+ """
133
+ inputs = PROCESSOR.tokenizer(
134
+ input_texts,
135
+ return_tensors="pt",
136
+ add_special_tokens=False,
137
+ padding=True,
138
+ )
139
 
140
+ output_images = [
141
+ [PROCESSOR.image_processor(img, transform=custom_transform) for img in im_list]
142
+ for im_list in image_lists
143
+ ]
144
+ total_batch_size = len(output_images)
145
+ max_num_images = max([len(img_l) for img_l in output_images])
146
+ if max_num_images > 0:
147
+ max_height = max([i.size(2) for img_l in output_images for i in img_l])
148
+ max_width = max([i.size(3) for img_l in output_images for i in img_l])
149
+ padded_image_tensor = torch.zeros(total_batch_size, max_num_images, 3, max_height, max_width)
150
+ padded_pixel_attention_masks = torch.zeros(
151
+ total_batch_size, max_num_images, max_height, max_width, dtype=torch.bool
152
+ )
153
+ for batch_idx, img_l in enumerate(output_images):
154
+ for img_idx, img in enumerate(img_l):
155
+ im_height, im_width = img.size()[2:]
156
+ padded_image_tensor[batch_idx, img_idx, :, :im_height, :im_width] = img
157
+ padded_pixel_attention_masks[batch_idx, img_idx, :im_height, :im_width] = True
158
 
159
+ inputs["pixel_values"] = padded_image_tensor
160
+ inputs["pixel_attention_mask"] = padded_pixel_attention_masks
161
 
162
+ return inputs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
 
165
+ # Chatbot utils
166
  def is_image(string: str) -> bool:
167
  """
168
  There are two ways for images: local image path or url.
 
181
  return all([result.scheme, result.netloc])
182
 
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  def prompt_list_to_model_input(prompt_list: List[str]) -> Tuple[str, List[Image.Image]]:
185
  """
186
+ Create the final input string and image list to feed to the model.
187
  """
188
  images = []
189
  for idx, part in enumerate(prompt_list):
190
  if is_image(part):
191
+ images.append(Image.open(part))
 
 
 
192
  prompt_list[idx] = f"{FAKE_TOK_AROUND_IMAGE}{'<image>' * IMAGE_SEQ_LEN}{FAKE_TOK_AROUND_IMAGE}"
193
  input_text = "".join(prompt_list)
194
  input_text = input_text.replace(FAKE_TOK_AROUND_IMAGE * 2, FAKE_TOK_AROUND_IMAGE)
 
196
  return input_text, images
197
 
198
 
199
+ def turn_is_pure_media(turn):
200
+ return turn[1] is None
 
 
 
201
 
202
 
 
203
  def format_user_prompt_with_im_history_and_system_conditioning(
204
+ user_prompt, chat_history
205
+ ) -> List[str]:
206
  """
207
  Produces the resulting list that needs to go inside the processor.
208
+ It handles the potential image(s), the history and the system conditionning.
209
  """
210
  resulting_list = copy.deepcopy(SYSTEM_PROMPT)
211
 
212
  # Format history
213
+ for turn in chat_history:
214
+ if turn_is_pure_media(turn):
215
+ media = turn[0][0]
216
+ if resulting_list == [] or (resulting_list != [] and resulting_list[-1].endswith("<end_of_utterance>")):
217
+ resulting_list.append("\nUser:")
218
+ resulting_list.append(media)
219
+ else:
220
+ user_utterance, assistant_utterance = turn
221
+ if resulting_list and is_image(resulting_list[-1]): # means that previous `turn` in `chat_history` was a pure media
222
+ resulting_list.append(f"{user_utterance.strip()}<end_of_utterance>\nAssistant: {assistant_utterance}<end_of_utterance>")
223
+ else:
224
+ resulting_list.append(f"\nUser: {user_utterance.strip()}<end_of_utterance>\nAssistant: {assistant_utterance}<end_of_utterance>")
225
 
226
  # Format current input
227
+ if not user_prompt["files"]:
228
+ resulting_list.append(f"\nUser: ")
 
 
 
 
 
 
 
 
 
 
 
 
229
  else:
230
  # Choosing to put the image first when the image is inputted through the UI, but this is an arbiratrary choice.
231
+ resulting_list.append("\nUser:")
232
+ resulting_list.extend([im["path"] for im in user_prompt["files"]])
233
+ resulting_list.append(f"{user_prompt['text']}<end_of_utterance>\nAssistant:")
234
+
235
+ return resulting_list
236
+
237
+
238
+ @spaces.GPU(duration=180)
239
+ def model_inference(
240
+ user_prompt,
241
+ chat_history,
242
+ model_selector,
243
+ decoding_strategy,
244
+ temperature,
245
+ max_new_tokens,
246
+ repetition_penalty,
247
+ top_p,
248
+ ):
249
+ if user_prompt["text"].strip() == "" and not user_prompt["files"]:
250
+ gr.Error("Please input a query and optionally image(s).")
251
+
252
+ if user_prompt["text"].strip() == "" and user_prompt["files"]:
253
+ gr.Error("Please input a text query along the image(s).")
254
+
255
+ for file in user_prompt["files"]:
256
+ if not file["mime_type"].startswith("image/"):
257
+ gr.Error("Idefics2 only supports images. Please input a valid image.")
258
+
259
+ formated_prompt_list = format_user_prompt_with_im_history_and_system_conditioning(
260
+ user_prompt=user_prompt,
261
+ chat_history=chat_history,
262
+ )
263
 
264
+ streamer = TextIteratorStreamer(
265
+ PROCESSOR.tokenizer,
266
+ skip_prompt=True,
267
+ timeout=5.,
268
+ )
269
 
270
+ # Common parameters to all decoding strategies
271
+ # This documentation is useful to read: https://huggingface.co/docs/transformers/main/en/generation_strategies
272
+ generation_args = {
273
+ "max_new_tokens": max_new_tokens,
274
+ "repetition_penalty": repetition_penalty,
275
+ "bad_words_ids": BAD_WORDS_IDS,
276
+ "eos_token_id": EOS_WORDS_IDS,
277
+ "streamer": streamer,
278
+ }
279
+
280
+ assert decoding_strategy in [
281
+ "Greedy",
282
+ "Top P Sampling",
283
+ ]
284
+ if decoding_strategy == "Greedy":
285
+ generation_args["do_sample"] = False
286
+ elif decoding_strategy == "Top P Sampling":
287
+ generation_args["temperature"] = temperature
288
+ generation_args["do_sample"] = True
289
+ generation_args["top_p"] = top_p
290
+
291
+
292
+ # Creating model inputs
293
+ input_text, images = prompt_list_to_model_input(formated_prompt_list)
294
+ inputs = create_model_inputs([input_text], [images])
295
+ inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
296
+ generation_args.update(inputs)
297
+
298
+ # # The regular non streaming generation mode
299
+ # _ = generation_args.pop("streamer")
300
+ # generated_ids = MODELS[model_selector].generate(**generation_args)
301
+ # generated_text = PROCESSOR.batch_decode(generated_ids, skip_special_tokens=True)[0]
302
+ # return generated_text
303
+
304
+ thread = Thread(
305
+ target=MODELS[model_selector].generate,
306
+ kwargs=generation_args,
307
+ )
308
+ thread.start()
309
 
310
+ print("start generating")
311
+ acc_text = ""
312
+ try:
313
+ for text_token in streamer:
314
+ acc_text += text_token
315
+ yield acc_text
316
+ except Exception as e:
317
+ print("error")
318
+ gr.Error(e)
319
+ print("success")
320
+
321
+
322
+ # Hyper-parameters for generation
323
+ max_new_tokens = gr.Slider(
324
+ minimum=8,
325
+ maximum=1024,
326
+ value=512,
327
+ step=1,
328
+ interactive=True,
329
+ label="Maximum number of new tokens to generate",
330
+ )
331
+ repetition_penalty = gr.Slider(
332
+ minimum=0.01,
333
+ maximum=5.0,
334
+ value=1.0,
335
+ step=0.01,
336
+ interactive=True,
337
+ label="Repetition penalty",
338
+ info="1.0 is equivalent to no penalty",
339
+ )
340
+ decoding_strategy = gr.Radio(
341
+ [
342
+ "Greedy",
343
+ "Top P Sampling",
344
+ ],
345
+ value="Greedy",
346
+ label="Decoding strategy",
347
+ interactive=True,
348
+ info="Higher values is equivalent to sampling more low-probability tokens.",
349
+ )
350
+ temperature = gr.Slider(
351
+ minimum=0.0,
352
+ maximum=5.0,
353
+ value=0.4,
354
+ step=0.1,
355
+ interactive=True,
356
+ label="Sampling temperature",
357
+ info="Higher values will produce more diverse outputs.",
358
+ )
359
+ top_p = gr.Slider(
360
+ minimum=0.01,
361
+ maximum=0.99,
362
+ value=0.8,
363
+ step=0.01,
364
+ interactive=True,
365
+ label="Top P",
366
+ info="Higher values is equivalent to sampling more low-probability tokens.",
367
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
368
 
369
+ with gr.Blocks(fill_height=True) as demo:
370
  with gr.Row(elem_id="model_selector_row"):
371
  model_selector = gr.Dropdown(
372
  choices=MODELS.keys(),
373
+ value=list(MODELS.keys())[0],
374
  interactive=True,
375
  show_label=False,
376
  container=False,
 
378
  visible=True,
379
  )
380
 
381
+ decoding_strategy.change(
382
+ fn=lambda selection: gr.Slider(
383
+ visible=(
384
+ selection in ["contrastive_sampling", "beam_sampling", "Top P Sampling", "sampling_top_k"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
  )
386
+ ),
387
+ inputs=decoding_strategy,
388
+ outputs=temperature,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389
  )
390
+ decoding_strategy.change(
391
+ fn=lambda selection: gr.Slider(visible=(selection in ["Top P Sampling"])),
392
+ inputs=decoding_strategy,
393
+ outputs=top_p,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  )
395
 
396
+ gr.ChatInterface(
 
 
 
 
 
 
 
 
 
397
  fn=model_inference,
398
+ # examples=[{"text": "hello"}, {"text": "hola"}, {"text": "merhaba"}],
399
+ title="Echo Bot",
400
+ multimodal=True,
401
+ additional_inputs=[model_selector, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p],
 
 
 
 
 
 
 
 
 
 
 
 
402
  )
403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
404
  demo.launch()
old_app_dialogue.py ADDED
@@ -0,0 +1,733 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import copy
2
+ import hashlib
3
+ import os
4
+ import re
5
+ import spaces
6
+ import subprocess
7
+ import torch
8
+ import PIL
9
+
10
+ from pathlib import Path
11
+ from threading import Thread
12
+ from typing import List, Optional, Tuple
13
+ from urllib.parse import urlparse
14
+ from PIL import Image
15
+
16
+ import gradio as gr
17
+ from gradio import processing_utils
18
+ from gradio_client.client import DEFAULT_TEMP_DIR
19
+ from transformers import AutoProcessor, AutoModelForCausalLM, TextIteratorStreamer, logging
20
+
21
+ from utils import create_model_inputs
22
+
23
+
24
+ subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
25
+
26
+ DEVICE = torch.device("cuda")
27
+ MODELS = {
28
+ "282 - mix1 fixed - opt 23'000": AutoModelForCausalLM.from_pretrained(
29
+ "HuggingFaceM4/idefics2",
30
+ trust_remote_code=True,
31
+ torch_dtype=torch.bfloat16,
32
+ token=os.environ["HF_AUTH_TOKEN"],
33
+ revision="a1bc6a2b0f74cde25844144f602dde2808a564d9",
34
+ ).to(DEVICE),
35
+ "286 - mix6 tables - opt 20'000": AutoModelForCausalLM.from_pretrained(
36
+ "HuggingFaceM4/idefics2",
37
+ trust_remote_code=True,
38
+ torch_dtype=torch.bfloat16,
39
+ token=os.environ["HF_AUTH_TOKEN"],
40
+ revision="b473d49caa964991b40b79fe7cb27d51d4d023f6",
41
+ ).to(DEVICE),
42
+ # "285 - continued pretraining on text sft - opt 2'000": AutoModelForCausalLM.from_pretrained(
43
+ # "HuggingFaceM4/idefics2",
44
+ # trust_remote_code=True,
45
+ # torch_dtype=torch.bfloat16,
46
+ # token=os.environ["HF_AUTH_TOKEN"],
47
+ # revision="b0a2a564e5dc311591886bb375e8d5a1aeaade83",
48
+ # ).to(DEVICE),
49
+ }
50
+ PROCESSOR = AutoProcessor.from_pretrained(
51
+ "HuggingFaceM4/idefics2",
52
+ token=os.environ["HF_AUTH_TOKEN"],
53
+ )
54
+ FAKE_TOK_AROUND_IMAGE = "<fake_token_around_image>"
55
+ BOS_TOKEN = PROCESSOR.tokenizer.bos_token
56
+ BAD_WORDS_IDS = PROCESSOR.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids
57
+ EOS_WORDS_IDS = PROCESSOR.tokenizer("<end_of_utterance>", add_special_tokens=False).input_ids + [PROCESSOR.tokenizer.eos_token_id]
58
+ IMAGE_SEQ_LEN = list(MODELS.values())[0].config.perceiver_config.resampler_n_latents
59
+
60
+ SYSTEM_PROMPT = [
61
+ # """The following is a conversation between a highly knowledgeable and intelligent visual AI assistant, called Assistant, and a human user, called User. In the following interactions, User and Assistant will converse in natural language, and Assistant will do its best to answer User’s questions. Assistant has the ability to perceive images and reason about the content of visual inputs. Assistant was built to be respectful, polite and inclusive. It knows a lot, and always tells the truth. When prompted with an image, it does not make up facts.
62
+
63
+ # The conversation begins:""",
64
+ # """\nUser:""",
65
+ # "https://i1.pickpik.com/photos/515/906/355/kittens-cats-pet-cute-preview.jpg",
66
+ # "Describe this image.<end_of_utterance>",
67
+ # """\nAssistant: Five kittens are standing together in the center of the photograph. From the left to right, there is one orange kitten, two kittens white and black stripes, and two brown kittens with an orange head. They are in bright green grass and it looks like they are moving forward.<end_of_utterance>""",
68
+ # "\nUser:How about this image?",
69
+ # "https://cdn.pixabay.com/photo/2017/09/25/13/12/puppy-2785074_1280.jpg",
70
+ # "Can you describe it too?<end_of_utterance>",
71
+ # """\nAssistant: A dog is lying on the floor, looking at the camera. It is looking directly at you. The dog has a white body and brown patches on its face and ears. Its eyes are dark. Its nose is black, and it has long, floppy ears, white paws, long fur, big eyes.<end_of_utterance>""",
72
+ # "\nUser: What kind of breed is it?<end_of_utterance>",
73
+ # """\nAssistant: It looks like a Cavalier King Charles Spaniel. These breeds are known for their gentle, affectionate nature. Their long ears and big, brown eyes give them a sweet, expressive face. Cavaliers are small dogs weighing between 13 and 20 pounds. They have a silky, medium-length coat that comes in various colors, including black and tan, tricolor, red and white, or black.<end_of_utterance>""",
74
+ # "\nUser: What can you tell me about this breed of dogs?<end_of_utterance>",
75
+ # """\nAssistant: One specific characteristic of the Cavalier King Charles Spaniel is its friendly and affectionate nature. This breed is known for being extremely sociable and forming strong bonds with their owners. They are often described as "velcro dogs" because they love to be close to their human companions, whether it's sitting on their laps, cuddling on the couch, or simply following them around the house.<end_of_utterance>""",
76
+ # "\nUser: ghjkhjabnufs<end_of_utterance>",
77
+ # """\nAssistant: That doesn’t seem to be a word. Could you ask me another way?<end_of_utterance>""",
78
+ # "\nUser: Do you like Cavalier King Charles Spaniel?<end_of_utterance>",
79
+ # """\nAssistant: I do not have personal opinions as I’m just a computer program. However, cavaliers are known for being excellent family pets due to their gentle and patient demeanor, making them great with children and other pets. Their affectionate nature also makes them ideal therapy dogs, as they can provide comfort and emotional support to those in need.<end_of_utterance>""",
80
+ # "\nUser: How many dogs do you see in this image?",
81
+ # "https://i.dailymail.co.uk/i/pix/2011/07/01/article-2010308-0CD22A8300000578-496_634x414.jpg",
82
+ # "<end_of_utterance>",
83
+ # """\nAssistant: There is no dogs in this image. The picture shows a tennis player jumping to volley the ball.<end_of_utterance>""",
84
+ ]
85
+
86
+ API_TOKEN = os.getenv("HF_AUTH_TOKEN")
87
+ # IDEFICS_LOGO = "https://huggingface.co/spaces/HuggingFaceM4/idefics_playground/resolve/main/IDEFICS_logo.png"
88
+ BOT_AVATAR = "IDEFICS_logo.png"
89
+
90
+
91
+ # Monkey patch adapted from gradio.components.image.Image - mostly to make the `save` step optional in `pil_to_temp_file`
92
+ def hash_bytes(bytes: bytes):
93
+ sha1 = hashlib.sha1()
94
+ sha1.update(bytes)
95
+ return sha1.hexdigest()
96
+
97
+
98
+ def pil_to_temp_file(img: PIL.Image.Image, dir: str = DEFAULT_TEMP_DIR, format: str = "png") -> str:
99
+ """Save a PIL image into a temp file"""
100
+ bytes_data = processing_utils.encode_pil_to_bytes(img, format)
101
+ temp_dir = Path(dir) / hash_bytes(bytes_data)
102
+ temp_dir.mkdir(exist_ok=True, parents=True)
103
+ filename = str(temp_dir / f"image.{format}")
104
+ if not os.path.exists(filename):
105
+ img.save(filename, pnginfo=processing_utils.get_pil_metadata(img))
106
+ return filename
107
+
108
+
109
+ def add_file(file):
110
+ return file.name, gr.update(label='🖼️ Uploaded!')
111
+
112
+
113
+ # Utils to handle the image markdown display logic
114
+ def split_str_on_im_markdown(string: str) -> List[str]:
115
+ """
116
+ Extract from a string (typically the user prompt string) the potential images from markdown
117
+ Examples:
118
+ - `User:![](/file=/my_temp/chicken_on_money.png)Describe this image.` would become `["User:", "/my_temp/chicken_on_money.png", "Describe this image."]`
119
+ """
120
+ IMAGES_PATTERN = re.compile(r"!\[[^\]]*\]\((.*?)\s*(\"(?:.*[^\"])\")?\s*\)")
121
+ parts = []
122
+ cursor = 0
123
+ for pattern in IMAGES_PATTERN.finditer(string):
124
+ start = pattern.start()
125
+ if start != cursor:
126
+ parts.append(string[cursor:start])
127
+ image_url = pattern.group(1)
128
+ if image_url.startswith("/file="):
129
+ image_url = image_url[6:] # Remove the 'file=' prefix
130
+ parts.append(image_url)
131
+ cursor = pattern.end()
132
+ if cursor != len(string):
133
+ parts.append(string[cursor:])
134
+ return parts
135
+
136
+
137
+ def is_image(string: str) -> bool:
138
+ """
139
+ There are two ways for images: local image path or url.
140
+ """
141
+ return is_url(string) or string.startswith(DEFAULT_TEMP_DIR)
142
+
143
+
144
+ def is_url(string: str) -> bool:
145
+ """
146
+ Checks if the passed string contains a valid url and nothing else. e.g. if space is included it's immediately
147
+ invalidated the url
148
+ """
149
+ if " " in string:
150
+ return False
151
+ result = urlparse(string)
152
+ return all([result.scheme, result.netloc])
153
+
154
+
155
+ def isolate_images_urls(prompt_list: List) -> List:
156
+ """
157
+ Convert a full string prompt to the list format expected by the processor.
158
+ In particular, image urls (as delimited by <fake_token_around_image>) should be their own elements.
159
+ From:
160
+ ```
161
+ [
162
+ "bonjour<fake_token_around_image><image:IMG_URL><fake_token_around_image>hello",
163
+ PIL.Image.Image,
164
+ "Aurevoir",
165
+ ]
166
+ ```
167
+ to:
168
+ ```
169
+ [
170
+ "bonjour",
171
+ IMG_URL,
172
+ "hello",
173
+ PIL.Image.Image,
174
+ "Aurevoir",
175
+ ]
176
+ ```
177
+ """
178
+ linearized_list = []
179
+ for prompt in prompt_list:
180
+ # Prompt can be either a string, or a PIL image
181
+ if isinstance(prompt, PIL.Image.Image):
182
+ linearized_list.append(prompt)
183
+ elif isinstance(prompt, str):
184
+ if "<fake_token_around_image>" not in prompt:
185
+ linearized_list.append(prompt)
186
+ else:
187
+ prompt_splitted = prompt.split("<fake_token_around_image>")
188
+ for ps in prompt_splitted:
189
+ if ps == "":
190
+ continue
191
+ if ps.startswith("<image:"):
192
+ linearized_list.append(ps[7:-1])
193
+ else:
194
+ linearized_list.append(ps)
195
+ else:
196
+ raise TypeError(
197
+ f"Unrecognized type for `prompt`. Got {type(type(prompt))}. Was expecting something in [`str`,"
198
+ " `PIL.Image.Image`]"
199
+ )
200
+ return linearized_list
201
+
202
+
203
+ def fetch_images(url_list: str) -> PIL.Image.Image:
204
+ """Fetching images"""
205
+ return PROCESSOR.image_processor.fetch_images(url_list)
206
+
207
+
208
+ def handle_manual_images_in_user_prompt(user_prompt: str) -> List[str]:
209
+ """
210
+ 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
211
+ by fetching them, saving them locally and replacing the whole sub-sequence the image local path.
212
+ """
213
+ if "<fake_token_around_image>" in user_prompt:
214
+ splitted_user_prompt = isolate_images_urls([user_prompt])
215
+ resulting_user_prompt = []
216
+ for u_p in splitted_user_prompt:
217
+ if is_url(u_p):
218
+ img = fetch_images([u_p])[0]
219
+ tmp_file = pil_to_temp_file(img)
220
+ resulting_user_prompt.append(tmp_file)
221
+ else:
222
+ resulting_user_prompt.append(u_p)
223
+ return resulting_user_prompt
224
+ else:
225
+ return [user_prompt]
226
+
227
+
228
+ def prompt_list_to_markdown(prompt_list: List[str]) -> str:
229
+ """
230
+ Convert a user prompt in the list format (i.e. elements are either a PIL image or a string) into
231
+ the markdown format that is used for the chatbot history and rendering.
232
+ """
233
+ resulting_string = ""
234
+ for elem in prompt_list:
235
+ if is_image(elem):
236
+ if is_url(elem):
237
+ resulting_string += f"![]({elem})"
238
+ else:
239
+ resulting_string += f"![](/file={elem})"
240
+ else:
241
+ resulting_string += elem
242
+ return resulting_string
243
+
244
+
245
+ def prompt_list_to_model_input(prompt_list: List[str]) -> Tuple[str, List[Image.Image]]:
246
+ """
247
+ Create the final input string and image list to feed to the model's processor.
248
+ """
249
+ images = []
250
+ for idx, part in enumerate(prompt_list):
251
+ if is_image(part):
252
+ if is_url(part):
253
+ images.append(fetch_images([part])[0])
254
+ else:
255
+ images.append(Image.open(part))
256
+ prompt_list[idx] = f"{FAKE_TOK_AROUND_IMAGE}{'<image>' * IMAGE_SEQ_LEN}{FAKE_TOK_AROUND_IMAGE}"
257
+ input_text = "".join(prompt_list)
258
+ input_text = input_text.replace(FAKE_TOK_AROUND_IMAGE * 2, FAKE_TOK_AROUND_IMAGE)
259
+ input_text = BOS_TOKEN + input_text.strip()
260
+ return input_text, images
261
+
262
+
263
+ def remove_spaces_around_token(text: str) -> str:
264
+ pattern = r"\s*(<fake_token_around_image>)\s*"
265
+ replacement = r"\1"
266
+ result = re.sub(pattern, replacement, text)
267
+ return result
268
+
269
+
270
+ # Chatbot utils
271
+ def format_user_prompt_with_im_history_and_system_conditioning(
272
+ current_user_prompt_str: str, current_image: Optional[str], history: List[Tuple[str, str]]
273
+ ) -> Tuple[List[str], List[str]]:
274
+ """
275
+ Produces the resulting list that needs to go inside the processor.
276
+ It handles the potential image box input, the history and the system conditionning.
277
+ """
278
+ resulting_list = copy.deepcopy(SYSTEM_PROMPT)
279
+
280
+ # Format history
281
+ for turn in history:
282
+ user_utterance, assistant_utterance = turn
283
+ splitted_user_utterance = split_str_on_im_markdown(user_utterance)
284
+
285
+ optional_space = ""
286
+ if not is_image(splitted_user_utterance[0]):
287
+ optional_space = " "
288
+ resulting_list.append(f"\nUser:{optional_space}")
289
+ resulting_list.extend(splitted_user_utterance)
290
+ resulting_list.append(f"<end_of_utterance>\nAssistant: {assistant_utterance}")
291
+
292
+ # Format current input
293
+ current_user_prompt_str = remove_spaces_around_token(current_user_prompt_str)
294
+ if current_image is None:
295
+ if "![](" in current_user_prompt_str:
296
+ current_user_prompt_list = split_str_on_im_markdown(current_user_prompt_str)
297
+ else:
298
+ current_user_prompt_list = handle_manual_images_in_user_prompt(current_user_prompt_str)
299
+
300
+ optional_space = ""
301
+ if not is_image(current_user_prompt_list[0]):
302
+ # Check if the first element is an image (and more precisely a path to an image)
303
+ optional_space = " "
304
+ resulting_list.append(f"\nUser:{optional_space}")
305
+ resulting_list.extend(current_user_prompt_list)
306
+ resulting_list.append("<end_of_utterance>\nAssistant:")
307
+ else:
308
+ # Choosing to put the image first when the image is inputted through the UI, but this is an arbiratrary choice.
309
+ resulting_list.extend(["\nUser:", current_image, f"{current_user_prompt_str}<end_of_utterance>\nAssistant:"])
310
+ current_user_prompt_list = [current_user_prompt_str]
311
+
312
+ return resulting_list, current_user_prompt_list
313
+
314
+
315
+ textbox = gr.Textbox(
316
+ placeholder="Upload an image and send a message",
317
+ show_label=False,
318
+ # value="Describe the battle against the fierce dragons.",
319
+ visible=True,
320
+ container=False,
321
+ label="Text input",
322
+ scale=6,
323
+ )
324
+ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
325
+ gr.HTML("""<h1 align="center">🐶 IDEFICS Playground</h1>""")
326
+ # with gr.Row(variant="panel"):
327
+ # with gr.Column(scale=1):
328
+ # gr.Image(IDEFICS_LOGO, elem_id="banner-image", show_label=False, show_download_button=False)
329
+ # with gr.Column(scale=5):
330
+ # gr.HTML("""
331
+ # <p>This demo showcases <strong>IDEFICS</strong>, a open-access large visual language model. Like GPT-4, the multimodal model accepts arbitrary sequences of image and text inputs and produces text outputs. IDEFICS can answer questions about images, describe visual content, create stories grounded in multiple images, etc.</p>
332
+ # <p>IDEFICS (which stands for <strong>I</strong>mage-aware <strong>D</strong>ecoder <strong>E</strong>nhanced à la <strong>F</strong>lamingo with <strong>I</strong>nterleaved <strong>C</strong>ross-attention<strong>S</strong>) is an open-access reproduction of <a href="https://huggingface.co/papers/2204.14198">Flamingo</a>, a closed-source visual language model developed by Deepmind. IDEFICS was built solely on publicly available data and models. It is currently the only visual language model of this scale (80 billion parameters) that is available in open-access.</p>
333
+ # <p>📚 The variants available in this demo were fine-tuned on a mixture of supervised and instruction fine-tuning datasets to make the models more suitable in conversational settings. For more details, we refer to our <a href="https://huggingface.co/blog/idefics">blog post</a>.</p>
334
+ # <p>🅿️ <strong>Intended uses:</strong> This demo along with the <a href="https://huggingface.co/models?sort=trending&amp;search=HuggingFaceM4%2Fidefics">supporting models</a> are provided as research artifacts to the community. We detail misuses and out-of-scope uses <a href="https://huggingface.co/HuggingFaceM4/idefics-80b#misuse-and-out-of-scope-use">here</a>.</p>
335
+ # <p>⛔️ <strong>Limitations:</strong> The model can produce factually incorrect texts, hallucinate facts (with or without an image) and will struggle with small details in images. While the model will tend to refuse answering questionable user requests, it can produce problematic outputs (including racist, stereotypical, and disrespectful texts), in particular when prompted to do so. We encourage users to read our findings from evaluating the model for potential biases in the <a href="https://huggingface.co/HuggingFaceM4/idefics-80b#bias-evaluation">model card</a>.</p>
336
+ # """)
337
+
338
+ with gr.Row(elem_id="model_selector_row"):
339
+ model_selector = gr.Dropdown(
340
+ choices=MODELS.keys(),
341
+ value="284 - neftune - opt 18'500",
342
+ interactive=True,
343
+ show_label=False,
344
+ container=False,
345
+ label="Model",
346
+ visible=True,
347
+ )
348
+
349
+ imagebox = gr.Image(type="filepath", label="Image input", visible=False)
350
+
351
+ with gr.Row():
352
+ # def prefetch_images_in_history(user_prompt_str):
353
+ # """
354
+ # Pre-fetch the images that are passed in the chatbot default history.
355
+ # """
356
+ # return prompt_list_to_markdown(handle_manual_images_in_user_prompt(user_prompt_str))
357
+
358
+ chatbot = gr.Chatbot(
359
+ elem_id="chatbot",
360
+ label="IDEFICS",
361
+ visible=True,
362
+ height=750,
363
+ avatar_images=[None, BOT_AVATAR]
364
+ )
365
+
366
+ with gr.Group():
367
+ with gr.Row():
368
+ textbox.render()
369
+ submit_btn = gr.Button(value="▶️ Submit", visible=True)
370
+ clear_btn = gr.ClearButton([textbox, imagebox, chatbot], value="🧹 Clear")
371
+ regenerate_btn = gr.Button(value="🔄 Regenerate", visible=True)
372
+ upload_btn = gr.UploadButton("📁 Upload image", file_types=["image"])
373
+
374
+ with gr.Row():
375
+ with gr.Accordion("Advanced settings", open=False, visible=True) as parameter_row:
376
+ max_new_tokens = gr.Slider(
377
+ minimum=8,
378
+ maximum=1024,
379
+ value=512,
380
+ step=1,
381
+ interactive=True,
382
+ label="Maximum number of new tokens to generate",
383
+ )
384
+ repetition_penalty = gr.Slider(
385
+ minimum=0.01,
386
+ maximum=5.0,
387
+ value=1.0,
388
+ step=0.01,
389
+ interactive=True,
390
+ label="Repetition penalty",
391
+ info="1.0 is equivalent to no penalty",
392
+ )
393
+ decoding_strategy = gr.Radio(
394
+ [
395
+ "Greedy",
396
+ "Top P Sampling",
397
+ ],
398
+ value="Greedy",
399
+ label="Decoding strategy",
400
+ interactive=True,
401
+ info="Higher values is equivalent to sampling more low-probability tokens.",
402
+ )
403
+ temperature = gr.Slider(
404
+ minimum=0.0,
405
+ maximum=5.0,
406
+ value=0.4,
407
+ step=0.1,
408
+ interactive=True,
409
+ visible=False,
410
+ label="Sampling temperature",
411
+ info="Higher values will produce more diverse outputs.",
412
+ )
413
+ decoding_strategy.change(
414
+ fn=lambda selection: gr.Slider(
415
+ visible=(
416
+ selection in ["contrastive_sampling", "beam_sampling", "Top P Sampling", "sampling_top_k"]
417
+ )
418
+ ),
419
+ inputs=decoding_strategy,
420
+ outputs=temperature,
421
+ )
422
+ top_p = gr.Slider(
423
+ minimum=0.01,
424
+ maximum=0.99,
425
+ value=0.8,
426
+ step=0.01,
427
+ interactive=True,
428
+ visible=False,
429
+ label="Top P",
430
+ info="Higher values is equivalent to sampling more low-probability tokens.",
431
+ )
432
+ decoding_strategy.change(
433
+ fn=lambda selection: gr.Slider(visible=(selection in ["Top P Sampling"])),
434
+ inputs=decoding_strategy,
435
+ outputs=top_p,
436
+ )
437
+
438
+ @spaces.GPU(duration=180)
439
+ def model_inference(
440
+ model_selector,
441
+ user_prompt_str,
442
+ chat_history,
443
+ image,
444
+ decoding_strategy,
445
+ temperature,
446
+ max_new_tokens,
447
+ repetition_penalty,
448
+ top_p,
449
+ ):
450
+ if user_prompt_str.strip() == "" and image is None:
451
+ return "", None, chat_history
452
+
453
+ formated_prompt_list, user_prompt_list = format_user_prompt_with_im_history_and_system_conditioning(
454
+ current_user_prompt_str=user_prompt_str.strip(),
455
+ current_image=image,
456
+ history=chat_history,
457
+ )
458
+
459
+ streamer = TextIteratorStreamer(
460
+ PROCESSOR.tokenizer,
461
+ skip_prompt=True,
462
+ )
463
+
464
+ # Common parameters to all decoding strategies
465
+ # This documentation is useful to read: https://huggingface.co/docs/transformers/main/en/generation_strategies
466
+ generation_args = {
467
+ "max_new_tokens": max_new_tokens,
468
+ "repetition_penalty": repetition_penalty,
469
+ "bad_words_ids": BAD_WORDS_IDS,
470
+ "eos_token_id": EOS_WORDS_IDS,
471
+ "streamer": streamer,
472
+ }
473
+
474
+ assert decoding_strategy in [
475
+ "Greedy",
476
+ "Top P Sampling",
477
+ ]
478
+ if decoding_strategy == "Greedy":
479
+ generation_args["do_sample"] = False
480
+ elif decoding_strategy == "Top P Sampling":
481
+ generation_args["temperature"] = temperature
482
+ generation_args["do_sample"] = True
483
+ generation_args["top_p"] = top_p
484
+
485
+ if image is None:
486
+ # Case where there is no image OR the image is passed as `<fake_token_around_image><image:IMAGE_URL><fake_token_around_image>`
487
+ chat_history.append([prompt_list_to_markdown(user_prompt_list), ''])
488
+ else:
489
+ # Case where the image is passed through the Image Box.
490
+ # Convert the image into base64 for both passing it through the chat history and
491
+ # displaying the image inside the same bubble as the text.
492
+ chat_history.append(
493
+ [
494
+ f"{prompt_list_to_markdown([image] + user_prompt_list)}",
495
+ '',
496
+ ]
497
+ )
498
+
499
+ # Creating model inputs
500
+ input_text, images = prompt_list_to_model_input(formated_prompt_list)
501
+ inputs = create_model_inputs([input_text], [images])
502
+ inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
503
+ generation_args.update(inputs)
504
+
505
+ thread = Thread(
506
+ target=MODELS[model_selector].generate,
507
+ kwargs=generation_args,
508
+ )
509
+
510
+ thread.start()
511
+ acc_text = ""
512
+ for idx, text_token in enumerate(streamer):
513
+
514
+ acc_text += text_token
515
+ last_turn = chat_history.pop(-1)
516
+ last_turn[-1] += acc_text
517
+ if last_turn[-1].endswith("\nUser"):
518
+ # Safeguard: sometimes (rarely), the model won't generate the token `<end_of_utterance>` and will go directly to generating `\nUser:`
519
+ # It will thus stop the generation on `\nUser:`. But when it exits, it will have already generated `\nUser`
520
+ # This post-processing ensures that we don't have an additional `\nUser` wandering around.
521
+ last_turn[-1] = last_turn[-1][:-5]
522
+ chat_history.append(last_turn)
523
+ yield "", None, chat_history
524
+ acc_text = ""
525
+
526
+ def process_example(message, image):
527
+ """
528
+ Same as `model_inference` but in greedy mode and with the 80b-instruct.
529
+ Specifically for pre-computing the default examples.
530
+ """
531
+ model_selector = "284 - neftune - opt 18'500"
532
+ user_prompt_str = message
533
+ chat_history = []
534
+ max_new_tokens = 512
535
+
536
+ formated_prompt_list, user_prompt_list = format_user_prompt_with_im_history_and_system_conditioning(
537
+ current_user_prompt_str=user_prompt_str.strip(),
538
+ current_image=image,
539
+ history=chat_history,
540
+ )
541
+
542
+ # Common parameters to all decoding strategies
543
+ # This documentation is useful to read: https://huggingface.co/docs/transformers/main/en/generation_strategies
544
+ generation_args = {
545
+ "max_new_tokens": max_new_tokens,
546
+ "repetition_penalty": None,
547
+ "bad_words_ids": BAD_WORDS_IDS,
548
+ "eos_token_id": EOS_WORDS_IDS,
549
+ "do_sample": False,
550
+ }
551
+
552
+ if image is None:
553
+ # Case where there is no image OR the image is passed as `<fake_token_around_image><image:IMAGE_URL><fake_token_around_image>`
554
+ chat_history.append([prompt_list_to_markdown(user_prompt_list), ''])
555
+ else:
556
+ # Case where the image is passed through the Image Box.
557
+ # Convert the image into base64 for both passing it through the chat history and
558
+ # displaying the image inside the same bubble as the text.
559
+ chat_history.append(
560
+ [
561
+ f"{prompt_list_to_markdown([image] + user_prompt_list)}",
562
+ '',
563
+ ]
564
+ )
565
+
566
+ # Creating model inputs
567
+ input_text, images = prompt_list_to_model_input(formated_prompt_list)
568
+ inputs = create_model_inputs([input_text], [images])
569
+ inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
570
+ generation_args.update(inputs)
571
+
572
+ generated_ids = MODELS[model_selector].generate(**generation_args)
573
+ generated_text = PROCESSOR.batch_decode(generated_ids, skip_special_tokens=True)[0]
574
+
575
+ if generated_text.endswith("\nUser"):
576
+ generated_text = generated_text[:-5]
577
+
578
+ last_turn = chat_history.pop(-1)
579
+ last_turn[-1] += generated_text
580
+ chat_history.append(last_turn)
581
+ return "", None, chat_history
582
+
583
+ textbox.submit(
584
+ fn=model_inference,
585
+ inputs=[
586
+ model_selector,
587
+ textbox,
588
+ chatbot,
589
+ imagebox,
590
+ decoding_strategy,
591
+ temperature,
592
+ max_new_tokens,
593
+ repetition_penalty,
594
+ top_p,
595
+ ],
596
+ outputs=[textbox, imagebox, chatbot],
597
+ )
598
+ submit_btn.click(
599
+ fn=model_inference,
600
+ inputs=[
601
+ model_selector,
602
+ textbox,
603
+ chatbot,
604
+ imagebox,
605
+ decoding_strategy,
606
+ temperature,
607
+ max_new_tokens,
608
+ repetition_penalty,
609
+ top_p,
610
+ ],
611
+ outputs=[
612
+ textbox,
613
+ imagebox,
614
+ chatbot,
615
+ ],
616
+ )
617
+
618
+ def remove_last_turn(chat_history):
619
+ if len(chat_history) == 0:
620
+ return gr.Update(), gr.Update()
621
+ last_interaction = chat_history[-1]
622
+ chat_history = chat_history[:-1]
623
+ chat_update = gr.update(value=chat_history)
624
+ text_update = gr.update(value=last_interaction[0])
625
+ return chat_update, text_update
626
+
627
+ regenerate_btn.click(fn=remove_last_turn, inputs=chatbot, outputs=[chatbot, textbox]).then(
628
+ fn=model_inference,
629
+ inputs=[
630
+ model_selector,
631
+ textbox,
632
+ chatbot,
633
+ imagebox,
634
+ decoding_strategy,
635
+ temperature,
636
+ max_new_tokens,
637
+ repetition_penalty,
638
+ top_p,
639
+ ],
640
+ outputs=[
641
+ textbox,
642
+ imagebox,
643
+ chatbot,
644
+ ],
645
+ )
646
+
647
+ upload_btn.upload(add_file, [upload_btn], [imagebox, upload_btn], queue=False)
648
+ submit_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
649
+ textbox.submit(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
650
+ clear_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
651
+
652
+ # examples_path = os.path.dirname(__file__)
653
+ # gr.Examples(
654
+ # examples=[
655
+ # [
656
+ # (
657
+ # "Which famous person does the person in the image look like? Could you craft an engaging narrative"
658
+ # " featuring this character from the image as the main protagonist?"
659
+ # ),
660
+ # f"{examples_path}/example_images/obama-harry-potter.jpg",
661
+ # ],
662
+ # [
663
+ # "Can you describe the image? Do you think it's real?",
664
+ # f"{examples_path}/example_images/rabbit_force.png",
665
+ # ],
666
+ # ["Explain this meme to me.", f"{examples_path}/example_images/meme_french.jpg"],
667
+ # ["Give me a short and easy recipe for this dish.", f"{examples_path}/example_images/recipe_burger.webp"],
668
+ # [
669
+ # "I want to go somewhere similar to the one in the photo. Give me destinations and travel tips.",
670
+ # f"{examples_path}/example_images/travel_tips.jpg",
671
+ # ],
672
+ # [
673
+ # "Can you name the characters in the image and give their French names?",
674
+ # f"{examples_path}/example_images/gaulois.png",
675
+ # ],
676
+ # ["Write a complete sales ad for this product.", f"{examples_path}/example_images/product_ad.jpg"],
677
+ # [
678
+ # (
679
+ # "As an art critic AI assistant, could you describe this painting in details and make a thorough"
680
+ # " critic?"
681
+ # ),
682
+ # f"{examples_path}/example_images/art_critic.png",
683
+ # ],
684
+ # [
685
+ # "Can you tell me a very short story based on this image?",
686
+ # f"{examples_path}/example_images/chicken_on_money.png",
687
+ # ],
688
+ # ["Write 3 funny meme texts about this image.", f"{examples_path}/example_images/elon_smoking.jpg"],
689
+ # [
690
+ # "Who is in this picture? Why do people find it surprising?",
691
+ # f"{examples_path}/example_images/pope_doudoune.webp",
692
+ # ],
693
+ # ["What are the armed baguettes guarding?", f"{examples_path}/example_images/baguettes_guarding_paris.png"],
694
+ # ["What is this animal and why is it unusual?", f"{examples_path}/example_images/blue_dog.png"],
695
+ # [
696
+ # "What is this object and do you think it is horrifying?",
697
+ # f"{examples_path}/example_images/can_horror.png",
698
+ # ],
699
+ # [
700
+ # (
701
+ # "What is this sketch for? How would you make an argument to prove this sketch was made by Picasso"
702
+ # " himself?"
703
+ # ),
704
+ # f"{examples_path}/example_images/cat_sketch.png",
705
+ # ],
706
+ # ["Which celebrity does this claymation figure look like?", f"{examples_path}/example_images/kanye.jpg"],
707
+ # ["What can you tell me about the cap in this image?", f"{examples_path}/example_images/ironman_cap.png"],
708
+ # [
709
+ # "Can you write an advertisement for Coca-Cola based on this image?",
710
+ # f"{examples_path}/example_images/polar_bear_coke.png",
711
+ # ],
712
+ # [
713
+ # "What is happening in this image? Which famous personality does this person in center looks like?",
714
+ # f"{examples_path}/example_images/gandhi_selfie.jpg",
715
+ # ],
716
+ # [
717
+ # "What do you think the dog is doing and is it unusual?",
718
+ # f"{examples_path}/example_images/surfing_dog.jpg",
719
+ # ],
720
+ # ],
721
+ # inputs=[textbox, imagebox],
722
+ # outputs=[textbox, imagebox, chatbot],
723
+ # fn=process_example,
724
+ # cache_examples=False,
725
+ # examples_per_page=6,
726
+ # label=(
727
+ # "Click on any example below to get started.\nFor convenience, the model generations have been"
728
+ # " pre-computed with `idefics-80b-instruct`."
729
+ # ),
730
+ # )
731
+
732
+ demo.queue(max_size=40)
733
+ demo.launch()
playground.py DELETED
@@ -1,404 +0,0 @@
1
- import copy
2
- import os
3
- import spaces
4
- import subprocess
5
- import torch
6
-
7
- from threading import Thread
8
- from typing import List, Tuple
9
- from urllib.parse import urlparse
10
- from PIL import Image
11
-
12
- import gradio as gr
13
- from gradio_client.client import DEFAULT_TEMP_DIR
14
- from transformers import AutoProcessor, AutoModelForCausalLM, TextIteratorStreamer
15
- from transformers.image_utils import to_numpy_array, PILImageResampling, ChannelDimension
16
- from transformers.image_transforms import resize, to_channel_dimension_format
17
-
18
- subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
19
-
20
- DEVICE = torch.device("cuda")
21
- MODELS = {
22
- "282 - mix1 fixed - opt 23'000": AutoModelForCausalLM.from_pretrained(
23
- "HuggingFaceM4/idefics2",
24
- trust_remote_code=True,
25
- torch_dtype=torch.bfloat16,
26
- token=os.environ["HF_AUTH_TOKEN"],
27
- revision="a1bc6a2b0f74cde25844144f602dde2808a564d9",
28
- ).to(DEVICE),
29
- "286 - mix6 tables - opt 20'000": AutoModelForCausalLM.from_pretrained(
30
- "HuggingFaceM4/idefics2",
31
- trust_remote_code=True,
32
- torch_dtype=torch.bfloat16,
33
- token=os.environ["HF_AUTH_TOKEN"],
34
- revision="b473d49caa964991b40b79fe7cb27d51d4d023f6",
35
- ).to(DEVICE),
36
- # "285 - continued pretraining on text sft - opt 2'000": AutoModelForCausalLM.from_pretrained(
37
- # "HuggingFaceM4/idefics2",
38
- # trust_remote_code=True,
39
- # torch_dtype=torch.bfloat16,
40
- # token=os.environ["HF_AUTH_TOKEN"],
41
- # revision="b0a2a564e5dc311591886bb375e8d5a1aeaade83",
42
- # ).to(DEVICE),
43
- }
44
- PROCESSOR = AutoProcessor.from_pretrained(
45
- "HuggingFaceM4/idefics2",
46
- token=os.environ["HF_AUTH_TOKEN"],
47
- )
48
- FAKE_TOK_AROUND_IMAGE = "<fake_token_around_image>"
49
- BOS_TOKEN = PROCESSOR.tokenizer.bos_token
50
- BAD_WORDS_IDS = PROCESSOR.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids
51
- EOS_WORDS_IDS = PROCESSOR.tokenizer("<end_of_utterance>", add_special_tokens=False).input_ids + [PROCESSOR.tokenizer.eos_token_id]
52
- IMAGE_SEQ_LEN = 64#list(MODELS.values())[0].config.perceiver_config.resampler_n_latents
53
-
54
- SYSTEM_PROMPT = [
55
- # """The following is a conversation between a highly knowledgeable and intelligent visual AI assistant, called Assistant, and a human user, called User. In the following interactions, User and Assistant will converse in natural language, and Assistant will do its best to answer User’s questions. Assistant has the ability to perceive images and reason about the content of visual inputs. Assistant was built to be respectful, polite and inclusive. It knows a lot, and always tells the truth. When prompted with an image, it does not make up facts.
56
-
57
- # The conversation begins:""",
58
- # """\nUser:""",
59
- # "https://i1.pickpik.com/photos/515/906/355/kittens-cats-pet-cute-preview.jpg",
60
- # "Describe this image.<end_of_utterance>",
61
- # """\nAssistant: Five kittens are standing together in the center of the photograph. From the left to right, there is one orange kitten, two kittens white and black stripes, and two brown kittens with an orange head. They are in bright green grass and it looks like they are moving forward.<end_of_utterance>""",
62
- # "\nUser:How about this image?",
63
- # "https://cdn.pixabay.com/photo/2017/09/25/13/12/puppy-2785074_1280.jpg",
64
- # "Can you describe it too?<end_of_utterance>",
65
- # """\nAssistant: A dog is lying on the floor, looking at the camera. It is looking directly at you. The dog has a white body and brown patches on its face and ears. Its eyes are dark. Its nose is black, and it has long, floppy ears, white paws, long fur, big eyes.<end_of_utterance>""",
66
- # "\nUser: What kind of breed is it?<end_of_utterance>",
67
- # """\nAssistant: It looks like a Cavalier King Charles Spaniel. These breeds are known for their gentle, affectionate nature. Their long ears and big, brown eyes give them a sweet, expressive face. Cavaliers are small dogs weighing between 13 and 20 pounds. They have a silky, medium-length coat that comes in various colors, including black and tan, tricolor, red and white, or black.<end_of_utterance>""",
68
- # "\nUser: What can you tell me about this breed of dogs?<end_of_utterance>",
69
- # """\nAssistant: One specific characteristic of the Cavalier King Charles Spaniel is its friendly and affectionate nature. This breed is known for being extremely sociable and forming strong bonds with their owners. They are often described as "velcro dogs" because they love to be close to their human companions, whether it's sitting on their laps, cuddling on the couch, or simply following them around the house.<end_of_utterance>""",
70
- # "\nUser: ghjkhjabnufs<end_of_utterance>",
71
- # """\nAssistant: That doesn’t seem to be a word. Could you ask me another way?<end_of_utterance>""",
72
- # "\nUser: Do you like Cavalier King Charles Spaniel?<end_of_utterance>",
73
- # """\nAssistant: I do not have personal opinions as I’m just a computer program. However, cavaliers are known for being excellent family pets due to their gentle and patient demeanor, making them great with children and other pets. Their affectionate nature also makes them ideal therapy dogs, as they can provide comfort and emotional support to those in need.<end_of_utterance>""",
74
- # "\nUser: How many dogs do you see in this image?",
75
- # "https://i.dailymail.co.uk/i/pix/2011/07/01/article-2010308-0CD22A8300000578-496_634x414.jpg",
76
- # "<end_of_utterance>",
77
- # """\nAssistant: There is no dogs in this image. The picture shows a tennis player jumping to volley the ball.<end_of_utterance>""",
78
- ]
79
-
80
- API_TOKEN = os.getenv("HF_AUTH_TOKEN")
81
- # IDEFICS_LOGO = "https://huggingface.co/spaces/HuggingFaceM4/idefics_playground/resolve/main/IDEFICS_logo.png"
82
- BOT_AVATAR = "IDEFICS_logo.png"
83
-
84
-
85
- # Model processing utils - these will be handled in the model processor directly ultimately
86
- def convert_to_rgb(image):
87
- # `image.convert("RGB")` would only work for .jpg images, as it creates a wrong background
88
- # for transparent images. The call to `alpha_composite` handles this case
89
- if image.mode == "RGB":
90
- return image
91
-
92
- image_rgba = image.convert("RGBA")
93
- background = Image.new("RGBA", image_rgba.size, (255, 255, 255))
94
- alpha_composite = Image.alpha_composite(background, image_rgba)
95
- alpha_composite = alpha_composite.convert("RGB")
96
- return alpha_composite
97
-
98
-
99
- def custom_transform(x):
100
- x = convert_to_rgb(x)
101
- x = to_numpy_array(x)
102
-
103
- height, width = x.shape[:2]
104
- aspect_ratio = width / height
105
- if width >= height and width > 980:
106
- width = 980
107
- height = int(width / aspect_ratio)
108
- elif height > width and height > 980:
109
- height = 980
110
- width = int(height * aspect_ratio)
111
- width = max(width, 378)
112
- height = max(height, 378)
113
-
114
- x = resize(x, (height, width), resample=PILImageResampling.BILINEAR)
115
- x = PROCESSOR.image_processor.rescale(x, scale=1 / 255)
116
- x = PROCESSOR.image_processor.normalize(
117
- x,
118
- mean=PROCESSOR.image_processor.image_mean,
119
- std=PROCESSOR.image_processor.image_std
120
- )
121
- x = to_channel_dimension_format(x, ChannelDimension.FIRST)
122
- x = torch.tensor(x)
123
- return x
124
-
125
-
126
- def create_model_inputs(
127
- input_texts: List[str],
128
- image_lists: List[List[Image.Image]],
129
- ):
130
- """
131
- All this logic will eventually be handled inside the model processor.
132
- """
133
- inputs = PROCESSOR.tokenizer(
134
- input_texts,
135
- return_tensors="pt",
136
- add_special_tokens=False,
137
- padding=True,
138
- )
139
-
140
- output_images = [
141
- [PROCESSOR.image_processor(img, transform=custom_transform) for img in im_list]
142
- for im_list in image_lists
143
- ]
144
- total_batch_size = len(output_images)
145
- max_num_images = max([len(img_l) for img_l in output_images])
146
- if max_num_images > 0:
147
- max_height = max([i.size(2) for img_l in output_images for i in img_l])
148
- max_width = max([i.size(3) for img_l in output_images for i in img_l])
149
- padded_image_tensor = torch.zeros(total_batch_size, max_num_images, 3, max_height, max_width)
150
- padded_pixel_attention_masks = torch.zeros(
151
- total_batch_size, max_num_images, max_height, max_width, dtype=torch.bool
152
- )
153
- for batch_idx, img_l in enumerate(output_images):
154
- for img_idx, img in enumerate(img_l):
155
- im_height, im_width = img.size()[2:]
156
- padded_image_tensor[batch_idx, img_idx, :, :im_height, :im_width] = img
157
- padded_pixel_attention_masks[batch_idx, img_idx, :im_height, :im_width] = True
158
-
159
- inputs["pixel_values"] = padded_image_tensor
160
- inputs["pixel_attention_mask"] = padded_pixel_attention_masks
161
-
162
- return inputs
163
-
164
-
165
- # Chatbot utils
166
- def is_image(string: str) -> bool:
167
- """
168
- There are two ways for images: local image path or url.
169
- """
170
- return is_url(string) or string.startswith(DEFAULT_TEMP_DIR)
171
-
172
-
173
- def is_url(string: str) -> bool:
174
- """
175
- Checks if the passed string contains a valid url and nothing else. e.g. if space is included it's immediately
176
- invalidated the url
177
- """
178
- if " " in string:
179
- return False
180
- result = urlparse(string)
181
- return all([result.scheme, result.netloc])
182
-
183
-
184
- def prompt_list_to_model_input(prompt_list: List[str]) -> Tuple[str, List[Image.Image]]:
185
- """
186
- Create the final input string and image list to feed to the model.
187
- """
188
- images = []
189
- for idx, part in enumerate(prompt_list):
190
- if is_image(part):
191
- images.append(Image.open(part))
192
- prompt_list[idx] = f"{FAKE_TOK_AROUND_IMAGE}{'<image>' * IMAGE_SEQ_LEN}{FAKE_TOK_AROUND_IMAGE}"
193
- input_text = "".join(prompt_list)
194
- input_text = input_text.replace(FAKE_TOK_AROUND_IMAGE * 2, FAKE_TOK_AROUND_IMAGE)
195
- input_text = BOS_TOKEN + input_text.strip()
196
- return input_text, images
197
-
198
-
199
- def turn_is_pure_media(turn):
200
- return turn[1] is None
201
-
202
-
203
- def format_user_prompt_with_im_history_and_system_conditioning(
204
- user_prompt, chat_history
205
- ) -> List[str]:
206
- """
207
- Produces the resulting list that needs to go inside the processor.
208
- It handles the potential image(s), the history and the system conditionning.
209
- """
210
- resulting_list = copy.deepcopy(SYSTEM_PROMPT)
211
-
212
- # Format history
213
- for turn in chat_history:
214
- if turn_is_pure_media(turn):
215
- media = turn[0][0]
216
- if resulting_list == [] or (resulting_list != [] and resulting_list[-1].endswith("<end_of_utterance>")):
217
- resulting_list.append("\nUser:")
218
- resulting_list.append(media)
219
- else:
220
- user_utterance, assistant_utterance = turn
221
- if resulting_list and is_image(resulting_list[-1]): # means that previous `turn` in `chat_history` was a pure media
222
- resulting_list.append(f"{user_utterance.strip()}<end_of_utterance>\nAssistant: {assistant_utterance}<end_of_utterance>")
223
- else:
224
- resulting_list.append(f"\nUser: {user_utterance.strip()}<end_of_utterance>\nAssistant: {assistant_utterance}<end_of_utterance>")
225
-
226
- # Format current input
227
- if not user_prompt["files"]:
228
- resulting_list.append(f"\nUser: ")
229
- else:
230
- # Choosing to put the image first when the image is inputted through the UI, but this is an arbiratrary choice.
231
- resulting_list.append("\nUser:")
232
- resulting_list.extend([im["path"] for im in user_prompt["files"]])
233
- resulting_list.append(f"{user_prompt['text']}<end_of_utterance>\nAssistant:")
234
-
235
- return resulting_list
236
-
237
-
238
- @spaces.GPU(duration=180)
239
- def model_inference(
240
- user_prompt,
241
- chat_history,
242
- model_selector,
243
- decoding_strategy,
244
- temperature,
245
- max_new_tokens,
246
- repetition_penalty,
247
- top_p,
248
- ):
249
- if user_prompt["text"].strip() == "" and not user_prompt["files"]:
250
- gr.Error("Please input a query and optionally image(s).")
251
-
252
- if user_prompt["text"].strip() == "" and user_prompt["files"]:
253
- gr.Error("Please input a text query along the image(s).")
254
-
255
- for file in user_prompt["files"]:
256
- if not file["mime_type"].startswith("image/"):
257
- gr.Error("Idefics2 only supports images. Please input a valid image.")
258
-
259
- formated_prompt_list = format_user_prompt_with_im_history_and_system_conditioning(
260
- user_prompt=user_prompt,
261
- chat_history=chat_history,
262
- )
263
-
264
- streamer = TextIteratorStreamer(
265
- PROCESSOR.tokenizer,
266
- skip_prompt=True,
267
- timeout=5.,
268
- )
269
-
270
- # Common parameters to all decoding strategies
271
- # This documentation is useful to read: https://huggingface.co/docs/transformers/main/en/generation_strategies
272
- generation_args = {
273
- "max_new_tokens": max_new_tokens,
274
- "repetition_penalty": repetition_penalty,
275
- "bad_words_ids": BAD_WORDS_IDS,
276
- "eos_token_id": EOS_WORDS_IDS,
277
- "streamer": streamer,
278
- }
279
-
280
- assert decoding_strategy in [
281
- "Greedy",
282
- "Top P Sampling",
283
- ]
284
- if decoding_strategy == "Greedy":
285
- generation_args["do_sample"] = False
286
- elif decoding_strategy == "Top P Sampling":
287
- generation_args["temperature"] = temperature
288
- generation_args["do_sample"] = True
289
- generation_args["top_p"] = top_p
290
-
291
-
292
- # Creating model inputs
293
- input_text, images = prompt_list_to_model_input(formated_prompt_list)
294
- inputs = create_model_inputs([input_text], [images])
295
- inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
296
- generation_args.update(inputs)
297
-
298
- # # The regular non streaming generation mode
299
- # _ = generation_args.pop("streamer")
300
- # generated_ids = MODELS[model_selector].generate(**generation_args)
301
- # generated_text = PROCESSOR.batch_decode(generated_ids, skip_special_tokens=True)[0]
302
- # return generated_text
303
-
304
- thread = Thread(
305
- target=MODELS[model_selector].generate,
306
- kwargs=generation_args,
307
- )
308
- thread.start()
309
-
310
- print("start generating")
311
- acc_text = ""
312
- try:
313
- for text_token in streamer:
314
- acc_text += text_token
315
- yield acc_text
316
- except Exception as e:
317
- print("error")
318
- gr.Error(e)
319
- print("success")
320
-
321
-
322
- # Hyper-parameters for generation
323
- max_new_tokens = gr.Slider(
324
- minimum=8,
325
- maximum=1024,
326
- value=512,
327
- step=1,
328
- interactive=True,
329
- label="Maximum number of new tokens to generate",
330
- )
331
- repetition_penalty = gr.Slider(
332
- minimum=0.01,
333
- maximum=5.0,
334
- value=1.0,
335
- step=0.01,
336
- interactive=True,
337
- label="Repetition penalty",
338
- info="1.0 is equivalent to no penalty",
339
- )
340
- decoding_strategy = gr.Radio(
341
- [
342
- "Greedy",
343
- "Top P Sampling",
344
- ],
345
- value="Greedy",
346
- label="Decoding strategy",
347
- interactive=True,
348
- info="Higher values is equivalent to sampling more low-probability tokens.",
349
- )
350
- temperature = gr.Slider(
351
- minimum=0.0,
352
- maximum=5.0,
353
- value=0.4,
354
- step=0.1,
355
- interactive=True,
356
- label="Sampling temperature",
357
- info="Higher values will produce more diverse outputs.",
358
- )
359
- top_p = gr.Slider(
360
- minimum=0.01,
361
- maximum=0.99,
362
- value=0.8,
363
- step=0.01,
364
- interactive=True,
365
- label="Top P",
366
- info="Higher values is equivalent to sampling more low-probability tokens.",
367
- )
368
-
369
- with gr.Blocks(fill_height=True) as demo:
370
- with gr.Row(elem_id="model_selector_row"):
371
- model_selector = gr.Dropdown(
372
- choices=MODELS.keys(),
373
- value=list(MODELS.keys())[0],
374
- interactive=True,
375
- show_label=False,
376
- container=False,
377
- label="Model",
378
- visible=True,
379
- )
380
-
381
- decoding_strategy.change(
382
- fn=lambda selection: gr.Slider(
383
- visible=(
384
- selection in ["contrastive_sampling", "beam_sampling", "Top P Sampling", "sampling_top_k"]
385
- )
386
- ),
387
- inputs=decoding_strategy,
388
- outputs=temperature,
389
- )
390
- decoding_strategy.change(
391
- fn=lambda selection: gr.Slider(visible=(selection in ["Top P Sampling"])),
392
- inputs=decoding_strategy,
393
- outputs=top_p,
394
- )
395
-
396
- gr.ChatInterface(
397
- fn=model_inference,
398
- # examples=[{"text": "hello"}, {"text": "hola"}, {"text": "merhaba"}],
399
- title="Echo Bot",
400
- multimodal=True,
401
- additional_inputs=[model_selector, decoding_strategy, temperature, max_new_tokens, repetition_penalty, top_p],
402
- )
403
-
404
- demo.launch()