Spaces:
Running
Running
drkareemkamal
commited on
Commit
•
b0e9204
1
Parent(s):
efb40b3
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_core.prompts import PromptTemplate
|
2 |
+
import os
|
3 |
+
from langchain_community.embeddings import HuggingFaceBgeEmbeddings
|
4 |
+
from langchain_community.vectorstores import FAISS
|
5 |
+
from langchain_community.llms.ctransformers import CTransformers
|
6 |
+
from langchain.chains.retrieval_qa.base import RetrievalQA
|
7 |
+
import streamlit as st
|
8 |
+
|
9 |
+
DB_FAISS_PATH = 'vectorstores/'
|
10 |
+
|
11 |
+
custom_prompt_template = '''use the following pieces of information to answer the user's questions.
|
12 |
+
If you don't know the answer, please just say that don't know the answer, don't try to make up an answer.
|
13 |
+
Context : {context}
|
14 |
+
Question : {question}
|
15 |
+
only return the helpful answer below and nothing else.
|
16 |
+
'''
|
17 |
+
|
18 |
+
def set_custom_prompt():
|
19 |
+
"""
|
20 |
+
Prompt template for QA retrieval for vector stores
|
21 |
+
"""
|
22 |
+
prompt = PromptTemplate(template=custom_prompt_template,
|
23 |
+
input_variables=['context', 'question'])
|
24 |
+
return prompt
|
25 |
+
|
26 |
+
def load_llm():
|
27 |
+
llm = CTransformers(
|
28 |
+
model='epfl-meditron-7b',
|
29 |
+
model_type='llma',
|
30 |
+
max_new_token=512,
|
31 |
+
temperature=0.5
|
32 |
+
)
|
33 |
+
return llm
|
34 |
+
|
35 |
+
def load_embeddings():
|
36 |
+
embeddings = HuggingFaceBgeEmbeddings(model_name='NeuML/pubmedbert-base-embeddings',
|
37 |
+
model_kwargs={'device': 'cpu'})
|
38 |
+
return embeddings
|
39 |
+
|
40 |
+
def load_faiss_index(embeddings):
|
41 |
+
db = FAISS.load_local(DB_FAISS_PATH, embeddings, allow_dangerous_deserialization=True)
|
42 |
+
return db
|
43 |
+
|
44 |
+
def retrieval_qa_chain(llm, prompt, db):
|
45 |
+
qa_chain = RetrievalQA.from_chain_type(
|
46 |
+
llm=llm,
|
47 |
+
chain_type='stuff',
|
48 |
+
retriever=db.as_retriever(search_kwargs={'k': 2}),
|
49 |
+
return_source_documents=True,
|
50 |
+
chain_type_kwargs={'prompt': prompt}
|
51 |
+
)
|
52 |
+
return qa_chain
|
53 |
+
|
54 |
+
def qa_bot():
|
55 |
+
embeddings = load_embeddings()
|
56 |
+
db = load_faiss_index(embeddings)
|
57 |
+
llm = load_llm()
|
58 |
+
qa_prompt = set_custom_prompt()
|
59 |
+
qa = retrieval_qa_chain(llm, qa_prompt, db)
|
60 |
+
return qa
|
61 |
+
|
62 |
+
def final_result(query):
|
63 |
+
qa_result = qa_bot()
|
64 |
+
response = qa_result({'query': query})
|
65 |
+
return response
|
66 |
+
|
67 |
+
import streamlit as st
|
68 |
+
|
69 |
+
# Initialize the bot
|
70 |
+
bot = qa_bot()
|
71 |
+
|
72 |
+
# Streamlit webpage title
|
73 |
+
st.title('Medical Chatbot')
|
74 |
+
|
75 |
+
# User input
|
76 |
+
user_query = st.text_input("Please enter your question:")
|
77 |
+
|
78 |
+
# Button to get answer
|
79 |
+
if st.button('Get Answer'):
|
80 |
+
if user_query:
|
81 |
+
# Call the function from your chatbot script
|
82 |
+
response = final_result(user_query)
|
83 |
+
if response:
|
84 |
+
# Displaying the response
|
85 |
+
st.write("### Answer")
|
86 |
+
st.write(response['result'])
|
87 |
+
|
88 |
+
# Displaying source document details if available
|
89 |
+
if 'source_documents' in response:
|
90 |
+
st.write("### Source Document Information")
|
91 |
+
for doc in response['source_documents']:
|
92 |
+
# Retrieve and format page content by replacing '\n' with new line
|
93 |
+
formatted_content = doc.page_content.replace("\\n", "\n")
|
94 |
+
st.write("#### Document Content")
|
95 |
+
st.text_area(label="Page Content", value=formatted_content, height=300)
|
96 |
+
|
97 |
+
# Retrieve source and page from metadata
|
98 |
+
source = doc.metadata.get('source', 'Unknown')
|
99 |
+
page = doc.metadata.get('page', 'Unknown')
|
100 |
+
st.write(f"Source: {source}")
|
101 |
+
st.write(f"Page Number: {page}")
|
102 |
+
|
103 |
+
else:
|
104 |
+
st.write("Sorry, I couldn't find an answer to your question.")
|
105 |
+
else:
|
106 |
+
st.write("Please enter a question to get an answer.")
|