File size: 31,488 Bytes
eead5d8
 
 
72edd2d
caf1faa
 
088e816
caf1faa
eead5d8
a5686cb
ff42e3f
f0fc5f8
71ab0a8
5f9881c
f842a0e
 
4b4bf28
bed4e9b
 
d26538b
887905a
4b4bf28
 
 
f0fc5f8
f842a0e
f0fc5f8
6d2199d
f0fc5f8
6b43c86
 
5f9881c
f0fc5f8
5f9881c
 
d14568c
088e816
5f9881c
481f3b1
5f9881c
6b43c86
5f9881c
caf1faa
088e816
 
6b43c86
088e816
6b43c86
f0fc5f8
d14568c
f0fc5f8
 
ff42e3f
 
 
6d2199d
ff42e3f
 
6b43c86
f0fc5f8
abfa81d
 
 
ff42e3f
46e3999
 
6d2199d
 
f0fc5f8
7498c33
 
 
 
 
99e2b1f
6d2199d
 
 
91c4196
6d2199d
 
 
 
91c4196
6d2199d
d4c1a74
6d2199d
 
91c4196
6d2199d
a4595fc
57a1ed7
ff42e3f
c974ee5
f0fc5f8
 
aa37f44
57a1ed7
 
f0fc5f8
6b43c86
 
d4c1a74
6b43c86
5f9881c
 
c974ee5
088e816
 
887905a
5f9881c
 
 
 
 
 
 
 
91f77da
3d561c7
6b43c86
 
91f77da
6b43c86
5f9881c
 
088e816
4ab6519
 
088e816
 
 
 
3d561c7
088e816
6b43c86
d4c1a74
5f9881c
 
caf1faa
5f9881c
6b43c86
088e816
d14568c
57a1ed7
088e816
 
 
 
 
7da1a3a
 
df5d08d
088e816
 
7da1a3a
 
 
 
 
 
 
d14568c
 
 
 
7da1a3a
d14568c
df5d08d
7da1a3a
 
 
 
 
 
 
 
 
 
 
 
34de7db
7da1a3a
 
 
df5d08d
 
 
48e003d
 
7335378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57a1ed7
7335378
57a1ed7
7335378
57a1ed7
7335378
 
 
 
24f8d00
 
 
7da1a3a
 
df5d08d
 
 
 
088e816
57a1ed7
df5d08d
24f8d00
7da1a3a
435c75a
24f8d00
 
 
 
 
 
 
63d1de4
24f8d00
 
 
 
 
4b4bf28
24f8d00
ee3f645
24f8d00
 
 
 
 
435c75a
24f8d00
d14568c
 
 
 
 
4b4bf28
d14568c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b4bf28
d14568c
 
4b4bf28
 
7fa8087
d14568c
4b4bf28
 
 
 
 
 
 
d14568c
 
 
 
4b4bf28
 
 
d14568c
 
 
4b4bf28
57a1ed7
46e3999
a5686cb
91c4196
 
 
6d2199d
91c4196
6d2199d
91c4196
 
 
 
12574b1
dc1d7e6
 
fdf1622
 
91c4196
6d2199d
91c4196
6d2199d
91c4196
 
caf1faa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0fc5f8
 
 
ff42e3f
 
787d3cb
c974ee5
787d3cb
38ed905
787d3cb
 
 
f0fc5f8
38ed905
787d3cb
 
7d9ec3d
 
 
 
5f9881c
787d3cb
f0fc5f8
 
c974ee5
 
 
 
 
 
6b43c86
 
 
 
 
 
 
c974ee5
3c9e1e2
6b43c86
df5d08d
49acaf1
 
 
 
5f9881c
f0fc5f8
fa9f031
f0fc5f8
5f9881c
bed4e9b
 
 
 
 
 
38ed905
12f47f8
27a9af5
4ab6519
a3bf481
c974ee5
3d561c7
 
 
a3bf481
887905a
4ab6519
fa9f031
49acaf1
fa9f031
3c9e1e2
 
5f9881c
3c9e1e2
5f9881c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8edfef8
5f9881c
 
 
 
 
3c9e1e2
5f9881c
caf1faa
3c9e1e2
 
d14568c
 
 
3c9e1e2
887905a
5f9881c
3c9e1e2
6b43c86
3c9e1e2
 
 
887905a
5f9881c
7335378
3c9e1e2
 
 
5f9881c
 
 
 
 
 
 
 
