Spaces:
Runtime error
Runtime error
ddovidovich commited on
Commit ·
797f3b1
1
Parent(s): 15c2291
test
Browse files- app.py +50 -46
- requirements.txt +6 -14
app.py
CHANGED
|
@@ -1,52 +1,56 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
os.system('pip install -q pytesseract')
|
| 10 |
-
#os.system('apt-get install poppler-utils')
|
| 11 |
|
| 12 |
os.environ["OPENAI_API_KEY"] = os.getenv("SECRET_KEY")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
from langchain.chat_models import ChatOpenAI
|
| 19 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 20 |
-
from PIL import Image
|
| 21 |
-
from datetime import datetime
|
| 22 |
-
from tempfile import NamedTemporaryFile
|
| 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 |
-
chain = load_qa_chain(ChatOpenAI(temperature=0), chain_type="stuff")
|
| 51 |
-
output = chain.run(input_documents=docs, question=query)
|
| 52 |
-
st.write(output)
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
import streamlit as st
|
| 3 |
+
from PyPDF2 import PdfReader
|
| 4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 5 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 6 |
+
from langchain.vectorstores import FAISS
|
| 7 |
+
from langchain.chains.question_answering import load_qa_chain
|
| 8 |
+
from langchain.llms import OpenAI
|
| 9 |
+
from langchain.callbacks import get_openai_callback
|
|
|
|
|
|
|
| 10 |
|
| 11 |
os.environ["OPENAI_API_KEY"] = os.getenv("SECRET_KEY")
|
| 12 |
|
| 13 |
+
def main():
|
| 14 |
+
load_dotenv()
|
| 15 |
+
st.set_page_config(page_title="Ask your PDF")
|
| 16 |
+
st.header("Ask your PDF 💬")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# upload file
|
| 19 |
+
pdf = st.file_uploader("Upload your PDF", type="pdf")
|
| 20 |
+
|
| 21 |
+
# extract the text
|
| 22 |
+
if pdf is not None:
|
| 23 |
+
pdf_reader = PdfReader(pdf)
|
| 24 |
+
text = ""
|
| 25 |
+
for page in pdf_reader.pages:
|
| 26 |
+
text += page.extract_text()
|
| 27 |
+
|
| 28 |
+
# split into chunks
|
| 29 |
+
text_splitter = CharacterTextSplitter(
|
| 30 |
+
separator="\n",
|
| 31 |
+
chunk_size=1000,
|
| 32 |
+
chunk_overlap=200,
|
| 33 |
+
length_function=len
|
| 34 |
+
)
|
| 35 |
+
chunks = text_splitter.split_text(text)
|
| 36 |
+
|
| 37 |
+
# create embeddings
|
| 38 |
+
embeddings = OpenAIEmbeddings()
|
| 39 |
+
knowledge_base = FAISS.from_texts(chunks, embeddings)
|
| 40 |
+
|
| 41 |
+
# show user input
|
| 42 |
+
user_question = st.text_input("Ask a question about your PDF:")
|
| 43 |
+
if user_question:
|
| 44 |
+
docs = knowledge_base.similarity_search(user_question)
|
| 45 |
+
|
| 46 |
+
llm = OpenAI()
|
| 47 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
| 48 |
+
with get_openai_callback() as cb:
|
| 49 |
+
response = chain.run(input_documents=docs, question=user_question)
|
| 50 |
+
print(cb)
|
| 51 |
+
|
| 52 |
+
st.write(response)
|
| 53 |
|
| 54 |
+
|
| 55 |
+
if __name__ == '__main__':
|
| 56 |
+
main()
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,14 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
unstructured
|
| 8 |
-
chromadb
|
| 9 |
-
tiktoken
|
| 10 |
-
pypdfium2
|
| 11 |
-
unstructured_inference
|
| 12 |
-
tesseract
|
| 13 |
-
poppler-utils
|
| 14 |
-
pdf2image
|
|
|
|
| 1 |
+
langchain==0.0.154
|
| 2 |
+
PyPDF2==3.0.1
|
| 3 |
+
python-dotenv==1.0.0
|
| 4 |
+
streamlit==1.18.1
|
| 5 |
+
faiss-cpu==1.7.4
|
| 6 |
+
altair<5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|