ASureevaA commited on
Commit
fb68e9f
·
1 Parent(s): 0ef80ce

fix image q

Browse files
Files changed (1) hide show
  1. app.py +237 -250
app.py CHANGED
@@ -296,7 +296,7 @@ def synthesize_speech(text_value: str, model_key: str):
296
  )
297
  return file_object.name
298
 
299
- raise ValueError(f"Неизвестная TTS модель: {model_key}")
300
 
301
 
302
 
@@ -599,35 +599,30 @@ def parse_point_coordinates_text(coordinates_text: str) -> List[List[int]]:
599
 
600
  def build_interface():
601
  with gr.Blocks(title="Multimodal AI Demo", theme=gr.themes.Soft()) as demo_block:
602
- gr.Markdown("#Мультимодальные AI модели")
603
- gr.Markdown(
604
- "Демонстрация различных задач компьютерного зрения "
605
- "и обработки звука с использованием Hugging Face Transformers",
606
- )
607
 
608
  with gr.Tab("Классификация аудио"):
609
- gr.Markdown("## Audio Classification")
610
  with gr.Row():
611
- with gr.Column():
612
- audio_input_component = gr.Audio(
613
- label="Загрузите аудиофайл",
614
- type="filepath",
615
- )
616
- audio_model_selector = gr.Dropdown(
617
- choices=["audio_classifier", "emotion_classifier"],
618
- label="Выберите модель",
619
- value="audio_classifier",
620
- info=(
621
- "audio_classifier - общая классификация (AST), "
622
- "emotion_classifier - эмоции в речи (HuBERT ER)"
623
- ),
624
- )
625
- audio_classify_button = gr.Button("Классифицировать")
626
- with gr.Column():
627
- audio_output_component = gr.Textbox(
628
- label="Результаты классификации",
629
- lines=10,
630
- )
631
 
632
  audio_classify_button.click(
633
  fn=classify_audio_file,
@@ -635,25 +630,24 @@ def build_interface():
635
  outputs=audio_output_component,
636
  )
637
 
638
- with gr.Tab("Zero-Shot аудио (CLAP)"):
639
- gr.Markdown("## Zero-Shot Audio Classification (CLAP)")
640
  with gr.Row():
641
- with gr.Column():
642
- clap_audio_input_component = gr.Audio(
643
- label="Загрузите аудиофайл",
644
- type="filepath",
645
- )
646
- clap_label_texts_component = gr.Textbox(
647
- label="Кандидатные метки (через запятую)",
648
- placeholder="лай собаки, шум дождя, музыка, разговор",
649
- lines=2,
650
- )
651
- clap_button = gr.Button("Классифицировать CLAP")
652
- with gr.Column():
653
- clap_output_component = gr.Textbox(
654
- label="Результаты zero-shot классификации",
655
- lines=10,
656
- )
657
 
658
  clap_button.click(
659
  fn=classify_audio_zero_shot_clap,
@@ -662,28 +656,27 @@ def build_interface():
662
  )
663
 
664
  with gr.Tab("Распознавание речи"):
665
- gr.Markdown("## Automatic Speech Recognition (ASR)")
666
  with gr.Row():
667
- with gr.Column():
668
- asr_audio_input_component = gr.Audio(
669
- label="Загрузите аудио с речью",
670
- type="filepath",
671
- )
672
- asr_model_selector = gr.Dropdown(
673
- choices=["whisper", "wav2vec2"],
674
- label="Выберите модель",
675
- value="whisper",
676
- info=(
677
- "whisper - distil-whisper/distil-small.en (модель из курса, EN),\n"
678
- "wav2vec2 - openai/whisper-small (альтернатива, мультиязычная)"
679
- ),
680
- )
681
- asr_button = gr.Button("Транскрибировать")
682
- with gr.Column():
683
- asr_output_component = gr.Textbox(
684
- label="Транскрипция",
685
- lines=5,
686
- )
687
 