3c9e1e2
 
 
 
 
 
 
 
 
6b43c86
 
57a1ed7
7335378
 
 
57a1ed7
7335378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49acaf1
7335378
d14568c
 
 
 
f0fc5f8
 
 
 
 
6b43c86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0fc5f8
4b4bf28
 
 
088e816
caf1faa
088e816
 
 
 
 
 
caf1faa
088e816
caf1faa
088e816
 
caf1faa
088e816
 
caf1faa
088e816
 
caf1faa
6b43c86
 
 
 
 
caf1faa
887905a
f0fc5f8
 
887905a
4b4bf28
 
 
bed4e9b
 
 
4b4bf28
 
 
6b43c86
4b4bf28
49acaf1
6b43c86
 
 
 
 
4b4bf28
 
0c4d82b
6b43c86
 
7335378
0c4d82b
4b4bf28
 
 
49acaf1
 
 
 
4b4bf28
0c4d82b
49acaf1
7335378
 
4b4bf28
 
 
 
 
 
 
 
 
 
 
 
 
887905a
b6bb4d7
d730458
e77b244
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
from climateqa.engine.embeddings import get_embeddings_function
embeddings_function = get_embeddings_function()

from climateqa.knowledge.openalex import OpenAlex
from sentence_transformers import CrossEncoder

# reranker = CrossEncoder("mixedbread-ai/mxbai-rerank-xsmall-v1")
oa = OpenAlex()

import gradio as gr
import pandas as pd
import numpy as np
import os
import time
import re
import json

from gradio import ChatMessage

# from gradio_modal import Modal

from io import BytesIO
import base64

from datetime import datetime
from azure.storage.fileshare import ShareServiceClient

from utils import create_user_id

from langchain_chroma import Chroma
from collections import defaultdict

# ClimateQ&A imports
from climateqa.engine.llm import get_llm
from climateqa.engine.vectorstore import get_pinecone_vectorstore
# from climateqa.knowledge.retriever import ClimateQARetriever
from climateqa.engine.reranker import get_reranker
from climateqa.engine.embeddings import get_embeddings_function
from climateqa.engine.chains.prompts import audience_prompts
from climateqa.sample_questions import QUESTIONS
from climateqa.constants import POSSIBLE_REPORTS, OWID_CATEGORIES
from climateqa.utils import get_image_from_azure_blob_storage
from climateqa.engine.keywords import make_keywords_chain
# from climateqa.engine.chains.answer_rag import make_rag_papers_chain
from climateqa.engine.graph import make_graph_agent,display_graph
from climateqa.engine.embeddings import get_embeddings_function

from front.utils import make_html_source,parse_output_llm_with_sources,serialize_docs,make_toolbox,generate_html_graphs

from front.utils import make_html_source, make_html_figure_sources,parse_output_llm_with_sources,serialize_docs,make_toolbox

# 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,
}

account_key = os.environ["BLOB_ACCOUNT_KEY"]
if len(account_key) == 86:
    account_key += "=="

credential = {
    "account_key": account_key,
    "account_name": os.environ["BLOB_ACCOUNT_NAME"],
}

account_url = os.environ["BLOB_ACCOUNT_URL"]
file_share_name = "climateqa"
service = ShareServiceClient(account_url=account_url, credential=credential)
share_client = service.get_share_client(file_share_name)

user_id = create_user_id()

vectorstore_graphs = Chroma(persist_directory="/home/tim/ai4s/climate_qa/climate-question-answering/data/vectorstore_owid", embedding_function=embeddings_function) # TODO make it api call


# Create vectorstore and retriever
vectorstore = get_pinecone_vectorstore(embeddings_function)
llm = get_llm(provider="openai",max_tokens = 1024,temperature = 0.0)
reranker = get_reranker("nano")
# agent = make_graph_agent(llm,vectorstore,reranker)

# agent = make_graph_agent(llm,vectorstore,reranker)
agent = make_graph_agent(llm=llm, vectorstore_ipcc=vectorstore, vectorstore_graphs=vectorstore_graphs, reranker=reranker)

