File size: 28,495 Bytes
2e2dda5
 
 
b23a15f
2e2dda5
b23a15f
 
 
2e2dda5
b23a15f
 
 
2e2dda5
 
 
5ae9dd3
b23a15f
5ae9dd3
2e2dda5
b23a15f
 
 
 
 
5ae9dd3
1fd73d7
5ae9dd3
b23a15f
 
2e2dda5
394970f
2034f95
 
b23a15f
 
 
 
 
 
2e2dda5
 
 
b23a15f
 
0b90a76
 
b23a15f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ae9dd3
 
b23a15f
 
5ae9dd3
 
 
b23a15f
 
 
 
 
5ae9dd3
b23a15f
 
 
5ae9dd3
b23a15f
 
 
 
 
 
 
 
 
 
5ae9dd3
 
 
 
 
 
 
b23a15f
 
5ae9dd3
 
b23a15f
 
5ae9dd3
2e2dda5
 
b23a15f
 
 
 
2e2dda5
 
 
b23a15f
 
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b90a76
b23a15f
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b23a15f
 
 
72c9086
2e2dda5
 
b23a15f
2e2dda5
b23a15f
 
 
72c9086
b23a15f
2e2dda5
b23a15f
 
2e2dda5
b23a15f
 
2e2dda5
 
b23a15f
 
 
2e2dda5
 
b23a15f
5ae9dd3
 
 
 
 
4ca2226
55b3b62
b23a15f
5ae9dd3
 
b23a15f
 
 
18caeb6
 
b23a15f
 
 
 
2e2dda5
5ae9dd3
 
 
 
 
b23a15f
2e2dda5
 
b23a15f
 
 
ad41a02
5ae9dd3
ad41a02
b23a15f
2e2dda5
 
b23a15f
2e2dda5
 
b23a15f
2e2dda5
18caeb6
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
b23a15f
5ae9dd3
 
 
 
 
 
 
 
 
 
cccd8e6
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da86235
5ae9dd3
 
 
 
 
 
 
 
 
34af0a6
5ae9dd3
 
 
 
 
 
 
34af0a6
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b90a76
 
 
 
5ae9dd3
0b90a76
 
 
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fd9bdf
5ae9dd3
8fd9bdf
 
 
 
 
 
 
 
 
 
 
 
 
5ae9dd3
34af0a6
8fd9bdf
 
 
 
 
34af0a6
8fd9bdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ae9dd3
 
8fd9bdf
 
 
 
 
 
5ae9dd3
 
 
 
 
 
b23a15f
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b23a15f
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b23a15f
5ae9dd3
 
b23a15f
5ae9dd3
b23a15f
 
 
 
5ae9dd3
 
 
 
 
 
b23a15f
5ae9dd3
b23a15f
 
 
 
5ae9dd3
 
2e2dda5
c23fe85
5ae9dd3
 
abde31c
9e5925c
5ae9dd3
 
 
 
 
9e5925c
5ae9dd3
 
9e5925c
5ae9dd3
 
394970f
 
 
 
5ae9dd3
 
 
 
 
 
394970f
5ae9dd3
394970f
 
 
5ae9dd3
394970f
5ae9dd3
394970f
 
 
 
 
 
 
 
 
 
 
 
5ae9dd3
394970f
b23a15f
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef7792d
394970f
 
 
5ae9dd3
394970f
 
 
 
 
 
5ae9dd3
 
 
 
 
 
394970f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ae9dd3
394970f
ef7792d
394970f
 
 
 
 
 
 
 
 
5ae9dd3
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import uuid
import os
import re
import sys
sys.path.insert(1, "/".join(os.path.realpath(__file__).split("/")[0:-2])+"/semantic_search")
sys.path.insert(1, "/".join(os.path.realpath(__file__).split("/")[0:-2])+"/RAG")
sys.path.insert(1, "/".join(os.path.realpath(__file__).split("/")[0:-2])+"/utilities")
import boto3
import requests
from boto3 import Session
import botocore.session
import json
import random
import string
import rag_DocumentLoader 
import rag_DocumentSearcher
#import colpali
import pandas as pd
from PIL import Image 
import shutil
import base64
import time
import botocore
#from langchain.callbacks.base import BaseCallbackHandler
#import streamlit_nested_layout
#from IPython.display import clear_output, display, display_markdown, Markdown
from requests_aws4auth import AWS4Auth
import colpali
from requests.auth import HTTPBasicAuth



