timeki commited on
Commit
463425a
·
1 Parent(s): b7b39b4

WIP multiple tabs

Browse files
Files changed (1) hide show
  1. app.py +223 -31
app.py CHANGED
@@ -572,20 +572,23 @@ def create_config_modal(config_open):
572
  outputs=[config_modal, config_open]
573
  )
574
 
575
- return (config_modal, dropdown_sources, dropdown_reports, dropdown_external_sources,
576
- search_only, dropdown_audience, after, output_query, output_language)
 
 
 
 
 
 
 
 
 
 
577
 
578
- # Main UI Assembly
579
- with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=theme, elem_id="main-component") as demo:
580
  # State variables
581
- chat_completed_state = gr.State(0)
582
  current_graphs = gr.State([])
583
- saved_graphs = gr.State({})
584
- new_sources_hmtl = gr.State([])
585
-
586
- config_open = gr.State(False)
587
-
588
- with gr.Tab("ClimateQ&A"):
589
  with gr.Row(elem_id="chatbot-row"):
590
  # Left column - Chat interface
591
  with gr.Column(scale=2):
@@ -620,13 +623,32 @@ with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=t
620
  "<h2>There are no graphs to be displayed at the moment. Try asking another question.</h2>",
621
  elem_id="graphs-container"
622
  )
623
- current_graphs.change(
624
- lambda x: x,
625
- inputs=[current_graphs],
626
- outputs=[graphs_container]
627
- )
628
-
629
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
630
  with gr.Tab("About", elem_classes="max-height other-tabs"):
631
  with gr.Row():
632
  with gr.Column(scale=1):
@@ -647,10 +669,50 @@ with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=t
647
  show_copy_button=True,
648
  lines=len(CITATION_TEXT.split('\n')),
649
  )
650
- # Configuration pannel
651
- config_modal, dropdown_sources, dropdown_reports, dropdown_external_sources, search_only, dropdown_audience, after, output_query, output_language = create_config_modal(config_open)
652
 
653
- # Event handlers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654
  config_button.click(
655
  fn=update_config_modal_visibility,
656
  inputs=[config_open],
@@ -659,25 +721,21 @@ with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=t
659
 
660
 
661
  (textbox
662
- .submit(start_chat, [textbox, chatbot, search_only], [textbox, tabs, chatbot, sources_raw], queue=False, api_name="start_chat_textbox")
663
  .then(chat, [textbox, chatbot, dropdown_audience, dropdown_sources, dropdown_reports, dropdown_external_sources, search_only], [chatbot, new_sources_hmtl, output_query, output_language, new_figures, current_graphs], concurrency_limit=8, api_name="chat_textbox")
664
- .then(finish_chat, None, [textbox], api_name="finish_chat_textbox")
665
  )
666
 
667
 
668
 
669
  (examples_hidden
670
- .change(start_chat, [examples_hidden, chatbot, search_only], [textbox, tabs, chatbot, sources_raw], queue=False, api_name="start_chat_examples")
671
  .then(chat, [examples_hidden, chatbot, dropdown_audience, dropdown_sources, dropdown_reports, dropdown_external_sources, search_only], [chatbot, new_sources_hmtl, output_query, output_language, new_figures, current_graphs], concurrency_limit=8, api_name="chat_textbox")
672
- .then(finish_chat, None, [textbox], api_name="finish_chat_examples")
673
  )
674
- def process_new_docs_html(new_docs, docs):
675
- if new_docs:
676
- return docs + new_docs
677
- return docs
678
- # return docs + new_docs
679
 
680
  new_sources_hmtl.change(lambda x : x, inputs = [new_sources_hmtl], outputs = [sources_textbox])
 
681
  new_figures.change(process_figures, inputs=[sources_raw, new_figures], outputs=[sources_raw, figures_cards, gallery_component])
682
 
683
  # Update sources numbers
@@ -692,7 +750,141 @@ with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=t
692
  # Search for papers
693
  textbox.submit(find_papers, [textbox, after, dropdown_external_sources], [papers_html, citations_network, papers_summary])
694
  examples_hidden.change(find_papers, [examples_hidden, after, dropdown_external_sources], [papers_html, citations_network, papers_summary])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
695
 
696
- demo.queue()
697
 
 
698
  demo.launch(ssr_mode=False)
 
572
  outputs=[config_modal, config_open]
573
  )
574
 
575
+ return {
576
+ "config_open" : config_open,
577
+ "config_modal": config_modal,
578
+ "dropdown_sources": dropdown_sources,
579
+ "dropdown_reports": dropdown_reports,
580
+ "dropdown_external_sources": dropdown_external_sources,
581
+ "search_only": search_only,
582
+ "dropdown_audience": dropdown_audience,
583
+ "after": after,
584
+ "output_query": output_query,
585
+ "output_language": output_language,
586
+ }
587
 