async def chat(query,history,audience,sources,reports,current_graphs):
    """taking a query and a message history, use a pipeline (reformulation, retriever, answering) to yield a tuple of:
    (messages in gradio format, messages in langchain format, source documents)"""

    date_now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    print(f">> NEW QUESTION ({date_now}) : {query}")

    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 default values
    if sources is None or len(sources) == 0:
        sources = ["IPCC", "IPBES", "IPOS"]

    if reports is None or len(reports) == 0:
        reports = []
    
    inputs = {"user_input": query,"audience": audience_prompt,"sources":sources}
    result = agent.astream_events(inputs,version = "v1") 
    
    # path_reformulation = "/logs/reformulation/final_output"
    # path_keywords = "/logs/keywords/final_output"
    # path_retriever = "/logs/find_documents/final_output"
    # path_answer = "/logs/answer/streamed_output_str/-"

    docs = []
    docs_used = True
    docs_html = ""
    output_query = ""
    output_language = ""
    output_keywords = ""
    gallery = []
    updates = []
    start_streaming = False
    figures = '<div class="figures-container"> <p> Go to the "Figures" tab at the top of the page to see full size images </p> </div>'
    graphs_html = ""    
    steps_display = {
        "categorize_intent":("πŸ”„οΈ Analyzing user message",True),
        "transform_query":("πŸ”„οΈ Thinking step by step to answer the question",True),
        "retrieve_documents":("πŸ”„οΈ Searching in the knowledge base",False),
    }
    
    used_documents = []
    answer_message_content = ""
    try:
        async for event in result:
            if "langgraph_node" in event["metadata"]:
                node = event["metadata"]["langgraph_node"]

                if event["event"] == "on_chain_end" and event["name"] == "retrieve_documents" :# when documents are retrieved
                    try:
                        docs = event["data"]["output"]["documents"]
                        docs_html = []
                        textual_docs = [d for d in docs if d.metadata["chunk_type"] == "text"]
                        for i, d in enumerate(textual_docs, 1):
                            if d.metadata["chunk_type"] == "text":
                                docs_html.append(make_html_source(d, i))
                        
                        used_documents = used_documents + [f"{d.metadata['short_name']} - {d.metadata['name']}" for d in docs]
                        history[-1].content = "Adding sources :\n\n - " + "\n - ".join(np.unique(used_documents))
                            
                        docs_html = "".join(docs_html)
                        
                    except Exception as e:
                        print(f"Error getting documents: {e}")
                        print(event)
 
                elif event["name"] in steps_display.keys() and event["event"] == "on_chain_start": #display steps
                    event_description,display_output = steps_display[node]
                    if not hasattr(history[-1], 'metadata') or history[-1].metadata["title"] != event_description: # if a new step begins
                        history.append(ChatMessage(role="assistant", content = "", metadata={'title' :event_description}))
 
                elif event["name"] != "transform_query" and event["event"] == "on_chat_model_stream" and node in ["answer_rag", "answer_search","answer_chitchat"]:# if streaming answer
                    if start_streaming == False:
                        start_streaming = True
                        history.append(ChatMessage(role="assistant", content = ""))
                    answer_message_content +=  event["data"]["chunk"].content
                    answer_message_content = parse_output_llm_with_sources(answer_message_content)
                    history[-1] = ChatMessage(role="assistant", content = answer_message_content)
                    # history.append(ChatMessage(role="assistant", content = new_message_content))

                elif event["name"] in ["retrieve_graphs", "retrieve_graphs_ai"] and event["event"] == "on_chain_end":
                    try:
                        recommended_content = event["data"]["output"]["recommended_content"]
                        # graphs = [
                        #     {
                        #         "embedding": x.metadata["returned_content"],
                        #         "metadata": {
                        #             "source": x.metadata["source"],
                        #             "category": x.metadata["category"]
                        #             }
                        #             } for x in recommended_content if x.metadata["source"] == "OWID"
                        #             ]
                        
                        unique_graphs = []
                        seen_embeddings = set()

                        for x in recommended_content:
                            embedding = x.metadata["returned_content"]
                            
                            # Check if the embedding has already been seen
                            if embedding not in seen_embeddings:
                                unique_graphs.append({
                                    "embedding": embedding,
                                    "metadata": {
                                        "source": x.metadata["source"],
                                        "category": x.metadata["category"]
                                    }
                                })
                                # Add the embedding to the seen set
                                seen_embeddings.add(embedding)


                        categories = {}
                        for graph in unique_graphs:
                            category = graph['metadata']['category']
                            if category not in categories:
                                categories[category] = []
                            categories[category].append(graph['embedding'])

                        
                        for category, embeddings in categories.items():
                            graphs_html += f"<h3>{category}</h3>"
                            for embedding in embeddings:
                                graphs_html += f"<div>{embedding}</div>"
                                
                                
                    except Exception as e:
                        print(f"Error getting graphs: {e}")



                if event["name"] == "transform_query" and event["event"] =="on_chain_end":
                    if hasattr(history[-1],"content"):
                        history[-1].content += "Decompose question into sub-questions: \n\n - " + "\n - ".join([q["question"] for q in event["data"]["output"]["remaining_questions"]])
                        
                if event["name"] == "categorize_intent" and event["event"] == "on_chain_start":
                    print("X")
            
            yield history, docs_html, output_query, output_language, gallery, figures, graphs_html #,output_query,output_keywords
 
    except Exception as e:
        print(event, "has failed")
        raise gr.Error(f"{e}")


    try:
        # Log answer on Azure Blob Storage
        if os.getenv("GRADIO_ENV") != "local":
            timestamp = str(datetime.now().timestamp())
            file = timestamp + ".json"
            prompt = history[1]["content"]
            logs = {
                "user_id": str(user_id),
                "prompt": prompt,
                "query": prompt,
                "question":output_query,
                "sources":sources,
                "docs":serialize_docs(docs),
                "answer": history[-1].content,
                "time": timestamp,
            }
            log_on_azure(file, logs, share_client)
    except Exception as e:
        print(f"Error logging on Azure Blob Storage: {e}")
        raise gr.Error(f"ClimateQ&A Error: {str(e)[:100]} - The error has been noted, try another question and if the error remains, you can contact us :)")



        
    # image_dict = {}
    # for i,doc in enumerate(docs):
        
    #     if doc.metadata["chunk_type"] == "image":
    #         try:
    #             key = f"Image {i+1}"
    #             image_path = doc.metadata["image_path"].split("documents/")[1]
    #             img = get_image_from_azure_blob_storage(image_path)

    #             # Convert the image to a byte buffer
    #             buffered = BytesIO()
    #             img.save(buffered, format="PNG")
    #             img_str = base64.b64encode(buffered.getvalue()).decode()

    #             # Embedding the base64 string in Markdown
    #             markdown_image = f"![Alt text](data:image/png;base64,{img_str})"
    #             image_dict[key] = {"img":img,"md":markdown_image,"short_name": doc.metadata["short_name"],"figure_code":doc.metadata["figure_code"],"caption":doc.page_content,"key":key,"figure_code":doc.metadata["figure_code"], "img_str" : img_str}
    #         except Exception as e:
    #             print(f"Skipped adding image {i} because of {e}")

    # if len(image_dict) > 0:

    #     gallery = [x["img"] for x in list(image_dict.values())]
    #     img = list(image_dict.values())[0]
    #     img_md = img["md"]
    #     img_caption = img["caption"]
    #     img_code = img["figure_code"]
    #     if img_code != "N/A":
    #         img_name = f"{img['key']} - {img['figure_code']}"
    #     else:
    #         img_name = f"{img['key']}"

    #     history.append(ChatMessage(role="assistant", content = f"\n\n{img_md}\n<p class='chatbot-caption'><b>{img_name}</b> - {img_caption}</p>"))
        
    docs_figures = [d for d in docs if d.metadata["chunk_type"] == "image"]
    for i, doc in enumerate(docs_figures):
        if doc.metadata["chunk_type"] == "image":
            try:
                key = f"Image {i+1}"

                image_path = doc.metadata["image_path"].split("documents/")[1]
                img = get_image_from_azure_blob_storage(image_path)

                # Convert the image to a byte buffer
                buffered = BytesIO()
                img.save(buffered, format="PNG")
                img_str = base64.b64encode(buffered.getvalue()).decode()
                
                figures = figures + make_html_figure_sources(doc, i, img_str)  
                
                gallery.append(img)

            except Exception as e:
                print(f"Skipped adding image {i} because of {e}")
   
        
    

    yield history, docs_html, output_query, output_language, gallery, figures, graphs_html#,output_query,output_keywords


