Spaces:
Runtime error
Runtime error
ThisIs-Developer
commited on
Commit
β’
3881b4e
1
Parent(s):
25fa5ff
Upload 6 files
Browse files- .gitattributes +2 -0
- data/71763-gale-encyclopedia-of-medicine.-vol.-1.-2nd-ed.pdf +3 -0
- ingest.py +23 -0
- model.py +131 -0
- requirements.txt +11 -0
- vectorstores/db_faiss/index.faiss +3 -0
- vectorstores/db_faiss/index.pkl +3 -0
.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 |
+
vectorstores/db_faiss/index.faiss filter=lfs diff=lfs merge=lfs -text
|
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
|
ingest.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
2 |
+
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
4 |
+
from langchain.vectorstores import FAISS
|
5 |
+
|
6 |
+
|
7 |
+
DATA_PATH="data/"
|
8 |
+
DB_FAISS_PATH="vectorstores/db_faiss"
|
9 |
+
|
10 |
+
def create_vector_db():
|
11 |
+
loader = DirectoryLoader(DATA_PATH, glob='*.pdf', loader_cls=PyPDFLoader)
|
12 |
+
documents =loader.load()
|
13 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
14 |
+
texts = text_splitter.split_documents(documents)
|
15 |
+
|
16 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
|
17 |
+
model_kwargs = {'device': 'cpu'})
|
18 |
+
|
19 |
+
db = FAISS.from_documents(texts, embeddings)
|
20 |
+
db.save_local(DB_FAISS_PATH)
|
21 |
+
|
22 |
+
if __name__ == "__main__":
|
23 |
+
create_vector_db()
|
model.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
3 |
+
from langchain import PromptTemplate
|
4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain.vectorstores import FAISS
|
6 |
+
from langchain.llms import CTransformers
|
7 |
+
from langchain.chains import RetrievalQA
|
8 |
+
|
9 |
+
DB_FAISS_PATH = 'vectorstores/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 |
+
prompt = PromptTemplate(template=custom_prompt_template,
|
23 |
+
input_variables=['context', 'question'])
|
24 |
+
return prompt
|
25 |
+
|
26 |
+
def retrieval_qa_chain(llm, prompt, db):
|
27 |
+
qa_chain = RetrievalQA.from_chain_type(llm=llm,
|
28 |
+
chain_type='stuff',
|
29 |
+
retriever=db.as_retriever(search_kwargs={'k': 2}),
|
30 |
+
return_source_documents=True,
|
31 |
+
chain_type_kwargs={'prompt': prompt}
|
32 |
+
)
|
33 |
+
return qa_chain
|
34 |
+
|
35 |
+
def load_llm():
|
36 |
+
llm = CTransformers(
|
37 |
+
model="TheBloke/Llama-2-7B-Chat-GGML",
|
38 |
+
model_type="llama",
|
39 |
+
max_new_tokens=512,
|
40 |
+
temperature=0.5
|
41 |
+
)
|
42 |
+
return llm
|
43 |
+
|
44 |
+
def qa_bot(query):
|
45 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
|
46 |
+
model_kwargs={'device': 'cpu'})
|
47 |
+
db = FAISS.load_local(DB_FAISS_PATH, embeddings)
|
48 |
+
llm = load_llm()
|
49 |
+
qa_prompt = set_custom_prompt()
|
50 |
+
qa = retrieval_qa_chain(llm, qa_prompt, db)
|
51 |
+
|
52 |
+
# Implement the question-answering logic here
|
53 |
+
response = qa({'query': query})
|
54 |
+
return response['result']
|
55 |
+
|
56 |
+
def add_vertical_space(spaces=1):
|
57 |
+
for _ in range(spaces):
|
58 |
+
st.markdown("---")
|
59 |
+
|
60 |
+
def main():
|
61 |
+
st.set_page_config(page_title="Llama-2-GGML Medical Chatbot")
|
62 |
+
|
63 |
+
with st.sidebar:
|
64 |
+
st.title('Llama-2-GGML Medical Chatbot! ππ€')
|
65 |
+
st.markdown('''
|
66 |
+
## About
|
67 |
+
|
68 |
+
The Llama-2-GGML Medical Chatbot uses the **Llama-2-7B-Chat-GGML** model and was trained on medical data from **"The GALE ENCYCLOPEDIA of MEDICINE"**.
|
69 |
+
|
70 |
+
### πBot evolving, stay tuned!
|
71 |
+
## Useful Links π
|
72 |
+
|
73 |
+
- **Model:** [Llama-2-7B-Chat-GGML](https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML) π
|
74 |
+
- **GitHub:** [ThisIs-Developer/Llama-2-GGML-Medical-Chatbot](https://github.com/ThisIs-Developer/Llama-2-GGML-Medical-Chatbot) π¬
|
75 |
+
''')
|
76 |
+
add_vertical_space(1) # Adjust the number of spaces as needed
|
77 |
+
st.write('Made by [@ThisIs-Developer](https://huggingface.co/ThisIs-Developer)')
|
78 |
+
|
79 |
+
st.title("Llama-2-GGML Medical Chatbot")
|
80 |
+
st.markdown(
|
81 |
+
"""
|
82 |
+
<style>
|
83 |
+
.chat-container {
|
84 |
+
display: flex;
|
85 |
+
flex-direction: column;
|
86 |
+
height: 400px;
|
87 |
+
overflow-y: auto;
|
88 |
+
padding: 10px;
|
89 |
+
}
|
90 |
+
.user-bubble {
|
91 |
+
background-color: #DCF8C6;
|
92 |
+
align-self: flex-end;
|
93 |
+
border-radius: 10px;
|
94 |
+
padding: 8px;
|
95 |
+
margin: 5px;
|
96 |
+
max-width: 70%;
|
97 |
+
word-wrap: break-word;
|
98 |
+
}
|
99 |
+
.bot-bubble {
|
100 |
+
background-color: #E0E0E0;
|
101 |
+
align-self: flex-start;
|
102 |
+
border-radius: 10px;
|
103 |
+
padding: 8px;
|
104 |
+
margin: 5px;
|
105 |
+
max-width: 70%;
|
106 |
+
word-wrap: break-word;
|
107 |
+
}
|
108 |
+
</style>
|
109 |
+
"""
|
110 |
+
, unsafe_allow_html=True)
|
111 |
+
|
112 |
+
conversation = st.session_state.get("conversation", [])
|
113 |
+
|
114 |
+
query = st.text_input("Ask your question here:", key="user_input")
|
115 |
+
if st.button("Get Answer"):
|
116 |
+
if query:
|
117 |
+
conversation.append({"role": "user", "message": query})
|
118 |
+
# Call your QA function
|
119 |
+
answer = qa_bot(query)
|
120 |
+
conversation.append({"role": "bot", "message": answer})
|
121 |
+
st.session_state.conversation = conversation
|
122 |
+
else:
|
123 |
+
st.warning("Please input a question.")
|
124 |
+
|
125 |
+
chat_container = st.empty()
|
126 |
+
chat_bubbles = ''.join([f'<div class="{c["role"]}-bubble">{c["message"]}</div>' for c in conversation])
|
127 |
+
chat_container.markdown(f'<div class="chat-container">{chat_bubbles}</div>', unsafe_allow_html=True)
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
main()
|
131 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pypdf==3.15.5
|
2 |
+
accelerate==0.22.0
|
3 |
+
bitsandbytes==0.41.1
|
4 |
+
chainlit==0.6.402
|
5 |
+
ctransformers==0.2.26
|
6 |
+
faiss-cpu==1.7.4
|
7 |
+
huggingface-hub==0.16.4
|
8 |
+
langchain==0.0.281
|
9 |
+
sentence-transformers==2.2.2
|
10 |
+
torch==2.0.1
|
11 |
+
transformers==4.33.0
|
vectorstores/db_faiss/index.faiss
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:41b1dd53e3fc2abc2535c8c24111b40ede2386c32a1604eaec17f3232646e7ee
|
3 |
+
size 10983981
|
vectorstores/db_faiss/index.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4007c732db0ecbd2a226c55a6f83f1bb9bf8d899079a2e52b971f8da3d78cea5
|
3 |
+
size 3567746
|