File size: 17,861 Bytes
a5686cb a4595fc a5686cb 71ab0a8 19a9d09 91c4196 c48e036 19a9d09 91c4196 fa7f0c5 669d503 99e2b1f c48e036 99e2b1f fdf1622 99e2b1f a4595fc 91c4196 99e2b1f fdf1622 669d503 a4595fc 91c4196 871aa55 91c4196 a4595fc 0b4f4a2 91c4196 c48e036 0b4f4a2 9f6c9bd 99e2b1f f3d1657 a4595fc 871aa55 bf93486 c48e036 68fbb90 f2033dd 68fbb90 121f27f c48e036 121f27f d271714 121f27f 91c4196 fdf1622 871aa55 a422880 121f27f a422880 a5686cb 91c4196 12574b1 dc1d7e6 fdf1622 91c4196 65b9706 19a9d09 78e5850 12574b1 65b9706 12574b1 65b9706 12574b1 65b9706 d271714 12574b1 65b9706 12574b1 65b9706 d271714 12574b1 d271714 af9539a d271714 4857c80 12574b1 4857c80 12574b1 4857c80 12574b1 4857c80 12574b1 78e5850 12574b1 4857c80 12574b1 4857c80 9655619 4857c80 12574b1 4857c80 12574b1 78e5850 12574b1 9655619 12574b1 4857c80 12574b1 19a9d09 12574b1 d271714 78e5850 65b9706 d271714 65b9706 d271714 78e5850 d271714 78e5850 4857c80 78e5850 12574b1 4dd3ec8 d730458 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
import gradio as gr
from haystack.document_stores import FAISSDocumentStore
from haystack.nodes import EmbeddingRetriever
import openai
import os
from utils import (
make_pairs,
set_openai_api_key,
create_user_id,
to_completion,
)
import numpy as np
from datetime import datetime
from azure.storage.fileshare import ShareServiceClient
system_template = {"role": "system", "content": os.environ["content"]}
openai.api_type = "azure"
openai.api_key = os.environ["api_key"]
openai.api_base = os.environ["ressource_endpoint"]
openai.api_version = "2022-12-01"
retrieve_all = EmbeddingRetriever(
document_store=FAISSDocumentStore.load(
index_path="./documents/climate_gpt.faiss",
config_path="./documents/climate_gpt.json",
),
embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
model_format="sentence_transformers",
)
retrieve_giec = EmbeddingRetriever(
document_store=FAISSDocumentStore.load(
index_path="./documents/climate_gpt_only_giec.faiss",
config_path="./documents/climate_gpt_only_giec.json",
),
embedding_model="sentence-transformers/multi-qa-mpnet-base-dot-v1",
model_format="sentence_transformers",
)
credential = {
"account_key": os.environ["account_key"],
"account_name": os.environ["account_name"],
}
account_url = os.environ["account_url"]
file_share_name = "climategpt"
service = ShareServiceClient(account_url=account_url, credential=credential)
share_client = service.get_share_client(file_share_name)
user_id = create_user_id(10)
def chat(
user_id: str,
query: str,
history: list = [system_template],
report_type: str = "All available",
threshold: float = 0.555,
) -> tuple:
"""retrieve relevant documents in the document store then query gpt-turbo
Args:
query (str): user message.
history (list, optional): history of the conversation. Defaults to [system_template].
report_type (str, optional): should be "All available" or "IPCC only". Defaults to "All available".
threshold (float, optional): similarity threshold, don't increase more than 0.568. Defaults to 0.56.
Yields:
tuple: chat gradio format, chat openai format, sources used.
"""
if report_type == "All available":
retriever = retrieve_all
elif report_type == "IPCC only":
retriever = retrieve_giec
else:
raise Exception("report_type arg should be in (All available, IPCC only)")
docs = retriever.retrieve(query=query, top_k=10)
messages = history + [{"role": "user", "content": query}]
sources = "\n\n".join(
f"doc {i}: {d.meta['file_name']} page {d.meta['page_number']}\n{d.content}"
for i, d in enumerate(docs, 1)
if d.score > threshold
)
if sources:
messages.append(
{"role": "system", "content": f"{os.environ['sources']}\n\n{sources}"}
)
response = openai.Completion.create(
engine="climateGPT",
prompt=to_completion(messages),
temperature=0.2,
stream=True,
max_tokens=1024,
)
if sources:
complete_response = ""
messages.pop()
else:
sources = "No climate science report was used to provide this answer."
complete_response = "No relevant documents found in the climate science reports, for a sourced answer you may want to try a more specific question.\n\n"
messages.append({"role": "assistant", "content": complete_response})
timestamp = str(datetime.now().timestamp())
file = user_id[0] + timestamp + ".json"
logs = {
"user_id": user_id[0],
"prompt": query,
"retrived": sources,
"report_type": report_type,
"prompt_eng": messages[0],
"answer": messages[-1]["content"],
"time": timestamp,
}
log_on_azure(file, logs, share_client)
for chunk in response:
if (
chunk_message := chunk["choices"][0].get("text")
) and chunk_message != "<|im_end|>":
complete_response += chunk_message
messages[-1]["content"] = complete_response
gradio_format = make_pairs([a["content"] for a in messages[1:]])
yield gradio_format, messages, sources
def save_feedback(feed: str, user_id):
if len(feed) > 1:
timestamp = str(datetime.now().timestamp())
file = user_id[0] + timestamp + ".json"
logs = {
"user_id": user_id[0],
"feedback": feed,
"time": timestamp,
}
log_on_azure(file, logs, share_client)
return "Feedback submitted, thank you!"
def reset_textbox():
return gr.update(value="")
def log_on_azure(file, logs, share_client):
file_client = share_client.get_file_client(file)
file_client.upload_file(str(logs))
with gr.Blocks(title="π ClimateGPT", css="style.css") as demo:
user_id_state = gr.State([user_id])
# Gradio
gr.Markdown("<h1><center>ClimateGPT π</center></h1>")
gr.Markdown(
"<h4><center>Ask climate-related questions to the IPCC reports</center></h4>"
)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown(
"""
<p><b>Climate change and environmental disruptions have become some of the most pressing challenges facing our planet today</b>. As global temperatures rise and ecosystems suffer, it is essential for individuals to understand the gravity of the situation in order to make informed decisions and advocate for appropriate policy changes.</p>
<p>However, comprehending the vast and complex scientific information can be daunting, as the scientific consensus references, such as <b>the Intergovernmental Panel on Climate Change (IPCC) reports, span thousands of pages</b> and are often laden with technical jargon. To bridge this gap and make climate science more accessible, we introduce <b>ClimateGPT as a tool to distill expert-level knowledge into easily digestible insights about climate science.</b></p>
<div class="tip-box">
<div class="tip-box-title">
<span class="light-bulb" role="img" aria-label="Light Bulb">π‘</span>
How does ClimateGPT work?
</div>
ClimateGPT harnesses modern OCR techniques to parse and preprocess IPCC reports. By leveraging state-of-the-art question-answering algorithms, <i>ClimateGPT is able to sift through the extensive collection of climate scientific reports and identify relevant passages in response to user inquiries</i>. Furthermore, the integration of the ChatGPT API allows ClimateGPT to present complex data in a user-friendly manner, summarizing key points and facilitating communication of climate science to a wider audience. This innovative chatbot effectively puts a climate expert in your pocket, empowering you to engage with crucial environmental issues in a more informed and meaningful way.
</div>
"""
)
with gr.Column(scale=1):
gr.Markdown("![](https://i.postimg.cc/fLvsvMzM/Untitled-design-5.png)")
gr.Markdown(
"*Source : IPCC AR6 - Synthesis Report of the IPCC 6th assessment report (AR6)*"
)
with gr.Row():
with gr.Column(scale=2):
chatbot = gr.Chatbot(elem_id="chatbot")
state = gr.State([system_template])
with gr.Row():
ask = gr.Textbox(
show_label=False,
placeholder="Ask here your climate-related question and press enter",
).style(container=False)
examples_questions = gr.Examples(
[
"What are the main causes of climate change?",
"What are the impacts of climate change?",
"Can climate change be reversed?",
"What is the difference between climate change and global warming?",
"What can individuals do to address climate change?",
"What evidence do we have of climate change?",
"What is the Paris Agreement and why is it important?",
"Which industries have the highest GHG emissions?",
"Is climate change caused by humans?",
"What are the different greenhouse gases (GHG)?",
"What is the warming power of methane?",
"What is the jet stream?",
"What is the breakdown of carbon sinks?",
"How do the GHGs work ? Why does temperature increase ?",
"What is the impact of global warming on ocean currents?",
"How much warming is possible in 2050?",
"What is the impact of climate change in Africa?",
"What is the impact of rising sea levels?",
"Will climate change accelerate diseases and epidemics like COVID?",
"What are the economic impacts of climate change?",
"What is the link between gender equality and climate change?",
"How much is the cost of inaction ?",
"What is the relationship between climate change and poverty?",
"What is the relationship between climate change and biodiversity loss?",
"What are the most effective strategies and technologies for reducing greenhouse gas (GHG) emissions?",
"Is economic growth possible? What do you think about degrowth?",
"Will technology save us?",
"Is climate change a natural phenomenon ?",
"Is climate change really happening or is it just a natural fluctuation in Earth's temperature?",
"Is the scientific consensus on climate change really as strong as it is claimed to be?",
"Is the impact of climate change really as severe as it is claimed to be?",
"Is climate change a hoax created by the government or environmental organizations?",
],
[ask],
)
with gr.Column(scale=1, variant="panel"):
gr.Markdown("### Sources")
sources_textbox = gr.Textbox(
interactive=False, show_label=False, max_lines=50
)
ask.submit(
fn=chat,
inputs=[
user_id_state,
ask,
state,
gr.inputs.Dropdown(
["IPCC only", "All available"],
default="All available",
label="Select reports",
),
],
outputs=[chatbot, state, sources_textbox],
)
ask.submit(reset_textbox, [], [ask])
gr.Markdown("## How to use ClimateGPT")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown(
"""
### πͺ Getting started
- In the chatbot section, simply type your climate-related question, and ClimateGPT will provide an answer with references to relevant IPCC reports.
- ClimateGPT retrieves specific passages from the IPCC reports to help answer your question accurately.
- Source information, including page numbers and passages, is displayed on the right side of the screen for easy verification.
- Feel free to ask follow-up questions within the chatbot for a more in-depth understanding.
- ClimateGPT integrates multiple sources (IPCC, IPBES, IEA, Limits to Growth, β¦ ) to cover various aspects of environmental science, such as climate change, biodiversity, energy, economy, and pollution. See all sources used below.
"""
)
with gr.Column(scale=1):
gr.Markdown(
"""
### β οΈ Limitations
<div class="warning-box">
<ul>
<li>Currently available in English only.</li>
<li>Please note that, like any AI, the model may occasionally generate an inaccurate or imprecise answer. Always refer to the provided sources to verify the validity of the information given. If you find any issues with the response, kindly provide feedback to help improve the system.</li>
<li>ClimateGPT is specifically designed for climate-related inquiries. If you ask a non-environmental question, the chatbot will politely remind you that its focus is on climate and environmental issues.</li>
</div>
"""
)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## π Feedback and feature requests")
gr.Markdown(
"""
### Beta test
- ClimateGPT welcomes community contributions. To participate, head over to the Community Tab and create a "New Discussion" to ask questions and share your insights.
- Provide feedback through our feedback form, letting us know which insights you found accurate, useful, or not. Your input will help us improve the platform.
- Only a few sources (see below) are integrated (all IPCC, IPBES, IEA recent reports), if you are a climate science researcher and net to sift through another report, please let us know.
### Feedbacks
"""
)
with gr.Row():
with gr.Column():
feedback = gr.Textbox(label="Write your feedback here")
with gr.Column(scale=1):
feedback_output = gr.Textbox(label="Submit status")
with gr.Row():
feedback_save = gr.Button(value="submit feedback")
feedback_save.click(
save_feedback,
inputs=[feedback, user_id_state],
outputs=feedback_output,
)
with gr.Row():
gr.Markdown(
"If you need us to ask another climate science report or ask any question, contact us at <b>theo.alvesdacosta@ekimetrics.com</b>"
)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("## OpenAI API")
gr.Markdown(
"To make climate science accessible to a wider audience, we have opened our own OpenAI API key with a monthly cap of $1000. If you already have an API key, please use it to help conserve bandwidth for others."
)
openai_api_key_textbox = gr.Textbox(
placeholder="Paste your OpenAI API key (sk-...) and hit Enter",
show_label=False,
lines=1,
type="password",
)
openai_api_key_textbox.change(set_openai_api_key, inputs=[openai_api_key_textbox])
openai_api_key_textbox.submit(set_openai_api_key, inputs=[openai_api_key_textbox])
gr.Markdown(
"""
## π Sources
| Source | Report | URL | Number of pages | Release date |
| --- | --- | --- | --- | --- |
| IPCC | IPCC AR6 - First Assessment Report on the Physical Science of Climate Change | https://report.ipcc.ch/ar6/wg1/IPCC_AR6_WGI_FullReport.pdf | 2049 pages | August 2021 |
| IPCC | IPCC AR6 - Second Assessment Report on Climate Change Adaptation | https://report.ipcc.ch/ar6/wg2/IPCC_AR6_WGII_FullReport.pdf | 3068 pages | February 2022 |
| IPCC | IPCC AR6 - Third Assessment Report on Climate Change Mitigation | https://www.ipcc.ch/report/ar6/wg3/downloads/report/IPCC_AR6_WGIII_FullReport.pdf | 2258 pages | April 2022 |
| IPCC | IPCC AR6 - Synthesis Report of the IPCC 6th assessment report (AR6) | https://report.ipcc.ch/ar6syr/pdf/IPCC_AR6_SYR_SPM.pdf | 36 pages | March 2023 |
| IPBES | IPBES Global report on Biodiversity - March 2022 | https://www.ipbes.net/global-assessment | 1148 pages | June 2022 |
| FAO | Food Outlook Biannual Report on Global Food Markets | https://www.fao.org/documents/card/en/c/cb9427en | 174 pages | June 2022 |
| IEA | IEAβs report on the Role of Critical Minerals in Clean Energy Transitions | https://www.iea.org/reports/the-role-of-critical-minerals-in-clean-energy-transitions | 287 pages | May 2021 |
| Club de Rome | Limits to Growth | https://www.donellameadows.org/wp-content/userfiles/Limits-to-Growth-digital-scan-version.pdf | 211 pages | 1972 |
| | Outside The Safe operating system of the Planetary Boundary for Novel Entities | https://pubs.acs.org/doi/10.1021/acs.est.1c04158 | 12 pages | January 2022 |
| | Planetary boundaries: Guiding human development on a changing planet | https://www.science.org/doi/10.1126/science.1259855 | 11 pages | February 2015 |
| UNFCCC | State of the Oceans report | https://unfccc.int/documents/568128 | 75 pages | August 2022 |
| IEA | Word Energy Outlook 2021 | https://www.iea.org/reports/world-energy-outlook-2021 | 386 pages | October 2021 |
| IEA | Word Energy Outlook 2022 | https://www.iea.org/reports/world-energy-outlook-2022 | 524 pages | October 2022 |
| EU parliament | The environmental impacts of plastics and micro plastics use, waste and polution EU and national measures | https://www.europarl.europa.eu/thinktank/en/document/IPOL_STU(2020)658279 | 76 pages | October 2020 |
## π’οΈ Carbon Footprint
Carbon emissions were measured during the development and inference process using CodeCarbon [https://github.com/mlco2/codecarbon](https://github.com/mlco2/codecarbon)
| Phase | Description | Emissions | Source |
| --- | --- | --- | --- |
| Development | OCR and parsing all pdf documents with AI | x kgCO2 | CodeCarbon |
| Development | Question Answering development | x kgCO2 | CodeCarbon |
| Inference | Question Answering | x kgCO2 / call | CodeCarbon |
| Inference | API call to turbo-GPT | x kgCO2 / call | OpenAI |
## π§ Contact
This tool has been developed by the R&D lab at **Ekimetrics** (Jean Lelong, Nina Achache, Gabriel Olympie, Nicolas Chesneau, Natalia De la Calzada, ThΓ©o Alves Da Costa)
If you have any questions or feature requests, please feel free to reach us out at <b>theo.alvesdacosta@ekimetrics.com</b>.
"""
)
demo.queue(concurrency_count=16)
demo.launch()
|