def save_feedback(feed: str, user_id):
    if len(feed) > 1:
        timestamp = str(datetime.now().timestamp())
        file = user_id + timestamp + ".json"
        logs = {
            "user_id": user_id,
            "feedback": feed,
            "time": timestamp,
        }
        log_on_azure(file, logs, share_client)
        return "Feedback submitted, thank you!"




def log_on_azure(file, logs, share_client):
    logs = json.dumps(logs)
    file_client = share_client.get_file_client(file)
    file_client.upload_file(logs)


def generate_keywords(query):
    chain = make_keywords_chain(llm)
    keywords = chain.invoke(query)
    keywords = " AND ".join(keywords["keywords"])
    return keywords



papers_cols_widths = {
    "doc":50,
    "id":100,
    "title":300,
    "doi":100,
    "publication_year":100,
    "abstract":500,
    "rerank_score":100,
    "is_oa":50,
}

papers_cols = list(papers_cols_widths.keys())
papers_cols_widths = list(papers_cols_widths.values())


# --------------------------------------------------------------------
# Gradio
# --------------------------------------------------------------------


init_prompt = """
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**.

❓ 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 the IPCC or IPBES reports, or both.

⚠️ 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.*

πŸ›ˆ Information
Please note that we log your questions for meta-analysis purposes, so avoid sharing any sensitive or personal information.


What do you want to learn ?
"""


