jbilcke-hf HF staff commited on
Commit
fc22a1a
1 Parent(s): 04d1ea7

Update app_dialogue.py

Browse files
Files changed (1) hide show
  1. app_dialogue.py +9 -170
app_dialogue.py CHANGED
@@ -15,6 +15,7 @@ from gradio_client.client import DEFAULT_TEMP_DIR
15
  from text_generation import Client
16
  from transformers import AutoProcessor
17
 
 
18
 
19
  MODELS = [
20
  # "HuggingFaceM4/idefics-9b-instruct",
@@ -335,6 +336,7 @@ textbox = gr.Textbox(
335
  )
336
  with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
337
  gr.HTML("""<h1 align="center">🐶 IDEFICS Playground</h1>""")
 
338
  with gr.Row(variant="panel"):
339
  with gr.Column(scale=1):
340
  gr.Image(IDEFICS_LOGO, elem_id="banner-image", show_label=False, show_download_button=False)
@@ -484,6 +486,7 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
484
  )
485
 
486
  def model_inference(
 
487
  model_selector,
488
  user_prompt_str,
489
  chat_history,
@@ -494,6 +497,9 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
494
  repetition_penalty,
495
  top_p,
496
  ):
 
 
 
497
  if user_prompt_str.strip() == "" and image is None:
498
  return "", None, chat_history
499
 
