-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44c8b11
commit b94db62
Showing
5 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control | ||
.pdm.toml | ||
.pdm-python | ||
.pdm-build/ | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
venv-app/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
#.idea/ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import streamlit as st | ||
from streamlit_extras.add_vertical_space import add_vertical_space | ||
from PyPDF2 import PdfReader | ||
from langchain.text_splitter import RecursiveCharacterTextSplitter | ||
from langchain_openai.embeddings import OpenAIEmbeddings | ||
from langchain_community.vectorstores import FAISS | ||
from langchain_openai import OpenAI | ||
from langchain.chains.question_answering import load_qa_chain | ||
from langchain.callbacks import get_openai_callback | ||
|
||
import pickle | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
load_dotenv() | ||
st.set_page_config(page_title="Chat with Pdf", page_icon=":robots:", layout="wide") | ||
|
||
##side bar components | ||
with st.sidebar: | ||
st.title("About") | ||
st.write("Chat with Pdf 🚀") | ||
|
||
add_vertical_space(5) | ||
st.write("Created by Manimaran [Blog](https://rmanimaran.wordpress.com)") | ||
|
||
import threading | ||
|
||
class PicklableFAISS(FAISS): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.lock = threading.RLock() | ||
|
||
def __getstate__(self): | ||
state = self.__dict__.copy() | ||
del state['lock'] | ||
return state | ||
|
||
def __setstate__(self, state): | ||
self.__dict__.update(state) | ||
self.lock = threading.RLock() | ||
|
||
|
||
|
||
def main(): | ||
st.title("Chat with Pdf") | ||
pdf = st.file_uploader("Upload a PDF", type="pdf") | ||
if pdf is not None: | ||
st.write("PDF uploaded successfully") | ||
# You can now use the 'pdf' object to process the PDF file | ||
pdf_reader = PdfReader(pdf) | ||
st.write("Number of pages:", len(pdf_reader.pages)) | ||
# extract the text from each page | ||
text = "" | ||
for page in pdf_reader.pages: | ||
text += page.extract_text() | ||
|
||
text_splitter = RecursiveCharacterTextSplitter( | ||
chunk_size=1000, | ||
chunk_overlap=200, | ||
length_function=len, | ||
separators=["\n\n", "\n", ".", " ", ""], | ||
) | ||
chunks = text_splitter.split_text(text) | ||
st.write("Number of chunks:", len(chunks)) | ||
store_name = pdf.name[:-4] | ||
# check if the vector store already exists | ||
if os.path.exists(f"{store_name}.faiss"): | ||
st.write("Vector store already exists") | ||
vectorstore = FAISS.load_local(f"{store_name}.faiss", OpenAIEmbeddings(), allow_dangerous_deserialization=True) | ||
else: | ||
st.write("Creating vector store...") | ||
#embeddings | ||
embeddings = OpenAIEmbeddings() | ||
#vectorstore = PicklableFAISS.from_texts(chunks, embeddings) | ||
vectorstore = FAISS.from_texts(chunks, embeddings) | ||
# store the vector for the pdf and save using picke | ||
|
||
vectorstore.save_local(f"{store_name}.faiss") | ||
st.write("Vector store saved successfully") | ||
|
||
#load the vector store | ||
vectorstore = FAISS.load_local(f"{store_name}.faiss", embeddings, allow_dangerous_deserialization=True) | ||
st.write("Vector store loaded successfully") | ||
|
||
#Accept user question | ||
query = st.text_input("Ask a question about your PDF:") | ||
if query: | ||
docs = vectorstore.similarity_search(query,k=3) | ||
#st.write(docs) | ||
#st.write("Answer:", docs) | ||
# Now create the chain and pass to LLM | ||
llm = OpenAI(temperature=0) | ||
chain = load_qa_chain(llm, chain_type="stuff") | ||
# Run the chain and get the response | ||
with st.spinner("Generating response..."): | ||
with get_openai_callback() as cb: | ||
response = chain.run(input_documents=docs, question=query) | ||
st.sidebar.success(f"Total Tokens: {cb.total_tokens}") | ||
st.sidebar.success(f"Prompt Tokens: {cb.prompt_tokens}") | ||
st.sidebar.success(f"Completion Tokens: {cb.completion_tokens}") | ||
st.sidebar.success(f"Total Cost (USD): ${cb.total_cost}") | ||
st.write(response) | ||
else: | ||
st.write("Please upload a PDF file") | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Final Output | ||
 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
langchain | ||
PyPDF2 | ||
python-dotenv | ||
streamlit | ||
faiss-cpu | ||
streamlit-extras | ||
langchain_community | ||
langchain_openai |