Francesco-A commited on
Commit
ebabcc8
1 Parent(s): 140ce7b

Remove quotation marks from .env

Files changed (6) hide show
  1. .gitignore +160 -0
  2. README.md +3 -3
  3. app.py +93 -0
  4. attention.pdf +0 -0
  5. bert.pdf +0 -0
  6. requirements.txt +12 -0
.gitignore ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/#use-with-ide
110
+ .pdm.toml
111
+
112
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113
+ __pypackages__/
114
+
115
+ # Celery stuff
116
+ celerybeat-schedule
117
+ celerybeat.pid
118
+
119
+ # SageMath parsed files
120
+ *.sage.py
121
+
122
+ # Environments
123
+ .env
124
+ .venv
125
+ env/
126
+ venv/
127
+ ENV/
128
+ env.bak/
129
+ venv.bak/
130
+
131
+ # Spyder project settings
132
+ .spyderproject
133
+ .spyproject
134
+
135
+ # Rope project settings
136
+ .ropeproject
137
+
138
+ # mkdocs documentation
139
+ /site
140
+
141
+ # mypy
142
+ .mypy_cache/
143
+ .dmypy.json
144
+ dmypy.json
145
+
146
+ # Pyre type checker
147
+ .pyre/
148
+
149
+ # pytype static type analyzer
150
+ .pytype/
151
+
152
+ # Cython debug symbols
153
+ cython_debug/
154
+
155
+ # PyCharm
156
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
159
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
160
+ #.idea/
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Langchain PDF-QA
3
- emoji: 📉
4
- colorFrom: gray
5
- colorTo: pink
6
  sdk: gradio
7
  sdk_version: 4.8.0
8
  app_file: app.py
 
1
  ---
2
  title: Langchain PDF-QA
3
+ emoji:
4
+ colorFrom: purple
5
+ colorTo: blue
6
  sdk: gradio
7
  sdk_version: 4.8.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # %% auto 0
2
+ __all__ = ['text_splitter', 'embeddings', 'hub_llm', 'pdf_example_1', 'pdf_example_2', 'question_example_1', 'question_example_2',
3
+ 'title', 'description', 'file_upload', 'question', 'output', 'split_pdf', 'ask_pdf']
4
+
5
+ import dotenv
6
+ from dotenv import load_dotenv
7
+ load_dotenv()
8
+
9
+ # Import qa chain
10
+ from langchain.chains.question_answering import load_qa_chain
11
+
12
+ # Import gradio for UI/app interface
13
+ import gradio as gr
14
+
15
+ # Import PDF Loaders
16
+ from langchain.document_loaders import PyPDFLoader
17
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
18
+
19
+ # Create an instance of RecursiveCharacterTextSplitter with your desired chunk_size and chunk_overlap
20
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=250)
21
+
22
+ def split_pdf(pdf):
23
+ if pdf is not None:
24
+ # load pdf
25
+ loader = PyPDFLoader(pdf)
26
+ # Split pages from pdf (SEE: https://api.python.langchain.com/en/latest/document_loaders/langchain.document_loaders.pdf.PyPDFLoader.html)
27
+ chunks = loader.load_and_split(text_splitter=text_splitter)
28
+ return chunks
29
+
30
+ # Import HuggingFace as main LLM service
31
+ from langchain.embeddings import HuggingFaceEmbeddings
32
+
33
+ embeddings = HuggingFaceEmbeddings(
34
+ model_name = "bert-base-nli-mean-tokens",
35
+ model_kwargs = {'device': 'cpu'},
36
+ encode_kwargs = {'normalize_embeddings': True}
37
+ )
38
+
39
+ # Import FAISS as the vector store
40
+ from langchain.vectorstores import FAISS
41
+
42
+ # import transformers
43
+ # from transformers import AutoTokenizer, AutoModelForCausalLM
44
+ from langchain import HuggingFaceHub
45
+ import torch
46
+
47
+ hub_llm = HuggingFaceHub(
48
+ repo_id = 'tiiuae/falcon-7b-instruct',
49
+ # repo_id = 'tiiuae/falcon-7b-instruct',
50
+ model_kwargs = {'temperature' : 0.01, # 'randomness' of outputs, 0.0 is the min and 1.0 the max
51
+ # 'top_p': 0.15, # select from top tokens whose probability add up to x%
52
+ 'top_k': 25, # select from top x tokens
53
+ 'max_new_tokens': 300, # mex number of tokens to generate in the output
54
+ 'repetition_penalty': 999 # without this output begins repeating
55
+ }
56
+ )
57
+
58
+ chain = load_qa_chain(hub_llm, chain_type="stuff")
59
+
60
+ # App Function
61
+ def ask_pdf(pdf, user_question):
62
+ if pdf is not None and user_question != '':
63
+ chunks = split_pdf(pdf) # split pdf into smaller chunks
64
+ store = FAISS.from_documents(chunks, embeddings) # Load documents into vector database
65
+ docs = store.similarity_search(user_question) # take user input and look for chunks that might contain the answer
66
+ # run chain
67
+ response = chain.run(input_documents=docs, question=user_question)
68
+
69
+ return response
70
+
71
+ pdf_example_1 = '/attention.pdf'
72
+ pdf_example_2 = '/bert.pdf'
73
+ question_example_1 = "What does 'attention' mean in this context?"
74
+ question_example_2 = "What does BERT stand for?"
75
+
76
+ title="LangChain project: Conversational Retrieval Chain"
77
+ description="Upload a PDF file and ask a question related to its content."
78
+
79
+ # Create Gradio interface
80
+ file_upload = gr.File(file_types = ['.pdf'],
81
+ file_count='single',
82
+ label="Upload a PDF file")
83
+ question = gr.Textbox(label="Write your question here:", show_copy_button= True)
84
+ output = gr.Textbox(label="Your answer:")
85
+
86
+ gr.Interface(
87
+ fn=ask_pdf,
88
+ inputs=[file_upload, question],
89
+ outputs=output,
90
+ title=title,
91
+ description=description,
92
+ examples=[[pdf_example_1, question_example_1], [pdf_example_2, question_example_2]]
93
+ ).launch() #share=True
attention.pdf ADDED
Binary file (569 kB). View file
 
bert.pdf ADDED
Binary file (775 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ python-dotenv==1.0.0
2
+ langchain==0.0.344
3
+ langchain-core==0.0.8
4
+ accelerate==0.25.0
5
+ sentence-transformers==2.2.2
6
+ sentence-transformers==2.2.2
7
+ transformers==4.35.2
8
+ faiss-gpu==1.7.2
9
+ pypdf==3.17.1
10
+ gradio==4.7.1
11
+ gradio_client==0.7.0
12
+ fastapi==0.104.1