File size: 3,528 Bytes
ebabcc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# %% auto 0
__all__ = ['text_splitter', 'embeddings', 'hub_llm', 'pdf_example_1', 'pdf_example_2', 'question_example_1', 'question_example_2',
           'title', 'description', 'file_upload', 'question', 'output', 'split_pdf', 'ask_pdf']

import dotenv
from dotenv import load_dotenv
load_dotenv()

# Import qa chain
from langchain.chains.question_answering import load_qa_chain

# Import gradio for UI/app interface
import gradio as gr

# Import PDF Loaders
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter

# Create an instance of RecursiveCharacterTextSplitter with your desired chunk_size and chunk_overlap
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=250)

def split_pdf(pdf):
  if pdf is not None:
      # load pdf
    loader = PyPDFLoader(pdf)
      # Split pages from pdf (SEE: https://api.python.langchain.com/en/latest/document_loaders/langchain.document_loaders.pdf.PyPDFLoader.html)
    chunks = loader.load_and_split(text_splitter=text_splitter)
    return chunks

# Import HuggingFace as main LLM service
from langchain.embeddings import HuggingFaceEmbeddings

embeddings = HuggingFaceEmbeddings(
    model_name = "bert-base-nli-mean-tokens",
    model_kwargs = {'device': 'cpu'},
    encode_kwargs = {'normalize_embeddings': True}
)

# Import FAISS as the vector store
from langchain.vectorstores import FAISS

# import transformers
# from transformers import AutoTokenizer, AutoModelForCausalLM
from langchain import HuggingFaceHub
import torch

hub_llm = HuggingFaceHub(
    repo_id = 'tiiuae/falcon-7b-instruct',
    # repo_id = 'tiiuae/falcon-7b-instruct',
    model_kwargs = {'temperature' : 0.01,  # 'randomness' of outputs, 0.0 is the min and 1.0 the max
                    # 'top_p': 0.15,  # select from top tokens whose probability add up to x%
                    'top_k': 25,  # select from top x tokens
                    'max_new_tokens': 300,  # mex number of tokens to generate in the output
                    'repetition_penalty': 999  # without this output begins repeating
                    }
)

chain = load_qa_chain(hub_llm, chain_type="stuff")

# App Function
def ask_pdf(pdf, user_question):
  if pdf is not None and user_question != '':
      chunks = split_pdf(pdf) # split pdf into smaller chunks
      store = FAISS.from_documents(chunks, embeddings) # Load documents into vector database
      docs = store.similarity_search(user_question) # take user input and look for chunks that might contain the answer
    # run chain
      response = chain.run(input_documents=docs, question=user_question)

  return response

pdf_example_1 = '/attention.pdf' 
pdf_example_2 = '/bert.pdf'
question_example_1 = "What does 'attention' mean in this context?"
question_example_2 = "What does BERT stand for?"

title="LangChain project: Conversational Retrieval Chain"
description="Upload a PDF file and ask a question related to its content."

# Create Gradio interface
file_upload = gr.File(file_types = ['.pdf'],
                              file_count='single',
                              label="Upload a PDF file")
question = gr.Textbox(label="Write your question here:", show_copy_button= True)
output = gr.Textbox(label="Your answer:")

gr.Interface(
    fn=ask_pdf,
    inputs=[file_upload, question],
    outputs=output,
    title=title,
    description=description,
    examples=[[pdf_example_1, question_example_1], [pdf_example_2, question_example_2]]
).launch() #share=True