import gradio as gr from utils import create_user_id # Langchain from langchain.embeddings import HuggingFaceEmbeddings from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler # ClimateQ&A imports from anyqa.embeddings import EMBEDDING_MODEL_NAME from anyqa.llm import get_llm from anyqa.qa_logging import log from anyqa.chains import load_qa_chain_with_text from anyqa.chains import load_reformulation_chain from anyqa.vectorstore import get_vectorstore from anyqa.retriever import QARetriever from anyqa.prompts import audience_prompts # Load environment variables in local mode try: from dotenv import load_dotenv load_dotenv() except Exception as e: pass # Set up Gradio Theme theme = gr.themes.Base( primary_hue="blue", secondary_hue="red", font=[gr.themes.GoogleFont("Poppins"), "ui-sans-serif", "system-ui", "sans-serif"], ) init_prompt = "" system_template = { "role": "system", "content": init_prompt, } user_id = create_user_id() # --------------------------------------------------------------------------- # ClimateQ&A core functions # --------------------------------------------------------------------------- from langchain.callbacks.base import BaseCallbackHandler from queue import Empty from threading import Thread from langchain.schema import LLMResult from typing import Any, Union, Dict, List from queue import SimpleQueue # # Create a Queue # Q = Queue() import re def parse_output_llm_with_sources(output): # Split the content into a list of text and "[Doc X]" references content_parts = re.split(r"\[(Doc\s?\d+(?:,\s?Doc\s?\d+)*)\]", output) parts = [] for part in content_parts: if part.startswith("Doc"): subparts = part.split(",") subparts = [ subpart.lower().replace("doc", "").strip() for subpart in subparts ] subparts = [ f"{subpart}" for subpart in subparts ] parts.append("".join(subparts)) else: parts.append(part) content_parts = "".join(parts) return content_parts job_done = object() # signals the processing is done class StreamingGradioCallbackHandler(BaseCallbackHandler): def __init__(self, q: SimpleQueue): self.q = q def on_llm_start( self, serialized: Dict[str, Any], prompts: List[str], **kwargs: Any ) -> None: """Run when LLM starts running. Clean the queue.""" while not self.q.empty(): try: self.q.get(block=False) except Empty: continue def on_llm_new_token(self, token: str, **kwargs: Any) -> None: """Run on new LLM token. Only available when streaming is enabled.""" self.q.put(token) def on_llm_end(self, response: LLMResult, **kwargs: Any) -> None: """Run when LLM ends running.""" self.q.put(job_done) def on_llm_error( self, error: Union[Exception, KeyboardInterrupt], **kwargs: Any ) -> None: """Run when LLM errors.""" self.q.put(job_done) # Create embeddings function and LLM embeddings_function = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL_NAME) # Create vectorstore and retriever vectorstore = get_vectorstore(embeddings_function) # --------------------------------------------------------------------------- # ClimateQ&A Streaming # From https://github.com/gradio-app/gradio/issues/5345 # And https://stackoverflow.com/questions/76057076/how-to-stream-agents-response-in-langchain # --------------------------------------------------------------------------- from threading import Thread def answer_user(query, query_example, history): if len(query) <= 2: raise Exception("Please ask a longer question") return query, history + [[query, ". . ."]] def answer_user_example(query, query_example, history): return query_example, history + [[query_example, ". . ."]] def fetch_sources(query, sources): # Prepare default values if len(sources) == 0: sources = ["IPCC"] llm_reformulation = get_llm( max_tokens=512, temperature=0.0, verbose=True, streaming=False ) retriever = QARetriever( vectorstore=vectorstore, sources=[], k_summary=0, k_total=10 ) reformulation_chain = load_reformulation_chain(llm_reformulation) # Calculate language output_reformulation = reformulation_chain({"query": query}) question = output_reformulation["question"] language = output_reformulation["language"] # Retrieve docs docs = retriever.get_relevant_documents(question) if len(docs) > 0: # Already display the sources sources_text = [] for i, d in enumerate(docs, 1): sources_text.append(make_html_source(d, i)) citations_text = "".join(sources_text) docs_text = "\n\n".join([d.page_content for d in docs]) return "", citations_text, docs_text, question, language else: sources_text = ( "⚠️ No relevant passages found in the scientific reports (IPCC and IPBES)" ) citations_text = "**⚠️ No relevant passages found in the sources, you may want to ask a more specific question.**" docs_text = "" return "", citations_text, docs_text, question, language def answer_bot(query, history, docs, question, language, audience): if audience == "Children": audience_prompt = audience_prompts["children"] elif audience == "General public": audience_prompt = audience_prompts["general"] elif audience == "Experts": audience_prompt = audience_prompts["experts"] else: audience_prompt = audience_prompts["experts"] # Prepare Queue for streaming LLMs Q = SimpleQueue() llm_streaming = get_llm( max_tokens=1024, temperature=0.0, verbose=True, streaming=True, callbacks=[StreamingGradioCallbackHandler(Q), StreamingStdOutCallbackHandler()], ) qa_chain = load_qa_chain_with_text(llm_streaming) def threaded_chain(question, audience, language, docs): try: response = qa_chain( { "question": question, "audience": audience, "language": language, "summaries": docs, } ) Q.put(response) Q.put(job_done) except Exception as e: print(e) history[-1][1] = "" textbox = gr.Textbox( placeholder=". . .", show_label=False, scale=1, lines=1, interactive=False ) if len(docs) > 0: # Start thread for streaming thread = Thread( target=threaded_chain, kwargs={ "question": question, "audience": audience_prompt, "language": language, "docs": docs, }, ) thread.start() while True: next_item = Q.get(block=True) # Blocks until an input is available if next_item is job_done: break elif isinstance(next_item, str): new_paragraph = history[-1][1] + next_item new_paragraph = parse_output_llm_with_sources(new_paragraph) history[-1][1] = new_paragraph yield textbox, history else: pass thread.join() log(question=question, history=history, docs=docs, user_id=user_id) else: complete_response = "**⚠️ No relevant passages found in the sources, you may want to ask a more specific question.**" history[-1][1] += complete_response yield "", history # --------------------------------------------------------------------------- # ClimateQ&A core functions # --------------------------------------------------------------------------- def make_html_source(source, i): meta = source.metadata content = source.page_content.split(":", 1)[1].strip() link = ( f'🔗' if "url" in meta else "" ) return f"""