def vote(data: gr.LikeData):
    if data.liked:
        print(data.value)
    else:
        print(data)

def save_graph(saved_graphs_state, embedding, category):
    print(f"\nCategory:\n{saved_graphs_state}\n")
    if category not in saved_graphs_state:
        saved_graphs_state[category] = []
    if embedding not in saved_graphs_state[category]:
        saved_graphs_state[category].append(embedding)
    return saved_graphs_state, gr.Button("Graph Saved")



with gr.Blocks(title="Climate Q&A", css_paths=os.getcwd()+ "/style.css", theme=theme,elem_id = "main-component") as demo:
    chat_completed_state = gr.State(0)
    current_graphs = gr.State([])
    saved_graphs = gr.State({})
    
    with gr.Tab("ClimateQ&A"):

        with gr.Row(elem_id="chatbot-row"):
            with gr.Column(scale=2):
                chatbot = gr.Chatbot(
                    value = [ChatMessage(role="assistant", content=init_prompt)],
                    type = "messages",
                    show_copy_button=True,
                    show_label = False,
                    elem_id="chatbot",
                    layout = "panel",
                    avatar_images = (None,"https://i.ibb.co/YNyd5W2/logo4.png"),
                    max_height="80vh",
                    height="100vh"
                )
                
                # bot.like(vote,None,None)



                with gr.Row(elem_id = "input-message"):
                    textbox=gr.Textbox(placeholder="Ask me anything here!",show_label=False,scale=7,lines = 1,interactive = True,elem_id="input-textbox")
                 

            with gr.Column(scale=1.5, 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(visible = False)
                        first_key = list(QUESTIONS.keys())[0]
                        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")

                        samples = []
                        for i,key in enumerate(QUESTIONS.keys()):

                            examples_visible = True if i == 0 else False

                            with gr.Row(visible = examples_visible) as group_examples:

                                examples_questions = gr.Examples(
                                    QUESTIONS[key],
                                    [examples_hidden],
                                    examples_per_page=8,
                                    run_on_click=False,
                                    elem_id=f"examples{i}",
                                    api_name=f"examples{i}",
                                    # label = "Click on the example question or enter your own",
                                    # cache_examples=True,
                                )
                            
                            samples.append(group_examples)


                    with gr.Tab("Sources",elem_id = "tab-citations",id = 1):
                        sources_textbox = gr.HTML(show_label=False, elem_id="sources-textbox")
                        docs_textbox = gr.State("")
                        

                        

                    # with Modal(visible = False) as config_modal:
                    with gr.Tab("Configuration",elem_id = "tab-config",id = 2):

                        gr.Markdown("Reminders: You can talk in any language, ClimateQ&A is multi-lingual!")


                        dropdown_sources = gr.CheckboxGroup(
                            ["IPCC", "IPBES","IPOS"],
                            label="Select source",
                            value=["IPCC"],
                            interactive=True,
                        )

                        dropdown_reports = gr.Dropdown(
                            POSSIBLE_REPORTS,
                            label="Or select specific reports",
                            multiselect=True,
                            value=None,
                            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)
                    
                
                    with gr.Tab("Recommended content", elem_id="tab-recommended_content", id=4) as recommended_content_tab:
                        # placeholder_message = gr.HTML("<h2>There are no graphs to be displayed at the moment. Try asking another question.</h2>")
                        graphs_container = gr.HTML("<h2>There are no graphs to be displayed at the moment. Try asking another question.</h2>")
                        current_graphs.change(lambda x : x, inputs=[current_graphs], outputs=[graphs_container])
                        gr.Textbox(current_graphs.value)

                        # @gr.render(inputs=[current_graphs])
                        # def display_default_recommended(current_graphs):
                        #     if len(current_graphs)==0:
                        #         placeholder_message = gr.HTML("<h2>There are no graphs to be displayed at the moment. Try asking another question.</h2>")

                        # @gr.render(inputs=[current_graphs],triggers=[chat_completed_state.change])
                        # def render_graphs(current_graph_list):
                        #     global saved_graphs
                        #     with gr.Column():
                        #         print(f"\ncurrent_graph_list:\n{current_graph_list}")
                        #         for (embedding, category) in current_graph_list:
                        #             graphs_placeholder = gr.HTML(embedding, elem_id="graphs-placeholder")
                        #             save_btn = gr.Button("Save Graph")
                        #             save_btn.click(
                        #                 save_graph,
                        #                 [saved_graphs, gr.State(embedding), gr.State(category)],
                        #                 [saved_graphs, save_btn]
                        #             )
                                    
                                                            
                        # # Display current_graphs
                        # with gr.Row():
                        #     for embedding in current_graphs:
                        #         with gr.Column():
                        #             gr.HTML(embedding, elem_id="graphs-placeholder")
                        #             save_btn = gr.Button("Save Graph")
                        #             save_btn.click(
                        #                 save_graph,
                        #                 [saved_graphs, gr.State(embedding)],
                        #                 [saved_graphs, save_btn]
                        #             )

                            
                    with gr.Tab("Figures",elem_id = "tab-figures",id = 3):
                        figures_cards = gr.HTML(show_label=False, elem_id="sources-figures")



#---------------------------------------------------------------------------------------
# OTHER TABS
#---------------------------------------------------------------------------------------

    # with gr.Tab("Recommended content", elem_id="tab-recommended_content2") as recommended_content_tab2:
        
    #     @gr.render(inputs=[current_graphs])
    #     def display_default_recommended_head(current_graphs_list):
    #         if len(current_graphs_list)==0:
    #             gr.HTML("<h2>There are no graphs to be displayed at the moment. Try asking another question.</h2>")

    #     @gr.render(inputs=[current_graphs],triggers=[chat_completed_state.change])
    #     def render_graphs_head(current_graph_list):
    #         global saved_graphs

    #         category_dict = defaultdict(list)
    #         for (embedding, category) in current_graph_list:
    #             category_dict[category].append(embedding)
            
    #         for category in category_dict:
    #             with gr.Tab(category):
    #                 splits = [category_dict[category][i:i+3] for i in range(0, len(category_dict[category]), 3)]
    #                 for row in splits:
    #                     with gr.Row():
    #                         for embedding in row:
    #                             with gr.Column():
    #                                 gr.HTML(embedding, elem_id="graphs-placeholder")
    #                                 save_btn = gr.Button("Save Graph")
    #                                 save_btn.click(
    #                                     save_graph,
    #                                     [saved_graphs, gr.State(embedding), gr.State(category)],
    #                                     [saved_graphs, save_btn]
    #                                 )



    # with gr.Tab("Saved Graphs", elem_id="tab-saved-graphs") as saved_graphs_tab:
        
    #     @gr.render(inputs=[saved_graphs])
    #     def display_default_save(saved):
    #         if len(saved)==0:
    #             gr.HTML("<h2>You have not saved any graphs yet</h2>")

    #     @gr.render(inputs=[saved_graphs], triggers=[saved_graphs.change])
    #     def view_saved_graphs(graphs_list):
    #         categories = [category for category in graphs_list] # graphs_list.keys()
    #         for category in categories:
    #             with gr.Tab(category):
    #                 splits = [graphs_list[category][i:i+3] for i in range(0, len(graphs_list[category]), 3)]
    #                 for row in splits:
    #                     with gr.Row():
    #                         for graph in row:
    #                             gr.HTML(graph, elem_id="graphs-placeholder")



    with gr.Tab("Figures",elem_id = "tab-images",elem_classes = "max-height other-tabs"):
        gallery_component = gr.Gallery()

    # with gr.Tab("Papers (beta)",elem_id = "tab-papers",elem_classes = "max-height other-tabs"):

    #     with gr.Row():
    #         with gr.Column(scale=1):
    #             query_papers = gr.Textbox(placeholder="Question",show_label=False,lines = 1,interactive = True,elem_id="query-papers")
    #             keywords_papers = gr.Textbox(placeholder="Keywords",show_label=False,lines = 1,interactive = True,elem_id="keywords-papers")
    #             after = gr.Slider(minimum=1950,maximum=2023,step=1,value=1960,label="Publication date",show_label=True,interactive=True,elem_id="date-papers")
    #             search_papers = gr.Button("Search",elem_id="search-papers",interactive=True)

    #         with gr.Column(scale=7):

    #             with gr.Tab("Summary",elem_id="papers-summary-tab"):
    #                 papers_summary = gr.Markdown(visible=True,elem_id="papers-summary")

    #             with gr.Tab("Relevant papers",elem_id="papers-results-tab"):
    #                 papers_dataframe = gr.Dataframe(visible=True,elem_id="papers-table",headers = papers_cols)

    #             with gr.Tab("Citations network",elem_id="papers-network-tab"):
    #                 citations_network = gr.HTML(visible=True,elem_id="papers-citations-network")

    # with gr.Tab("Saved Graphs", elem_id="tab-saved-graphs", id=4) as saved_graphs_tab:
    #     @gr.render(inputs=[saved_graphs], triggers=[saved_graphs.change])
    #     def view_saved_graphs(graphs_list):
    #         for graph in graphs_list:
    #             gr.HTML(graph, elem_id="graphs-placeholder")
            
    with gr.Tab("About",elem_classes = "max-height other-tabs"):
        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("See more info at [https://climateqa.com](https://climateqa.com/docs/intro/)")


    def start_chat(query,history):
        # history = history + [(query,None)]
        # history = [tuple(x) for x in history]
        history = history + [ChatMessage(role="user", content=query)]
        return (gr.update(interactive = False),gr.update(selected=1),history)
    
    def finish_chat():
        return (gr.update(interactive = True,value = ""),gr.update(selected=3))


    def change_completion_status(current_state):
        current_state = 1 - current_state
        return current_state
    
    
    (textbox
        .submit(start_chat, [textbox,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_textbox")
        .then(chat, [textbox,chatbot,dropdown_audience, dropdown_sources,dropdown_reports, current_graphs], [chatbot,sources_textbox,output_query,output_language,gallery_component, figures_cards, current_graphs],concurrency_limit = 8,api_name = "chat_textbox")
        .then(finish_chat, None, [textbox,tabs],api_name = "finish_chat_textbox")
        .then(change_completion_status, [chat_completed_state], [chat_completed_state])
        # .then(lambda graphs : generate_html_graphs(graphs), [current_graphs], [graphs_container],)

    )

    (examples_hidden
        # .change(start_chat, [examples_hidden,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_examples")
        # .then(chat, [examples_hidden,chatbot,dropdown_audience, dropdown_sources,dropdown_reports,current_graphs], [chatbot,sources_textbox,output_query,output_language,gallery_component, current_graphs],concurrency_limit = 8,api_name = "chat_examples")
        # .then(finish_chat, None, [textbox,tabs],api_name = "finish_chat_examples")
        # .then(change_completion_status, [chat_completed_state], [chat_completed_state])
        .change(start_chat, [examples_hidden,chatbot], [textbox,tabs,chatbot],queue = False,api_name = "start_chat_examples")
        .then(chat, [examples_hidden,chatbot,dropdown_audience, dropdown_sources,dropdown_reports, current_graphs], [chatbot,sources_textbox,output_query,output_language,gallery_component, figures_cards, current_graphs],concurrency_limit = 8,api_name = "chat_examples")
        .then(finish_chat, None, [textbox],api_name = "finish_chat_examples")
        # .then(lambda graphs : graphs, [current_graphs], [graphs_container])

    )


    def change_sample_questions(key):
        index = list(QUESTIONS.keys()).index(key)
        visible_bools = [False] * len(samples)
        visible_bools[index] = True
        return [gr.update(visible=visible_bools[i]) for i in range(len(samples))]



    dropdown_samples.change(change_sample_questions,dropdown_samples,samples)


    demo.queue()

demo.launch(ssr_mode=False)