588
+ def cqa_tab(tab_name):
 
589
  # State variables
 
590
  current_graphs = gr.State([])
591
+ with gr.Tab(tab_name):
 
 
 
 
 
592
  with gr.Row(elem_id="chatbot-row"):
593
  # Left column - Chat interface
594
  with gr.Column(scale=2):
 
623
  "<h2>There are no graphs to be displayed at the moment. Try asking another question.</h2>",
624
  elem_id="graphs-container"
625
  )
626
+ return {
627
+ "chatbot": chatbot,
628
+ "textbox": textbox,
629
+ "tabs": tabs,
630
+ "sources_raw": sources_raw,
631
+ "new_figures": new_figures,
632
+ "current_graphs": current_graphs,
633
+ "examples_hidden": examples_hidden,
634
+ "dropdown_samples": dropdown_samples,
635
+ "samples": samples,
636
+ "sources_textbox": sources_textbox,
637
+ "figures_cards": figures_cards,
638
+ "gallery_component": gallery_component,
639
+ "config_button": config_button,
640
+ "papers_html": papers_html,
641
+ "citations_network": citations_network,
642
+ "papers_summary": papers_summary,
643
+ "tab_recommended_content": tab_recommended_content,
644
+ "tab_sources": tab_sources,
645
+ "tab_figures": tab_figures,
646
+ "tab_graphs": tab_graphs,
647
+ "tab_papers": tab_papers,
648
+ "graph_container": graphs_container
649
+ }
650
+
651
+ def about_tab():
652
  with gr.Tab("About", elem_classes="max-height other-tabs"):
653
  with gr.Row():
654
  with gr.Column(scale=1):
 
669
  show_copy_button=True,
670
  lines=len(CITATION_TEXT.split('\n')),
671
  )
 
 
672
 