Doc {i} - {meta['short_name']} - Page {int(meta['page_number'])}

{content}

""" def reset_textbox(): return gr.update(value="") # -------------------------------------------------------------------- # Gradio # -------------------------------------------------------------------- init_prompt = """ Hello, I'm a conversational assistant. I will answer your questions by **sifting through trusted data sources**. 💡 How to use - **Language**: You can ask me your questions in any language. - **Audience**: You can specify your audience (children, general public, experts) to get a more adapted answer. - **Sources**: You can choose to search in which sources you want me to look for answers. By default, I will search in all sources. ⚠️ Limitations *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.* ❓ What do you want to learn ? """ def vote(data: gr.LikeData): if data.liked: print(data.value) else: print(data) def change_tab(): return gr.Tabs.update(selected=1) with gr.Blocks(title="❓ Q&A", css="style.css", theme=theme) as demo: # user_id_state = gr.State([user_id]) with gr.Tab("❓ Q&A"): with gr.Row(elem_id="chatbot-row"): with gr.Column(scale=2): # state = gr.State([system_template]) bot = gr.Chatbot( value=[[None, init_prompt]], show_copy_button=True, show_label=False, elem_id="chatbot", layout="panel", avatar_images=("assets/bot_avatar.png", None), ) # bot.like(vote,None,None) with gr.Row(elem_id="input-message"): textbox = gr.Textbox( placeholder="Ask me anything here!", show_label=False, scale=1, lines=1, interactive=True, ) # submit_button = gr.Button(">",scale = 1,elem_id = "submit-button") with gr.Column(scale=1, variant="panel", elem_id="right-panel"): with gr.Tabs() as tabs: with gr.TabItem("📝 Examples", elem_id="tab-examples", id=0): examples_hidden = gr.Textbox(elem_id="hidden-message") questions = [ "How does doaism view our dependence on modern technology?", "From a doaism perspective, should we embrace or challenge the rise of AI?", "How might doaism influence sustainable economic practices?", "Does doaism support the idea of a minimalistic economy over consumerism?", "How does doaism interpret the dynamics of modern relationships?", "From a doaism viewpoint, how should society handle conflicts and disagreements?", "How might doaism guide our approach to mental and physical health?", "Does doaism offer insights into balancing work-life pressures in the modern age?", "How does doaism view the purpose and methods of modern education?", "From a doaism perspective, should learning be more experiential than theoretical?", ] examples_questions = gr.Examples( questions, [examples_hidden], examples_per_page=10, run_on_click=False, # cache_examples=True, ) with gr.Tab("📚 Citations", elem_id="tab-citations", id=1): sources_textbox = gr.HTML( show_label=False, elem_id="sources-textbox" ) docs_textbox = gr.State("") with gr.Tab("⚙️ Configuration", elem_id="tab-config", id=2): gr.Markdown( "Reminder: You can talk in any language, this tool is multi-lingual!" ) dropdown_sources = gr.CheckboxGroup( ["IPCC", "IPBES"], label="Select reports", value=["IPCC"], interactive=True, ) dropdown_audience = gr.Dropdown( ["Children", "General public", "Experts"], label="Select audience", value="Experts", interactive=True, ) output_query = gr.Textbox( label="Query used for retrieval", show_label=True, elem_id="reformulated-query", lines=2, interactive=False, ) output_language = gr.Textbox( label="Language", show_label=True, elem_id="language", lines=1, interactive=False, ) ( textbox.submit( answer_user, [textbox, examples_hidden, bot], [textbox, bot], queue=False, ) .success(change_tab, None, tabs) .success( fetch_sources, [textbox, dropdown_sources], [ textbox, sources_textbox, docs_textbox, output_query, output_language, ], ) .success( answer_bot, [ textbox, bot, docs_textbox, output_query, output_language, dropdown_audience, ], [textbox, bot], queue=True, ) .success(lambda x: textbox, [textbox], [textbox]) ) ( examples_hidden.change( answer_user_example, [textbox, examples_hidden, bot], [textbox, bot], queue=False, ) .success(change_tab, None, tabs) .success( fetch_sources, [textbox, dropdown_sources], [ textbox, sources_textbox, docs_textbox, output_query, output_language, ], ) .success( answer_bot, [ textbox, bot, docs_textbox, output_query, output_language, dropdown_audience, ], [textbox, bot], queue=True, ) .success(lambda x: textbox, [textbox], [textbox]) ) # --------------------------------------------------------------------------------------- # OTHER TABS # --------------------------------------------------------------------------------------- with gr.Tab("ℹ️ About ClimateQ&A", elem_classes="max-height"): with gr.Row(): with gr.Column(scale=1): gr.Markdown( """