st.set_page_config(
    #page_title="Semantic Search using OpenSearch",
    layout="wide",
    page_icon="images/opensearch_mark_default.png"
)
parent_dirname = "/".join((os.path.dirname(__file__)).split("/")[0:-1])
USER_ICON = "images/user.png"
AI_ICON = "images/opensearch-twitter-card.png"
REGENERATE_ICON = "images/regenerate.png"
s3_bucket_ = "pdf-repo-uploads"
            #"pdf-repo-uploads"
# polly_client = boto3.Session(
#             region_name='us-east-1').client('polly')

# Check if the user ID is already stored in the session state
if 'user_id' in st.session_state:
    user_id = st.session_state['user_id']
    #print(f"User ID: {user_id}")

# If the user ID is not yet stored in the session state, generate a random UUID
else:
    user_id = str(uuid.uuid4())
    st.session_state['user_id'] = user_id


if 'session_id' not in st.session_state:
    st.session_state['session_id'] = ""
    
if "chats" not in st.session_state:
    st.session_state.chats = [
        {
            'id': 0,
            'question': '',
            'answer': ''
        }
    ]

if "questions_" not in st.session_state:
    st.session_state.questions_ = []
    
    
if "show_columns" not in st.session_state:
    st.session_state.show_columns = False
    
if "answer_ready" not in st.session_state:
    st.session_state.answer_ready = False

if "answers_" not in st.session_state:
    st.session_state.answers_ = []

if "input_index" not in st.session_state:
    st.session_state.input_index = "hpijan2024hometrack"#"globalwarmingnew"#"hpijan2024hometrack_no_img_no_table"
    
if "input_is_rerank" not in st.session_state:
    st.session_state.input_is_rerank = True
    
if "input_is_colpali" not in st.session_state:
    st.session_state.input_is_colpali = False

if "input_copali_rerank" not in st.session_state:
    st.session_state.input_copali_rerank = False
    
if "input_table_with_sql" not in st.session_state:
    st.session_state.input_table_with_sql = False
    
if "input_query" not in st.session_state:
    if(st.session_state.input_index == "globalwarming"):
        st.session_state.input_query="What is the projected energy percentage from renewable sources in future ?"#"What is the projected energy percentage from renewable sources in future?"#"Which city in United Kingdom has the highest average housing price ?"#"How many aged above 85 years died due to covid ?"# What is the projected energy from renewable sources ?"
    if(st.session_state.input_index == "hpijan2024hometrack"):
        st.session_state.input_query="which city has the highest average housing price in UK ?"#"What is the projected energy percentage from renewable sources in future?"#"Which city in United Kingdom has the highest average housing price ?"#"How many aged above 85 years died due to covid ?"# What is the projected energy from renewable sources ?"
    if(st.session_state.input_index == "covid19ie"):
        st.session_state.input_query="How many aged above 85 years died due to covid ?"#"What is the projected energy percentage from renewable sources in future?"#"Which city in United Kingdom has the highest average housing price ?"#"How many aged above 85 years died due to covid ?"# What is the projected energy from renewable sources ?"


if "input_rag_searchType" not in st.session_state:
    st.session_state.input_rag_searchType = ["Vector Search"]
    



st.markdown("""
    <style>
    [data-testid=column]:nth-of-type(2) [data-testid=stVerticalBlock]{
        gap: 0rem;
    }
    [data-testid=column]:nth-of-type(1) [data-testid=stVerticalBlock]{
        gap: 0rem;
    }
    </style>
    """,unsafe_allow_html=True)

################ OpenSearch Py client #####################
    
# credentials = boto3.Session().get_credentials()
# awsauth = AWSV4SignerAuth(credentials, region, service)

# ospy_client = OpenSearch(
#     hosts = [{'host': 'search-opensearchservi-75ucark0bqob-bzk6r6h2t33dlnpgx2pdeg22gi.us-east-1.es.amazonaws.com', 'port': 443}],
#     http_auth = awsauth,
#     use_ssl = True,
#     verify_certs = True,
#     connection_class = RequestsHttpConnection,
#     pool_maxsize = 20
# )

################# using boto3 credentials ###################

awsauth = HTTPBasicAuth('master',st.secrets['ml_search_demo_api_access'])


################# using boto3 credentials ####################



# if "input_searchType" not in st.session_state:
#     st.session_state.input_searchType = "Conversational Search (RAG)"

