File size: 1,781 Bytes
71dd084
 
 
 
 
 
 
 
7ce23f5
9b71521
71dd084
 
d7b6bb8
71dd084
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a342d1
71dd084
 
 
 
 
 
1a342d1
 
71dd084
 
 
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
import os
from PyPDF2 import PdfReader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.chains.question_answering import load_qa_chain
import gradio as gr
from langchain.embeddings import HuggingFaceEmbeddings
from langchain import HuggingFaceHub



def submitYourDocument(doc):
  reader = PdfReader(doc)
  # read data from the file and put them into a variable called raw_text
  raw_text = ''
  for i, page in enumerate(reader.pages):
    text = page.extract_text()
    if text:
        raw_text += text
  text_splitter = CharacterTextSplitter(        
    separator = "\n",
    chunk_size = 1000,
    chunk_overlap  = 200,
    length_function = len,
  )
  texts = text_splitter.split_text(raw_text)
  return texts

    
def main(doc,prompt):
  result=submitYourDocument(doc)
  embeddings = HuggingFaceEmbeddings()
  db = FAISS.from_texts(result, embeddings)
  llm=HuggingFaceHub(repo_id="google/flan-t5-xxl",model_kwargs={"temperature":1, "max_length":512})
  chain=load_qa_chain(llm,chain_type="stuff")
  query =prompt
  docs = db.similarity_search(query)
  return chain.run(input_documents=docs, question=query)

interface=gr.Interface(fn=main,inputs=[gr.File(label="Upload file"),gr.components.Textbox(label="Type Question Related to Uploaded Document")],
                       outputs=gr.components.Textbox(label="Answer.."),
                       examples=[["FYP_Proposal.pdf","what is the summary of attached document?"],["FYP_Proposal.pdf","who is Mukesh Ambani?"],["FYP_Proposal.pdf","what is the title of document?"]])
interface.launch(debug=True)