673
+ def event_handling(
674
+ main_tab_components,
675
+ config_components,
676
+ tab_name="ClimateQ&A"
677
+ ):
678
+ chatbot = main_tab_components["chatbot"]
679
+ textbox = main_tab_components["textbox"]
680
+ tabs = main_tab_components["tabs"]
681
+ sources_raw = main_tab_components["sources_raw"]
682
+ new_figures = main_tab_components["new_figures"]
683
+ current_graphs = main_tab_components["current_graphs"]
684
+ examples_hidden = main_tab_components["examples_hidden"]
685
+ dropdown_samples = main_tab_components["dropdown_samples"]
686
+ samples = main_tab_components["samples"]
687
+ sources_textbox = main_tab_components["sources_textbox"]
688
+ figures_cards = main_tab_components["figures_cards"]
689
+ gallery_component = main_tab_components["gallery_component"]
690
+ config_button = main_tab_components["config_button"]
691
+ papers_html = main_tab_components["papers_html"]
692
+ citations_network = main_tab_components["citations_network"]
693
+ papers_summary = main_tab_components["papers_summary"]
694
+ tab_recommended_content = main_tab_components["tab_recommended_content"]
695
+ tab_sources = main_tab_components["tab_sources"]
696
+ tab_figures = main_tab_components["tab_figures"]
697
+ tab_graphs = main_tab_components["tab_graphs"]
698
+ tab_papers = main_tab_components["tab_papers"]
699
+ graphs_container = main_tab_components["graph_container"]
700
+
701
+ config_open = config_components["config_open"]
702
+ config_modal = config_components["config_modal"]
703
+ dropdown_sources = config_components["dropdown_sources"]
704
+ dropdown_reports = config_components["dropdown_reports"]
705
+ dropdown_external_sources = config_components["dropdown_external_sources"]
706
+ search_only = config_components["search_only"]
707
+ dropdown_audience = config_components["dropdown_audience"]
708
+ after = config_components["after"]
709
+ output_query = config_components["output_query"]
710
+ output_language = config_components["output_language"]
711
+
712
+ new_sources_hmtl = gr.State([])
713
+
714
+
715
+
716
  config_button.click(
717
  fn=update_config_modal_visibility,
718
  inputs=[config_open],
 
721
 
722
 
723
  (textbox
724
+ .submit(start_chat, [textbox, chatbot, search_only], [textbox, tabs, chatbot, sources_raw], queue=False, api_name=f"start_chat_textbox_{tab_name}")
725
  .then(chat, [textbox, chatbot, dropdown_audience, dropdown_sources, dropdown_reports, dropdown_external_sources, search_only], [chatbot, new_sources_hmtl, output_query, output_language, new_figures, current_graphs], concurrency_limit=8, api_name="chat_textbox")
726
+ .then(finish_chat, None, [textbox], api_name=f"finish_chat_textbox_{tab_name}")
727
  )
728
 
729
 
730
 
731
  (examples_hidden
732
+ .change(start_chat, [examples_hidden, chatbot, search_only], [textbox, tabs, chatbot, sources_raw], queue=False, api_name=f"start_chat_examples_{tab_name}")
733
  .then(chat, [examples_hidden, chatbot, dropdown_audience, dropdown_sources, dropdown_reports, dropdown_external_sources, search_only], [chatbot, new_sources_hmtl, output_query, output_language, new_figures, current_graphs], concurrency_limit=8, api_name="chat_textbox")
734
+ .then(finish_chat, None, [textbox], api_name=f"finish_chat_examples_{tab_name}")
735
  )
 
 
 
 
 
736
 
737
  new_sources_hmtl.change(lambda x : x, inputs = [new_sources_hmtl], outputs = [sources_textbox])
738
+ current_graphs.change(lambda x: x, inputs=[current_graphs], outputs=[graphs_container])
739
  new_figures.change(process_figures, inputs=[sources_raw, new_figures], outputs=[sources_raw, figures_cards, gallery_component])
740
 
741
  # Update sources numbers
 
750
  # Search for papers
751
  textbox.submit(find_papers, [textbox, after, dropdown_external_sources], [papers_html, citations_network, papers_summary])
752
  examples_hidden.change(find_papers, [examples_hidden, after, dropdown_external_sources], [papers_html, citations_network, papers_summary])
753
+
754
+
755
+ def main_ui():
756
+ config_open = gr.State(False)
757
+ with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=theme, elem_id="main-component") as demo:
758
+ config_components = create_config_modal(config_open)
759
+ with gr.Tabs():
760
+ cqa_components = cqa_tab(tab_name = "ClimateQ&A")
761
+ local_cqa_components = cqa_tab(tab_name = "Beta - POC Adapt'Action")
762
+
763
+ about_tab()
764
+
765
+ event_handling(cqa_components, config_components, tab_name = 'ClimateQ&A')
766
+ event_handling(local_cqa_components, config_components, tab_name = 'Beta - POC Adapt\'Action')
767
+ demo.queue()
768
+
769
+ return demo
770
+
771
+
772
+
773
+
774
+
775
+
776
+ # # Main UI Assembly
777
+ # with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=theme, elem_id="main-component") as demo:
778
+
779
+ # # State variables
780
+ # # chat_completed_state = gr.State(0)
781
+ # current_graphs = gr.State([])
782
+ # saved_graphs = gr.State({})
783
+ # new_sources_hmtl = gr.State([])
784
+
785
+ # config_open = gr.State(False)
786
+
787
+ # with gr.Tab("ClimateQ&A"):
788
+ # with gr.Row(elem_id="chatbot-row"):
789
+ # # Left column - Chat interface
790
+ # with gr.Column(scale=2):
791
+ # chatbot, textbox, config_button = create_chat_interface()
792
+
793
+ # # Right column - Content panels
794
+ # with gr.Column(scale=2, variant="panel", elem_id="right-panel"):
795
+ # with gr.Tabs(elem_id="right_panel_tab") as tabs:
796
+ # # Examples tab
797
+ # with gr.TabItem("Examples", elem_id="tab-examples", id=0):
798
+ # examples_hidden, dropdown_samples, samples = create_examples_tab()
799
+
800
+ # # Sources tab
801
+ # with gr.Tab("Sources", elem_id="tab-sources", id=1) as tab_sources:
802
+ # sources_textbox = gr.HTML(show_label=False, elem_id="sources-textbox")
803
+
804
+
805
+ # # Recommended content tab
806
+ # with gr.Tab("Recommended content", elem_id="tab-recommended_content", id=2) as tab_recommended_content:
807
+ # with gr.Tabs(elem_id="group-subtabs") as tabs_recommended_content:
808
+ # # Figures subtab
809
+ # with gr.Tab("Figures", elem_id="tab-figures", id=3) as tab_figures:
810
+ # sources_raw, new_figures, used_figures, gallery_component, figures_cards, figure_modal = create_figures_tab()
811
+
812
+ # # Papers subtab
813
+ # with gr.Tab("Papers", elem_id="tab-citations", id=4) as tab_papers:
814
+ # papers_summary, papers_html, citations_network, papers_modal = create_papers_tab()
815
+
816
+ # # Graphs subtab
817
+ # with gr.Tab("Graphs", elem_id="tab-graphs", id=5) as tab_graphs:
818
+ # graphs_container = gr.HTML(
819
+ # "<h2>There are no graphs to be displayed at the moment. Try asking another question.</h2>",
820
+ # elem_id="graphs-container"
821
+ # )
822
+
823
+
824
+
825
+ # with gr.Tab("About", elem_classes="max-height other-tabs"):
826
+ # with gr.Row():
827
+ # with gr.Column(scale=1):
828
+ # gr.Markdown(
829
+ # """
830
+ # ### More info
831
+ # - See more info at [https://climateqa.com](https://climateqa.com/docs/intro/)
832
+ # - Feedbacks on this [form](https://forms.office.com/e/1Yzgxm6jbp)
833
+
834
+ # ### Citation
835
+ # """
836
+ # )
837
+ # with gr.Accordion(CITATION_LABEL, elem_id="citation", open=False):
838
+ # gr.Textbox(
839
+ # value=CITATION_TEXT,
840
+ # label="",
841
+ # interactive=False,
842
+ # show_copy_button=True,
843
+ # lines=len(CITATION_TEXT.split('\n')),
844
+ # )
845
+ # # Configuration pannel
846
+ # config_modal, dropdown_sources, dropdown_reports, dropdown_external_sources, search_only, dropdown_audience, after, output_query, output_language = create_config_modal(config_open)
847
+
848
+ # # Event handlers
849
+ # config_button.click(
850
+ # fn=update_config_modal_visibility,
851
+ # inputs=[config_open],
852
+ # outputs=[config_modal, config_open]
853
+ # )
854
+
855
+
856
+ # (textbox
857
+ # .submit(start_chat, [textbox, chatbot, search_only], [textbox, tabs, chatbot, sources_raw], queue=False, api_name="start_chat_textbox")
858
+ # .then(chat, [textbox, chatbot, dropdown_audience, dropdown_sources, dropdown_reports, dropdown_external_sources, search_only], [chatbot, new_sources_hmtl, output_query, output_language, new_figures, current_graphs], concurrency_limit=8, api_name="chat_textbox")
859
+ # .then(finish_chat, None, [textbox], api_name="finish_chat_textbox")
860
+ # )
861
+
862
+
863
+
864
+ # (examples_hidden
865
+ # .change(start_chat, [examples_hidden, chatbot, search_only], [textbox, tabs, chatbot, sources_raw], queue=False, api_name="start_chat_examples")
866
+ # .then(chat, [examples_hidden, chatbot, dropdown_audience, dropdown_sources, dropdown_reports, dropdown_external_sources, search_only], [chatbot, new_sources_hmtl, output_query, output_language, new_figures, current_graphs], concurrency_limit=8, api_name="chat_textbox")
867
+ # .then(finish_chat, None, [textbox], api_name="finish_chat_examples")
868
+ # )
869
+
870
+ # new_sources_hmtl.change(lambda x : x, inputs = [new_sources_hmtl], outputs = [sources_textbox])
871
+ # new_figures.change(process_figures, inputs=[sources_raw, new_figures], outputs=[sources_raw, figures_cards, gallery_component])
872
+ # current_graphs.change(lambda x: x, inputs=[current_graphs], outputs=[graphs_container])
873
+
874
+ # # Update sources numbers
875
+ # sources_textbox.change(update_sources_number_display, [sources_textbox, figures_cards, current_graphs, papers_html], [tab_recommended_content, tab_sources, tab_figures, tab_graphs, tab_papers])
876
+ # figures_cards.change(update_sources_number_display, [sources_textbox, figures_cards, current_graphs, papers_html], [tab_recommended_content, tab_sources, tab_figures, tab_graphs, tab_papers])
877
+ # current_graphs.change(update_sources_number_display, [sources_textbox, figures_cards, current_graphs, papers_html], [tab_recommended_content, tab_sources, tab_figures, tab_graphs, tab_papers])
878
+ # papers_html.change(update_sources_number_display, [sources_textbox, figures_cards, current_graphs, papers_html], [tab_recommended_content, tab_sources, tab_figures, tab_graphs, tab_papers])
879
+
880
+ # # Other questions examples
881
+ # dropdown_samples.change(change_sample_questions, dropdown_samples, samples)
882
+
883
+ # # Search for papers
884
+ # textbox.submit(find_papers, [textbox, after, dropdown_external_sources], [papers_html, citations_network, papers_summary])
885
+ # examples_hidden.change(find_papers, [examples_hidden, after, dropdown_external_sources], [papers_html, citations_network, papers_summary])
886
 
887
+ # demo.queue()
888
 
889
+ demo = main_ui()
890
  demo.launch(ssr_mode=False)