# if "input_temperature" not in st.session_state:
#     st.session_state.input_temperature = "0.001"

# if "input_topK" not in st.session_state:
#     st.session_state.input_topK = 200

# if "input_topP" not in st.session_state:
#     st.session_state.input_topP = 0.95

# if "input_maxTokens" not in st.session_state:
#     st.session_state.input_maxTokens = 1024


def write_logo():
    col1, col2, col3 = st.columns([5, 1, 5])
    with col2:
        st.image(AI_ICON, use_column_width='always') 

def write_top_bar():
    col1, col2 = st.columns([77,23])
    with col1:
        st.write("")
        st.header("Chat with your data",divider='rainbow')
        
        #st.image(AI_ICON, use_column_width='always')
    
    with col2:
        st.write("")
        st.write("")
        clear = st.button("Clear")
    st.write("")
    st.write("")
    return clear

clear = write_top_bar()

if clear:
    st.session_state.questions_ = []
    st.session_state.answers_ = []
    st.session_state.input_query=""
    # st.session_state.input_searchType="Conversational Search (RAG)"
    # st.session_state.input_temperature = "0.001"
    # st.session_state.input_topK = 200
    # st.session_state.input_topP = 0.95
    # st.session_state.input_maxTokens = 1024


def handle_input():
    # st.session_state.answer_ready = True
    # st.session_state.show_columns = False  # reset column display
    print("Question: "+st.session_state.input_query)
    print("-----------")
    print("\n\n")
    if(st.session_state.input_query==''):
        return ""
    inputs = {}
    for key in st.session_state:
        if key.startswith('input_'):
            inputs[key.removeprefix('input_')] = st.session_state[key]
    st.session_state.inputs_ = inputs
    
    #######
    
    
    #st.write(inputs) 
    question_with_id = {
        'question': inputs["query"],
        'id': len(st.session_state.questions_)
    }
    st.session_state.questions_.append(question_with_id)
    if(st.session_state.input_is_colpali):
        out_ = colpali.colpali_search_rerank(st.session_state.input_query)
        #print(out_)
    else:
        out_ = rag_DocumentSearcher.query_(awsauth, inputs, st.session_state['session_id'],st.session_state.input_rag_searchType)
    st.session_state.answers_.append({
        'answer': out_['text'],
        'source':out_['source'],
        'id': len(st.session_state.questions_),
        'image': out_['image'],
        'table':out_['table']
    })
    st.session_state.input_query=""
    

    
# search_type = st.selectbox('Select the Search type',
#     ('Conversational Search (RAG)',
#     'OpenSearch vector search', 
#     'LLM Text Generation'
#     ),
   
#     key = 'input_searchType',
#     help = "Select the type of retriever\n1. Conversational Search (Recommended) - This will include both the OpenSearch and LLM in the retrieval pipeline \n (note: This will put opensearch response as context to LLM to answer) \n2. OpenSearch vector search - This will put only OpenSearch's vector search in the pipeline, \n(Warning: this will lead to unformatted results )\n3. LLM Text Generation - This will include only LLM in the pipeline, \n(Warning: This will give hallucinated and out of context answers_)"
#     )

# col1, col2, col3, col4 = st.columns(4)
    
# with col1:
#     st.text_input('Temperature', value = "0.001", placeholder='LLM Temperature', key = 'input_temperature',help = "Set the temperature of the Large Language model. \n Note: 1. Set this to values lower to 1 in the order of 0.001, 0.0001, such low values reduces hallucination and creativity in the LLM response; 2. This applies only when LLM is a part of the retriever pipeline")
# with col2:
#     st.number_input('Top K', value = 200, placeholder='Top K', key = 'input_topK', step = 50, help = "This limits the LLM's predictions to the top k most probable tokens at each step of generation, this applies only when LLM is a prt of the retriever pipeline")
# with col3:
#     st.number_input('Top P', value = 0.95, placeholder='Top P', key = 'input_topP', step = 0.05, help = "This sets a threshold probability and selects the top tokens whose cumulative probability exceeds the threshold while the tokens are generated by the LLM")
# with col4:
#     st.number_input('Max Output Tokens', value = 500, placeholder='Max Output Tokens', key = 'input_maxTokens', step = 100, help = "This decides the total number of tokens generated as the final response. Note: Values greater than 1000 takes longer response time")

# st.markdown('---')


