File size: 2,224 Bytes
c7058bc
 
7372149
 
c7058bc
 
7372149
57c9d91
 
7372149
c7058bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec97476
 
c7058bc
 
68de5df
c7058bc
 
7372149
57c9d91
7372149
61534eb
c7058bc
64c1b5e
57c9d91
 
 
61534eb
57c9d91
 
c7058bc
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import PyPDF2
#rom langchain.embeddings.openai import OpenAIEmbeddings
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores.faiss import FAISS
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain import HuggingFaceHub
from langchain.document_loaders import UnstructuredPDFLoader
from langchain.indexes import VectorstoreIndexCreator

from langchain import OpenAI, VectorDBQA

import os


def pdf_to_text(pdf_file, query):
  # Open the PDF file in binary mode
  with open(pdf_file.name, 'rb') as pdf_file:
      # Create a PDF reader object
      pdf_reader = PyPDF2.PdfReader(pdf_file)

      # Create an empty string to store the text
      text = ""

      # Loop through each page of the PDF
      for page_num in range(len(pdf_reader.pages)):
          # Get the page object
          page = pdf_reader.pages[page_num]
          # Extract the texst from the page and add it to the text variable
          text += page.extract_text()
    #embedding step 
  from langchain.text_splitter import CharacterTextSplitter
  text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
  texts = text_splitter.split_text(text)

  embeddings = HuggingFaceEmbeddings()
  #vector store
  vectorstore = FAISS.from_texts(texts, embeddings)
  llm = HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":0, "max_length":512})
  loaders = UnstructuredPDFLoader(pdf_file)

  index = vectorstore.as_retriever()
    #inference
  #qa = VectorDBQA.from_chain_type(llm=llm, chain_type="stuff", vectorstore=vectorstore)
  from langchain.chains import RetrievalQA
  chain = RetrievalQA.from_chain_type(llm=llm, 
                                    chain_type="stuff", 
                                    retriever=index, 
                                    input_key="question")
  return chain.run(query)


      


# Define the Gradio interface
pdf_input = gr.inputs.File(label="PDF File")
query_input = gr.inputs.Textbox(label="Query")
outputs = gr.outputs.Textbox(label="Chatbot Response")
interface = gr.Interface(fn=pdf_to_text, inputs=[pdf_input, query_input], outputs=outputs)

# Run the interface
interface.launch(debug = True)