Noobian commited on
Commit
57c9d91
1 Parent(s): c95b96a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -5,6 +5,8 @@ from langchain.embeddings import HuggingFaceEmbeddings
5
  from langchain.vectorstores.faiss import FAISS
6
  from langchain.text_splitter import RecursiveCharacterTextSplitter
7
  from langchain import HuggingFaceHub
 
 
8
 
9
  from langchain import OpenAI, VectorDBQA
10
 
@@ -35,11 +37,19 @@ def pdf_to_text(pdf_file, query):
35
  #vector store
36
  vectorstore = FAISS.from_texts(texts, embeddings)
37
  llm = HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":0, "max_length":512})
 
38
 
39
-
 
 
40
  #inference
41
  qa = VectorDBQA.from_chain_type(llm=llm, chain_type="stuff", vectorstore=vectorstore)
42
- return qa.run(query)
 
 
 
 
 
43
 
44
 
45
 
 
5
  from langchain.vectorstores.faiss import FAISS
6
  from langchain.text_splitter import RecursiveCharacterTextSplitter
7
  from langchain import HuggingFaceHub
8
+ from langchain.document_loaders import UnstructuredPDFLoader
9
+ from langchain.indexes import VectorstoreIndexCreator
10
 
11
  from langchain import OpenAI, VectorDBQA
12
 
 
37
  #vector store
38
  vectorstore = FAISS.from_texts(texts, embeddings)
39
  llm = HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":0, "max_length":512})
40
+ loaders = UnstructuredPDFLoader(pdf_file)
41
 
42
+ index = VectorstoreIndexCreator(
43
+ embedding=HuggingFaceEmbeddings(),
44
+ text_splitter=CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)).from_loaders(loaders)
45
  #inference
46
  qa = VectorDBQA.from_chain_type(llm=llm, chain_type="stuff", vectorstore=vectorstore)
47
+ from langchain.chains import RetrievalQA
48
+ chain = RetrievalQA.from_chain_type(llm=llm,
49
+ chain_type="stuff",
50
+ retriever=index.vectorstore.as_retriever(),
51
+ input_key="question")
52
+ return chain.run(query)
53
 
54
 
55