Spaces:
Runtime error
Runtime error
Upload 15 files
Browse files- .dockerignore +27 -0
- .gitattributes +2 -0
- Dockerfile +19 -0
- LICENSE +21 -0
- README copy.md +2 -0
- app.py +95 -0
- chainlit.md +11 -0
- data/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdf +3 -0
- docker-compose.debug.yml +11 -0
- docker-compose.yml +8 -0
- ingest.py +28 -0
- requirements.txt +10 -0
- vectorstore/db_faiss/index.faiss +3 -0
- vectorstore/db_faiss/index.pkl +3 -0
.dockerignore
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
**/__pycache__
|
2 |
+
**/.venv
|
3 |
+
**/.classpath
|
4 |
+
**/.dockerignore
|
5 |
+
**/.env
|
6 |
+
**/.git
|
7 |
+
**/.gitignore
|
8 |
+
**/.project
|
9 |
+
**/.settings
|
10 |
+
**/.toolstarget
|
11 |
+
**/.vs
|
12 |
+
**/.vscode
|
13 |
+
**/*.*proj.user
|
14 |
+
**/*.dbmdl
|
15 |
+
**/*.jfm
|
16 |
+
**/bin
|
17 |
+
**/charts
|
18 |
+
**/docker-compose*
|
19 |
+
**/compose*
|
20 |
+
**/Dockerfile*
|
21 |
+
**/node_modules
|
22 |
+
**/npm-debug.log
|
23 |
+
**/obj
|
24 |
+
**/secrets.dev.yaml
|
25 |
+
**/values.dev.yaml
|
26 |
+
LICENSE
|
27 |
+
README.md
|
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
data/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdf filter=lfs diff=lfs merge=lfs -text
|
37 |
+
vectorstore/db_faiss/index.faiss filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# For more information, please refer to https://aka.ms/vscode-docker-python
|
2 |
+
FROM python:3.10-slim
|
3 |
+
|
4 |
+
# Keeps Python from generating .pyc files in the container
|
5 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
6 |
+
|
7 |
+
# Turns off buffering for easier container logging
|
8 |
+
ENV PYTHONUNBUFFERED=1
|
9 |
+
|
10 |
+
FROM python:3.9
|
11 |
+
|
12 |
+
WORKDIR /code
|
13 |
+
|
14 |
+
COPY ./requirements.txt /code/requirements.txt
|
15 |
+
|
16 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
17 |
+
|
18 |
+
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
|
19 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 AI Anytime
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README copy.md
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# Llama2-Medical-Chatbot
|
2 |
+
This is a medical bot built using Llama2 and Sentence Transformers. The bot is powered by Langchain and Chainlit. The bot runs on a decent CPU machine with a minimum of 16GB of RAM.
|
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
2 |
+
from langchain import PromptTemplate
|
3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
from langchain.llms import CTransformers
|
6 |
+
from langchain.chains import RetrievalQA
|
7 |
+
import chainlit as cl
|
8 |
+
|
9 |
+
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
10 |
+
|
11 |
+
custom_prompt_template = """Use the following pieces of information to answer the user's question.
|
12 |
+
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
13 |
+
|
14 |
+
Context: {context}
|
15 |
+
Question: {question}
|
16 |
+
|
17 |
+
Only return the helpful answer below and nothing else.
|
18 |
+
Helpful answer:
|
19 |
+
"""
|
20 |
+
|
21 |
+
def set_custom_prompt():
|
22 |
+
"""
|
23 |
+
Prompt template for QA retrieval for each vectorstore
|
24 |
+
"""
|
25 |
+
prompt = PromptTemplate(template=custom_prompt_template,
|
26 |
+
input_variables=['context', 'question'])
|
27 |
+
return prompt
|
28 |
+
|
29 |
+
#Retrieval QA Chain
|
30 |
+
def retrieval_qa_chain(llm, prompt, db):
|
31 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm,
|
32 |
+
chain_type='stuff',
|
33 |
+
retriever=db.as_retriever(search_kwargs={'k': 2}),
|
34 |
+
return_source_documents=True,
|
35 |
+
chain_type_kwargs={'prompt': prompt}
|
36 |
+
)
|
37 |
+
return qa_chain
|
38 |
+
|
39 |
+
#Loading the model
|
40 |
+
def load_llm():
|
41 |
+
# Load the locally downloaded model here
|
42 |
+
llm = CTransformers(
|
43 |
+
model = "TheBloke/Llama-2-7B-Chat-GGML",
|
44 |
+
model_type="llama",
|
45 |
+
max_new_tokens = 512,
|
46 |
+
temperature = 0.5
|
47 |
+
)
|
48 |
+
return llm
|
49 |
+
|
50 |
+
#QA Model Function
|
51 |
+
def qa_bot():
|
52 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
|
53 |
+
model_kwargs={'device': 'cpu'})
|
54 |
+
db = FAISS.load_local(DB_FAISS_PATH, embeddings)
|
55 |
+
llm = load_llm()
|
56 |
+
qa_prompt = set_custom_prompt()
|
57 |
+
qa = retrieval_qa_chain(llm, qa_prompt, db)
|
58 |
+
|
59 |
+
return qa
|
60 |
+
|
61 |
+
#output function
|
62 |
+
def final_result(query):
|
63 |
+
qa_result = qa_bot()
|
64 |
+
response = qa_result({'query': query})
|
65 |
+
return response
|
66 |
+
|
67 |
+
#chainlit code
|
68 |
+
@cl.on_chat_start
|
69 |
+
async def start():
|
70 |
+
chain = qa_bot()
|
71 |
+
msg = cl.Message(content="Starting the bot...")
|
72 |
+
await msg.send()
|
73 |
+
msg.content = "Hi, Welcome to Medical Bot. What is your query?"
|
74 |
+
await msg.update()
|
75 |
+
|
76 |
+
cl.user_session.set("chain", chain)
|
77 |
+
|
78 |
+
@cl.on_message
|
79 |
+
async def main(message):
|
80 |
+
chain = cl.user_session.get("chain")
|
81 |
+
cb = cl.AsyncLangchainCallbackHandler(
|
82 |
+
stream_final_answer=True, answer_prefix_tokens=["FINAL", "ANSWER"]
|
83 |
+
)
|
84 |
+
cb.answer_reached = True
|
85 |
+
res = await chain.acall(message, callbacks=[cb])
|
86 |
+
answer = res["result"]
|
87 |
+
sources = res["source_documents"]
|
88 |
+
|
89 |
+
if sources:
|
90 |
+
answer += f"\nSources:" + str(sources)
|
91 |
+
else:
|
92 |
+
answer += "\nNo sources found"
|
93 |
+
|
94 |
+
await cl.Message(content=answer).send()
|
95 |
+
|
chainlit.md
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Welcome to Llama2 Med-Bot! ππ€
|
2 |
+
|
3 |
+
Hi there, π We're excited to have you on board. This is a powerful bot designed to help you ask queries related to your data/knowledge.
|
4 |
+
|
5 |
+
## Useful Links π
|
6 |
+
|
7 |
+
- **Data:** This is the data which has been used as a knowledge base. [Knowledge Base](https://docs.chainlit.io) π
|
8 |
+
- **Join AI Anytime Community:** Join our friendly [WhatsApp Group](https://discord.gg/ZThrUxbAYw) to ask questions, share your projects, and connect with other developers! π¬
|
9 |
+
|
10 |
+
Happy chatting! π»π
|
11 |
+
|
data/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:753cd53b7a3020bbd91f05629b0e3ddcfb6a114d7bbedb22c2298b66f5dd00cc
|
3 |
+
size 16127037
|
docker-compose.debug.yml
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: '3.4'
|
2 |
+
|
3 |
+
services:
|
4 |
+
mediwebhugginfaces:
|
5 |
+
image: mediwebhugginfaces
|
6 |
+
build:
|
7 |
+
context: .
|
8 |
+
dockerfile: ./Dockerfile
|
9 |
+
command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 app.py "]
|
10 |
+
ports:
|
11 |
+
- 5678:5678
|
docker-compose.yml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: '3.4'
|
2 |
+
|
3 |
+
services:
|
4 |
+
mediwebhugginfaces:
|
5 |
+
image: mediwebhugginfaces
|
6 |
+
build:
|
7 |
+
context: .
|
8 |
+
dockerfile: ./Dockerfile
|
ingest.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
2 |
+
from langchain.vectorstores import FAISS
|
3 |
+
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
|
6 |
+
DATA_PATH = 'data/'
|
7 |
+
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
8 |
+
|
9 |
+
# Create vector database
|
10 |
+
def create_vector_db():
|
11 |
+
loader = DirectoryLoader(DATA_PATH,
|
12 |
+
glob='*.pdf',
|
13 |
+
loader_cls=PyPDFLoader)
|
14 |
+
|
15 |
+
documents = loader.load()
|
16 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500,
|
17 |
+
chunk_overlap=50)
|
18 |
+
texts = text_splitter.split_documents(documents)
|
19 |
+
|
20 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2',
|
21 |
+
model_kwargs={'device': 'cpu'})
|
22 |
+
|
23 |
+
db = FAISS.from_documents(texts, embeddings)
|
24 |
+
db.save_local(DB_FAISS_PATH)
|
25 |
+
|
26 |
+
if __name__ == "__main__":
|
27 |
+
create_vector_db()
|
28 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pypdf
|
2 |
+
langchain
|
3 |
+
torch
|
4 |
+
accelerate
|
5 |
+
bitsandbytes
|
6 |
+
transformers
|
7 |
+
sentence_transformers
|
8 |
+
faiss_cpu
|
9 |
+
chainlit
|
10 |
+
ctransformer
|
vectorstore/db_faiss/index.faiss
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:71cb9d7859158ba922504166d439220bda5f1b03d89c0425b0705a31621b6539
|
3 |
+
size 10983981
|
vectorstore/db_faiss/index.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4f2fdf338f626ea3f40025e5ac735e0dee5350128d97079fa3be3600ef876bcc
|
3 |
+
size 3567746
|