Spaces:
Sleeping
Sleeping
File size: 1,762 Bytes
b370feb |
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 64 65 66 67 68 |
from PyPDF2 import PdfReader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
import streamlit as st
from dotenv import load_dotenv,find_dotenv
from streamlit_extras.add_vertical_space import add_vertical_space
import pickle
import os
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
## Slide-bar
with st.sidebar:
st.title('PDF Q&A')
st.markdown('''
## About
This app is an LLM-powered chatbot built using:
- [Streamlit](https://streamlit.io/)
- [LangChain](https://python.langchain.com/)
- [OpenAI](https://platform.openai.com/docs/models) LLM model
''')
add_vertical_space(5)
st.write('Made by Harshit')
def main():
st.header("Q&A from Pdfs: ")
load_dotenv(find_dotenv())
pdf_reader = PdfReader('48lawsofpower.pdf')
# st.write(pdf_reader)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
text_splitter = CharacterTextSplitter(
separator = "\n",
chunk_size = 1000,
chunk_overlap = 200,
length_function = len,
)
## Chunk Formation
chunks = text_splitter.split_text(text= text)
## Embedding
embeddings = OpenAIEmbeddings()
document_search = FAISS.from_texts(chunks, embeddings)
query = st.text_input("Ask your questions: ")
docs = document_search.similarity_search(query=query)
llm = OpenAI()
chain = load_qa_chain(llm=llm, chain_type="stuff")
response = chain.run(input_documents=docs, question=query)
st.write(response)
if __name__ == '__main__':
main()
|