misbah1955
commited on
Commit
·
88eaeda
1
Parent(s):
e75ab96
Upload 10 files
Browse files- Dockerfile.txt +30 -0
- LICENSE.txt +201 -0
- Link to images.txt +4 -0
- README.md +13 -0
- app.py +309 -0
- bot.jpg +0 -0
- bot.png +0 -0
- gitignore.txt +10 -0
- requirements.txt +16 -0
- user.jfif +0 -0
Dockerfile.txt
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10
|
2 |
+
|
3 |
+
WORKDIR /src
|
4 |
+
|
5 |
+
COPY requirements.txt .
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
8 |
+
|
9 |
+
# Set up a new user named "user" with user ID 1000
|
10 |
+
RUN useradd -m -u 1000 user
|
11 |
+
# Switch to the "user" user
|
12 |
+
USER user
|
13 |
+
# Set home to the user's home directory
|
14 |
+
ENV HOME=/home/user \
|
15 |
+
PATH=/home/user/.local/bin:$PATH \
|
16 |
+
PYTHONPATH=$HOME/app \
|
17 |
+
PYTHONUNBUFFERED=1 \
|
18 |
+
GRADIO_ALLOW_FLAGGING=never \
|
19 |
+
GRADIO_NUM_PORTS=1 \
|
20 |
+
GRADIO_SERVER_NAME=0.0.0.0 \
|
21 |
+
GRADIO_THEME=huggingface \
|
22 |
+
SYSTEM=spaces
|
23 |
+
|
24 |
+
# Set the working directory to the user's home directory
|
25 |
+
WORKDIR $HOME/app
|
26 |
+
|
27 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
28 |
+
COPY --chown=user . $HOME/app
|
29 |
+
|
30 |
+
CMD ["python", "app.py"]
|
LICENSE.txt
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Apache License
|
2 |
+
Version 2.0, January 2004
|
3 |
+
http://www.apache.org/licenses/
|
4 |
+
|
5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6 |
+
|
7 |
+
1. Definitions.
|
8 |
+
|
9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
11 |
+
|
12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13 |
+
the copyright owner that is granting the License.
|
14 |
+
|
15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
16 |
+
other entities that control, are controlled by, or are under common
|
17 |
+
control with that entity. For the purposes of this definition,
|
18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
19 |
+
direction or management of such entity, whether by contract or
|
20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22 |
+
|
23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24 |
+
exercising permissions granted by this License.
|
25 |
+
|
26 |
+
"Source" form shall mean the preferred form for making modifications,
|
27 |
+
including but not limited to software source code, documentation
|
28 |
+
source, and configuration files.
|
29 |
+
|
30 |
+
"Object" form shall mean any form resulting from mechanical
|
31 |
+
transformation or translation of a Source form, including but
|
32 |
+
not limited to compiled object code, generated documentation,
|
33 |
+
and conversions to other media types.
|
34 |
+
|
35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
36 |
+
Object form, made available under the License, as indicated by a
|
37 |
+
copyright notice that is included in or attached to the work
|
38 |
+
(an example is provided in the Appendix below).
|
39 |
+
|
40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41 |
+
form, that is based on (or derived from) the Work and for which the
|
42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
44 |
+
of this License, Derivative Works shall not include works that remain
|
45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46 |
+
the Work and Derivative Works thereof.
|
47 |
+
|
48 |
+
"Contribution" shall mean any work of authorship, including
|
49 |
+
the original version of the Work and any modifications or additions
|
50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
54 |
+
means any form of electronic, verbal, or written communication sent
|
55 |
+
to the Licensor or its representatives, including but not limited to
|
56 |
+
communication on electronic mailing lists, source code control systems,
|
57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
59 |
+
excluding communication that is conspicuously marked or otherwise
|
60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
61 |
+
|
62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
64 |
+
subsequently incorporated within the Work.
|
65 |
+
|
66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
71 |
+
Work and such Derivative Works in Source or Object form.
|
72 |
+
|
73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76 |
+
(except as stated in this section) patent license to make, have made,
|
77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78 |
+
where such license applies only to those patent claims licensable
|
79 |
+
by such Contributor that are necessarily infringed by their
|
80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
82 |
+
institute patent litigation against any entity (including a
|
83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84 |
+
or a Contribution incorporated within the Work constitutes direct
|
85 |
+
or contributory patent infringement, then any patent licenses
|
86 |
+
granted to You under this License for that Work shall terminate
|
87 |
+
as of the date such litigation is filed.
|
88 |
+
|
89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
90 |
+
Work or Derivative Works thereof in any medium, with or without
|
91 |
+
modifications, and in Source or Object form, provided that You
|
92 |
+
meet the following conditions:
|
93 |
+
|
94 |
+
(a) You must give any other recipients of the Work or
|
95 |
+
Derivative Works a copy of this License; and
|
96 |
+
|
97 |
+
(b) You must cause any modified files to carry prominent notices
|
98 |
+
stating that You changed the files; and
|
99 |
+
|
100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
101 |
+
that You distribute, all copyright, patent, trademark, and
|
102 |
+
attribution notices from the Source form of the Work,
|
103 |
+
excluding those notices that do not pertain to any part of
|
104 |
+
the Derivative Works; and
|
105 |
+
|
106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107 |
+
distribution, then any Derivative Works that You distribute must
|
108 |
+
include a readable copy of the attribution notices contained
|
109 |
+
within such NOTICE file, excluding those notices that do not
|
110 |
+
pertain to any part of the Derivative Works, in at least one
|
111 |
+
of the following places: within a NOTICE text file distributed
|
112 |
+
as part of the Derivative Works; within the Source form or
|
113 |
+
documentation, if provided along with the Derivative Works; or,
|
114 |
+
within a display generated by the Derivative Works, if and
|
115 |
+
wherever such third-party notices normally appear. The contents
|
116 |
+
of the NOTICE file are for informational purposes only and
|
117 |
+
do not modify the License. You may add Your own attribution
|
118 |
+
notices within Derivative Works that You distribute, alongside
|
119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
120 |
+
that such additional attribution notices cannot be construed
|
121 |
+
as modifying the License.
|
122 |
+
|
123 |
+
You may add Your own copyright statement to Your modifications and
|
124 |
+
may provide additional or different license terms and conditions
|
125 |
+
for use, reproduction, or distribution of Your modifications, or
|
126 |
+
for any such Derivative Works as a whole, provided Your use,
|
127 |
+
reproduction, and distribution of the Work otherwise complies with
|
128 |
+
the conditions stated in this License.
|
129 |
+
|
130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
132 |
+
by You to the Licensor shall be under the terms and conditions of
|
133 |
+
this License, without any additional terms or conditions.
|
134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135 |
+
the terms of any separate license agreement you may have executed
|
136 |
+
with Licensor regarding such Contributions.
|
137 |
+
|
138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
140 |
+
except as required for reasonable and customary use in describing the
|
141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
142 |
+
|
143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144 |
+
agreed to in writing, Licensor provides the Work (and each
|
145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147 |
+
implied, including, without limitation, any warranties or conditions
|
148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150 |
+
appropriateness of using or redistributing the Work and assume any
|
151 |
+
risks associated with Your exercise of permissions under this License.
|
152 |
+
|
153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
154 |
+
whether in tort (including negligence), contract, or otherwise,
|
155 |
+
unless required by applicable law (such as deliberate and grossly
|
156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157 |
+
liable to You for damages, including any direct, indirect, special,
|
158 |
+
incidental, or consequential damages of any character arising as a
|
159 |
+
result of this License or out of the use or inability to use the
|
160 |
+
Work (including but not limited to damages for loss of goodwill,
|
161 |
+
work stoppage, computer failure or malfunction, or any and all
|
162 |
+
other commercial damages or losses), even if such Contributor
|
163 |
+
has been advised of the possibility of such damages.
|
164 |
+
|
165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168 |
+
or other liability obligations and/or rights consistent with this
|
169 |
+
License. However, in accepting such obligations, You may act only
|
170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171 |
+
of any other Contributor, and only if You agree to indemnify,
|
172 |
+
defend, and hold each Contributor harmless for any liability
|
173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
174 |
+
of your accepting any such warranty or additional liability.
|
175 |
+
|
176 |
+
END OF TERMS AND CONDITIONS
|
177 |
+
|
178 |
+
APPENDIX: How to apply the Apache License to your work.
|
179 |
+
|
180 |
+
To apply the Apache License to your work, attach the following
|
181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182 |
+
replaced with your own identifying information. (Don't include
|
183 |
+
the brackets!) The text should be enclosed in the appropriate
|
184 |
+
comment syntax for the file format. We also recommend that a
|
185 |
+
file or class name and description of purpose be included on the
|
186 |
+
same "printed page" as the copyright notice for easier
|
187 |
+
identification within third-party archives.
|
188 |
+
|
189 |
+
Copyright [yyyy] [name of copyright owner]
|
190 |
+
|
191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192 |
+
you may not use this file except in compliance with the License.
|
193 |
+
You may obtain a copy of the License at
|
194 |
+
|
195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
196 |
+
|
197 |
+
Unless required by applicable law or agreed to in writing, software
|
198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200 |
+
See the License for the specific language governing permissions and
|
201 |
+
limitations under the License.
|
Link to images.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Robot emoji: https://commons.wikimedia.org/wiki/File:Fluent_Emoji_high_contrast_1f916.svg
|
2 |
+
|
3 |
+
Bing smile emoji: https://www.bing.com/images/create/a-black-and-white-emoji-with-a-simple-smile2c-black/6523d2c320df409581e85bec80ef3ba8?id=KTdVbixG8oRqR9BzF6AblQ%3d%3d&view=detailv2&idpp=genimg&idpclose=1&FORM=SYDBIC
|
4 |
+
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Light PDF web QA chatbot
|
3 |
+
emoji: 🌍
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: yellow
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.35.2
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: apache-2.0
|
11 |
+
---
|
12 |
+
|
13 |
+
Chat with a pdf file or web page using a light language model through a Gradio interface. Quick responses even just using CPU.
|
app.py
ADDED
@@ -0,0 +1,309 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# # Load in packages
|
2 |
+
|
3 |
+
# +
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Need to overwrite version of gradio present in Huggingface spaces as it doesn't have like buttons/avatars (Oct 2023)
|
7 |
+
#os.system("pip uninstall -y gradio")
|
8 |
+
os.system("pip install gradio==3.42.0")
|
9 |
+
|
10 |
+
from typing import TypeVar
|
11 |
+
from langchain.embeddings import HuggingFaceEmbeddings#, HuggingFaceInstructEmbeddings
|
12 |
+
from langchain.vectorstores import FAISS
|
13 |
+
import gradio as gr
|
14 |
+
|
15 |
+
from transformers import AutoTokenizer
|
16 |
+
|
17 |
+
# Alternative model sources
|
18 |
+
from ctransformers import AutoModelForCausalLM
|
19 |
+
|
20 |
+
PandasDataFrame = TypeVar('pd.core.frame.DataFrame')
|
21 |
+
|
22 |
+
# Disable cuda devices if necessary
|
23 |
+
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
24 |
+
|
25 |
+
#from chatfuncs.chatfuncs import *
|
26 |
+
import chatfuncs.ingest as ing
|
27 |
+
|
28 |
+
## Load preset embeddings, vectorstore, and model
|
29 |
+
|
30 |
+
embeddings_name = "BAAI/bge-base-en-v1.5"
|
31 |
+
|
32 |
+
def load_embeddings(embeddings_name = "BAAI/bge-base-en-v1.5"):
|
33 |
+
|
34 |
+
|
35 |
+
#if embeddings_name == "hkunlp/instructor-large":
|
36 |
+
# embeddings_func = HuggingFaceInstructEmbeddings(model_name=embeddings_name,
|
37 |
+
# embed_instruction="Represent the paragraph for retrieval: ",
|
38 |
+
# query_instruction="Represent the question for retrieving supporting documents: "
|
39 |
+
# )
|
40 |
+
|
41 |
+
#else:
|
42 |
+
embeddings_func = HuggingFaceEmbeddings(model_name=embeddings_name)
|
43 |
+
|
44 |
+
global embeddings
|
45 |
+
|
46 |
+
embeddings = embeddings_func
|
47 |
+
|
48 |
+
return embeddings
|
49 |
+
|
50 |
+
def get_faiss_store(faiss_vstore_folder,embeddings):
|
51 |
+
import zipfile
|
52 |
+
with zipfile.ZipFile(faiss_vstore_folder + '/' + faiss_vstore_folder + '.zip', 'r') as zip_ref:
|
53 |
+
zip_ref.extractall(faiss_vstore_folder)
|
54 |
+
|
55 |
+
faiss_vstore = FAISS.load_local(folder_path=faiss_vstore_folder, embeddings=embeddings)
|
56 |
+
os.remove(faiss_vstore_folder + "/index.faiss")
|
57 |
+
os.remove(faiss_vstore_folder + "/index.pkl")
|
58 |
+
|
59 |
+
global vectorstore
|
60 |
+
|
61 |
+
vectorstore = faiss_vstore
|
62 |
+
|
63 |
+
return vectorstore
|
64 |
+
|
65 |
+
import chatfuncs.chatfuncs as chatf
|
66 |
+
|
67 |
+
chatf.embeddings = load_embeddings(embeddings_name)
|
68 |
+
chatf.vectorstore = get_faiss_store(faiss_vstore_folder="faiss_embedding",embeddings=globals()["embeddings"])
|
69 |
+
|
70 |
+
def load_model(model_type, gpu_layers, gpu_config=None, cpu_config=None, torch_device=None):
|
71 |
+
print("Loading model")
|
72 |
+
|
73 |
+
# Default values inside the function
|
74 |
+
if gpu_config is None:
|
75 |
+
gpu_config = chatf.gpu_config
|
76 |
+
if cpu_config is None:
|
77 |
+
cpu_config = chatf.cpu_config
|
78 |
+
if torch_device is None:
|
79 |
+
torch_device = chatf.torch_device
|
80 |
+
|
81 |
+
if model_type == "Mistral Open Orca (larger, slow)":
|
82 |
+
if torch_device == "cuda":
|
83 |
+
gpu_config.update_gpu(gpu_layers)
|
84 |
+
else:
|
85 |
+
gpu_config.update_gpu(gpu_layers)
|
86 |
+
cpu_config.update_gpu(gpu_layers)
|
87 |
+
|
88 |
+
print("Loading with", cpu_config.gpu_layers, "model layers sent to GPU.")
|
89 |
+
|
90 |
+
print(vars(gpu_config))
|
91 |
+
print(vars(cpu_config))
|
92 |
+
|
93 |
+
try:
|
94 |
+
#model = AutoModelForCausalLM.from_pretrained('Aryanne/Orca-Mini-3B-gguf', model_type='llama', model_file='q5_0-orca-mini-3b.gguf', **vars(gpu_config)) # **asdict(CtransRunConfig_cpu())
|
95 |
+
#model = AutoModelForCausalLM.from_pretrained('Aryanne/Wizard-Orca-3B-gguf', model_type='llama', model_file='q4_1-wizard-orca-3b.gguf', **vars(gpu_config)) # **asdict(CtransRunConfig_cpu())
|
96 |
+
model = AutoModelForCausalLM.from_pretrained('TheBloke/Mistral-7B-OpenOrca-GGUF', model_type='mistral', model_file='mistral-7b-openorca.Q4_K_M.gguf', **vars(gpu_config)) # **asdict(CtransRunConfig_cpu())
|
97 |
+
#model = AutoModelForCausalLM.from_pretrained('TheBloke/MistralLite-7B-GGUF', model_type='mistral', model_file='mistrallite.Q4_K_M.gguf', **vars(gpu_config)) # **asdict(CtransRunConfig_cpu())
|
98 |
+
|
99 |
+
except:
|
100 |
+
#model = AutoModelForCausalLM.from_pretrained('Aryanne/Orca-Mini-3B-gguf', model_type='llama', model_file='q5_0-orca-mini-3b.gguf', **vars(cpu_config)) #**asdict(CtransRunConfig_gpu())
|
101 |
+
#model = AutoModelForCausalLM.from_pretrained('Aryanne/Wizard-Orca-3B-gguf', model_type='llama', model_file='q4_1-wizard-orca-3b.gguf', **vars(cpu_config)) # **asdict(CtransRunConfig_cpu())
|
102 |
+
model = AutoModelForCausalLM.from_pretrained('TheBloke/Mistral-7B-OpenOrca-GGUF', model_type='mistral', model_file='mistral-7b-openorca.Q4_K_M.gguf', **vars(cpu_config)) # **asdict(CtransRunConfig_cpu())
|
103 |
+
#model = AutoModelForCausalLM.from_pretrained('TheBloke/MistralLite-7B-GGUF', model_type='mistral', model_file='mistrallite.Q4_K_M.gguf', **vars(cpu_config)) # **asdict(CtransRunConfig_cpu())
|
104 |
+
|
105 |
+
tokenizer = []
|
106 |
+
|
107 |
+
if model_type == "Flan Alpaca (small, fast)":
|
108 |
+
# Huggingface chat model
|
109 |
+
hf_checkpoint = 'declare-lab/flan-alpaca-large'#'declare-lab/flan-alpaca-base' # # #
|
110 |
+
|
111 |
+
def create_hf_model(model_name):
|
112 |
+
|
113 |
+
from transformers import AutoModelForSeq2SeqLM, AutoModelForCausalLM
|
114 |
+
|
115 |
+
if torch_device == "cuda":
|
116 |
+
if "flan" in model_name:
|
117 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, device_map="auto")
|
118 |
+
else:
|
119 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
120 |
+
else:
|
121 |
+
if "flan" in model_name:
|
122 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
123 |
+
else:
|
124 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
125 |
+
|
126 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, model_max_length = chatf.context_length)
|
127 |
+
|
128 |
+
return model, tokenizer, model_type
|
129 |
+
|
130 |
+
model, tokenizer, model_type = create_hf_model(model_name = hf_checkpoint)
|
131 |
+
|
132 |
+
chatf.model = model
|
133 |
+
chatf.tokenizer = tokenizer
|
134 |
+
chatf.model_type = model_type
|
135 |
+
|
136 |
+
load_confirmation = "Finished loading model: " + model_type
|
137 |
+
|
138 |
+
print(load_confirmation)
|
139 |
+
return model_type, load_confirmation, model_type
|
140 |
+
|
141 |
+
# Both models are loaded on app initialisation so that users don't have to wait for the models to be downloaded
|
142 |
+
#model_type = "Mistral Open Orca (larger, slow)"
|
143 |
+
#load_model(model_type, chatf.gpu_layers, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
144 |
+
|
145 |
+
model_type = "Flan Alpaca (small, fast)"
|
146 |
+
load_model(model_type, 0, chatf.gpu_config, chatf.cpu_config, chatf.torch_device)
|
147 |
+
|
148 |
+
def docs_to_faiss_save(docs_out:PandasDataFrame, embeddings=embeddings):
|
149 |
+
|
150 |
+
print(f"> Total split documents: {len(docs_out)}")
|
151 |
+
|
152 |
+
print(docs_out)
|
153 |
+
|
154 |
+
vectorstore_func = FAISS.from_documents(documents=docs_out, embedding=embeddings)
|
155 |
+
|
156 |
+
|
157 |
+
chatf.vectorstore = vectorstore_func
|
158 |
+
|
159 |
+
out_message = "Document processing complete"
|
160 |
+
|
161 |
+
return out_message, vectorstore_func
|
162 |
+
|
163 |
+
# Gradio chat
|
164 |
+
|
165 |
+
block = gr.Blocks(theme = gr.themes.Base())#css=".gradio-container {background-color: black}")
|
166 |
+
|
167 |
+
with block:
|
168 |
+
ingest_text = gr.State()
|
169 |
+
ingest_metadata = gr.State()
|
170 |
+
ingest_docs = gr.State()
|
171 |
+
|
172 |
+
model_type_state = gr.State(model_type)
|
173 |
+
embeddings_state = gr.State(globals()["embeddings"])
|
174 |
+
vectorstore_state = gr.State(globals()["vectorstore"])
|
175 |
+
|
176 |
+
model_state = gr.State() # chatf.model (gives error)
|
177 |
+
tokenizer_state = gr.State() # chatf.tokenizer (gives error)
|
178 |
+
|
179 |
+
chat_history_state = gr.State()
|
180 |
+
instruction_prompt_out = gr.State()
|
181 |
+
|
182 |
+
gr.Markdown("<h1><center>Lightweight PDF / web page QA bot</center></h1>")
|
183 |
+
|
184 |
+
gr.Markdown("Chat with PDF, web page or (new) csv/Excel documents. The default is a small model (Flan Alpaca), that can only answer specific questions that are answered in the text. It cannot give overall impressions of, or summarise the document. The alternative (Mistral Open Orca (larger, slow)), can reason a little better, but is much slower (See Advanced tab).\n\nBy default the Lambeth Borough Plan '[Lambeth 2030 : Our Future, Our Lambeth](https://www.lambeth.gov.uk/better-fairer-lambeth/projects/lambeth-2030-our-future-our-lambeth)' is loaded. If you want to talk about another document or web page, please select from the second tab. If switching topic, please click the 'Clear chat' button.\n\nCaution: This is a public app. Please ensure that the document you upload is not sensitive is any way as other users may see it! Also, please note that LLM chatbots may give incomplete or incorrect information, so please use with care.")
|
185 |
+
|
186 |
+
with gr.Row():
|
187 |
+
current_source = gr.Textbox(label="Current data source(s)", value="Lambeth_2030-Our_Future_Our_Lambeth.pdf", scale = 10)
|
188 |
+
current_model = gr.Textbox(label="Current model", value=model_type, scale = 3)
|
189 |
+
|
190 |
+
with gr.Tab("Chatbot"):
|
191 |
+
|
192 |
+
with gr.Row():
|
193 |
+
#chat_height = 500
|
194 |
+
chatbot = gr.Chatbot(avatar_images=('user.jfif', 'bot.jpg'),bubble_full_width = False, scale = 1) # , height=chat_height
|
195 |
+
with gr.Accordion("Open this tab to see the source paragraphs used to generate the answer", open = False):
|
196 |
+
sources = gr.HTML(value = "Source paragraphs with the most relevant text will appear here", scale = 1) # , height=chat_height
|
197 |
+
|
198 |
+
with gr.Row():
|
199 |
+
message = gr.Textbox(
|
200 |
+
label="Enter your question here",
|
201 |
+
lines=1,
|
202 |
+
)
|
203 |
+
with gr.Row():
|
204 |
+
submit = gr.Button(value="Send message", variant="secondary", scale = 1)
|
205 |
+
clear = gr.Button(value="Clear chat", variant="secondary", scale=0)
|
206 |
+
stop = gr.Button(value="Stop generating", variant="secondary", scale=0)
|
207 |
+
|
208 |
+
examples_set = gr.Radio(label="Examples for the Lambeth Borough Plan",
|
209 |
+
#value = "What were the five pillars of the previous borough plan?",
|
210 |
+
choices=["What were the five pillars of the previous borough plan?",
|
211 |
+
"What is the vision statement for Lambeth?",
|
212 |
+
"What are the commitments for Lambeth?",
|
213 |
+
"What are the 2030 outcomes for Lambeth?"])
|
214 |
+
|
215 |
+
|
216 |
+
current_topic = gr.Textbox(label="Feature currently disabled - Keywords related to current conversation topic.", placeholder="Keywords related to the conversation topic will appear here")
|
217 |
+
|
218 |
+
|
219 |
+
|
220 |
+
with gr.Tab("Load in a different file to chat with"):
|
221 |
+
with gr.Accordion("PDF file", open = False):
|
222 |
+
in_pdf = gr.File(label="Upload pdf", file_count="multiple", file_types=['.pdf'])
|
223 |
+
load_pdf = gr.Button(value="Load in file", variant="secondary", scale=0)
|
224 |
+
|
225 |
+
with gr.Accordion("Web page", open = False):
|
226 |
+
with gr.Row():
|
227 |
+
in_web = gr.Textbox(label="Enter web page url")
|
228 |
+
in_div = gr.Textbox(label="(Advanced) Web page div for text extraction", value="p", placeholder="p")
|
229 |
+
load_web = gr.Button(value="Load in webpage", variant="secondary", scale=0)
|
230 |
+
|
231 |
+
with gr.Accordion("CSV/Excel file", open = False):
|
232 |
+
in_csv = gr.File(label="Upload CSV/Excel file", file_count="multiple", file_types=['.csv', '.xlsx'])
|
233 |
+
in_text_column = gr.Textbox(label="Enter column name where text is stored")
|
234 |
+
load_csv = gr.Button(value="Load in CSV/Excel file", variant="secondary", scale=0)
|
235 |
+
|
236 |
+
ingest_embed_out = gr.Textbox(label="File/web page preparation progress")
|
237 |
+
|
238 |
+
with gr.Tab("Advanced features"):
|
239 |
+
out_passages = gr.Slider(minimum=1, value = 2, maximum=10, step=1, label="Choose number of passages to retrieve from the document. Numbers greater than 2 may lead to increased hallucinations or input text being truncated.")
|
240 |
+
temp_slide = gr.Slider(minimum=0.1, value = 0.1, maximum=1, step=0.1, label="Choose temperature setting for response generation.")
|
241 |
+
with gr.Row():
|
242 |
+
model_choice = gr.Radio(label="Choose a chat model", value="Flan Alpaca (small, fast)", choices = ["Flan Alpaca (small, fast)", "Mistral Open Orca (larger, slow)"])
|
243 |
+
change_model_button = gr.Button(value="Load model", scale=0)
|
244 |
+
with gr.Accordion("Choose number of model layers to send to GPU (WARNING: please don't modify unless you are sure you have a GPU).", open = False):
|
245 |
+
gpu_layer_choice = gr.Slider(label="Choose number of model layers to send to GPU.", value=0, minimum=0, maximum=5, step = 1, visible=True)
|
246 |
+
|
247 |
+
load_text = gr.Text(label="Load status")
|
248 |
+
|
249 |
+
|
250 |
+
gr.HTML(
|
251 |
+
"<center>This app is based on the models Flan Alpaca and Mistral Open Orca. It powered by Gradio, Transformers, Ctransformers, and Langchain.</a></center>"
|
252 |
+
)
|
253 |
+
|
254 |
+
examples_set.change(fn=chatf.update_message, inputs=[examples_set], outputs=[message])
|
255 |
+
|
256 |
+
change_model_button.click(fn=chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
|
257 |
+
then(fn=load_model, inputs=[model_choice, gpu_layer_choice], outputs = [model_type_state, load_text, current_model]).\
|
258 |
+
then(lambda: chatf.restore_interactivity(), None, [message], queue=False).\
|
259 |
+
then(chatf.clear_chat, inputs=[chat_history_state, sources, message, current_topic], outputs=[chat_history_state, sources, message, current_topic]).\
|
260 |
+
then(lambda: None, None, chatbot, queue=False)
|
261 |
+
|
262 |
+
# Load in a pdf
|
263 |
+
load_pdf_click = load_pdf.click(ing.parse_file, inputs=[in_pdf], outputs=[ingest_text, current_source]).\
|
264 |
+
then(ing.text_to_docs, inputs=[ingest_text], outputs=[ingest_docs]).\
|
265 |
+
then(docs_to_faiss_save, inputs=[ingest_docs], outputs=[ingest_embed_out, vectorstore_state]).\
|
266 |
+
then(chatf.hide_block, outputs = [examples_set])
|
267 |
+
|
268 |
+
# Load in a webpage
|
269 |
+
load_web_click = load_web.click(ing.parse_html, inputs=[in_web, in_div], outputs=[ingest_text, ingest_metadata, current_source]).\
|
270 |
+
then(ing.html_text_to_docs, inputs=[ingest_text, ingest_metadata], outputs=[ingest_docs]).\
|
271 |
+
then(docs_to_faiss_save, inputs=[ingest_docs], outputs=[ingest_embed_out, vectorstore_state]).\
|
272 |
+
then(chatf.hide_block, outputs = [examples_set])
|
273 |
+
|
274 |
+
# Load in a csv/excel file
|
275 |
+
load_csv_click = load_csv.click(ing.parse_csv_or_excel, inputs=[in_csv, in_text_column], outputs=[ingest_text, current_source]).\
|
276 |
+
then(ing.csv_excel_text_to_docs, inputs=[ingest_text, in_text_column], outputs=[ingest_docs]).\
|
277 |
+
then(docs_to_faiss_save, inputs=[ingest_docs], outputs=[ingest_embed_out, vectorstore_state]).\
|
278 |
+
then(chatf.hide_block, outputs = [examples_set])
|
279 |
+
|
280 |
+
# Load in a webpage
|
281 |
+
|
282 |
+
# Click/enter to send message action
|
283 |
+
response_click = submit.click(chatf.create_full_prompt, inputs=[message, chat_history_state, current_topic, vectorstore_state, embeddings_state, model_type_state, out_passages], outputs=[chat_history_state, sources, instruction_prompt_out], queue=False, api_name="retrieval").\
|
284 |
+
then(chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
|
285 |
+
then(chatf.produce_streaming_answer_chatbot, inputs=[chatbot, instruction_prompt_out, model_type_state, temp_slide], outputs=chatbot)
|
286 |
+
response_click.then(chatf.highlight_found_text, [chatbot, sources], [sources]).\
|
287 |
+
then(chatf.add_inputs_answer_to_history,[message, chatbot, current_topic], [chat_history_state, current_topic]).\
|
288 |
+
then(lambda: chatf.restore_interactivity(), None, [message], queue=False)
|
289 |
+
|
290 |
+
response_enter = message.submit(chatf.create_full_prompt, inputs=[message, chat_history_state, current_topic, vectorstore_state, embeddings_state, model_type_state, out_passages], outputs=[chat_history_state, sources, instruction_prompt_out], queue=False).\
|
291 |
+
then(chatf.turn_off_interactivity, inputs=[message, chatbot], outputs=[message, chatbot], queue=False).\
|
292 |
+
then(chatf.produce_streaming_answer_chatbot, [chatbot, instruction_prompt_out, model_type_state, temp_slide], chatbot)
|
293 |
+
response_enter.then(chatf.highlight_found_text, [chatbot, sources], [sources]).\
|
294 |
+
then(chatf.add_inputs_answer_to_history,[message, chatbot, current_topic], [chat_history_state, current_topic]).\
|
295 |
+
then(lambda: chatf.restore_interactivity(), None, [message], queue=False)
|
296 |
+
|
297 |
+
# Stop box
|
298 |
+
stop.click(fn=None, inputs=None, outputs=None, cancels=[response_click, response_enter])
|
299 |
+
|
300 |
+
# Clear box
|
301 |
+
clear.click(chatf.clear_chat, inputs=[chat_history_state, sources, message, current_topic], outputs=[chat_history_state, sources, message, current_topic])
|
302 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
303 |
+
|
304 |
+
# Thumbs up or thumbs down voting function
|
305 |
+
chatbot.like(chatf.vote, [chat_history_state, instruction_prompt_out, model_type_state], None)
|
306 |
+
|
307 |
+
block.queue(concurrency_count=1).launch(debug=True)
|
308 |
+
# -
|
309 |
+
|
bot.jpg
ADDED
bot.png
ADDED
gitignore.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.pyc
|
2 |
+
*.ipynb
|
3 |
+
*.pdf
|
4 |
+
*.spec
|
5 |
+
*.toc
|
6 |
+
*.csv
|
7 |
+
bootstrapper.py
|
8 |
+
build/*
|
9 |
+
dist/*
|
10 |
+
Q tests/*
|
requirements.txt
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
beautifulsoup4
|
3 |
+
pandas
|
4 |
+
transformers
|
5 |
+
--extra-index-url https://download.pytorch.org/whl/cu113
|
6 |
+
torch
|
7 |
+
sentence_transformers
|
8 |
+
faiss-cpu
|
9 |
+
pypdf
|
10 |
+
python-docx
|
11 |
+
ctransformers[cuda]
|
12 |
+
keybert
|
13 |
+
span_marker
|
14 |
+
gensim
|
15 |
+
gradio==3.42.0
|
16 |
+
gradio_client
|
user.jfif
ADDED
Binary file (53.4 kB). View file
|
|