def write_user_message(md):
    col1, col2 = st.columns([3,97])
    
    with col1:
        st.image(USER_ICON, use_column_width='always')
    with col2:
        #st.warning(md['question'])

        st.markdown("<div style='color:#e28743';font-size:18px;padding:3px 7px 3px 7px;borderWidth: 0px;borderColor: red;borderStyle: solid;width: fit-content;height: fit-content;border-radius: 10px;font-style: italic;'>"+md['question']+"</div>", unsafe_allow_html = True)
       


def render_answer(question,answer,index,res_img):
    
    
    col1, col2, col_3 = st.columns([4,74,22])
    with col1:
        st.image(AI_ICON, use_column_width='always')
    with col2:
        ans_ = answer['answer']
        st.write(ans_)
       
        
        
        # def stream_():
        #     #use for streaming response on the client side
        #     for word in ans_.split(" "):
        #         yield word + " "
        #         time.sleep(0.04)
        #     #use for streaming response from Llm directly
        #     if(isinstance(ans_,botocore.eventstream.EventStream)):
        #         for event in ans_:
        #             chunk = event.get('chunk')
                    
        #             if chunk:
                        
        #                 chunk_obj = json.loads(chunk.get('bytes').decode())
                        
        #                 if('content_block' in chunk_obj or ('delta' in chunk_obj and 'text' in chunk_obj['delta'])):
        #                     key_ = list(chunk_obj.keys())[2]
        #                     text = chunk_obj[key_]['text']
                            
        #                     clear_output(wait=True)
        #                     output.append(text)
        #                     yield text
        #                     time.sleep(0.04)
            
                
        
        # if(index == len(st.session_state.questions_)):
        #     st.write_stream(stream_)
        #     if(isinstance(st.session_state.answers_[index-1]['answer'],botocore.eventstream.EventStream)):
        #         st.session_state.answers_[index-1]['answer'] = "".join(output)
        # else:
        #     st.write(ans_)
        

        # polly_response = polly_client.synthesize_speech(VoiceId='Joanna',
        #                 OutputFormat='ogg_vorbis', 
        #                 Text = ans_,
        #                 Engine = 'neural')

        # audio_col1, audio_col2 = st.columns([50,50])
        # with audio_col1:
        #     st.audio(polly_response['AudioStream'].read(), format="audio/ogg")
            
        rdn_key_1 = ''.join([random.choice(string.ascii_letters)
                              for _ in range(10)])
        def show_maxsim():
            st.session_state.show_columns = True
            st.session_state.maxSimImages = colpali.img_highlight(st.session_state.top_img, st.session_state.query_token_vectors, st.session_state.query_tokens)
            handle_input()
            with placeholder.container():
                render_all()    
        if(st.session_state.input_is_colpali):
            st.button("Show similarity map",key=rdn_key_1,on_click = show_maxsim)
                
        
        
        #st.markdown("<div style='font-size:18px;padding:3px 7px 3px 7px;borderWidth: 0px;borderColor: red;borderStyle: solid;border-radius: 10px;'>"+ans_+"</div>", unsafe_allow_html = True)
    #st.markdown("<div style='color:#e28743';padding:3px 7px 3px 7px;borderWidth: 0px;borderColor: red;borderStyle: solid;width: fit-content;height: fit-content;border-radius: 10px;'><b>Relevant images from the document :</b></div>", unsafe_allow_html = True)
    #st.write("")
    colu1,colu2,colu3 = st.columns([4,82,20])
    with colu2:
        with st.expander("Relevant Sources:"):
            with st.container():
                if(len(res_img)>0):
                    #with st.expander("Images:"):
                        
                    idx = 0
                    print(res_img)
                    for i in range(0,len(res_img)):
                        
                        if(st.session_state.input_is_colpali):
                            if(st.session_state.show_columns == True):
                                cols_per_row = 3
                                st.session_state.image_placeholder=st.empty()
                                with st.session_state.image_placeholder.container():
                                    row = st.columns(cols_per_row)
                                for j, item in enumerate(res_img[i:i+cols_per_row]):
                                    with row[j]:
                                        st.image(item['file'])
                                
                            else:
                                st.session_state.image_placeholder = st.empty()
                                with st.session_state.image_placeholder.container():
                                    col3_,col4_,col5_ = st.columns([33,33,33])
                                    with col3_:
                                        st.image(res_img[i]['file'])
                                
                                

                            
                            
                        else:
                            if(res_img[i]['file'].lower()!='none' and idx < 2):
                                col3,col4,col5 = st.columns([33,33,33])
                                cols = [col3,col4]
                                img = res_img[i]['file'].split(".")[0]
                                caption = res_img[i]['caption']
                            
                                with cols[idx]:
                                    
                                    st.image(parent_dirname+"/figures/"+st.session_state.input_index+"/"+img+".jpg")
                            #st.write(caption)
                            idx = idx+1
                    if(st.session_state.show_columns == True):
                        st.session_state.show_columns = False
                #st.markdown("<div style='color:#e28743';padding:3px 7px 3px 7px;borderWidth: 0px;borderColor: red;borderStyle: solid;width: fit-content;height: fit-content;border-radius: 10px;'><b>Sources from the document:</b></div>", unsafe_allow_html = True)
                if(len(answer["table"] )>0):
                    #with st.expander("Table:"):
                    df = pd.read_csv(answer["table"][0]['name'],skipinitialspace = True, on_bad_lines='skip',delimiter='`')
                    df.fillna(method='pad', inplace=True)
                    st.table(df)
                #with st.expander("Raw sources:"):
                st.write(answer["source"])
                
        
        
    with col_3:
        
        #st.markdown("<div style='color:#e28743;borderWidth: 0px;borderColor: red;borderStyle: solid;width: fit-content;height: fit-content;border-radius: 5px;'><b>"+",".join(st.session_state.input_rag_searchType)+"</b></div>", unsafe_allow_html = True)
        
       
        
        if(index == len(st.session_state.questions_)):

            rdn_key = ''.join([random.choice(string.ascii_letters)
                              for _ in range(10)])
            # rdn_key_1 = ''.join([random.choice(string.ascii_letters)
            #                   for _ in range(10)])
            currentValue = ''.join(st.session_state.input_rag_searchType)+str(st.session_state.input_is_rerank)+str(st.session_state.input_table_with_sql)+st.session_state.input_index
            oldValue = ''.join(st.session_state.inputs_["rag_searchType"])+str(st.session_state.inputs_["is_rerank"])+str(st.session_state.inputs_["table_with_sql"])+str(st.session_state.inputs_["index"])
            #print("changing values-----------------")
            def on_button_click():
                if(currentValue!=oldValue or 1==1): 
                    st.session_state.input_query = st.session_state.questions_[-1]["question"]
                    st.session_state.answers_.pop()
                    st.session_state.questions_.pop()
                    
                    
            # def show_maxsim():
            #     st.session_state.show_columns = True
            #     st.session_state.maxSimImages = colpali.img_highlight(st.session_state.top_img, st.session_state.query_token_vectors, st.session_state.query_tokens)
            #     handle_input()
            #     with placeholder.container():
            #         render_all()
                

            if("currentValue"  in st.session_state):
                del st.session_state["currentValue"]

            try:
                del regenerate
            except:
                pass  

            placeholder__ = st.empty()
            placeholder__.button("🔄",key=rdn_key,on_click=on_button_click)
            #placeholder__.button("Show similarity map",key=rdn_key_1,on_click = show_maxsim)
        
        
     