688
  asr_button.click(
689
  fn=recognize_speech,
@@ -693,27 +686,26 @@ def build_interface():
693
  with gr.Tab("Синтез речи"):
694
  gr.Markdown("## Text-to-Speech")
695
  with gr.Row():
696
- with gr.Column():
697
- tts_text_component = gr.Textbox(
698
- label="Введите текст для синтеза",
699
- placeholder="Введите текст на русском или английском языке...",
700
- lines=3,
701
- )
702
- tts_model_selector = gr.Dropdown(
703
- choices=["vits-ljs", "Google TTS"],
704
- label="Выберите модель",
705
- value="vits-ljs",
706
- info=(
707
- "kakao-enterprise/vits-ljs"
708
- "Google TTS"
709
- ),
710
- )
711
- tts_button = gr.Button("Синтезировать речь")
712
- with gr.Column():
713
- tts_audio_output_component = gr.Audio(
714
- label="Синтезированная речь",
715
- type="filepath",
716
- )
717
 
718
  tts_button.click(
719
  fn=synthesize_speech,
@@ -722,30 +714,29 @@ def build_interface():
722
  )
723
 
724
  with gr.Tab("Детекция объектов"):
725
- gr.Markdown("## Object Detection")
726
  with gr.Row():
727
- with gr.Column():
728
- object_input_image = gr.Image(
729
- label="Загрузите изображение",
730
- type="pil",
731
- )
732
- object_model_selector = gr.Dropdown(
733
- choices=[
734
- "object_detection_conditional_detr",
735
- "object_detection_yolos_small",
736
- ],
737
- label="Модель детекции",
738
- value="object_detection_conditional_detr",
739
- info=(
740
- "object_detection_conditional_detr - microsoft/conditional-detr-resnet-50\n"
741
- "object_detection_yolos_small - hustvl/yolos-small"
742
- ),
743
- )
744
- object_detect_button = gr.Button("Обнаружить объекты")
745
- with gr.Column():
746
- object_output_image = gr.Image(
747
- label="Результат детекции",
748
- )
749
 
750
  object_detect_button.click(
751
  fn=detect_objects_on_image,
@@ -754,18 +745,17 @@ def build_interface():
754
  )
755
 
756
  with gr.Tab("Сегментация"):
757
- gr.Markdown("## Image Segmentation (SegFormer)")
758
  with gr.Row():
759
- with gr.Column():
760
- segmentation_input_image = gr.Image(
761
- label="Загрузите изображение",
762
- type="pil",
763
- )
764
- segmentation_button = gr.Button("Сегментировать")
765
- with gr.Column():
766
- segmentation_output_image = gr.Image(
767
- label="Маска сегментации",
768
- )
769
 
770
  segmentation_button.click(
771
  fn=segment_image,
@@ -773,19 +763,19 @@ def build_interface():
773
  outputs=segmentation_output_image,
774
  )
775
 
776
- with gr.Tab("Глубина (Depth Estimation)"):
777
- gr.Markdown("## Depth Estimation (DPT)")
778
  with gr.Row():
779
- with gr.Column():
780
- depth_input_image = gr.Image(
781
- label="Загрузите изображение",
782
- type="pil",
783
- )
784
- depth_button = gr.Button("Оценить глубину")
785
- with gr.Column():
786
- depth_output_image = gr.Image(
787
- label="Карта глубины",
788
- )
789
 
790
  depth_button.click(
791
  fn=estimate_image_depth,
@@ -794,31 +784,30 @@ def build_interface():
794
  )
795
 
796
  with gr.Tab("Описание изображений"):
797
- gr.Markdown("## Image Captioning")
798
  with gr.Row():
799
- with gr.Column():
800
- caption_input_image = gr.Image(
801
- label="Загрузите изображение",
802
- type="pil",
803
- )
804
- caption_model_selector = gr.Dropdown(
805
- choices=[
806
- "captioning_blip_base",
807
- "captioning_blip_large",
808
- ],
809
- label="Модель captioning",
810
- value="captioning_blip_base",
811
- info=(
812
- "captioning_blip_base - Salesforce/blip-image-captioning-base (курс)\n"
813
- "captioning_blip_large - Salesforce/blip-image-captioning-large (альтернатива)"
814
- ),
815
- )
816
- caption_button = gr.Button("Сгенерировать описание")
817
- with gr.Column():
818
- caption_output_text = gr.Textbox(
819
- label="Описание изображения",
820
- lines=3,
821
- )
822
 
823
  caption_button.click(
824
  fn=generate_image_caption,
@@ -829,34 +818,33 @@ def build_interface():
829
  with gr.Tab("Визуальные вопросы"):
830
  gr.Markdown("## Visual Question Answering")
831
  with gr.Row():
832
- with gr.Column():
833
- vqa_input_image = gr.Image(
834
- label="Загрузите изображение",
835
- type="pil",
836
- )
837
- vqa_question_text = gr.Textbox(
838
- label="Вопрос об изображении",
839
- placeholder="Что происходит на этом изображении?",
840
- lines=2,
841
- )
842
- vqa_model_selector = gr.Dropdown(
843
- choices=[
844
- "vqa_blip_base",
845
- "vqa_vilt_b32",
846
- ],
847
- label="Модель VQA",
848
- value="vqa_blip_base",
849
- info=(
850
- "vqa_blip_base - Salesforce/blip-vqa-base (курс)\n"
851
- "vqa_vilt_b32 - dandelin/vilt-b32-finetuned-vqa (альтернатива)"
852
- ),
853
- )
854
- vqa_button = gr.Button("Ответить на вопрос")
855
- with gr.Column():
856
- vqa_output_text = gr.Textbox(
857
- label="Ответ",
858
- lines=3,
859
- )
860
 
861
  vqa_button.click(
862
  fn=answer_visual_question,
@@ -865,36 +853,35 @@ def build_interface():
865
  )
866
 
867
  with gr.Tab("Zero-Shot классификация"):
868
- gr.Markdown("## Zero-Shot Image Classification")
869
  with gr.Row():
870
- with gr.Column():
871
- zero_shot_input_image = gr.Image(
872
- label="Загрузите изображение",
873
- type="pil",
874
- )
875
- zero_shot_classes_text = gr.Textbox(
876
- label="Классы для классификации (через запятую)",
877
- placeholder="человек, машина, дерево, здание, животное",
878
- lines=2,
879
- )
880
- clip_model_selector = gr.Dropdown(
881
- choices=[
882
- "clip_large_patch14",
883
- "clip_base_patch32",
884
- ],
885
- label="CLIP модель",
886
- value="clip_large_patch14",
887
- info=(
888
- "clip_large_patch14 - openai/clip-vit-large-patch14 (курс)\n"
889
- "clip_base_patch32 - openai/clip-vit-base-patch32 (альтернатива)"
890
- ),
891
- )
892
- zero_shot_button = gr.Button("Классифицировать")
893
- with gr.Column():
894
- zero_shot_output_text = gr.Textbox(
895
- label="Результаты классификации",
896
- lines=10,
897
- )
898
 
899
  zero_shot_button.click(
900
  fn=perform_zero_shot_classification,
@@ -903,40 +890,40 @@ def build_interface():
903
  )
904
 
905
  with gr.Tab("Поиск изображений"):
906
- gr.Markdown("## Image Retrieval")
907
  with gr.Row():
908
- with gr.Column():
909
- retrieval_dir = gr.File(
910
- label="Загрузите папку с изображениями",
911
- file_count="directory",
912
- file_types=["image"],
913
- type="filepath",
914
- )
915
- retrieval_query_text = gr.Textbox(
916
- label="Текстовый запрос",
917
- placeholder="описание того, что вы ищете...",
918
- lines=2,
919
- )
920
- retrieval_clip_selector = gr.Dropdown(
921
- choices=[
922
- "clip_large_patch14",
923
- "clip_base_patch32",
924
- ],
925
- label="CLIP модель",
926
- value="clip_large_patch14",
927
- info=(
928
- "clip_large_patch14 - openai/clip-vit-large-patch14 (курс)\n"
929
- "clip_base_patch32 - openai/clip-vit-base-patch32 (альтернатива)"
930
- ),
931
- )
932
- retrieval_button = gr.Button("Найти изображение")
933
- with gr.Column():
934
- retrieval_output_text = gr.Textbox(
935
- label="Результат поиска",
936
- )
937
- retrieval_output_image = gr.Image(
938
- label="Найденное изображение",
939
- )
940
 
941
  retrieval_button.click(
942
  fn=retrieve_best_image,
@@ -948,9 +935,9 @@ def build_interface():
948
  gr.Markdown("### Задачи:")
949
  gr.Markdown(
950
  """
951
- - Аудио: классификация (supervised и zero-shot через CLAP), распознавание речи, синтез речи
952
- - Компьютерное зрение: детекция объектов, семантическая сегментация (SegFormer), оценка глубины (DPT), интерактивная сегментация по точкам (SlimSAM), генерация описаний изображений
953
- - Мультимодальные задачи: визуальные вопросы (VQA), zero-shot классификация изображений, поиск по изображениям по текстовому запросу
954
  """
955
  )
956
  return demo_block
 
296
  )
297
  return file_object.name
298
 
299
+ raise ValueError(f"Неизвестная модель: {model_key}")
300
 
301
 
302
 
 
599
 
600
  def build_interface():
601
  with gr.Blocks(title="Multimodal AI Demo", theme=gr.themes.Soft()) as demo_block:
602
+ gr.Markdown("# AI модели")
 
 
 
 
603
 
604
  with gr.Tab("Классификация аудио"):
605
+ gr.Markdown("## Классификация аудио")
606
  with gr.Row():
607
+ audio_input_component = gr.Audio(
608
+ label="Загрузите аудиофайл",
609
+ type="filepath",
610
+ )
611
+ audio_model_selector = gr.Dropdown(
612
+ choices=["audio_classifier", "emotion_classifier"],
613
+ label="Выберите модель",
614
+ value="audio_classifier",
615
+ info=(
616
+ "audio_classifier - общая классификация (курс)"
617
+ "emotion_classifier - эмоции в речи "
618
+ ),
619
+ )
620
+ audio_classify_button = gr.Button("Применить")
621
+
622
+ audio_output_component = gr.Textbox(
623
+ label="Результаты классификации",
624
+ lines=10,
625
+ )
 
626
 
627
  audio_classify_button.click(
628
  fn=classify_audio_file,
 
630
  outputs=audio_output_component,
631
  )
632
 
633
+ with gr.Tab("Zero-Shot аудио"):
634
+ gr.Markdown("## Zero-Shot аудио классификатор")
635
  with gr.Row():
636
+ clap_audio_input_component = gr.Audio(
637
+ label="Загрузите аудиофайл",
638
+ type="filepath",
639
+ )
640
+ clap_label_texts_component = gr.Textbox(
641
+ label="Кандидатные метки (через запятую)",
642
+ placeholder="лай собаки, шум дождя, музыка, разговор",
643
+ lines=2,
644
+ )
645
+ clap_button = gr.Button("Применить")
646
+
647
+ clap_output_component = gr.Textbox(
648
+ label="Результаты zero-shot классификации",
649
+ lines=10,
650
+ )
 
651
 
652
  clap_button.click(
653
  fn=classify_audio_zero_shot_clap,
 
656
  )
657
 
658
  with gr.Tab("Распознавание речи"):
659
+ gr.Markdown("## Распознавание реч")
660
  with gr.Row():
661
+ asr_audio_input_component = gr.Audio(
662
+ label="Загрузите аудио с речью",
663
+ type="filepath",
664
+ )
665
+ asr_model_selector = gr.Dropdown(
666
+ choices=["whisper", "wav2vec2"],
667
+ label="Выберите модель",
668
+ value="whisper",
669
+ info=(
670
+ "whisper - distil-whisper/distil-small.en (курс),\n"
671
+ "wav2vec2 - openai/whisper-small"
672
+ ),
673
+ )
674
+ asr_button = gr.Button("Применить")
675
+
676
+ asr_output_component = gr.Textbox(
677
+ label="Транскрипция",
678
+ lines=5,
679
+ )
 
680
 
681
  asr_button.click(
682
  fn=recognize_speech,
 
686
  with gr.Tab("Синтез речи"):
687
  gr.Markdown("## Text-to-Speech")
688
  with gr.Row():
689
+ tts_text_component = gr.Textbox(
690
+ label="Введите текст для синтеза",
691
+ placeholder="Введите текст на русском или английском языке...",
692
+ lines=3,
693
+ )
694
+ tts_model_selector = gr.Dropdown(
695
+ choices=["vits-ljs", "Google TTS"],
696
+ label="Выберите модель",
697
+ value="vits-ljs",
698
+ info=(
699
+ "kakao-enterprise/vits-ljs\n"
700
+ "Google TTS"
701
+ ),
702
+ )
703
+ tts_button = gr.Button("Применить")
704
+
705
+ tts_audio_output_component = gr.Audio(
706
+ label="Синтезированная речь",
707
+ type="filepath",
708
+ )
 
709
 
710
  tts_button.click(
711
  fn=synthesize_speech,
 
714
  )
715
 
716
  with gr.Tab("Детекция объектов"):
717
+ gr.Markdown("## Детекция объектов")
718
  with gr.Row():
719
+ object_input_image = gr.Image(
720
+ label="Загрузите изображение",
721
+ type="pil",
722
+ )
723
+ object_model_selector = gr.Dropdown(
724
+ choices=[
725
+ "object_detection_conditional_detr",
726
+ "object_detection_yolos_small",
727
+ ],
728
+ label="Модель",
729
+ value="object_detection_conditional_detr",
730
+ info=(
731
+ "object_detection_conditional_detr - microsoft/conditional-detr-resnet-50\n"
732
+ "object_detection_yolos_small - hustvl/yolos-small"
733
+ ),
734
+ )
735
+ object_detect_button = gr.Button("Применить")
736
+
737
+ object_output_image = gr.Image(
738
+ label="Результат",
739
+ )
 
740
 
741
  object_detect_button.click(
742
  fn=detect_objects_on_image,
 
745
  )
746
 
747
  with gr.Tab("Сегментация"):
748
+ gr.Markdown("## Сегментация")
749
  with gr.Row():
750
+ segmentation_input_image = gr.Image(
751
+ label="Загрузите изображение",
752
+ type="pil",
753
+ )
754
+ segmentation_button = gr.Button("Применить")
755
+
756
+ segmentation_output_image = gr.Image(
757
+ label="Маска",
758
+ )
 
759
 
760
  segmentation_button.click(
761
  fn=segment_image,
 
763
  outputs=segmentation_output_image,
764
  )
765
 
766
+ with gr.Tab("Глубина"):
767
+ gr.Markdown("## Глубина (Depth Estimation)")
768
  with gr.Row():
769
+
770
+ depth_input_image = gr.Image(
771
+ label="Загрузите изображение",
772
+ type="pil",
773
+ )
774
+ depth_button = gr.Button("Применить")
775
+
776
+ depth_output_image = gr.Image(
777
+ label="Глубины",
778
+ )
779
 
780
  depth_button.click(
781
  fn=estimate_image_depth,
 
784
  )
785
 
786
  with gr.Tab("Описание изображений"):
787
+ gr.Markdown("## Описание изображений")
788
  with gr.Row():
789
+ caption_input_image = gr.Image(
790
+ label="Загрузите изображение",
791
+ type="pil",
792
+ )
793
+ caption_model_selector = gr.Dropdown(
794
+ choices=[
795
+ "captioning_blip_base",
796
+ "captioning_blip_large",
797
+ ],
798
+ label="Модель",
799
+ value="captioning_blip_base",
800
+ info=(
801
+ "captioning_blip_base - Salesforce/blip-image-captioning-base (курс)\n"
802
+ "captioning_blip_large - Salesforce/blip-image-captioning-large"
803
+ ),
804
+ )
805
+ caption_button = gr.Button("Применить")
806
+
807
+ caption_output_text = gr.Textbox(
808
+ label="Описание изображения",
809
+ lines=3,
810
+ )
 
811
 
812
  caption_button.click(
813
  fn=generate_image_caption,
 
818
  with gr.Tab("Визуальные вопросы"):
819
  gr.Markdown("## Visual Question Answering")
820
  with gr.Row():
821
+ vqa_input_image = gr.Image(
822
+ label="Загрузите изображение",
823
+ type="pil",
824
+ )
825
+ vqa_question_text = gr.Textbox(
826
+ label="Вопрос",
827
+ placeholder="Вопрос",
828
+ lines=2,
829
+ )
830
+ vqa_model_selector = gr.Dropdown(
831
+ choices=[
832
+ "vqa_blip_base",
833
+ "vqa_vilt_b32",
834
+ ],
835
+ label="Модель",
836
+ value="vqa_blip_base",
837
+ info=(
838
+ "vqa_blip_base - Salesforce/blip-vqa-base (курс)\n"
839
+ "vqa_vilt_b32 - dandelin/vilt-b32-finetuned-vqa"
840
+ ),
841
+ )
842
+ vqa_button = gr.Button("Ответить на вопрос")
843
+
844
+ vqa_output_text = gr.Textbox(
845
+ label="Ответ",
846
+ lines=3,
847
+ )
 
848
 
849
  vqa_button.click(
850
  fn=answer_visual_question,
 
853
  )
854
 
855
  with gr.Tab("Zero-Shot классификация"):
856
+ gr.Markdown("## Zero-Shot классификация")
857
  with gr.Row():
858
+ zero_shot_input_image = gr.Image(
859
+ label="Загрузите изображение",
860
+ type="pil",
861
+ )
862
+ zero_shot_classes_text = gr.Textbox(
863
+ label="Классы для классификации (через запятую)",
864
+ placeholder="человек, машина, дерево, здание, животное",
865
+ lines=2,
866
+ )
867
+ clip_model_selector = gr.Dropdown(
868
+ choices=[
869
+ "clip_large_patch14",
870
+ "clip_base_patch32",
871
+ ],
872
+ label="модель",
873
+ value="clip_large_patch14",
874
+ info=(
875
+ "clip_large_patch14 - openai/clip-vit-large-patch14 (курс)\n"
876
+ "clip_base_patch32 - openai/clip-vit-base-patch32"
877
+ ),
878
+ )
879
+ zero_shot_button = gr.Button("Применить")
880
+
881
+ zero_shot_output_text = gr.Textbox(
882
+ label="Результаты",
883
+ lines=10,
884
+ )
 
885
 
886
  zero_shot_button.click(
887
  fn=perform_zero_shot_classification,
 
890
  )
891
 
892
  with gr.Tab("Поиск изображений"):
893
+ gr.Markdown("## Поиск изображений")
894
  with gr.Row():
895
+
896
+ retrieval_dir = gr.File(
897
+ label="Загрузите папку с изображениями",
898
+ file_count="directory",
899
+ file_types=["image"],
900
+ type="filepath",
901
+ )
902
+ retrieval_query_text = gr.Textbox(
903
+ label="Текстовый запрос",
904
+ placeholder="описание того, что вы ищете...",
905
+ lines=2,
906
+ )
907
+ retrieval_clip_selector = gr.Dropdown(
908
+ choices=[
909
+ "clip_large_patch14",
910
+ "clip_base_patch32",
911
+ ],
912
+ label="модель",
913
+ value="clip_large_patch14",
914
+ info=(
915
+ "clip_large_patch14 - openai/clip-vit-large-patch14 (курс)\n"
916
+ "clip_base_patch32 - openai/clip-vit-base-patch32 (альтернатива)"
917
+ ),
918
+ )
919
+ retrieval_button = gr.Button("Поиск")
920
+
921
+ retrieval_output_text = gr.Textbox(
922
+ label="Результат",
923
+ )
924
+ retrieval_output_image = gr.Image(
925
+ label="Наиболее подходящее изображение",
926
+ )
927
 
928
  retrieval_button.click(
929
  fn=retrieve_best_image,
 
935
  gr.Markdown("### Задачи:")
936
  gr.Markdown(
937
  """
938
+ - Аудио: классификация, распознавание речи, синтез речи
939
+ - Компьютерное зрение: детекция объектов, сегментация, оценка глубины, генерация описаний изображений
940
+ - Мультимодальные задачи: вопросы к изображению, zero-shot классификация изображений, поиск по изображениям по текстовому запросу
941
  """
942
  )
943
  return demo_block