Added CSS for sources and IPBES support
Browse files
app.py
CHANGED
@@ -74,6 +74,23 @@ from queue import SimpleQueue
|
|
74 |
# # Create a Queue
|
75 |
# Q = Queue()
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
|
79 |
Q = SimpleQueue()
|
@@ -119,9 +136,6 @@ llm_streaming = get_llm(max_tokens = 1024,temperature = 0.0,verbose = True,strea
|
|
119 |
|
120 |
# Create vectorstore and retriever
|
121 |
vectorstore = get_pinecone_vectorstore(embeddings_function)
|
122 |
-
retriever = ClimateQARetriever(vectorstore=vectorstore,sources = ["IPCC"],k_summary = 3,k_total = 10)
|
123 |
-
chain = load_climateqa_chain(retriever,llm_reformulation,llm_streaming)
|
124 |
-
|
125 |
|
126 |
#---------------------------------------------------------------------------
|
127 |
# ClimateQ&A Streaming
|
@@ -131,15 +145,25 @@ chain = load_climateqa_chain(retriever,llm_reformulation,llm_streaming)
|
|
131 |
|
132 |
from threading import Thread
|
133 |
|
134 |
-
|
135 |
-
response = chain({"query":query,"audience":audience})
|
136 |
-
Q.put(response)
|
137 |
-
Q.put(job_done)
|
138 |
|
139 |
def answer_user(message,history):
|
140 |
return message, history + [[message, None]]
|
141 |
|
142 |
-
def answer_bot(message,history,audience):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
if audience == "Children":
|
145 |
audience_prompt = audience_prompts["children"]
|
@@ -187,7 +211,9 @@ def answer_bot(message,history,audience):
|
|
187 |
break
|
188 |
|
189 |
elif isinstance(next_item, str):
|
190 |
-
history[-1][1]
|
|
|
|
|
191 |
yield "", history, ""
|
192 |
|
193 |
thread.join()
|
@@ -351,27 +377,30 @@ def log_on_azure(file, logs, share_client):
|
|
351 |
|
352 |
|
353 |
init_prompt = """
|
354 |
-
Hello
|
355 |
|
356 |
π‘ How to use
|
357 |
- **Language**: You can ask me your questions in any language.
|
358 |
- **Audience**: You can specify your audience (children, general public, experts) to get a more adapted answer.
|
359 |
- **Sources**: You can choose to search in the IPCC or IPBES reports, or both.
|
360 |
|
361 |
-
|
362 |
*Please note that the AI is not perfect and may sometimes give irrelevant answers. If you are not satisfied with the answer, please ask a more specific question or report your feedback to help us improve the system.*
|
363 |
|
364 |
β What do you want to learn ?
|
365 |
"""
|
366 |
|
367 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
368 |
with gr.Blocks(title="π Climate Q&A", css="style.css", theme=theme) as demo:
|
369 |
# user_id_state = gr.State([user_id])
|
370 |
|
371 |
-
# Gradio
|
372 |
-
# gr.Markdown("<h1><center>Climate Q&A π</center></h1>")
|
373 |
-
# gr.Markdown("<h4><center>Ask climate-related questions to the IPCC and IPBES reports using AI</center></h4>")
|
374 |
-
|
375 |
with gr.Tab("π ClimateQ&A"):
|
376 |
|
377 |
with gr.Row(elem_id="chatbot-row"):
|
@@ -381,6 +410,8 @@ with gr.Blocks(title="π Climate Q&A", css="style.css", theme=theme) as demo:
|
|
381 |
value=[[None,init_prompt]],
|
382 |
show_copy_button=True,show_label = False,elem_id="chatbot",layout = "panel",avatar_images = ("assets/logo4.png",None))
|
383 |
|
|
|
|
|
384 |
with gr.Row(elem_id = "input-message"):
|
385 |
textbox=gr.Textbox(placeholder="Ask me anything here!",show_label=False,scale=7)
|
386 |
submit_button = gr.Button(">",scale = 1,elem_id = "submit-button")
|
@@ -429,6 +460,7 @@ with gr.Blocks(title="π Climate Q&A", css="style.css", theme=theme) as demo:
|
|
429 |
],
|
430 |
[examples_hidden],
|
431 |
examples_per_page=10,
|
|
|
432 |
)
|
433 |
|
434 |
with gr.Tab("π Citations",elem_id = "tab-citations"):
|
@@ -441,24 +473,28 @@ with gr.Blocks(title="π Climate Q&A", css="style.css", theme=theme) as demo:
|
|
441 |
dropdown_sources = gr.CheckboxGroup(
|
442 |
["IPCC", "IPBES"],
|
443 |
label="Select reports",
|
|
|
|
|
444 |
)
|
445 |
|
446 |
dropdown_audience = gr.Dropdown(
|
447 |
["Children","General public","Experts"],
|
448 |
label="Select audience",
|
|
|
|
|
449 |
)
|
450 |
|
451 |
|
452 |
# textbox.submit(predict_climateqa,[textbox,bot],[None,bot,sources_textbox])
|
453 |
|
454 |
textbox.submit(answer_user, [textbox, bot], [textbox, bot], queue=False).then(
|
455 |
-
answer_bot, [textbox,bot,dropdown_audience], [textbox,bot,sources_textbox]
|
456 |
)
|
457 |
examples_hidden.change(answer_user, [examples_hidden, bot], [textbox, bot], queue=False).then(
|
458 |
-
answer_bot, [textbox,bot,dropdown_audience], [textbox,bot,sources_textbox]
|
459 |
)
|
460 |
submit_button.click(answer_user, [textbox, bot], [textbox, bot], queue=False).then(
|
461 |
-
answer_bot, [textbox,bot,dropdown_audience], [textbox,bot,sources_textbox]
|
462 |
)
|
463 |
|
464 |
|
|
|
74 |
# # Create a Queue
|
75 |
# Q = Queue()
|
76 |
|
77 |
+
import re
|
78 |
+
|
79 |
+
def parse_output_llm_with_sources(output):
|
80 |
+
# Split the content into a list of text and "[Doc X]" references
|
81 |
+
content_parts = re.split(r'\[(Doc\s?\d+(?:,\s?Doc\s?\d+)*)\]', output)
|
82 |
+
parts = []
|
83 |
+
for part in content_parts:
|
84 |
+
if part.startswith("Doc"):
|
85 |
+
subparts = part.split(",")
|
86 |
+
subparts = [subpart.lower().replace("doc","").strip() for subpart in subparts]
|
87 |
+
subparts = [f"<span class='doc-ref'><sup>{subpart}</sup></span>" for subpart in subparts]
|
88 |
+
parts.append("".join(subparts))
|
89 |
+
else:
|
90 |
+
parts.append(part)
|
91 |
+
content_parts = "".join(parts)
|
92 |
+
return content_parts
|
93 |
+
|
94 |
|
95 |
|
96 |
Q = SimpleQueue()
|
|
|
136 |
|
137 |
# Create vectorstore and retriever
|
138 |
vectorstore = get_pinecone_vectorstore(embeddings_function)
|
|
|
|
|
|
|
139 |
|
140 |
#---------------------------------------------------------------------------
|
141 |
# ClimateQ&A Streaming
|
|
|
145 |
|
146 |
from threading import Thread
|
147 |
|
148 |
+
|
|
|
|
|
|
|
149 |
|
150 |
def answer_user(message,history):
|
151 |
return message, history + [[message, None]]
|
152 |
|
153 |
+
def answer_bot(message,history,audience,sources):
|
154 |
+
|
155 |
+
# if len(message) <= 2:
|
156 |
+
# complete_response = "**β οΈ No relevant passages found in the climate science reports (IPCC and IPBES), you may want to ask a more specific question (specifying your question on climate and biodiversity issues).**"
|
157 |
+
# history[-1][1] += "\n\n" + complete_response
|
158 |
+
# return "", history, ""
|
159 |
+
|
160 |
+
retriever = ClimateQARetriever(vectorstore=vectorstore,sources = sources,k_summary = 3,k_total = 10)
|
161 |
+
chain = load_climateqa_chain(retriever,llm_reformulation,llm_streaming)
|
162 |
+
|
163 |
+
def threaded_chain(query,audience):
|
164 |
+
response = chain({"query":query,"audience":audience})
|
165 |
+
Q.put(response)
|
166 |
+
Q.put(job_done)
|
167 |
|
168 |
if audience == "Children":
|
169 |
audience_prompt = audience_prompts["children"]
|
|
|
211 |
break
|
212 |
|
213 |
elif isinstance(next_item, str):
|
214 |
+
new_paragraph = history[-1][1] + next_item
|
215 |
+
new_paragraph = parse_output_llm_with_sources(new_paragraph)
|
216 |
+
history[-1][1] = new_paragraph
|
217 |
yield "", history, ""
|
218 |
|
219 |
thread.join()
|
|
|
377 |
|
378 |
|
379 |
init_prompt = """
|
380 |
+
Hello, I am ClimateQ&A, a conversational assistant designed to help you understand climate change and biodiversity loss. I will answer your questions by **sifting through the IPCC and IPBES scientific reports**.
|
381 |
|
382 |
π‘ How to use
|
383 |
- **Language**: You can ask me your questions in any language.
|
384 |
- **Audience**: You can specify your audience (children, general public, experts) to get a more adapted answer.
|
385 |
- **Sources**: You can choose to search in the IPCC or IPBES reports, or both.
|
386 |
|
387 |
+
β οΈ Limitations
|
388 |
*Please note that the AI is not perfect and may sometimes give irrelevant answers. If you are not satisfied with the answer, please ask a more specific question or report your feedback to help us improve the system.*
|
389 |
|
390 |
β What do you want to learn ?
|
391 |
"""
|
392 |
|
393 |
|
394 |
+
def vote(data: gr.LikeData):
|
395 |
+
if data.liked:
|
396 |
+
print(data.value)
|
397 |
+
else:
|
398 |
+
print(data)
|
399 |
+
|
400 |
+
|
401 |
with gr.Blocks(title="π Climate Q&A", css="style.css", theme=theme) as demo:
|
402 |
# user_id_state = gr.State([user_id])
|
403 |
|
|
|
|
|
|
|
|
|
404 |
with gr.Tab("π ClimateQ&A"):
|
405 |
|
406 |
with gr.Row(elem_id="chatbot-row"):
|
|
|
410 |
value=[[None,init_prompt]],
|
411 |
show_copy_button=True,show_label = False,elem_id="chatbot",layout = "panel",avatar_images = ("assets/logo4.png",None))
|
412 |
|
413 |
+
# bot.like(vote,None,None)
|
414 |
+
|
415 |
with gr.Row(elem_id = "input-message"):
|
416 |
textbox=gr.Textbox(placeholder="Ask me anything here!",show_label=False,scale=7)
|
417 |
submit_button = gr.Button(">",scale = 1,elem_id = "submit-button")
|
|
|
460 |
],
|
461 |
[examples_hidden],
|
462 |
examples_per_page=10,
|
463 |
+
# cache_examples=True,
|
464 |
)
|
465 |
|
466 |
with gr.Tab("π Citations",elem_id = "tab-citations"):
|
|
|
473 |
dropdown_sources = gr.CheckboxGroup(
|
474 |
["IPCC", "IPBES"],
|
475 |
label="Select reports",
|
476 |
+
value=["IPCC"],
|
477 |
+
interactive=True,
|
478 |
)
|
479 |
|
480 |
dropdown_audience = gr.Dropdown(
|
481 |
["Children","General public","Experts"],
|
482 |
label="Select audience",
|
483 |
+
value="Experts",
|
484 |
+
interactive=True,
|
485 |
)
|
486 |
|
487 |
|
488 |
# textbox.submit(predict_climateqa,[textbox,bot],[None,bot,sources_textbox])
|
489 |
|
490 |
textbox.submit(answer_user, [textbox, bot], [textbox, bot], queue=False).then(
|
491 |
+
answer_bot, [textbox,bot,dropdown_audience,dropdown_sources], [textbox,bot,sources_textbox]
|
492 |
)
|
493 |
examples_hidden.change(answer_user, [examples_hidden, bot], [textbox, bot], queue=False).then(
|
494 |
+
answer_bot, [textbox,bot,dropdown_audience,dropdown_sources], [textbox,bot,sources_textbox]
|
495 |
)
|
496 |
submit_button.click(answer_user, [textbox, bot], [textbox, bot], queue=False).then(
|
497 |
+
answer_bot, [textbox,bot,dropdown_audience,dropdown_sources], [textbox,bot,sources_textbox]
|
498 |
)
|
499 |
|
500 |
|
style.css
CHANGED
@@ -84,7 +84,7 @@ a {
|
|
84 |
font-weight: bold;
|
85 |
margin-bottom: 10px;
|
86 |
margin-top:0px !important;
|
87 |
-
color:#
|
88 |
}
|
89 |
|
90 |
.card-content p {
|
@@ -263,3 +263,11 @@ body.dark .card-footer {
|
|
263 |
body.dark .card-footer span {
|
264 |
color:white !important;
|
265 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
font-weight: bold;
|
85 |
margin-bottom: 10px;
|
86 |
margin-top:0px !important;
|
87 |
+
color:#dc2626!important;;
|
88 |
}
|
89 |
|
90 |
.card-content p {
|
|
|
263 |
body.dark .card-footer span {
|
264 |
color:white !important;
|
265 |
}
|
266 |
+
|
267 |
+
|
268 |
+
.doc-ref{
|
269 |
+
color:#dc2626!important;
|
270 |
+
margin-right:1px;
|
271 |
+
}
|
272 |
+
|
273 |
+
|