#Each answer will have context of the question asked in order to associate the provided feedback with the respective question
def write_chat_message(md, q,index):
    if(st.session_state.show_columns):
        res_img = st.session_state.maxSimImages
    else:
        res_img = md['image']
    #st.session_state['session_id'] = res['session_id']   to be added in memory
    chat = st.container()
    with chat:
        #print("st.session_state.input_index------------------")
        #print(st.session_state.input_index)
        render_answer(q,md,index,res_img)
    
def render_all():  
    index = 0
    for (q, a) in zip(st.session_state.questions_, st.session_state.answers_):
        index = index +1
        
        write_user_message(q)
        write_chat_message(a, q,index)

placeholder = st.empty()
with placeholder.container():
  render_all()

st.markdown("")
col_2, col_3 = st.columns([75,20])
#col_1, col_2, col_3 = st.columns([7.5,71.5,22])
# with col_1:
#     st.markdown("<p style='padding:0px 0px 0px 0px; color:#FF9900;font-size:120%'><b>Ask:</b></p>",unsafe_allow_html=True, help = 'Enter the questions and click on "GO"')
    
with col_2:
    #st.markdown("")
    input = st.text_input( "Ask here",label_visibility = "collapsed",key="input_query")
with col_3:
    #hidden = st.button("RUN",disabled=True,key = "hidden")
    play = st.button("Go",on_click=handle_input,key = "play")
