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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c52926
 
5d8959c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
774a2c5
5d8959c
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
"""
This code uses the PyMuPDF package.

PyMuPDF is AGPL licensed, please refer to:
https://pymupdf.readthedocs.io/en/latest/about.html#license-and-copyright
"""

"""
Code below is based on an implementation by Sunil Kumar Dash:

MIT License

Copyright (c) 2023 Sunil Kumar Dash

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

from huggingface_hub import InferenceClient
from langchain_openai import AzureOpenAIEmbeddings
#from langchain_community.chat_models import AzureChatOpenAI
from langchain_openai import AzureChatOpenAI





from typing import Any
import gradio as gr
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
import chromadb
#to handle the tenant issue
chromadb.api.client.SharedSystemClient.clear_system_cache()

from langchain.chains import ConversationalRetrievalChain
from langchain_openai import ChatOpenAI

from langchain_community.document_loaders import PyMuPDFLoader
from langchain.schema.document import Document

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.text_splitter import CharacterTextSplitter
from langchain.memory import ConversationBufferMemory
from langchain_community.llms import HuggingFaceEndpoint
from langchain_community.embeddings import HuggingFaceEmbeddings 


# for hugging face llm
from transformers import AutoTokenizer
import transformers
import torch
import tqdm 
import accelerate


import pymupdf
from PIL import Image
import os
import re
import uuid

import os
import wget
import subprocess
import urllib.request
import requests

from pathlib import Path
from unidecode import unidecode

api_key = os.getenv("OPENAI_API_KEY")
user_agent = os.getenv("USER_AGENT")

dr_link_url1 = os.getenv("DR_LINK_1")
dr_link_url2 = os.getenv("DR_LINK_2") 
azure_endpt = os.getenv("AZURE_ENDPT") 
huffingface_read_token = os.getenv("huffingface_read_token")

list_llm = ["mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.1", \
    "google/gemma-7b-it","google/gemma-2b-it", \
    "HuggingFaceH4/zephyr-7b-beta", "HuggingFaceH4/zephyr-7b-gemma-v0.1", \
    "meta-llama/Llama-2-7b-chat-hf", "microsoft/phi-2", \
    "TinyLlama/TinyLlama-1.1B-Chat-v1.0", "mosaicml/mpt-7b-instruct", "tiiuae/falcon-7b-instruct", \
    "google/flan-t5-xxl"
]
list_llm_simple = [os.path.basename(llm) for llm in list_llm]


"""
enable_box = gr.Textbox(
    value=None, placeholder="Upload your OpenAI API key", interactive=True
)
disable_box = gr.Textbox(value="OpenAI API key is set", interactive=False)
"""

def set_apikey(api_key: str):
    print("API Key set")
    app.OPENAI_API_KEY = api_key
    #return disable_box

"""
def enable_api_box():
    return enable_box
