Theo Alves Da Costa commited on
Commit
3c9e1e2
β€’
1 Parent(s): 9a16b50

Added switch tabs and raise errors

Browse files
Files changed (1) hide show
  1. app.py +100 -90
app.py CHANGED
@@ -146,6 +146,8 @@ from threading import Thread
146
  import json
147
 
148
  def answer_user(query,query_example,history):
 
 
149
  return query, history + [[query, ". . ."]]
150
 
151
  def answer_user_example(query,query_example,history):
@@ -474,6 +476,10 @@ def vote(data: gr.LikeData):
474
  print(data)
475
 
476
 
 
 
 
 
477
  with gr.Blocks(title="🌍 Climate Q&A", css="style.css", theme=theme) as demo:
478
  # user_id_state = gr.State([user_id])
479
 
@@ -497,96 +503,100 @@ with gr.Blocks(title="🌍 Climate Q&A", css="style.css", theme=theme) as demo:
497
 
498
  with gr.Column(scale=1, variant="panel",elem_id = "right-panel"):
499
 
500
- with gr.Tab("πŸ“ Examples",elem_id = "tab-examples"):
501
-
502
- examples_hidden = gr.Textbox(elem_id="hidden-message")
503
-
504
- examples_questions = gr.Examples(
505
- [
506
- "Is climate change caused by humans?",
507
- "What evidence do we have of climate change?",
508
- "What are the impacts of climate change?",
509
- "Can climate change be reversed?",
510
- "What is the difference between climate change and global warming?",
511
- "What can individuals do to address climate change?",
512
- "What are the main causes of climate change?",
513
- "What is the Paris Agreement and why is it important?",
514
- "Which industries have the highest GHG emissions?",
515
- "Is climate change a hoax created by the government or environmental organizations?",
516
- "What is the relationship between climate change and biodiversity loss?",
517
- "What is the link between gender equality and climate change?",
518
- "Is the impact of climate change really as severe as it is claimed to be?",
519
- "What is the impact of rising sea levels?",
520
- "What are the different greenhouse gases (GHG)?",
521
- "What is the warming power of methane?",
522
- "What is the jet stream?",
523
- "What is the breakdown of carbon sinks?",
524
- "How do the GHGs work ? Why does temperature increase ?",
525
- "What is the impact of global warming on ocean currents?",
526
- "How much warming is possible in 2050?",
527
- "What is the impact of climate change in Africa?",
528
- "Will climate change accelerate diseases and epidemics like COVID?",
529
- "What are the economic impacts of climate change?",
530
- "How much is the cost of inaction ?",
531
- "What is the relationship between climate change and poverty?",
532
- "What are the most effective strategies and technologies for reducing greenhouse gas (GHG) emissions?",
533
- "Is economic growth possible? What do you think about degrowth?",
534
- "Will technology save us?",
535
- "Is climate change a natural phenomenon ?",
536
- "Is climate change really happening or is it just a natural fluctuation in Earth's temperature?",
537
- "Is the scientific consensus on climate change really as strong as it is claimed to be?",
538
- ],
539
- [examples_hidden],
540
- examples_per_page=10,
541
- # cache_examples=True,
542
- )
543
-
544
- with gr.Tab("πŸ“š Citations",elem_id = "tab-citations"):
545
- sources_textbox = gr.HTML(show_label=False, elem_id="sources-textbox")
546
- docs_textbox = gr.State("")
547
-
548
- with gr.Tab("βš™οΈ Configuration",elem_id = "tab-config"):
549
-
550
- gr.Markdown("Reminder: You can talk in any language, ClimateQ&A is multi-lingual!")
551
-
552
-
553
- dropdown_sources = gr.CheckboxGroup(
554
- ["IPCC", "IPBES"],
555
- label="Select reports",
556
- value=["IPCC"],
557
- interactive=True,
558
- )
559
-
560
- dropdown_audience = gr.Dropdown(
561
- ["Children","General public","Experts"],
562
- label="Select audience",
563
- value="Experts",
564
- interactive=True,
565
- )
566
-
567
- output_query = gr.Textbox(label="Query used for retrieval",show_label = True,elem_id = "reformulated-query",lines = 2,interactive = False)
568
- output_language = gr.Textbox(label="Language",show_label = True,elem_id = "language",lines = 1,interactive = False)
569
-
570
-
571
-
572
- # textbox.submit(predict_climateqa,[textbox,bot],[None,bot,sources_textbox])
573
- (textbox
574
- .submit(answer_user, [textbox,examples_hidden, bot], [textbox, bot],queue = False)
575
- .success(fetch_sources,[textbox,dropdown_sources], [textbox,sources_textbox,docs_textbox,output_query,output_language])
576
- .success(answer_bot, [textbox,bot,docs_textbox,output_query,output_language,dropdown_audience], [textbox,bot],queue = True)
577
- .success(lambda x : textbox,[textbox],[textbox])
578
- )
579
-
580
- (examples_hidden
581
- .change(answer_user_example, [textbox,examples_hidden, bot], [textbox, bot],queue = False)
582
- # .then(disable_component, [examples_questions], [examples_questions],queue = False)
583
- .success(fetch_sources,[textbox,dropdown_sources], [textbox,sources_textbox,docs_textbox,output_query,output_language])
584
- .success(answer_bot, [textbox,bot,docs_textbox,output_query,output_language,dropdown_audience], [textbox,bot],queue=True)
585
- .success(lambda x : textbox,[textbox],[textbox])
586
- )
587
- # submit_button.click(answer_user, [textbox, bot], [textbox, bot], queue=True).then(
588
- # answer_bot, [textbox,bot,dropdown_audience,dropdown_sources], [textbox,bot,sources_textbox]
589
- # )
 
 
 
 
590
 
