Spaces:
Sleeping
Sleeping
import streamlit as st | |
from PyPDF2 import PdfReader | |
from langchain.text_splitter import CharacterTextSplitter | |
from langchain.embeddings.openai import OpenAIEmbeddings | |
from langchain.vectorstores import FAISS | |
from langchain.chains.question_answering import load_qa_chain | |
from langchain.llms import OpenAI | |
from langchain.callbacks import get_openai_callback | |
import os | |
import openai | |
from streamlit_chat import message | |
def openai_pdf(): | |
OPENAI_API_KEY = st.text_input("Input your OpenAI API key", "") | |
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY | |
# st.header("Ask your PDF 💬") | |
# upload file | |
pdf = st.file_uploader("Upload your PDF", type="pdf") | |
# extract the text | |
if pdf is not None: | |
pdf_reader = PdfReader(pdf) | |
text = "" | |
for page in pdf_reader.pages: | |
text += page.extract_text() | |
# split into chunks | |
text_splitter = CharacterTextSplitter( | |
separator="\n", | |
chunk_size=1000, | |
chunk_overlap=200, | |
length_function=len | |
) | |
chunks = text_splitter.split_text(text) | |
# create embeddings | |
embeddings = OpenAIEmbeddings() | |
knowledge_base = FAISS.from_texts(chunks, embeddings) | |
if 'generated' not in st.session_state: | |
st.session_state['generated'] = [] | |
if 'past' not in st.session_state: | |
st.session_state['past'] = [] | |
# show user input | |
user_question = st.text_input("Ask a question about your PDF:") | |
if user_question: | |
docs = knowledge_base.similarity_search(user_question) | |
llm = OpenAI() | |
chain = load_qa_chain(llm, chain_type="stuff") | |
with get_openai_callback() as cb: | |
response = chain.run(input_documents=docs, question=user_question) | |
print(cb) | |
#st.write(response) | |
st.session_state.past.append(user_question) | |
st.session_state.generated.append(response) | |
if st.session_state['generated']: | |
for i in range(len(st.session_state['generated'])-1, -1, -1): | |
message(st.session_state["generated"][i], key=str(i)) | |
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user') |