"""

def add_text(history, text: str):
    if not text:
        raise gr.Error("enter text")
    print("in add_text history="+str(history))
    print("in add_text text="+str(text))
    
    history = history + [(text, "")]
    return history


class my_app:
    def __init__(self, OPENAI_API_KEY: str = None) -> None:
        print("init")
        self.OPENAI_API_KEY: str = api_key
        #self.chain = None
        #self.chat_history: list = []
        #self.N: int = 0
        self.count: int = 0

    def __call__(self, file: str) -> Any:
        print("call")
        #if self.count == 0:
            #vincent added
            #self.chain = None
            #self.chat_history: list = []
            #self.N: int = 0
            #self.count: int = 0
            
            #self.chain = self.build_chain(file)
            #self.count += 1
            
        #vincent added
        
        #else:
            #self.chain = self.build_chain(file)
            #self.count += 1
            
        #return self.chain
    

def process_file2(file: str):
    
    loader = PyMuPDFLoader(file.name)
    documents = loader.load()
    pattern = r"/([^/]+)$"
    match = re.search(pattern, file.name)
    try:
        file_name = match.group(1)
    except:
        file_name = os.path.basename(file)
        
    return documents, file_name

def create_collection_name(filepath):
    # Extract filename without extension
    collection_name = Path(filepath).stem
    # Fix potential issues from naming convention
    ## Remove space
    collection_name = collection_name.replace(" ","-") 
    ## ASCII transliterations of Unicode text
    collection_name = unidecode(collection_name)
    ## Remove special characters
    #collection_name = re.findall("[\dA-Za-z]*", collection_name)[0]
    collection_name = re.sub('[^A-Za-z0-9]+', '-', collection_name)
    ## Limit length to 50 characters
    collection_name = collection_name[:50]
    ## Minimum length of 3 characters
    if len(collection_name) < 3:
        collection_name = collection_name + 'xyz'
    ## Enforce start and end as alphanumeric character
    if not collection_name[0].isalnum():
        collection_name = 'A' + collection_name[1:]
    if not collection_name[-1].isalnum():
        collection_name = collection_name[:-1] + 'Z'
    print('Filepath: ', filepath)
    print('Collection name: ', collection_name)
    return collection_name

#collection_name, vector_db, btn, llm_btn, slider_temperature, slider_maxtokens, slider_topk]
def build_qa_chain(collection_name, vector_db, file: str, llm_option, temperature, max_tokens, top_k, progress=gr.Progress()):

    if file == None or not file:
        raise gr.Error("Please upload a PDF first!")
        
    print("in build_qa_chain="+file.name) 

    
        
    documents, file_name = process_file2(file)
    
    # Load embeddings model
    #embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)

    #vincent for old LLM
    """
    embeddings = AzureOpenAIEmbeddings(
    model="text-embedding-ada-002",
    # dimensions: Optional[int] = None, # Can specify dimensions with new text-embedding-3 models   
    azure_endpoint=azure_endpt , # If not provided, will read env variable AZURE_OPENAI_ENDPOINT
    openai_api_key=api_key, # Can provide an API key directly. If missing read env variable AZURE_OPENAI_API_KEY   
    #openai_api_version="2023-05-15", # If not provided, will read env variable AZURE_OPENAI_API_VERSION
    openai_api_version="2023-05-15", # If not provided, will read env variable AZURE_OPENAI_API_VERSION
    )
    """    
    
    #vincent for new LLM
    embeddings = HuggingFaceEmbeddings()
    
    
    #vincent added to handle the tenant problem 20250211
    chromadb.api.client.SharedSystemClient.clear_system_cache()   
    new_client = chromadb.EphemeralClient()
    memory = ConversationBufferMemory(
        memory_key="chat_history",
        output_key='answer',
        return_messages=True
    )
    
    # added by vincent
    text_splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=10)
    chunked_documents = text_splitter.split_documents(documents)

    #list_file_path = [x.name for x in list_file_obj if x is not None]
    list_file_path = file.name 
    # Create collection_name for vector database
    # vincent fix InvalidCollectionException 20250212
    #collection_name = create_collection_name(list_file_path[0])
    collection_name = "pdf_docs_l_"+file.name[-10:]
    collection_name = collection_name.replace("/","_" )
    
    vector_db = Chroma.from_documents(
            documents=chunked_documents,
            embedding=embeddings,
            client=new_client,
            #collection_name=file_name,
            #persist_directory = "db_" + file_name,
            collection_name=collection_name,
        )
    """
    chain = ConversationalRetrievalChain.from_llm(
    ChatOpenAI(temperature=0.0, openai_api_key=self.OPENAI_API_KEY),
    retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),
    return_source_documents=True,
    )
    """

    #vincent added for old LLM
    """
    chain = ConversationalRetrievalChain.from_llm(
    #ChatOpenAI(temperature=0.0, openai_api_key=self.OPENAI_API_KEY),
    
    AzureChatOpenAI(
                temperature=0.0, openai_api_key=api_key, api_version="2024-08-01-preview", 
                model_name="gpt-4o", azure_endpoint=azure_endpt),
    #vincent modified
    retriever=vector_db.as_retriever(),
    #retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),            
    return_source_documents=True,
    chain_type="stuff",     
    memory=memory,
    )
    
    """
    #vincent for new LLM
    #llm_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
    #llm_model = "meta-llama/Llama-2-7b-chat-hf"

    llm_model = list_llm[llm_option]
    task="text-generation"  # Explicitly specify task

    if llm_model == "mistralai/Mixtral-8x7B-Instruct-v0.1":
        llm = HuggingFaceEndpoint(
            repo_id=llm_model, 
            huggingfacehub_api_token = huffingface_read_token,
            task=task,  # Explicitly specify task
            # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "load_in_8bit": True}
            temperature = temperature,
            max_new_tokens = max_tokens,
            top_k = top_k,
            load_in_8bit = True,
        )
    elif llm_model in ["HuggingFaceH4/zephyr-7b-gemma-v0.1","mosaicml/mpt-7b-instruct"]:
        raise gr.Error("LLM model is too large to be loaded automatically on free inference endpoint")
        llm = HuggingFaceEndpoint(
            repo_id=llm_model, 
            task=task,  # Explicitly specify task
            temperature = temperature,
            max_new_tokens = max_tokens,
            top_k = top_k,
        )
    elif llm_model == "microsoft/phi-2":
        # raise gr.Error("phi-2 model requires 'trust_remote_code=True', currently not supported by langchain HuggingFaceHub...")
        llm = HuggingFaceEndpoint(
            repo_id=llm_model, 
            task=task,  # Explicitly specify task
            # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
            temperature = temperature,
            max_new_tokens = max_tokens,
            top_k = top_k,
            trust_remote_code = True,
            torch_dtype = "auto",
        )
    elif llm_model == "TinyLlama/TinyLlama-1.1B-Chat-v1.0":
        llm = HuggingFaceEndpoint(
            repo_id=llm_model, 
            task=task,  # Explicitly specify task
            # model_kwargs={"temperature": temperature, "max_new_tokens": 250, "top_k": top_k}
            temperature = temperature,
            max_new_tokens = 250,
            top_k = top_k,
        )
    elif llm_model == "meta-llama/Llama-2-7b-chat-hf":
        raise gr.Error("Llama-2-7b-chat-hf model requires a Pro subscription...")
        llm = HuggingFaceEndpoint(
            repo_id=llm_model,
            task=task,  # Explicitly specify task
            # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k}
            temperature = temperature,
            max_new_tokens = max_tokens,
            top_k = top_k,
        )
    else:
        llm = HuggingFaceEndpoint(
            repo_id=llm_model, 
            task=task,  # Explicitly specify task
            # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
            # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k}
            temperature = temperature,
            max_new_tokens = max_tokens,
            top_k = top_k,
        )


    """
    llm = HuggingFaceEndpoint(
            repo_id=llm_model, 
            task="text-generation",  # Explicitly specify task
            # model_kwargs={"temperature": temperature, "max_new_tokens": 250, "top_k": top_k}
            temperature = 0.01,
            max_new_tokens = 250,
            top_k = 3,
        )
    """
    chain = ConversationalRetrievalChain.from_llm(
        llm,
        retriever=vector_db.as_retriever(),
        chain_type="stuff", 
        memory=memory,
        # combine_docs_chain_kwargs={"prompt": your_prompt})
        return_source_documents=True,
        #return_generated_question=False,
        verbose=False,
    )
    
    #vincent added 20250211
    app.count += 1
    return collection_name, vector_db, chain, "Complete!"


def get_response(collection_name, vector_db, qa_chain, history, query, file):
    #vincent added
    set_apikey(api_key)
    print("in get_response count=" + str(app.count))
    if not file:
        raise gr.Error(message="Upload a PDF")
        
    formatted_chat_history = list(history)
    formatted_chat_history = formatted_chat_history[:len(formatted_chat_history)-1]
    print("in get_response query="+ query)
    #print("in get_response chat_history="+ str(app.chat_history))
    print("in get_response formatted_chat_history="+ str(formatted_chat_history))
    #print("in get_response history="+ str(history))

    chat_history_tuples = []
    for message in formatted_chat_history:
        chat_history_tuples.append((message[0], message[1]))

    #vincent added 20250211
    if app.count == 0:
        #collection_name, vector_db, qa_chain = build_qa_chain(collection_name, vector_db, file)    
        raise gr.Error("Please initialize the Chain first!")
    result = qa_chain.invoke(
        {"question": query, "chat_history": chat_history_tuples}, return_only_outputs=True
        #{"question": query, "chat_history": format_chat_history(query, history)}, return_only_outputs=True
    )
    
    
    #app.chat_history += [(query, result["answer"])]
    ##app.N = list(result["source_documents"][0])[1][1]["page"]
    for char in result["answer"]:
        history[-1][-1] += char
        yield collection_name, vector_db, qa_chain, history, ""

    #print("answer:"+ result["answer"])


def render_file(file):
    #print("in render_file="+file.name+" count="+str(app.count))
    doc = pymupdf.open(file.name)
    # vincent: issue in N
    page = doc[N]
    # Render the page as a PNG image with a resolution of 150 DPI
    pix = page.get_pixmap(dpi=150)
    image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
    return image


def purge_chat_and_render_first(file):
    print("purge_chat_and_render_first")
    # Purges the previous chat session so that the bot has no concept of previous documents
    chat_history = []
    history = []
    #count = 0

    #vincent added 20250211
    #count = count + 1
    app.count = 0

    # Use PyMuPDF to render the first page of the uploaded document
    doc = pymupdf.open(file.name)
    page = doc[0]
    # Render the page as a PNG image with a resolution of 150 DPI
    pix = page.get_pixmap(dpi=150)
    image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
    return image, []

app = my_app()

with gr.Blocks() as demo:
    
    vector_db = gr.State()
    qa_chain = gr.State()
    collection_name = gr.State()
    #N = gr.Number()
    #count = gr.Number()

    N = 0
    count = 0
    
    #chat_history = gr.State()
    #chat_history: list = []
    #chat_history = []
    
    with gr.Column():
        """
        with gr.Row():

            with gr.Column(scale=1):
                api_key = gr.Textbox(
                    placeholder="Enter OpenAI API key and hit <RETURN>",
                    show_label=False,
                    interactive=True
                )
        """
        with gr.Row():
                llm_btn = gr.Radio(list_llm_simple, \
                    label="LLM models", value = list_llm_simple[0], type="index", info="Choose your LLM model")

        with gr.Accordion("Advanced options - LLM model", open=False):
                with gr.Row():
                    slider_temperature = gr.Slider(minimum = 0.01, maximum = 1.0, value=0.7, step=0.1, label="Temperature", info="Model temperature", interactive=True)
                with gr.Row():
                    slider_maxtokens = gr.Slider(minimum = 224, maximum = 4096, value=1024, step=32, label="Max Tokens", info="Model max tokens", interactive=True)
                with gr.Row():
                    slider_topk = gr.Slider(minimum = 1, maximum = 10, value=3, step=1, label="top-k samples", info="Model top-k samples", interactive=True)
        
        with gr.Row():
                llm_progress = gr.Textbox(value="None",label="QA chain initialization")
        with gr.Row():
                qachain_btn = gr.Button("Initialize Question Answering chain")    
            
        with gr.Row():
            with gr.Column(scale=2):
                with gr.Row():
                    chatbot = gr.Chatbot(value=[], elem_id="chatbot")
                with gr.Row():
                    txt = gr.Textbox(
                        show_label=False,
                        placeholder="Enter text and press submit",
                        scale=2
                    )
                    submit_btn = gr.Button("submit", scale=1)

            with gr.Column(scale=1):
                with gr.Row():
                    show_img = gr.Image(label="Upload PDF")
                with gr.Row():
                    btn = gr.UploadButton("📁 upload a PDF", file_types=[".pdf"])

    """
    api_key.submit(
        fn=set_apikey,
        inputs=[api_key],
        outputs=[
            api_key,
        ],
    )
    """

    btn.upload(
        fn=purge_chat_and_render_first,
        inputs=[btn],
        outputs=[show_img, chatbot],
    )

    qachain_btn.click(build_qa_chain, \
            inputs=[collection_name, vector_db, btn, llm_btn, slider_temperature, slider_maxtokens, slider_topk], \
            outputs=[collection_name, vector_db, qa_chain, llm_progress]).then(lambda:[None], \
            #inputs=None, \
            #outputs=[chatbot], \
            queue=False)

    submit_btn.click(
        fn=add_text,
        inputs=[chatbot, txt],
        outputs=[
            chatbot,
        ],
        queue=False,
    ).success(
        fn=get_response, inputs=[collection_name,vector_db, qa_chain, chatbot, txt, btn], outputs=[collection_name,vector_db, qa_chain, chatbot, txt]
    ).success(
        fn=render_file, inputs=[btn], outputs=[show_img]
    )

#demo.queue()
#demo.launch(share=True, ssr_mode=False)
#demo.launch()
demo.queue().launch(share=True,  show_error=True)
#demo.launch(share=True)