dincertolga commited on
Commit
564ff2c
·
verified ·
1 Parent(s): 9bb75dd

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +90 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import langchain
3
+ import os
4
+ import streamlit as st
5
+ from PyPDF2 import PdfReader
6
+ from langchain.document_loaders import PyPDFDirectoryLoader
7
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
8
+ from langchain.embeddings.openai import OpenAIEmbeddings
9
+ from langchain.chains.question_answering import load_qa_chain
10
+ from langchain.prompts import PromptTemplate
11
+ from langchain.vectorstores import FAISS
12
+ from langchain.llms import OpenAI
13
+ from langchain.chat_models import ChatOpenAI
14
+
15
+ def get_pdf_text(pdf_docs):
16
+ text = ""
17
+ for pdf in pdf_docs:
18
+ pdf_reader = PdfReader(pdf)
19
+ for page in pdf_reader.pages:
20
+ text += page.extract_text()
21
+ return text
22
+
23
+ def get_text_chunks(text):
24
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
25
+ chunks = text_splitter.split_text(text)
26
+ return chunks
27
+
28
+ def get_vector_store(text_chunks):
29
+ embeddings = OpenAIEmbeddings(api_key=os.environ.get("OPENAI_API_KEY"))
30
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
31
+ vector_store.save_local("faiss_index")
32
+
33
+
34
+ def get_conversational_chain():
35
+
36
+ prompt_template = """
37
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
38
+ provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
39
+ Context:\n {context}?\n
40
+ Question: \n{question}\n
41
+
42
+ Answer:
43
+ """
44
+
45
+ model = ChatOpenAI(openai_api_key=os.environ.get("OPENAI_API_KEY"))
46
+
47
+ prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
48
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
49
+
50
+ return chain
51
+
52
+ def user_input(user_question):
53
+
54
+ embeddings = OpenAIEmbeddings(api_key=os.environ.get("OPENAI_API_KEY"))
55
+
56
+ new_db = FAISS.load_local("faiss_index", embeddings)
57
+ docs = new_db.similarity_search(user_question)
58
+
59
+ chain = get_conversational_chain()
60
+
61
+ response = chain(
62
+ {"input_documents":docs, "question": user_question}
63
+ , return_only_outputs=True)
64
+
65
+ print(response)
66
+ st.write("Reply: ", response["output_text"])
67
+
68
+ def main():
69
+ st.set_page_config("Chat PDF")
70
+ st.header("Chat with PDF using OpenAI💁")
71
+
72
+ user_question = st.text_input("Ask a Question from the PDF Files")
73
+
74
+ if user_question:
75
+ user_input(user_question)
76
+
77
+ with st.sidebar:
78
+ st.title("Menu:")
79
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
80
+ if st.button("Submit & Process"):
81
+ with st.spinner("Processing..."):
82
+ raw_text = get_pdf_text(pdf_docs)
83
+ text_chunks = get_text_chunks(raw_text)
84
+ get_vector_store(text_chunks)
85
+ st.success("Done")
86
+
87
+
88
+
89
+ if __name__ == "__main__":
90
+ main()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ openai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF2
6
+ chromadb
7
+ faiss-cpu
8
+ tiktoken