591
 
592
 
 
146
  import json
147
 
148
  def answer_user(query,query_example,history):
149
+ if len(query) <= 2:
150
+ raise Exception("Please ask a longer question")
151
  return query, history + [[query, ". . ."]]
152
 
153
  def answer_user_example(query,query_example,history):
 
476
  print(data)
477
 
478
 
479
+ def change_tab():
480
+ return gr.Tabs.update(selected=1)
481
+
482
+
483
  with gr.Blocks(title="🌍 Climate Q&A", css="style.css", theme=theme) as demo:
484
  # user_id_state = gr.State([user_id])
485
 
 
503
 
504
  with gr.Column(scale=1, variant="panel",elem_id = "right-panel"):
505
 
506
+
507
+ with gr.Tabs() as tabs:
508
+ with gr.TabItem("πŸ“ Examples",elem_id = "tab-examples",id = 0):
509
+
510
+ examples_hidden = gr.Textbox(elem_id="hidden-message")
511
+
512
+ examples_questions = gr.Examples(
513
+ [
514
+ "Is climate change caused by humans?",
515
+ "What evidence do we have of climate change?",
516
+ "What are the impacts of climate change?",
517
+ "Can climate change be reversed?",
518
+ "What is the difference between climate change and global warming?",
519
+ "What can individuals do to address climate change?",
520
+ "What are the main causes of climate change?",
521
+ "What is the Paris Agreement and why is it important?",
522
+ "Which industries have the highest GHG emissions?",
523
+ "Is climate change a hoax created by the government or environmental organizations?",
524
+ "What is the relationship between climate change and biodiversity loss?",
525
+ "What is the link between gender equality and climate change?",
526
+ "Is the impact of climate change really as severe as it is claimed to be?",
527
+ "What is the impact of rising sea levels?",
528
+ "What are the different greenhouse gases (GHG)?",
529
+ "What is the warming power of methane?",
530
+ "What is the jet stream?",
531
+ "What is the breakdown of carbon sinks?",
532
+ "How do the GHGs work ? Why does temperature increase ?",
533
+ "What is the impact of global warming on ocean currents?",
534
+ "How much warming is possible in 2050?",
535
+ "What is the impact of climate change in Africa?",
536
+ "Will climate change accelerate diseases and epidemics like COVID?",
537
+ "What are the economic impacts of climate change?",
538
+ "How much is the cost of inaction ?",
539
+ "What is the relationship between climate change and poverty?",
540
+ "What are the most effective strategies and technologies for reducing greenhouse gas (GHG) emissions?",
541
+ "Is economic growth possible? What do you think about degrowth?",
542
+ "Will technology save us?",
543
+ "Is climate change a natural phenomenon ?",
544
+ "Is climate change really happening or is it just a natural fluctuation in Earth's temperature?",
545
+ "Is the scientific consensus on climate change really as strong as it is claimed to be?",
546
+ ],
547
+ [examples_hidden],
548
+ examples_per_page=10,
549
+ run_on_click=False,
550
+ # cache_examples=True,
551
+ )
552
+
553
+ with gr.Tab("πŸ“š Citations",elem_id = "tab-citations",id = 1):
554
+ sources_textbox = gr.HTML(show_label=False, elem_id="sources-textbox")
555
+ docs_textbox = gr.State("")
556
+
557
+ with gr.Tab("βš™οΈ Configuration",elem_id = "tab-config",id = 2):
558
+
559
+ gr.Markdown("Reminder: You can talk in any language, ClimateQ&A is multi-lingual!")
560
+
561
+
562
+ dropdown_sources = gr.CheckboxGroup(
563
+ ["IPCC", "IPBES"],
564
+ label="Select reports",
565
+ value=["IPCC"],
566
+ interactive=True,
567
+ )
568
+
569
+ dropdown_audience = gr.Dropdown(
570
+ ["Children","General public","Experts"],
571
+ label="Select audience",
572
+ value="Experts",
573
+ interactive=True,
574
+ )
575
+
576
+ output_query = gr.Textbox(label="Query used for retrieval",show_label = True,elem_id = "reformulated-query",lines = 2,interactive = False)
577
+ output_language = gr.Textbox(label="Language",show_label = True,elem_id = "language",lines = 1,interactive = False)
578
+
579
+
580
+
581
+ # textbox.submit(predict_climateqa,[textbox,bot],[None,bot,sources_textbox])
582
+ (textbox
583
+ .submit(answer_user, [textbox,examples_hidden, bot], [textbox, bot],queue = False)
584
+ .success(change_tab,None,tabs)
585
+ .success(fetch_sources,[textbox,dropdown_sources], [textbox,sources_textbox,docs_textbox,output_query,output_language])
586
+ .success(answer_bot, [textbox,bot,docs_textbox,output_query,output_language,dropdown_audience], [textbox,bot],queue = True)
587
+ .success(lambda x : textbox,[textbox],[textbox])
588
+ )
589
+
590
+ (examples_hidden
591
+ .change(answer_user_example, [textbox,examples_hidden, bot], [textbox, bot],queue = False)
592
+ .success(change_tab,None,tabs)
593
+ .success(fetch_sources,[textbox,dropdown_sources], [textbox,sources_textbox,docs_textbox,output_query,output_language])
594
+ .success(answer_bot, [textbox,bot,docs_textbox,output_query,output_language,dropdown_audience], [textbox,bot],queue=True)
595
+ .success(lambda x : textbox,[textbox],[textbox])
596
+ )
597
+ # submit_button.click(answer_user, [textbox, bot], [textbox, bot], queue=True).then(
598
+ # answer_bot, [textbox,bot,dropdown_audience,dropdown_sources], [textbox,bot,sources_textbox]
599
+ # )
600
 
601
 
602