Spaces:
Sleeping
Sleeping
Harshit-Pradhan
commited on
Commit
•
b370feb
1
Parent(s):
26679e5
Upload 3 files
Browse files- 48lawsofpower.pdf +0 -0
- app.py +68 -0
- requirements.txt +9 -0
48lawsofpower.pdf
ADDED
Binary file (105 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PyPDF2 import PdfReader
|
2 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
3 |
+
from langchain.text_splitter import CharacterTextSplitter
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
import streamlit as st
|
6 |
+
from dotenv import load_dotenv,find_dotenv
|
7 |
+
from streamlit_extras.add_vertical_space import add_vertical_space
|
8 |
+
import pickle
|
9 |
+
import os
|
10 |
+
|
11 |
+
from langchain.chains.question_answering import load_qa_chain
|
12 |
+
from langchain.llms import OpenAI
|
13 |
+
|
14 |
+
## Slide-bar
|
15 |
+
with st.sidebar:
|
16 |
+
st.title('PDF Q&A')
|
17 |
+
st.markdown('''
|
18 |
+
## About
|
19 |
+
This app is an LLM-powered chatbot built using:
|
20 |
+
- [Streamlit](https://streamlit.io/)
|
21 |
+
- [LangChain](https://python.langchain.com/)
|
22 |
+
- [OpenAI](https://platform.openai.com/docs/models) LLM model
|
23 |
+
|
24 |
+
''')
|
25 |
+
add_vertical_space(5)
|
26 |
+
st.write('Made by Harshit')
|
27 |
+
|
28 |
+
def main():
|
29 |
+
st.header("Q&A from Pdfs: ")
|
30 |
+
|
31 |
+
|
32 |
+
load_dotenv(find_dotenv())
|
33 |
+
|
34 |
+
pdf_reader = PdfReader('48lawsofpower.pdf')
|
35 |
+
# st.write(pdf_reader)
|
36 |
+
|
37 |
+
text = ""
|
38 |
+
for page in pdf_reader.pages:
|
39 |
+
text += page.extract_text()
|
40 |
+
|
41 |
+
text_splitter = CharacterTextSplitter(
|
42 |
+
separator = "\n",
|
43 |
+
chunk_size = 1000,
|
44 |
+
chunk_overlap = 200,
|
45 |
+
length_function = len,
|
46 |
+
)
|
47 |
+
## Chunk Formation
|
48 |
+
chunks = text_splitter.split_text(text= text)
|
49 |
+
|
50 |
+
## Embedding
|
51 |
+
embeddings = OpenAIEmbeddings()
|
52 |
+
document_search = FAISS.from_texts(chunks, embeddings)
|
53 |
+
|
54 |
+
|
55 |
+
query = st.text_input("Ask your questions: ")
|
56 |
+
|
57 |
+
docs = document_search.similarity_search(query=query)
|
58 |
+
|
59 |
+
llm = OpenAI()
|
60 |
+
chain = load_qa_chain(llm=llm, chain_type="stuff")
|
61 |
+
response = chain.run(input_documents=docs, question=query)
|
62 |
+
st.write(response)
|
63 |
+
|
64 |
+
|
65 |
+
if __name__ == '__main__':
|
66 |
+
main()
|
67 |
+
|
68 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
openai
|
3 |
+
PyPDF2
|
4 |
+
faiss-cpu
|
5 |
+
tiktoken
|
6 |
+
streamlit
|
7 |
+
python-dotenv
|
8 |
+
pickle
|
9 |
+
os
|