with st.sidebar:
    st.page_link("app.py", label=":orange[Home]", icon="🏠")
    st.subheader(":blue[Sample Data]")
    coln_1,coln_2 = st.columns([70,30])
    # index_select = st.radio("Choose one index",["UK Housing","Covid19 impacts on Ireland","Environmental Global Warming","BEIR Research"],
    #                         captions = ['[preview](https://github.com/aws-samples/AI-search-with-amazon-opensearch-service/blob/b559f82c07dfcca973f457c0a15d6444752553ab/rag/sample_pdfs/HPI-Jan-2024-Hometrack.pdf)',
    #                                     '[preview](https://github.com/aws-samples/AI-search-with-amazon-opensearch-service/blob/b559f82c07dfcca973f457c0a15d6444752553ab/rag/sample_pdfs/covid19_ie.pdf)',
    #                                     '[preview](https://github.com/aws-samples/AI-search-with-amazon-opensearch-service/blob/b559f82c07dfcca973f457c0a15d6444752553ab/rag/sample_pdfs/global_warming.pdf)',
    #                                     '[preview](https://github.com/aws-samples/AI-search-with-amazon-opensearch-service/blob/b559f82c07dfcca973f457c0a15d6444752553ab/rag/sample_pdfs/BEIR.pdf)'],
    #                         key="input_rad_index")
    with coln_1:
        index_select = st.radio("Choose one index",["UK Housing","Global Warming stats","Covid19 impacts on Ireland"],key="input_rad_index")
    with coln_2:
        st.markdown("<p style='font-size:15px'>Preview file</p>",unsafe_allow_html=True)
        st.write("[:eyes:](https://github.com/aws-samples/AI-search-with-amazon-opensearch-service/blob/b559f82c07dfcca973f457c0a15d6444752553ab/rag/sample_pdfs/HPI-Jan-2024-Hometrack.pdf)")
        st.write("[:eyes:](https://github.com/aws-samples/AI-search-with-amazon-opensearch-service/blob/b559f82c07dfcca973f457c0a15d6444752553ab/rag/sample_pdfs/global_warming.pdf)")
        st.write("[:eyes:](https://github.com/aws-samples/AI-search-with-amazon-opensearch-service/blob/b559f82c07dfcca973f457c0a15d6444752553ab/rag/sample_pdfs/covid19_ie.pdf)")
        #st.write("[:eyes:](https://github.com/aws-samples/AI-search-with-amazon-opensearch-service/blob/b559f82c07dfcca973f457c0a15d6444752553ab/rag/sample_pdfs/BEIR.pdf)")
    st.markdown("""
    <style>
    [data-testid=column]:nth-of-type(2) [data-testid=stVerticalBlock]{
        gap: 0rem;
    }
    [data-testid=column]:nth-of-type(1) [data-testid=stVerticalBlock]{
        gap: 0rem;
    }
    </style>
    """,unsafe_allow_html=True)   
    with st.expander("Sample questions:"):
        st.markdown("<span style = 'color:#FF9900;'>UK Housing</span> - which city has the highest average housing price in UK ?",unsafe_allow_html=True)
        st.markdown("<span style = 'color:#FF9900;'>Global Warming stats</span> - What is the projected energy percentage from renewable sources in future?",unsafe_allow_html=True)
        st.markdown("<span style = 'color:#FF9900;'>Covid19 impacts</span> - How many aged above 85 years died due to covid ?",unsafe_allow_html=True)
        
    
    
    # st.subheader(":blue[Your multi-modal documents]")
    # pdf_doc_ = st.file_uploader(
    #     "Upload your PDFs here and click on 'Process'", accept_multiple_files=False)
                    
                
    # pdf_docs = [pdf_doc_]
    # if st.button("Process"):
    #     with st.spinner("Processing"):
    #         if os.path.isdir(parent_dirname+"/pdfs") == False:
    #             os.mkdir(parent_dirname+"/pdfs")
            
    #         for pdf_doc in pdf_docs:
    #             print(type(pdf_doc))
    #             pdf_doc_name = (pdf_doc.name).replace(" ","_")
    #             with open(os.path.join(parent_dirname+"/pdfs",pdf_doc_name),"wb") as f: 
    #                 f.write(pdf_doc.getbuffer())  
                    
    #             request_ = { "bucket": s3_bucket_,"key": pdf_doc_name}
    #             # if(st.session_state.input_copali_rerank):
    #             #     copali.process_doc(request_)
    #             # else:
    #             rag_DocumentLoader.load_docs(request_)
    #             print('lambda done')
    #     st.success('you can start searching on your PDF')
        
    ############## haystach demo temporary addition ############    
    # st.subheader(":blue[Multimodality]")
    # colu1,colu2 = st.columns([50,50])
    # with colu1:
    #     in_images = st.toggle('Images', key = 'in_images', disabled = False)
    # with colu2:
    #     in_tables = st.toggle('Tables', key = 'in_tables', disabled = False)   
    # if(in_tables):
    #     st.session_state.input_table_with_sql = True
    # else:
    #     st.session_state.input_table_with_sql = False
        
     ############## haystach demo temporary addition ############       
    #if(pdf_doc_ is None or pdf_doc_ == ""):
    if(index_select == "Global Warming stats"):
        st.session_state.input_index = "globalwarmingnew"
    if(index_select == "Covid19 impacts on Ireland"):
        st.session_state.input_index = "covid19ie"#"choosetheknnalgorithmforyourbillionscaleusecasewithopensearchawsbigdatablog"
    if(index_select == "BEIR"):
        st.session_state.input_index = "2104"
    if(index_select == "UK Housing"):
        st.session_state.input_index = "hpijan2024hometrack"
          
    # custom_index = st.text_input("If uploaded the file already, enter the original file name", value = "")
    # if(custom_index!=""):
    #     st.session_state.input_index = re.sub('[^A-Za-z0-9]+', '', (custom_index.lower().replace(".pdf","").split("/")[-1].split(".")[0]).lower())
    
    
    
    st.subheader(":blue[Retriever]")
    search_type = st.multiselect('Select the Retriever(s)',
    ['Keyword Search',
    'Vector Search', 
    'Sparse Search',
    ],
    ['Vector Search'],

    key = 'input_rag_searchType',
    help = "Select the type of Search, adding more than one search type will activate hybrid search"#\n1. Conversational Search (Recommended) - This will include both the OpenSearch and LLM in the retrieval pipeline \n (note: This will put opensearch response as context to LLM to answer) \n2. OpenSearch vector search - This will put only OpenSearch's vector search in the pipeline, \n(Warning: this will lead to unformatted results )\n3. LLM Text Generation - This will include only LLM in the pipeline, \n(Warning: This will give hallucinated and out of context answers)"
    )
    
    re_rank = st.checkbox('Re-rank results', key = 'input_re_rank', disabled = False, value = True, help = "Checking this box will re-rank the results using a cross-encoder model")
     
    if(re_rank):
        st.session_state.input_is_rerank = True
    else:
        st.session_state.input_is_rerank = False
    
    st.subheader(":blue[Multi-vector retrieval]")
    
    #st.write("Dataset indexed: https://huggingface.co/datasets/vespa-engine/gpfg-QA")
    colpali_search_rerank =  st.checkbox('Try Colpali multi-vector retrieval on the [sample dataset](https://huggingface.co/datasets/vespa-engine/gpfg-QA)', key = 'input_colpali', disabled = False, value = False, help = "Checking this box will use colpali as the embedding model and retrieval is performed using multi-vectors followed by re-ranking using MaxSim")
        
    if(colpali_search_rerank):
        st.session_state.input_is_colpali = True
        #st.session_state.input_query = ""
    else:
        st.session_state.input_is_colpali = False
    
    with st.expander("Sample questions for Colpali retriever:"):
        st.write("1. Proportion of female new hires 2021-2023? \n\n 2. First-half 2021 return on unlisted real estate investments? \n\n 3. Trend of the fund's expected absolute volatility between January 2014 and January 2016? \n\n 4. Fund return percentage in 2017? \n\n 5. Annualized gross return of the fund from 1997 to 2008?")
    
        
    # copali_rerank = st.checkbox("Search and Re-rank with Token level vectors",key = 'copali_rerank',help = "Enabling this option uses 'Copali' model's page level image embeddings to retrieve documents and MaxSim to re-rank the pages.\n\n Hugging Face Model: https://huggingface.co/vidore/colpali")
        
    # if(copali_rerank):
    #     st.session_state.input_copali_rerank = True
    # else:
    #     st.session_state.input_copali_rerank = False