Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
import time
|
6 |
+
import re
|
7 |
+
import json
|
8 |
+
|
9 |
+
with gr.Blocks(title="Climate Q&A", css="style.css", theme=theme,elem_id = "main-component") as demo:
|
10 |
+
# user_id_state = gr.State([user_id])
|
11 |
+
|
12 |
+
with gr.Tab("ClimateQ&A"):
|
13 |
+
|
14 |
+
with gr.Row(elem_id="chatbot-row"):
|
15 |
+
with gr.Column(scale=2):
|
16 |
+
# state = gr.State([system_template])
|
17 |
+
chatbot = gr.Chatbot(
|
18 |
+
value=[(None,init_prompt)],
|
19 |
+
show_copy_button=True,show_label = False,elem_id="chatbot",layout = "panel",
|
20 |
+
avatar_images = (None,"https://i.ibb.co/YNyd5W2/logo4.png"),
|
21 |
+
)#,avatar_images = ("assets/logo4.png",None))
|
22 |
+
|
23 |
+
# bot.like(vote,None,None)
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
with gr.Row(elem_id = "input-message"):
|
28 |
+
textbox=gr.Textbox(placeholder="Ask me anything here!",show_label=False,scale=7,lines = 1,interactive = True,elem_id="input-textbox")
|
29 |
+
# submit = gr.Button("",elem_id = "submit-button",scale = 1,interactive = True,icon = "https://static-00.iconduck.com/assets.00/settings-icon-2048x2046-cw28eevx.png")
|
30 |
+
|
31 |
+
|
32 |
+
with gr.Column(scale=1, variant="panel",elem_id = "right-panel"):
|
33 |
+
|
34 |
+
|
35 |
+
with gr.Tabs() as tabs:
|
36 |
+
with gr.TabItem("Examples",elem_id = "tab-examples",id = 0):
|
37 |
+
|
38 |
+
examples_hidden = gr.Textbox(visible = False)
|
39 |
+
first_key = list(QUESTIONS.keys())[0]
|
40 |
+
dropdown_samples = gr.Dropdown(QUESTIONS.keys(),value = first_key,interactive = True,show_label = True,label = "Select a category of sample questions",elem_id = "dropdown-samples")
|
41 |
+
|
42 |
+
samples = []
|
43 |
+
for i,key in enumerate(QUESTIONS.keys()):
|
44 |
+
|
45 |
+
examples_visible = True if i == 0 else False
|
46 |
+
|
47 |
+
with gr.Row(visible = examples_visible) as group_examples:
|
48 |
+
|
49 |
+
examples_questions = gr.Examples(
|
50 |
+
QUESTIONS[key],
|
51 |
+
[examples_hidden],
|
52 |
+
examples_per_page=8,
|
53 |
+
run_on_click=False,
|
54 |
+
elem_id=f"examples{i}",
|
55 |
+
api_name=f"examples{i}",
|
56 |
+
# label = "Click on the example question or enter your own",
|
57 |
+
# cache_examples=True,
|
58 |
+
)
|
59 |
+
|
60 |
+
samples.append(group_examples)
|
61 |
+
|
62 |
+
|
63 |
+
with gr.Tab("Sources",elem_id = "tab-citations",id = 1):
|
64 |
+
sources_textbox = gr.HTML(show_label=False, elem_id="sources-textbox")
|
65 |
+
docs_textbox = gr.State("")
|
66 |
+
|
67 |
+
# with Modal(visible = False) as config_modal:
|
68 |
+
with gr.Tab("Configuration",elem_id = "tab-config",id = 2):
|
69 |
+
|
70 |
+
gr.Markdown("Reminder: You can talk in any language, ClimateQ&A is multi-lingual!")
|
71 |
+
|
72 |
+
|
73 |
+
dropdown_sources = gr.CheckboxGroup(
|
74 |
+
["IPCC", "IPBES","IPOS"],
|
75 |
+
label="Select source",
|
76 |
+
value=["IPCC"],
|
77 |
+
interactive=True,
|
78 |
+
)
|
79 |
+
|
80 |
+
dropdown_reports = gr.Dropdown(
|
81 |
+
POSSIBLE_REPORTS,
|
82 |
+
label="Or select specific reports",
|
83 |
+
multiselect=True,
|
84 |
+
value=None,
|
85 |
+
interactive=True,
|
86 |
+
)
|
87 |
+
|
88 |
+
dropdown_audience = gr.Dropdown(
|
89 |
+
["Children","General public","Experts"],
|
90 |
+
label="Select audience",
|
91 |
+
value="Experts",
|
92 |
+
interactive=True,
|
93 |
+
)
|
94 |
+
|
95 |
+
output_query = gr.Textbox(label="Query used for retrieval",show_label = True,elem_id = "reformulated-query",lines = 2,interactive = False)
|
96 |
+
output_language = gr.Textbox(label="Language",show_label = True,elem_id = "language",lines = 1,interactive = False)
|
97 |
+
|
98 |
+
with gr.Tab("About",elem_classes = "max-height other-tabs"):
|
99 |
+
with gr.Row():
|
100 |
+
with gr.Column(scale=1):
|
101 |
+
gr.Markdown("See more info at [https://www.oag.go.ug/](https://www.oag.go.ug/welcome)")
|
102 |
+
|
103 |
+
|
104 |
+
def start_chat(query,history):
|
105 |
+
history = history + [(query,None)]
|
106 |
+
history = [tuple(x) for x in history]
|
107 |
+
return (gr.update(interactive = False),gr.update(selected=1),history)
|
108 |
+
|
109 |
+
def finish_chat():
|
110 |
+
return (gr.update(interactive = True,value = ""))
|
111 |
+
|
112 |
+
(textbox
|
113 |
+
.submit(start_chat, [textbox,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_textbox")
|
114 |
+
.then(chat, [textbox,chatbot,dropdown_audience, dropdown_sources,dropdown_reports], [chatbot,sources_textbox,output_query,output_language,gallery_component,query_papers,keywords_papers],concurrency_limit = 8,api_name = "chat_textbox")
|
115 |
+
.then(finish_chat, None, [textbox],api_name = "finish_chat_textbox")
|
116 |
+
)
|
117 |
+
|
118 |
+
(examples_hidden
|
119 |
+
.change(start_chat, [examples_hidden,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_examples")
|
120 |
+
.then(chat, [examples_hidden,chatbot,dropdown_audience, dropdown_sources,dropdown_reports], [chatbot,sources_textbox,output_query,output_language,gallery_component,query_papers,keywords_papers],concurrency_limit = 8,api_name = "chat_examples")
|
121 |
+
.then(finish_chat, None, [textbox],api_name = "finish_chat_examples")
|
122 |
+
)
|
123 |
+
|
124 |
+
|
125 |
+
def change_sample_questions(key):
|
126 |
+
index = list(QUESTIONS.keys()).index(key)
|
127 |
+
visible_bools = [False] * len(samples)
|
128 |
+
visible_bools[index] = True
|
129 |
+
return [gr.update(visible=visible_bools[i]) for i in range(len(samples))]
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
dropdown_samples.change(change_sample_questions,dropdown_samples,samples)
|
134 |
+
|
135 |
+
demo.queue()
|
136 |
+
|
137 |
+
demo.launch()
|