Climate change and environmental disruptions have become some of the most pressing challenges facing our planet today. 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.

However, comprehending the vast and complex scientific information can be daunting, as the scientific consensus references, such as the Intergovernmental Panel on Climate Change (IPCC) reports, span thousands of pages. To bridge this gap and make climate science more accessible, we introduce ClimateQ&A as a tool to distill expert-level knowledge into easily digestible insights about climate science.

💡 How does ClimateQ&A work?
ClimateQ&A harnesses modern OCR techniques to parse and preprocess IPCC reports. By leveraging state-of-the-art question-answering algorithms, ClimateQ&A is able to sift through the extensive collection of climate scientific reports and identify relevant passages in response to user inquiries. Furthermore, the integration of the ChatGPT API allows ClimateQ&A to present complex data in a user-friendly manner, summarizing key points and facilitating communication of climate science to a wider audience.
""" ) 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)*" ) gr.Markdown("## How to use ClimateQ&A") with gr.Row(): with gr.Column(scale=1): gr.Markdown( """ ### 💪 Getting started - In the chatbot section, simply type your climate-related question, and ClimateQ&A will provide an answer with references to relevant IPCC reports. - ClimateQ&A 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. - You can ask question in any language, ClimateQ&A is multi-lingual ! - ClimateQ&A integrates multiple sources (IPCC and IPBES, … ) to cover various aspects of environmental science, such as climate change and biodiversity. See all sources used below. """ ) with gr.Column(scale=1): gr.Markdown( """ ### ⚠️ Limitations
""" ) with gr.Tab("📧 Contact, feedback and feature requests"): gr.Markdown( """ 🤞 For any question or press request, contact Théo Alves Da Costa at theo.alvesdacosta@ekimetrics.com - ClimateQ&A 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 email, 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), if you are a climate science researcher and net to sift through another report, please let us know. *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)* """ ) with gr.Tab("📚 Sources", elem_classes="max-height"): gr.Markdown( """ | Source | Report | URL | Number of pages | Release date | | --- | --- | --- | --- | --- | IPCC | Summary for Policymakers. In: Climate Change 2021: The Physical Science Basis. Contribution of the WGI to the AR6 of the IPCC. | https://www.ipcc.ch/report/ar6/wg1/downloads/report/IPCC_AR6_WGI_SPM.pdf | 32 | 2021 IPCC | Full Report. In: Climate Change 2021: The Physical Science Basis. Contribution of the WGI to the AR6 of the IPCC. | https://report.ipcc.ch/ar6/wg1/IPCC_AR6_WGI_FullReport.pdf | 2409 | 2021 IPCC | Technical Summary. In: Climate Change 2021: The Physical Science Basis. Contribution of the WGI to the AR6 of the IPCC. | https://www.ipcc.ch/report/ar6/wg1/downloads/report/IPCC_AR6_WGI_TS.pdf | 112 | 2021 IPCC | Summary for Policymakers. In: Climate Change 2022: Impacts, Adaptation and Vulnerability. Contribution of the WGII to the AR6 of the IPCC. | https://www.ipcc.ch/report/ar6/wg2/downloads/report/IPCC_AR6_WGII_SummaryForPolicymakers.pdf | 34 | 2022 IPCC | Technical Summary. In: Climate Change 2022: Impacts, Adaptation and Vulnerability. Contribution of the WGII to the AR6 of the IPCC. | https://www.ipcc.ch/report/ar6/wg2/downloads/report/IPCC_AR6_WGII_TechnicalSummary.pdf | 84 | 2022 IPCC | Full Report. In: Climate Change 2022: Impacts, Adaptation and Vulnerability. Contribution of the WGII to the AR6 of the IPCC. | https://report.ipcc.ch/ar6/wg2/IPCC_AR6_WGII_FullReport.pdf | 3068 | 2022 IPCC | Summary for Policymakers. In: Climate Change 2022: Mitigation of Climate Change. Contribution of the WGIII to the AR6 of the IPCC. | https://www.ipcc.ch/report/ar6/wg3/downloads/report/IPCC_AR6_WGIII_SummaryForPolicymakers.pdf | 50 | 2022 IPCC | Technical Summary. In: Climate Change 2022: Mitigation of Climate Change. Contribution of the WGIII to the AR6 of the IPCC. | https://www.ipcc.ch/report/ar6/wg3/downloads/report/IPCC_AR6_WGIII_TechnicalSummary.pdf | 102 | 2022 IPCC | Full Report. In: Climate Change 2022: Mitigation of Climate Change. Contribution of the WGIII to the AR6 of the IPCC. | https://www.ipcc.ch/report/ar6/wg3/downloads/report/IPCC_AR6_WGIII_FullReport.pdf | 2258 | 2022 IPCC | Summary for Policymakers. In: Global Warming of 1.5°C. An IPCC Special Report on the impacts of global warming of 1.5°C above pre-industrial levels and related global greenhouse gas emission pathways, in the context of strengthening the global response to the threat of climate change, sustainable development, and efforts to eradicate poverty. | https://www.ipcc.ch/site/assets/uploads/sites/2/2022/06/SPM_version_report_LR.pdf | 24 | 2018 IPCC | Summary for Policymakers. In: Climate Change and Land: an IPCC special report on climate change, desertification, land degradation, sustainable land management, food security, and greenhouse gas fluxes in terrestrial ecosystems. | https://www.ipcc.ch/site/assets/uploads/sites/4/2022/11/SRCCL_SPM.pdf | 36 | 2019 IPCC | Summary for Policymakers. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2022/03/01_SROCC_SPM_FINAL.pdf | 36 | 2019 IPCC | Technical Summary. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2022/03/02_SROCC_TS_FINAL.pdf | 34 | 2019 IPCC | Chapter 1 - Framing and Context of the Report. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2022/03/03_SROCC_Ch01_FINAL.pdf | 60 | 2019 IPCC | Chapter 2 - High Mountain Areas. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2022/03/04_SROCC_Ch02_FINAL.pdf | 72 | 2019 IPCC | Chapter 3 - Polar Regions. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2022/03/05_SROCC_Ch03_FINAL.pdf | 118 | 2019 IPCC | Chapter 4 - Sea Level Rise and Implications for Low-Lying Islands, Coasts and Communities. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2022/03/06_SROCC_Ch04_FINAL.pdf | 126 | 2019 IPCC | Chapter 5 - Changing Ocean, Marine Ecosystems, and Dependent Communities. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2022/03/07_SROCC_Ch05_FINAL.pdf | 142 | 2019 IPCC | Chapter 6 - Extremes, Abrupt Changes and Managing Risk. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2022/03/08_SROCC_Ch06_FINAL.pdf | 68 | 2019 IPCC | Cross-Chapter Box 9: Integrative Cross-Chapter Box on Low-Lying Islands and Coasts. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2019/11/11_SROCC_CCB9-LLIC_FINAL.pdf | 18 | 2019 IPCC | Annex I: Glossary [Weyer, N.M. (ed.)]. In: IPCC Special Report on the Ocean and Cryosphere in a Changing Climate. | https://www.ipcc.ch/site/assets/uploads/sites/3/2022/03/10_SROCC_AnnexI-Glossary_FINAL.pdf | 28 | 2019 IPBES | Full Report. Global assessment report on biodiversity and ecosystem services of the IPBES. | https://zenodo.org/record/6417333/files/202206_IPBES%20GLOBAL%20REPORT_FULL_DIGITAL_MARCH%202022.pdf | 1148 | 2019 IPBES | Summary for Policymakers. Global assessment report on biodiversity and ecosystem services of the IPBES (Version 1). | https://zenodo.org/record/3553579/files/ipbes_global_assessment_report_summary_for_policymakers.pdf | 60 | 2019 IPBES | Full Report. Thematic assessment of the sustainable use of wild species of the IPBES. | https://zenodo.org/record/7755805/files/IPBES_ASSESSMENT_SUWS_FULL_REPORT.pdf | 1008 | 2022 IPBES | Summary for Policymakers. Summary for policymakers of the thematic assessment of the sustainable use of wild species of the IPBES. | https://zenodo.org/record/7411847/files/EN_SPM_SUSTAINABLE%20USE%20OF%20WILD%20SPECIES.pdf | 44 | 2022 IPBES | Full Report. Regional Assessment Report on Biodiversity and Ecosystem Services for Africa. | https://zenodo.org/record/3236178/files/ipbes_assessment_report_africa_EN.pdf | 494 | 2018 IPBES | Summary for Policymakers. Regional Assessment Report on Biodiversity and Ecosystem Services for Africa. | https://zenodo.org/record/3236189/files/ipbes_assessment_spm_africa_EN.pdf | 52 | 2018 IPBES | Full Report. Regional Assessment Report on Biodiversity and Ecosystem Services for the Americas. | https://zenodo.org/record/3236253/files/ipbes_assessment_report_americas_EN.pdf | 660 | 2018 IPBES | Summary for Policymakers. Regional Assessment Report on Biodiversity and Ecosystem Services for the Americas. | https://zenodo.org/record/3236292/files/ipbes_assessment_spm_americas_EN.pdf | 44 | 2018 IPBES | Full Report. Regional Assessment Report on Biodiversity and Ecosystem Services for Asia and the Pacific. | https://zenodo.org/record/3237374/files/ipbes_assessment_report_ap_EN.pdf | 616 | 2018 IPBES | Summary for Policymakers. Regional Assessment Report on Biodiversity and Ecosystem Services for Asia and the Pacific. | https://zenodo.org/record/3237383/files/ipbes_assessment_spm_ap_EN.pdf | 44 | 2018 IPBES | Full Report. Regional Assessment Report on Biodiversity and Ecosystem Services for Europe and Central Asia. | https://zenodo.org/record/3237429/files/ipbes_assessment_report_eca_EN.pdf | 894 | 2018 IPBES | Summary for Policymakers. Regional Assessment Report on Biodiversity and Ecosystem Services for Europe and Central Asia. | https://zenodo.org/record/3237468/files/ipbes_assessment_spm_eca_EN.pdf | 52 | 2018 IPBES | Full Report. Assessment Report on Land Degradation and Restoration. | https://zenodo.org/record/3237393/files/ipbes_assessment_report_ldra_EN.pdf | 748 | 2018 IPBES | Summary for Policymakers. Assessment Report on Land Degradation and Restoration. | https://zenodo.org/record/3237393/files/ipbes_assessment_report_ldra_EN.pdf | 48 | 2018 """ ) with gr.Tab("🛢️ Carbon Footprint"): gr.Markdown( """ 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 | 28gCO2e | CodeCarbon | | Development | Question Answering development | 114gCO2e | CodeCarbon | | Inference | Question Answering | ~0.102gCO2e / call | CodeCarbon | | Inference | API call to turbo-GPT | ~0.38gCO2e / call | https://medium.com/@chrispointon/the-carbon-footprint-of-chatgpt-e1bc14e4cc2a | Carbon Emissions are **relatively low but not negligible** compared to other usages: one question asked is around 0.482gCO2e - equivalent to 2.2m by car (https://datagir.ademe.fr/apps/impact-co2/) Or around 2 to 4 times more than a typical Google search. """ ) with gr.Tab("🪄 Changelog"): gr.Markdown( """ ##### v1.0.0 - 2023-10-25 - Cloned from ClimateQ&A - Added support for other topics """ ) demo.queue(concurrency_count=16) demo.launch()