VictorSanh commited on
Commit
2ee5884
1 Parent(s): 84f6ca8

fix precomputing examples

Browse files
Files changed (1) hide show
  1. app_dialogue.py +26 -2
app_dialogue.py CHANGED
@@ -1,4 +1,5 @@
1
  import copy
 
2
  import hashlib
3
  import logging
4
  import os
@@ -96,6 +97,21 @@ def add_file(file):
96
  return file.name
97
 
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  # Utils to handle the image markdown display logic
100
  def split_str_on_im_markdown(string: str) -> List[str]:
101
  """
@@ -593,8 +609,16 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
593
  ]
594
  )
595
 
 
 
 
 
 
 
 
 
596
  query = prompt_list_to_tgi_input(formated_prompt_list)
597
- generated_text = client.generate(prompt=query, **generation_args)
598
  if generated_text.endswith("\nUser"):
599
  generated_text = generated_text[:-5]
600
 
@@ -835,7 +859,7 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
835
  examples_per_page=6,
836
  label=(
837
  "Click on any example below to get started.\nFor convenience, the model generations have been"
838
- " pre-computed."
839
  ),
840
  )
841
 
 
1
  import copy
2
+ import glob
3
  import hashlib
4
  import logging
5
  import os
 
97
  return file.name
98
 
99
 
100
+ # This is a hack to make pre-computing the default examples work.
101
+ # During normal inference, we pass images as url to a local file using the method `gradio_link`
102
+ # which allows the tgi server to fetch the local image from the frontend server.
103
+ # however, we are building the space (and pre-computing is part of building the space), the frontend is not available
104
+ # and won't answer. So tgi server will try to fetch an image that is not available yet, which will result in a timeout error
105
+ # because tgi will never be able to return the generation.
106
+ # To bypass that, we pass instead the images URLs from the spaces repo.
107
+ all_images = glob.glob(f"{os.path.dirname(__file__)}/example_images/*")
108
+ DEFAULT_IMAGES_TMP_PATH_TO_URL = {}
109
+ for im_path in all_images:
110
+ H = gr.Image(im_path, visible=False, type="filepath")
111
+ tmp_filename = H.preprocess(H.value)
112
+ DEFAULT_IMAGES_TMP_PATH_TO_URL[tmp_filename] = f"https://huggingface.co/spaces/HuggingFaceM4/idefics_playground/resolve/main/example_images/{os.path.basename(im_path)}"
113
+
114
+
115
  # Utils to handle the image markdown display logic
116
  def split_str_on_im_markdown(string: str) -> List[str]:
117
  """
 
609
  ]
610
  )
611
 
612
+ # Hack - see explanation in `DEFAULT_IMAGES_TMP_PATH_TO_URL`
613
+ for idx, i in enumerate(formated_prompt_list):
614
+ if i.startswith(DEFAULT_TEMP_DIR):
615
+ for k, v in DEFAULT_IMAGES_TMP_PATH_TO_URL.items():
616
+ if k == i:
617
+ formated_prompt_list[idx] = v
618
+ break
619
+
620
  query = prompt_list_to_tgi_input(formated_prompt_list)
621
+ generated_text = client.generate(prompt=query, **generation_args).generated_text
622
  if generated_text.endswith("\nUser"):
623
  generated_text = generated_text[:-5]
624
 
 
859
  examples_per_page=6,
860
  label=(
861
  "Click on any example below to get started.\nFor convenience, the model generations have been"
862
+ " pre-computed with `idefics-80b-instruct`."
863
  ),
864
  )
865