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