@@ -639,6 +645,7 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
639
  textbox.submit(
640
  fn=model_inference,
641
  inputs=[
 
642
  model_selector,
643
  textbox,
644
  chatbot,
@@ -654,6 +661,7 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
654
  submit_btn.click(
655
  fn=model_inference,
656
  inputs=[
 
657
  model_selector,
658
  textbox,
659
  chatbot,
@@ -683,6 +691,7 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
683
  regenerate_btn.click(fn=remove_last_turn, inputs=chatbot, outputs=[chatbot, textbox]).then(
684
  fn=model_inference,
685
  inputs=[
 
686
  model_selector,
687
  textbox,
688
  chatbot,
@@ -704,176 +713,6 @@ with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
704
  submit_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
705
  textbox.submit(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
706
  clear_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
707
-
708
- # Using Flagging for saving dope and problematic examples
709
- # Dope examples flagging
710
- # dope_callback.setup(
711
- # [
712
- # model_selector,
713
- # textbox,
714
- # chatbot,
715
- # imagebox,
716
- # decoding_strategy,
717
- # temperature,
718
- # max_new_tokens,
719
- # repetition_penalty,
720
- # top_p,
721
- # ],
722
- # "gradio_dope_data_points",
723
- # )
724
- # dope_bttn.click(
725
- # lambda *args: dope_callback.flag(args),
726
- # [
727
- # model_selector,
728
- # textbox,
729
- # chatbot,
730
- # imagebox,
731
- # decoding_strategy,
732
- # temperature,
733
- # max_new_tokens,
734
- # repetition_penalty,
735
- # top_p,
736
- # ],
737
- # None,
738
- # preprocess=False,
739
- # )
740
- # # Problematic examples flagging
741
- # problematic_callback.setup(
742
- # [
743
- # model_selector,
744
- # textbox,
745
- # chatbot,
746
- # imagebox,
747
- # decoding_strategy,
748
- # temperature,
749
- # max_new_tokens,
750
- # repetition_penalty,
751
- # top_p,
752
- # ],
753
- # "gradio_problematic_data_points",
754
- # )
755
- # problematic_bttn.click(
756
- # lambda *args: problematic_callback.flag(args),
757
- # [
758
- # model_selector,
759
- # textbox,
760
- # chatbot,
761
- # imagebox,
762
- # decoding_strategy,
763
- # temperature,
764
- # max_new_tokens,
765
- # repetition_penalty,
766
- # top_p,
767
- # ],
768
- # None,
769
- # preprocess=False,
770
- # )
771
-
772
- # gr.Markdown("""## How to use?
773
-
774
- # There are two ways to provide image inputs:
775
- # - Using the image box on the left panel
776
- # - Using the inline syntax: `text<fake_token_around_image><image:URL_IMAGE><fake_token_around_image>text`
777
-
778
- # The second syntax allows inputting an arbitrary number of images.""")
779
-
780
- examples_path = os.path.dirname(__file__)
781
- gr.Examples(
782
- examples=[
783
- [
784
- (
785
- "Which famous person does the person in the image look like? Could you craft an engaging narrative"
786
- " featuring this character from the image as the main protagonist?"
787
- ),
788
- f"{examples_path}/example_images/obama-harry-potter.jpg",
789
- ],
790
- [
791
- "Can you describe the image? Do you think it's real?",
792
- f"{examples_path}/example_images/rabbit_force.png",
793
- ],
794
- ["Explain this meme to me.", f"{examples_path}/example_images/meme_french.jpg"],
795
- ["Give me a short and easy recipe for this dish.", f"{examples_path}/example_images/recipe_burger.webp"],
796
- [
797
- "I want to go somewhere similar to the one in the photo. Give me destinations and travel tips.",
798
- f"{examples_path}/example_images/travel_tips.jpg",
799
- ],
800
- [
801
- "Can you name the characters in the image and give their French names?",
802
- f"{examples_path}/example_images/gaulois.png",
803
- ],
804
- # ["Describe this image in detail.", f"{examples_path}/example_images/plant_bulb.webp"],
805
- ["Write a complete sales ad for this product.", f"{examples_path}/example_images/product_ad.jpg"],
806
- [
807
- (
808
- "As an art critic AI assistant, could you describe this painting in details and make a thorough"
809
- " critic?"
810
- ),
811
- f"{examples_path}/example_images/art_critic.png",
812
- ],
813
- [
814
- "Can you tell me a very short story based on this image?",
815
- f"{examples_path}/example_images/chicken_on_money.png",
816
- ],
817
- ["Write 3 funny meme texts about this image.", f"{examples_path}/example_images/elon_smoking.jpg"],
818
- [
819
- "Who is in this picture? Why do people find it surprising?",
820
- f"{examples_path}/example_images/pope_doudoune.webp",
821
- ],
822
- # ["<fake_token_around_image><image:https://assets.stickpng.com/images/6308b83261b3e2a522f01467.png><fake_token_around_image>Make a poem about the company in the image<fake_token_around_image><image:https://miro.medium.com/v2/resize:fit:1400/0*jvDu2oQreHn63-fJ><fake_token_around_image>organizing the Woodstock of AI event,<fake_token_around_image><image:https://nationaltoday.com/wp-content/uploads/2019/12/national-llama-day-1200x834.jpg><fake_token_around_image>and the fact they brought those to the event.", None],
823
- ["What are the armed baguettes guarding?", f"{examples_path}/example_images/baguettes_guarding_paris.png"],
824
- # ["Can you describe the image?", f"{examples_path}/example_images/bear_costume.png"],
825
- ["What is this animal and why is it unusual?", f"{examples_path}/example_images/blue_dog.png"],
826
- [
827
- "What is this object and do you think it is horrifying?",
828
- f"{examples_path}/example_images/can_horror.png",
829
- ],
830
- [
831
- (
832
- "What is this sketch for? How would you make an argument to prove this sketch was made by Picasso"
833
- " himself?"
834
- ),
835
- f"{examples_path}/example_images/cat_sketch.png",
836
- ],
837
- ["Which celebrity does this claymation figure look like?", f"{examples_path}/example_images/kanye.jpg"],
838
- # [
839
- # "Is there a celebrity look-alike in this image? What is happening to the person?",
840
- # f"{examples_path}/example_images/ryan-reynolds-borg.jpg",
841
- # ],
842
- # ["Can you describe this image in details please?", f"{examples_path}/example_images/dragons_playing.png"],
843
- ["What can you tell me about the cap in this image?", f"{examples_path}/example_images/ironman_cap.png"],
844
- [
845
- "Can you write an advertisement for Coca-Cola based on this image?",
846
- f"{examples_path}/example_images/polar_bear_coke.png",
847
- ],
848
- # [
849
- # "What is the rabbit doing in this image? Do you think this image is real?",
850
- # f"{examples_path}/example_images/rabbit_force.png",
851
- # ],
852
- # ["What is happening in this image and why is it unusual?", f"{examples_path}/example_images/ramen.png"],
853
- # [
854
- # "What I should look most forward to when I visit this place?",
855
- # f"{examples_path}/example_images/tree_fortress.jpg",
856
- # ],
857
- # ["Who is the person in the image and what is he doing?", f"{examples_path}/example_images/tom-cruise-astronaut-pegasus.jpg"],
858
- [
859
- "What is happening in this image? Which famous personality does this person in center looks like?",
860
- f"{examples_path}/example_images/gandhi_selfie.jpg",
861
- ],
862
- [
863
- "What do you think the dog is doing and is it unusual?",
864
- f"{examples_path}/example_images/surfing_dog.jpg",
865
- ],
866
- ],
867
- inputs=[textbox, imagebox],
868
- outputs=[textbox, imagebox, chatbot],
869
- fn=process_example,
870
- cache_examples=True,
871
- examples_per_page=6,
872
- label=(
873
- "Click on any example below to get started.\nFor convenience, the model generations have been"
874
- " pre-computed with `idefics-80b-instruct`."
875
- ),
876
- )
877
 
878
  demo.queue(concurrency_count=40, max_size=40)
879
  demo.launch()
 
15
  from text_generation import Client
16
  from transformers import AutoProcessor
17
 
18
+ SECRET_TOKEN = os.getenv('SECRET_TOKEN', 'default_secret')
19
 
20
  MODELS = [
21
  # "HuggingFaceM4/idefics-9b-instruct",
 
336
  )
337
  with gr.Blocks(title="IDEFICS Playground", theme=gr.themes.Base()) as demo:
338
  gr.HTML("""<h1 align="center">🐶 IDEFICS Playground</h1>""")
339
+ secret_token = gr.Textbox(label="Secret token")
340
  with gr.Row(variant="panel"):
341
  with gr.Column(scale=1):
342
  gr.Image(IDEFICS_LOGO, elem_id="banner-image", show_label=False, show_download_button=False)
 
486
  )
487
 
488
  def model_inference(
489
+ secret_token,
490
  model_selector,
491
  user_prompt_str,
492
  chat_history,
 
497
  repetition_penalty,
498
  top_p,
499
  ):
500
+ if secret_token != SECRET_TOKEN:
501
+ raise gr.Error(f'Invalid secret token. Please fork the original space if you want to use it for yourself.')
502
+
503
  if user_prompt_str.strip() == "" and image is None:
504
  return "", None, chat_history
505
 
 
645
  textbox.submit(
646
  fn=model_inference,
647
  inputs=[
648
+ secret_token,
649
  model_selector,
650
  textbox,
651
  chatbot,
 
661
  submit_btn.click(
662
  fn=model_inference,
663
  inputs=[
664
+ secret_token,
665
  model_selector,
666
  textbox,
667
  chatbot,
 
691
  regenerate_btn.click(fn=remove_last_turn, inputs=chatbot, outputs=[chatbot, textbox]).then(
692
  fn=model_inference,
693
  inputs=[
694
+ secret_token,
695
  model_selector,
696
  textbox,
697
  chatbot,
 
713
  submit_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
714
  textbox.submit(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
715
  clear_btn.click(lambda : gr.update(label='📁 Upload image', interactive=True), [], upload_btn)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
716
 
717
  demo.queue(concurrency_count=40, max_size=40)
718
  demo.launch()